PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/metar/exporters.py

https://github.com/phobson/python-metar
Python | 169 lines | 134 code | 19 blank | 16 comment | 7 complexity | b3dc7a2ab2b3064dceae92697719673e MD5 | raw file
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.ticker import FuncFormatter
  4. import matplotlib.dates as dates
  5. import pandas
  6. from .graphics import _resampler
  7. states = {
  8. 'Alabama': 1,
  9. 'New Jersey': 28,
  10. 'Arizona': 2,
  11. 'New Mexico': 29,
  12. 'Arkansas': 3,
  13. 'New York': 30,
  14. 'California': 4,
  15. 'North Carolina': 31,
  16. 'Colorado': 5,
  17. 'North Dakota': 32,
  18. 'Connecticut': 6,
  19. 'Ohio': 33,
  20. 'Delaware': 7,
  21. 'Oklahoma': 34,
  22. 'Florida': 8,
  23. 'Oregon': 35,
  24. 'Georgia': 9,
  25. 'Pennsylvania': 36,
  26. 'Idaho': 10,
  27. 'Rhode Island': 37,
  28. 'Illinois': 11,
  29. 'South Carolina': 38,
  30. 'Indiana': 12,
  31. 'South Dakota': 39,
  32. 'Iowa': 13,
  33. 'Tennessee': 40,
  34. 'Kansas': 14,
  35. 'Texas': 41,
  36. 'Kentucky': 15,
  37. 'Utah': 42,
  38. 'Louisiana': 16,
  39. 'Vermont': 43,
  40. 'Maine': 17,
  41. 'Virginia': 44,
  42. 'Maryland': 18,
  43. 'Washington': 45,
  44. 'Massachusetts': 19,
  45. 'West Virginia': 46,
  46. 'Michigan': 20,
  47. 'Wisconsin': 47,
  48. 'Minnesota': 21,
  49. 'Wyoming': 48,
  50. 'Mississippi': 22,
  51. 'Not Used': 49,
  52. 'Missouri': 23,
  53. 'Alaska': 50,
  54. 'Montana': 24,
  55. 'Hawaii': 51,
  56. 'Nebraska': 25,
  57. 'Puerto Rico': 66,
  58. 'Nevada': 26,
  59. 'Virgin Islands': 67,
  60. 'New Hampshire': 27,
  61. 'Pacific Islands': 91
  62. }
  63. def SWMM5Format(dataframe, stationid, col='Precip', freq='hourly', dropzeros=True,
  64. filename=None, sep='\t'):
  65. # resample the `col` column of `dataframe`, returns a series
  66. data, rule, plotkind = _resampler(dataframe, col, freq=freq, how='sum')
  67. # set the precip column's name and make the series a dataframe
  68. data.name = col.lower()
  69. data = pandas.DataFrame(data)
  70. # add all of the data/time columns
  71. data['station'] = stationid
  72. data['year'] = data.index.year
  73. data['month'] = data.index.month
  74. data['day'] = data.index.day
  75. data['hour'] = data.index.hour
  76. data['minute'] = data.index.minute
  77. # drop the zeros if we need to
  78. if dropzeros:
  79. data = data[data['precip'] > 0]
  80. # make a file name if not provided
  81. if filename is None:
  82. filename = "{0}_{1}.dat".format(stationid, freq)
  83. # force the order of columns that we need
  84. data = data[['station', 'year', 'month', 'day', 'hour', 'minute', 'precip']]
  85. data.precip = np.round(data.precip, 2)
  86. # export and return the data
  87. data.to_csv(filename, index=False, sep=sep)
  88. return data
  89. def NCDCFormat(dataframe, coopid, statename, col='Precip', filename=None):
  90. '''
  91. Always resamples to hourly
  92. '''
  93. # constants
  94. RECORDTYPE = 'HPD'
  95. ELEMENT = '00HPCP'
  96. UNITS = 'HI'
  97. STATECODE = states[statename]
  98. data, rule, plotkind = _resampler(dataframe, col, freq='hourly', how='sum')
  99. data.index.names = ['Datetime']
  100. data.name = col
  101. data = pandas.DataFrame(data)
  102. data = pandas.DataFrame(data[data[col] > 0])
  103. data['Date'] = data.index.date
  104. data['Hour'] = data.index.hour
  105. data['Hour'] += 1
  106. data = data.reset_index().set_index(['Date', 'Hour'])[[col]]
  107. data = data.unstack(level='Hour')[col]
  108. def makeNCDCRow(row, flags=None):
  109. newrow = row.dropna() * 100
  110. newrow = newrow.astype(int)
  111. newrow = newrow.append(pandas.Series(newrow.sum(), index=[25]))
  112. if flags is None:
  113. flags = [" "] * len(newrow)
  114. precipstrings = ' '.join([
  115. '{0:02d}00 {1:05d}{2}'.format(hour, int(val), flag) \
  116. for hour, val, flag in zip(newrow.index, newrow, flags)
  117. ])
  118. ncdcstring = '{0}{1:02d}{2}{3}{4}{5}{6:02d}{7:04d}{8:03d}{9} \n'.format(
  119. RECORDTYPE, STATECODE, coopid,
  120. ELEMENT, UNITS, row.name.year,
  121. row.name.month, row.name.day, row.count(),
  122. precipstrings
  123. )
  124. return ncdcstring
  125. data['ncdcstring'] = data.apply(makeNCDCRow, axis=1)
  126. if filename is not None:
  127. with open(filename, 'w') as output:
  128. output.writelines(data['ncdcstring'].values)
  129. return data
  130. def hourXtab(dataframe, col, filename=None, flag=None):
  131. '''
  132. Always resamples to hourly
  133. '''
  134. # constants
  135. data, rule, plotkind = _resampler(dataframe, col, freq='hourly', how='sum')
  136. data.index.names = ['Datetime']
  137. data.name = col
  138. data = pandas.DataFrame(data)
  139. #data = pandas.DataFrame(data[data.Precip > 0])
  140. data['Year'] = data.index.year
  141. data['Month'] = data.index.month
  142. data['Day'] = data.index.day
  143. data['Hour'] = data.index.hour
  144. data['Hour'] += 1
  145. data = data.reset_index().set_index(['Year', 'Month', 'Day', 'Hour'])[[col]]
  146. data = data.unstack(level='Hour')[col]
  147. data[25] = data.sum(axis=1)
  148. if filename is not None:
  149. data.to_csv(filename)
  150. return data