PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/sunpy/lightcurve/sources/sdo.py

http://github.com/sunpy/sunpy
Python | 104 lines | 93 code | 3 blank | 8 comment | 0 complexity | 2cdfe960cdaa6e626d043d230904903d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # -*- coding: utf-8 -*-
  2. """Provides programs to process and analyze EVE data."""
  3. from __future__ import absolute_import
  4. from sunpy.lightcurve import LightCurve
  5. import os
  6. from pandas.io.parsers import read_csv
  7. from datetime import datetime
  8. class EVELightCurve(LightCurve):
  9. """SDO EVE light curve definition
  10. Examples
  11. --------
  12. >>> import sunpy
  13. >>> eve = sunpy.lightcurve.EVELightCurve()
  14. >>> eve = sunpy.lightcurve.EVELightCurve('~/Downloads/EVE_Fe_IX_171_averages.csv')
  15. >>> eve = sunpy.lightcurve.EVELightCurve('2012/06/20')
  16. >>> eve = sunpy.lightcurve.EVELightCurve("http://lasp.colorado.edu/eve/data_access/quicklook/quicklook_data/L0CS/LATEST_EVE_L0CS_DIODES_1m.txt")
  17. >>>
  18. >>> eve.show()
  19. References
  20. ----------
  21. | http://lasp.colorado.edu/home/eve/data/data-access/
  22. """
  23. def __init__(self, *args, **kwargs):
  24. LightCurve.__init__(self, *args, **kwargs)
  25. def show(self, **kwargs):
  26. # Choose title if none was specified
  27. if not kwargs.has_key("title"):
  28. if len(self.data.columns) > 1:
  29. kwargs['title'] = 'EVE GOES Proxy Xray Flux (1 minute data)'
  30. else:
  31. if self._filename is not None:
  32. base = self._filename.replace('_', ' ')
  33. kwargs['title'] = os.path.splitext(base)[0]
  34. else:
  35. kwargs['title'] = 'EVE Averages'
  36. #LightCurve.show(kwargs)
  37. from matplotlib import pyplot as plt
  38. self.data.plot(**kwargs)
  39. plt.show()
  40. def _get_default_uri(self):
  41. """Load latest level 0CS if no other data is specified"""
  42. return "http://lasp.colorado.edu/eve/data_access/quicklook/quicklook_data/L0CS/LATEST_EVE_L0CS_DIODES_1m.txt"
  43. def _get_url_for_date(self, date):
  44. """Returns a URL to the EVE data for the specified date
  45. @NOTE: currently only supports downloading level 0 data
  46. """
  47. base_url = 'http://lasp.colorado.edu/eve/data/quicklook/L0CS/SpWx/'
  48. return base_url + date.strftime('%Y/%Y%m%d') + '_EVE_L0CS_DIODES_1m.txt'
  49. def _parse_csv(self, filepath):
  50. """Parses an EVE CSV file"""
  51. fp = open(filepath, 'rb')
  52. # Determine type of EVE CSV file and parse
  53. line1 = fp.readline()
  54. fp.seek(0)
  55. if line1.startswith("Date"):
  56. return self._parse_average_csv(fp)
  57. elif line1.startswith(";"):
  58. return self._parse_level_0cs(fp)
  59. def _parse_average_csv(self, fp):
  60. """Parses an EVE Averages file"""
  61. return "", read_csv(fp, sep=",", index_col=0, parse_dates=True)
  62. def _parse_level_0cs(self, fp):
  63. """Parses and EVE Level 0CS file"""
  64. header = ""
  65. fields = ('xrs-b', 'xrs-a', 'sem', 'ESPquad', 'esp171',
  66. 'esp257', 'esp304', 'esp366', 'espdark', 'megsp', 'megsdark',
  67. 'q0esp', 'q1esp', 'q2esp', 'q3esp', 'cmlat', 'cmlon')
  68. line = fp.readline()
  69. # Read comment at top of file
  70. while line.startswith(";"):
  71. header += line
  72. line = fp.readline()
  73. # Next line is YYYY DOY MM DD
  74. parts = line.split(" ")
  75. year = int(parts[0])
  76. month = int(parts[2])
  77. day = int(parts[3])
  78. # function to parse date column (HHMM)
  79. parser = lambda x: datetime(year, month, day, int(x[0:2]), int(x[2:4]))
  80. data = read_csv(fp, sep="\s*", names=fields, index_col=0, date_parser=parser)
  81. return header, data