PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/plot_and_correct_humidity095b.py

https://gitlab.com/pkormos/phase_python
Python | 188 lines | 135 code | 24 blank | 29 comment | 5 complexity | a23f345af7306b1cd8c83aa7870d1083 MD5 | raw file
  1. '''
  2. Created on Dec 2, 2016
  3. script to plot and correct relative humidity data from 095b based on the plots from
  4. humidity_analysis.py. and time series plot from this script. also updates the netCDF file
  5. now located on bunnyhill. plots up before and after histograms.
  6. @author: pkormos
  7. '''
  8. import netCDF4 as nc
  9. import numpy as np
  10. import matplotlib.pyplot as plt
  11. from pandas import Series, date_range
  12. def rhstretch(data,old_low,new_low,old_high):
  13. slp = (1-new_low)/(old_high-old_low)
  14. itc = new_low - old_low * slp
  15. return(data*slp+itc)
  16. stn0 = 'rc-095b'
  17. wyoi = np.arange(1984, 2015) # water years of interest
  18. # bring in precipitation data
  19. pnc = '/Volumes/megadozer/CZO/nc_working_files/rcew_ppt_pk.nc' # netcdf file name
  20. pn = nc.Dataset(pnc, 'r') # create precip ncdf file
  21. pvar = pn.variables["precipitation"]
  22. p_st = pn.variables['stations'] # id for stations
  23. tt_list = ["station_%s" % i for i in range(np.shape(pvar)[1])]
  24. p_list = [p_st.getncattr(tt_list[k]) for k in range(np.shape(pvar)[1])] # sorted rh station list
  25. ppt = Series(np.ma.filled(pvar[:,p_list.index(stn0)],np.nan).ravel(),index=date_range('1962-1-1', '2014-12-31 23:00', freq='H')) # make pandas dataset
  26. pn.close()
  27. # # bring in relative humidity data
  28. rnc = '/Volumes/megadozer/CZO/nc_working_files/rcew_rh_pk.nc' # netcdf file name
  29. rn = nc.Dataset(rnc, 'r') # create precip ncdf file
  30. rvar = rn.variables["relative_humidity"]
  31. rh_st = rn.variables['stations'] # id for stations
  32. tt_list = ["station_%s" % i for i in range(np.shape(rvar)[1])]
  33. r_list = [rh_st.getncattr(tt_list[k]) for k in range(np.shape(rvar)[1])] # sorted rh station list
  34. rh = Series(np.ma.filled(rvar[:,r_list.index(stn0)],np.nan).ravel(),index=date_range('18-Jun-1981 11:00:00', '01-Oct-2014', freq='H')) # make pandas dataset
  35. rn.close()
  36. # # plot up data to check it
  37. # fig0 = plt.figure(num=10, figsize=(17,8.5))
  38. # ax3 = fig0.add_subplot(1,1,1)
  39. # rh.plot()
  40. # ax3.set_xlim('1999/10/1','2014/10/1')
  41. # ax3.hlines(y=1,xmin='1983/10/1',xmax='2014/10/1')
  42. # ax3.hlines(y=.1,xmin='1983/10/1',xmax='2014/10/1')
  43. # plot original and corrected data
  44. rh = rh.clip_upper(1)
  45. t1 = '6/1/2001 0:00'
  46. t2 = '9/30/2014 23:00'
  47. fig2 = plt.figure(num=2, figsize=(14,8.5), dpi=100, facecolor='w')
  48. ax12 = fig2.add_axes([.05, .05, .9, .9])
  49. ppt[t1:t2].plot(style= 'g-', ax=ax12)
  50. ax2 = ax12.twinx()
  51. rh[t1:t2].plot(style = 'b-', ax=ax2) # plot time series before modification
  52. l0 = ax2.hlines(y=1,xmin=t1,xmax = t2,color='k')
  53. l1 = ax2.hlines(y=.1,xmin=t1,xmax = t2,color='k')
  54. ## correct first section of data
  55. st1 = '04/01/2003 1:00'
  56. nd1 = '10/01/2004 0:00'
  57. strt = 0.98 # what the upper rh does go up to (increase this.)
  58. strb = 0.1 # what the lower rh should drop to
  59. strbb = 0.1 # what the lower rh does drop to
  60. l1 = ax2.hlines(y=strt, xmin=st1, xmax=nd1,color='r', linewidth=2) # plot line showing area to be changed
  61. l2 = ax2.vlines(ymin=0,ymax=1,x=st1,color='r', linewidth=2) # plot line showing area to be changed
  62. l3 = ax2.vlines(ymin=0,ymax=1,x=nd1,color='r',linewidth=2)
  63. rh[st1:nd1] = rhstretch(rh[st1:nd1],strbb,strb,strt)
  64. rh[st1:nd1] = rh[st1:nd1].clip_upper(1)
  65. l4 = rh[st1:nd1].plot(style='c-',ax=ax2)
  66. ## correct 2nd section of data
  67. st1 = '10/01/2004 0:00'
  68. nd1 = '10/01/2005 0:00'
  69. strt = 0.95 # what the upper rh does go up to (increase this.)
  70. strb = 0.1 # what the lower rh should drop to
  71. strbb = 0.1 # what the lower rh does drop to
  72. l1 = ax2.hlines(y=strt, xmin=st1, xmax=nd1,color='r', linewidth=2) # plot line showing area to be changed
  73. l2 = ax2.vlines(ymin=0,ymax=1,x=st1,color='r', linewidth=2) # plot line showing area to be changed
  74. l3 = ax2.vlines(ymin=0,ymax=1,x=nd1,color='r',linewidth=2)
  75. rh[st1:nd1] = rhstretch(rh[st1:nd1],strbb,strb,strt)
  76. rh[st1:nd1] = rh[st1:nd1].clip_upper(1)
  77. l4 = rh[st1:nd1].plot(style='k-',ax=ax2)
  78. ## correct 3rd section of data
  79. st1 = '10/01/2005 1:00'
  80. nd1 = '10/01/2006 0:00'
  81. strt = 0.97 # what the upper rh does go up to (increase this.)
  82. strb = 0.1 # what the lower rh should drop to
  83. strbb = 0.1 # what the lower rh does drop to
  84. l1 = ax2.hlines(y=strt, xmin=st1, xmax=nd1,color='r', linewidth=2) # plot line showing area to be changed
  85. l2 = ax2.vlines(ymin=0,ymax=1,x=st1,color='r', linewidth=2) # plot line showing area to be changed
  86. l3 = ax2.vlines(ymin=0,ymax=1,x=nd1,color='r',linewidth=2)
  87. rh[st1:nd1] = rhstretch(rh[st1:nd1],strbb,strb,strt)
  88. rh[st1:nd1] = rh[st1:nd1].clip_upper(1)
  89. l4 = rh[st1:nd1].plot(style='m-',ax=ax2)
  90. ## correct 4th section of data
  91. st1 = '10/01/2006 1:00'
  92. nd1 = '10/01/2007 0:00'
  93. strt = 0.93 # what the upper rh does go up to (increase this.)
  94. strb = 0.1 # what the lower rh should drop to
  95. strbb = 0.1 # what the lower rh does drop to
  96. l1 = ax2.hlines(y=strt, xmin=st1, xmax=nd1,color='r', linewidth=2) # plot line showing area to be changed
  97. l2 = ax2.vlines(ymin=0,ymax=1,x=st1,color='r', linewidth=2) # plot line showing area to be changed
  98. l3 = ax2.vlines(ymin=0,ymax=1,x=nd1,color='r',linewidth=2)
  99. rh[st1:nd1] = rhstretch(rh[st1:nd1],strbb,strb,strt)
  100. rh[st1:nd1] = rh[st1:nd1].clip_upper(1)
  101. l4 = rh[st1:nd1].plot(style='c-',ax=ax2)
  102. ## correct 5th section of data
  103. st1 = '10/01/2007 1:00'
  104. nd1 = '10/01/2008 0:00'
  105. strt = 0.96 # what the upper rh does go up to (increase this.)
  106. strb = 0.1 # what the lower rh should drop to
  107. strbb = 0.1 # what the lower rh does drop to
  108. l1 = ax2.hlines(y=strt, xmin=st1, xmax=nd1,color='r', linewidth=2) # plot line showing area to be changed
  109. l2 = ax2.vlines(ymin=0,ymax=1,x=st1,color='r', linewidth=2) # plot line showing area to be changed
  110. l3 = ax2.vlines(ymin=0,ymax=1,x=nd1,color='r',linewidth=2)
  111. rh[st1:nd1] = rhstretch(rh[st1:nd1],strbb,strb,strt)
  112. rh[st1:nd1] = rh[st1:nd1].clip_upper(1)
  113. l4 = rh[st1:nd1].plot(style='k-',ax=ax2)
  114. ## correct 6th section of data
  115. st1 = '10/01/2011 1:00'
  116. nd1 = '10/01/2012 0:00'
  117. strt = 0.98 # what the upper rh does go up to (increase this.)
  118. strb = 0.1 # what the lower rh should drop to
  119. strbb = 0.1 # what the lower rh does drop to
  120. l1 = ax2.hlines(y=strt, xmin=st1, xmax=nd1,color='r', linewidth=2) # plot line showing area to be changed
  121. l2 = ax2.vlines(ymin=0,ymax=1,x=st1,color='r', linewidth=2) # plot line showing area to be changed
  122. l3 = ax2.vlines(ymin=0,ymax=1,x=nd1,color='r',linewidth=2)
  123. rh[st1:nd1] = rhstretch(rh[st1:nd1],strbb,strb,strt)
  124. rh[st1:nd1] = rh[st1:nd1].clip_upper(1)
  125. l4 = rh[st1:nd1].plot(style='m-',ax=ax2)
  126. ## correct 7th section of data
  127. st1 = '10/01/2012 1:00'
  128. nd1 = '10/01/2014 0:00'
  129. strt = 0.99 # what the upper rh does go up to (increase this.)
  130. strb = 0.1 # what the lower rh should drop to
  131. strbb = 0.1 # what the lower rh does drop to
  132. l1 = ax2.hlines(y=strt, xmin=st1, xmax=nd1,color='r', linewidth=2) # plot line showing area to be changed
  133. l2 = ax2.vlines(ymin=0,ymax=1,x=st1,color='r', linewidth=2) # plot line showing area to be changed
  134. l3 = ax2.vlines(ymin=0,ymax=1,x=nd1,color='r',linewidth=2)
  135. rh[st1:nd1] = rhstretch(rh[st1:nd1],strbb,strb,strt)
  136. rh[st1:nd1] = rh[st1:nd1].clip_upper(1)
  137. l4 = rh[st1:nd1].plot(style='c-',ax=ax2)
  138. # plot histograms for 144 rh during precip events
  139. year_st = 2003
  140. fig1 = plt.figure(num=1, figsize=(8.5,14), dpi=100, facecolor='w')
  141. for y in np.arange(year_st,year_st+12):
  142. rh0 = rh['10/1/%s 0:00'%(y-1):'9/30/%s 23:00'%y] # get rh time slice for stn0
  143. ppt0 = ppt['10/1/%s 0:00'%(y-1):'9/30/%s 23:00'%y] # get rh time slice for stn0]
  144. pind = ppt0>0 # index to times with precip
  145. rind = np.isfinite(rh0)
  146. ind = pind&rind
  147. b_edg = np.linspace(0, 1, num=51) # bin edges
  148. width = 0.02
  149. wts = ppt0[ind].dropna()
  150. whst = np.histogram(rh0[ind].dropna(),bins = b_edg,weights=wts) # get weighted histogram bar heights
  151. ax1 = fig1.add_subplot(6,2,y-year_st+1)
  152. rects1 = ax1.bar(whst[1][0:np.size(whst[0])]+.01,whst[0], width, color='green')
  153. plt.axis([0,1.01,0,plt.ylim()[1]])
  154. plt.title('WY%s R.H.'%(y),y=.83, x=0.4)
  155. plt.grid(True)
  156. plt.show()
  157. ## update the netcdf file with the corrected data
  158. rn = nc.Dataset(rnc, 'r+') # create precip ncdf file
  159. rvar = rn.variables["relative_humidity"]
  160. rvar[:,r_list.index(stn0)] = np.array(rh)
  161. # a = rvar[:,r_list.index(stn0)]
  162. # plt.plot(a)
  163. rn.close()