PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/plot_and_correct_humidity076.py

https://gitlab.com/pkormos/phase_python
Python | 239 lines | 91 code | 30 blank | 118 comment | 0 complexity | 40f864a0b58c63f968ead714ab3db2af MD5 | raw file
  1. '''
  2. Created on Oct 31, 2016
  3. script to plot and correct relative humidity data based on the plots from
  4. humidity_analysis.py.
  5. @author: pkormos
  6. '''
  7. import netCDF4 as nc
  8. import numpy as np
  9. import matplotlib.pyplot as plt
  10. from pandas import DataFrame, date_range
  11. def rhstretch(data,old_low,new_low,old_high):
  12. slp = (1-new_low)/(old_high-old_low)
  13. itc = new_low - old_low * slp
  14. return(data*slp+itc)
  15. wyoi = np.arange(1984, 2015) # water years of interest
  16. # bring in precipitation data
  17. p_list = np.array(['rc-047', 'rc-055', 'rc-059', 'rc-074', 'rc-075', 'rc-076', 'rc-078', 'rc-083', # manually bring in column headers
  18. 'rc-088', 'rc-095b', 'rc-097', 'rc-106', 'rc-114', 'rc-119', 'rc-128', 'rc.fl-057',
  19. 'rc.lsc-127', 'rc.mc-043041', 'rc.mc-053', 'rc.mc-054', 'rc.mc-061', 'rc.mc-072',
  20. 'rc.ng-098c', 'rc.ng-108', 'rc.sc-012', 'rc.sc-015', 'rc.sc-023', 'rc.sc-024',
  21. 'rc.sc-031','rc.sc-045', 'rc.sc.mp-033', 'rc.sum-049', 'rc.tg-116c','rc.tg-126',
  22. 'rc.tg-145', 'rc.tg-147', 'rc.tg-155', 'rc.tg-156', 'rc.tg-165', 'rc.tg-167',
  23. 'rc.tg-174_ppt', 'rc.tg.dc-144', 'rc.tg.dc-154', 'rc.tg.dc-163', 'rc.tg.dc-163',
  24. 'rc.tg.dc.jd-124', 'rc.tg.dc.jd-124b', 'rc.tg.dc.jd-125', 'rc.tg.rme-166b',
  25. 'rc.tg.rme-176', 'rc.tg.rme-rmsp', 'rc.tg.rmw-166x94','rc.usc-138031',
  26. 'rc.usc-138044', 'rc.usc-d03','rc.usc-j10', 'rc.usc-l21'])
  27. pnc = '/Volumes/bunnyhill/HUMIDITY_WORK_2016/rcew_ppt_pk.nc' # netcdf file name
  28. pn = nc.Dataset(pnc, 'r') # create precip ncdf file
  29. pvar = pn.variables["precipitation"]
  30. ppt = DataFrame(pvar[:], index=date_range('1962-1-1', '2014-12-31 23:00', freq='H'), columns = p_list) # make pandas dataset
  31. pn.close()
  32. # bring in relative humidity data
  33. r_list = np.array(['rc-076','rc-095b','rc-128','rc.lsc-127','rc.sc-012','rc.sc-031',
  34. 'rc.tg-145','rc.tg-167','rc.tg-167b','rc.tg-174','rc.tg.dc-144',
  35. 'rc.tg.dc-163',
  36. 'rc.tg.dc.jd-124',
  37. 'rc.tg.dc.jd-124b',
  38. 'rc.tg.dc.jd-125',
  39. 'rc.tg.rme-166b',
  40. 'rc.tg.rme-176',
  41. 'rc.tg.rme-rmsp',
  42. 'rc.usc-d03',
  43. 'rc.usc-j10',
  44. 'rc.usc-l21',
  45. 'rc-076_tf',
  46. 'rc-128_tf',
  47. 'rc.lsc-127_tf',
  48. 'rc.tg.rme-176_tf'])
  49. rnc = '/Volumes/bunnyhill/HUMIDITY_WORK_2016/rcew_rh_pk.nc' # netcdf file name
  50. rn = nc.Dataset(rnc, 'r') # create precip ncdf file
  51. rvar = rn.variables["relative_humidity"]
  52. rh = DataFrame(rvar[:], index=date_range('18-Jun-1981 11:00:00', '01-Oct-2014', freq='H'), columns = r_list) # make pandas dataset
  53. rn.close()
  54. # code to correct 076 from 9/22/1985 17:00 to 10/26/1995 0:00
  55. stn0 = 'rc-076'
  56. # # plot histograms for rh during precip events
  57. # fig1 = plt.figure(num=1, figsize=(8.5,14), dpi=100, facecolor='w')
  58. # for y in np.nditer(wyoi[0:14]):
  59. # rh0 = rh.ix['10/1/%s 0:00'%(y-1):'9/30/%s 23:00'%y,[stn0]] # get rh time slice for stn0
  60. # ppt0 = ppt.ix['10/1/%s 0:00'%(y-1):'9/30/%s 23:00'%y,[stn0]] # get rh time slice for stn0]
  61. # pind = ppt0>0 # index to times with precip
  62. # b_edg = np.linspace(0, 1, num=51) # bin edges
  63. # width = 0.02
  64. # wts = ppt0[pind].dropna()
  65. # whst = np.histogram(rh0[pind].dropna(),bins = b_edg,weights=wts) # get weighted histogram bar heights
  66. # ax1 = fig1.add_subplot(7,2,y-1983)
  67. # rects1 = ax1.bar(whst[1][0:np.size(whst[0])]+.01,whst[0], width, color='green')
  68. # plt.axis([0,1.01,0,plt.ylim()[1]])
  69. # plt.title('WY%s R.H.'%(y),y=.83, x=0.4)
  70. # plt.grid(True)
  71. # plt.show()
  72. # fig1.savefig('/Users/pkormos/rcew_met_dist/snow_data/phase_analysis/phase_python/rh_hist_076_before.tif', dpi=100)
  73. # plot original and corrected data
  74. stretcher1=0.84
  75. stretcher2=0.88
  76. # fig2 = plt.figure(num=2, figsize=(14,8.5), dpi=100, facecolor='w')
  77. # ax2 = fig2.add_axes([.05, .05, .9, .9])
  78. # rh.ix['10/1/1982 0:00':'9/30/1999 23:00',[stn0]].plot(style = 'b-', ax=ax2) # plot time series before modification
  79. # ax2.hlines(y=stretcher1,xmin='9/22/1985 17:00', xmax='6/1/1992 12:00',color='r', linewidth=2) # plot line showing area to be changed
  80. # ax2.hlines(y=stretcher2,xmin='6/1/1992 13:00', xmax='10/26/1995 0:00',color='r', linewidth=2) # plot line showing area to be changed
  81. # xmin, xmax = ax2.get_xlim()
  82. # ax2.hlines(y=.13, xmin=xmin, xmax=xmax-1, color='r', linewidth=2)
  83. # apply stretch correction
  84. rh.ix['9/22/1985 17:00':'6/1/1992 12:00',[stn0]] = rh.ix['9/22/1985 17:00':'6/1/1992 12:00',[stn0]]/stretcher1
  85. rh.ix['9/22/1985 17:00':'6/1/1992 12:00',[stn0]] = rh.ix['9/22/1985 17:00':'6/1/1992 12:00',[stn0]].clip_upper(1)
  86. rh.ix['6/1/1992 13:00':'10/26/1995 0:00',[stn0]] = rh.ix['6/1/1992 13:00':'10/26/1995 0:00',[stn0]]/stretcher2
  87. rh.ix['6/1/1992 13:00':'10/26/1995 0:00',[stn0]] = rh.ix['6/1/1992 13:00':'10/26/1995 0:00',[stn0]].clip_upper(1)
  88. # rh.ix['9/22/1985 17:00':'6/1/1992 12:00',[stn0]].plot(style = 'g-', ax=ax2) # plot time series after mod. green
  89. # rh.ix['6/1/1992 13:00':'10/26/1995 0:00',[stn0]].plot(style = 'k-', ax=ax2) # plot time series after mod. green
  90. # # this method made the minimums too high compared to wy 1984-5 and wy96-99
  91. # rh.ix['9/22/1985 17:00':'9/30/1995 23:00',[stn0]] = rh.ix['9/22/1985 17:00':'9/30/1995 23:00',[stn0]]+0.15
  92. # rh.ix['9/22/1985 17:00':'9/30/1995 23:00',[stn0]] = rh.ix['9/22/1985 17:00':'9/30/1995 23:00',[stn0]].clip_upper(1)
  93. # ax12 = ax2.twinx()
  94. # ppt.ix['10/1/1982 0:00':'9/30/1999 23:00',[stn0]].plot(style= 'g-', ax=ax12)
  95. # fig2.savefig('/Users/pkormos/rcew_met_dist/snow_data/phase_analysis/phase_python/rh_076_before_after.tif', dpi=100)
  96. # ## replot histograms after correction
  97. # fig3 = plt.figure(num=3, figsize=(8.5,14), dpi=100, facecolor='w')
  98. # for y in np.nditer(wyoi[0:14]):
  99. # rh0 = rh.ix['10/1/%s 0:00'%(y-1):'9/30/%s 23:00'%y,[stn0]] # get rh time slice for stn0
  100. # ppt0 = ppt.ix['10/1/%s 0:00'%(y-1):'9/30/%s 23:00'%y,[stn0]] # get rh time slice for stn0]
  101. # pind = ppt0>0 # index to times with precip
  102. # b_edg = np.linspace(0, 1, num=51) # bin edges
  103. # width = 0.02
  104. # wts = ppt0[pind].dropna()
  105. # whst = np.histogram(rh0[pind].dropna(),bins = b_edg,weights=wts) # get weighted histogram bar heights
  106. # ax3 = fig3.add_subplot(7,2,y-1983)
  107. # rects1 = ax3.bar(whst[1][0:np.size(whst[0])]+.01,whst[0], width, color='green')
  108. # plt.axis([0,1.01,0,plt.ylim()[1]])
  109. # plt.title('WY%s R.H.'%(y),y=.83, x=0.4)
  110. # fig3.savefig('/Users/pkormos/rcew_met_dist/snow_data/phase_analysis/phase_python/rh_hist_076_after.tif', dpi=100)
  111. # # plot histograms for rh 2000 -> 2014 during precip events
  112. # fig4 = plt.figure(num=4, figsize=(8.5,14), dpi=100, facecolor='w')
  113. # for y in np.nditer(wyoi[17:31]):
  114. # rh0 = rh.ix['10/1/%s 0:00'%(y-1):'9/30/%s 23:00'%y,[stn0]] # get rh time slice for stn0
  115. # ppt0 = ppt.ix['10/1/%s 0:00'%(y-1):'9/30/%s 23:00'%y,[stn0]] # get rh time slice for stn0]
  116. # pind = ppt0>0 # index to times with precip
  117. # b_edg = np.linspace(0, 1, num=51) # bin edges
  118. # width = 0.02
  119. # wts = ppt0[pind].dropna()
  120. # whst = np.histogram(rh0[pind].dropna(),bins = b_edg,weights=wts) # get weighted histogram bar heights
  121. # ax1 = fig4.add_subplot(7,2,y-2000)
  122. # rects1 = ax1.bar(whst[1][0:np.size(whst[0])]+.01,whst[0], width, color='green')
  123. # plt.axis([0,1.01,0,plt.ylim()[1]])
  124. # plt.title('WY%s R.H.'%(y),y=.83, x=0.4)
  125. # plt.grid(True)
  126. # plt.show()
  127. # fig4.savefig('/Users/pkormos/rcew_met_dist/snow_data/phase_analysis/phase_python/rh_hist_076_before_2001_2014.tif', dpi=100)
  128. ################################################################################################################
  129. ## plot original and corrected data 2003-2014
  130. ################################################################################################################
  131. fig5 = plt.figure(num=5, figsize=(14,8.5), dpi=100, facecolor='w')
  132. ax15 = fig5.add_axes([.05, .05, .9, .9])
  133. ppt.ix['10/1/1984 0:00':'9/30/2014 23:00',[stn0]].plot(style= 'g-', ax=ax15)
  134. ax5 = ax15.twinx()
  135. rh.ix['10/1/1984 0:00':'9/30/2014 23:00',[stn0]].plot(style = 'b-', ax=ax5) # plot time series before modification
  136. plt.legend(loc='center', bbox_to_anchor=(1, 0.5))
  137. ## correct first section of data 05-14
  138. stt='2005-10-01 01:00'
  139. ndt='2007-10-01 00:00'
  140. strt = 0.95 # what the upper rh does go up to
  141. strb = 0.00 # what the lower rh should drop to
  142. strbb = 0.00 # what the lower rh does drop to
  143. # l1 = ax5.hlines(y=strt,xmin=stt, xmax=ndt,color='r', linewidth=2) # plot line showing area to be changed
  144. # correct first section of data and plot it 05-14
  145. rh.ix[stt:ndt,[stn0]] = rhstretch(rh.ix[stt:ndt,[stn0]],strbb,strb,strt)
  146. rh.ix[stt:ndt,[stn0]] = rh.ix[stt:ndt,[stn0]].clip_upper(1)
  147. # l2 = rh.ix[stt:ndt,[stn0]].plot(style='c-',ax=ax5)
  148. ## correct 2nd section of data 05-14
  149. stt='2007-10-01 01:00'
  150. ndt='2009-10-01 00:00'
  151. strt = 0.97 # what the upper rh does go up to
  152. strb = 0.00 # what the lower rh should drop to
  153. strbb = 0.00 # what the lower rh does drop to
  154. # l1 = ax5.hlines(y=strt,xmin=stt, xmax=ndt,color='r', linewidth=2) # plot line showing area to be changed
  155. # correct 2nd section of data and plot it 05-14
  156. rh.ix[stt:ndt,[stn0]] = rhstretch(rh.ix[stt:ndt,[stn0]],strbb,strb,strt)
  157. rh.ix[stt:ndt,[stn0]] = rh.ix[stt:ndt,[stn0]].clip_upper(1)
  158. # l2 = rh.ix[stt:ndt,[stn0]].plot(style='y-',ax=ax5)
  159. ## correct 3rd section of data 05-14
  160. stt='2011-10-01 01:00'
  161. ndt='2012-10-01 00:00'
  162. strt = 0.90 # what the upper rh does go up to
  163. strb = 0.00 # what the lower rh should drop to
  164. strbb = 0.00 # what the lower rh does drop to
  165. # l1 = ax5.hlines(y=strt,xmin=stt, xmax=ndt,color='r', linewidth=2) # plot line showing area to be changed
  166. # correct 3rd section of data and plot it 05-14
  167. rh.ix[stt:ndt,[stn0]] = rhstretch(rh.ix[stt:ndt,[stn0]],strbb,strb,strt)
  168. rh.ix[stt:ndt,[stn0]] = rh.ix[stt:ndt,[stn0]].clip_upper(1)
  169. # l2 = rh.ix[stt:ndt,[stn0]].plot(style='m-',ax=ax5)
  170. ## correct 4th section of data 05-14
  171. stt='2012-10-01 01:00'
  172. ndt='2014-10-01 00:00'
  173. strt = 0.94 # what the upper rh does go up to
  174. strb = 0.00 # what the lower rh should drop to
  175. strbb = 0.00 # what the lower rh does drop to
  176. # l1 = ax5.hlines(y=strt,xmin=stt, xmax=ndt,color='r', linewidth=2) # plot line showing area to be changed
  177. # correct 4th section of data and plot it 05-14
  178. rh.ix[stt:ndt,[stn0]] = rhstretch(rh.ix[stt:ndt,[stn0]],strbb,strb,strt)
  179. rh.ix[stt:ndt,[stn0]] = rh.ix[stt:ndt,[stn0]].clip_upper(1)
  180. # l2 = rh.ix[stt:ndt,[stn0]].plot(style='m-',ax=ax5)
  181. #
  182. # fig5.savefig('/Users/pkormos/rcew_met_dist/snow_data/phase_analysis/phase_python/rh_076_before_after2015.tif', dpi=100)
  183. # fig6 = plt.figure(num=6, figsize=(8.5,14), dpi=100, facecolor='w')
  184. # for y in np.nditer(wyoi[17:31]):
  185. # rh0 = rh.ix['10/1/%s 0:00'%(y-1):'9/30/%s 23:00'%y,[stn0]] # get rh time slice for stn0
  186. # ppt0 = ppt.ix['10/1/%s 0:00'%(y-1):'9/30/%s 23:00'%y,[stn0]] # get rh time slice for stn0]
  187. # pind = ppt0>0 # index to times with precip
  188. # b_edg = np.linspace(0, 1, num=51) # bin edges
  189. # width = 0.02
  190. # wts = ppt0[pind].dropna()
  191. # whst = np.histogram(rh0[pind].dropna(),bins = b_edg,weights=wts) # get weighted histogram bar heights
  192. # ax1 = fig6.add_subplot(7,2,y-2000)
  193. # rects1 = ax1.bar(whst[1][0:np.size(whst[0])]+.01,whst[0], width, color='green')
  194. # plt.axis([0,1.01,0,plt.ylim()[1]])
  195. # plt.title('WY%s R.H.'%(y),y=.83, x=0.4)
  196. # plt.grid(True)
  197. # plt.show()
  198. # fig6.savefig('/Users/pkormos/rcew_met_dist/snow_data/phase_analysis/phase_python/rh_hist_076_after_2001_2014.tif', dpi=100)
  199. rh[stn0] = rh[stn0].clip_upper(1) # clip all of the values greater than 1
  200. # rh[stn0].plot()
  201. ## update the netcdf file with the corrected data
  202. rn = nc.Dataset(rnc, 'r+') # create precip ncdf file
  203. rvar = rn.variables["relative_humidity"]
  204. rvar[:,r_list==stn0] = np.array(rh[stn0])
  205. # a = rvar[:,r_list.index(stn0)]
  206. # plt.plot(a)
  207. rn.close()