def make_fig(point): plt.close() #print org[:,point] fig=plt.figure()#figsize=(8.27,11.69)) G = gridspec.GridSpec(1,1)#4,2) ax= fig.add_subplot(G[0,0]) #fig, ax = plt.subplots() #org_Q=grdc.grdc_Q(pname[point],start_dt,last_dt) #org_Q=np.array(org_Q) org_Q=grdc_Q(staid[point],start_dt,last_dt) #print org, org_Q ed=np.shape(org_Q)[0] #print ed , np.shape(org[:,point]) org=nc.outflw[:,ylist[point],xlist[point]] print ed , np.shape(org) ax.plot(np.arange(start,last),org,label="CaMa-Flood",color="blue",linewidth=0.7,zorder=102) if ed == 0: print "no GRDC data" #return 0 else: ax.plot(np.arange(start,last),ma.masked_less_equal(org_Q,0.0),label="GRDC",color="black",linewidth=0.7,zorder=101) NS1=NS(org,org_Q) #NS2=1-((np.sum((org[:ed,point]-org_Q)**2))/(np.sum((org_Q-np.mean(org_Q))**2))) #print point,NS1,NS2 Nash="NS:%4.2f"%(NS1) ax.text(0.02,0.95,Nash,ha="left",va="center",transform=ax.transAxes,fontsize=10) plt.legend(loc=1,ncol=1,prop={"size":8}) # Make the y-axis label, ticks and tick labels match the line color. ax.set_ylabel('$discharge$ (m$^3$/s)', color='k') ax.set_xlim(xmin=0,xmax=ed+1) ax.set_ylim(ymin=0)#,xmax=ed+1) ax.tick_params('y', colors='k') days=np.arange(start_dt.year,last_dt.year+1,5) xxlist=np.linspace(1,N,len(days)) ax.set_xticks(xxlist) ax.set_xticklabels(days,fontsize=8) #ax1.set_yticklabels(fontsize=11) ax.set_xlabel('$year$', color='k') # scentific notaion ax.ticklabel_format(style="sci",axis="y",scilimits=(0,0)) plt.tight_layout(pad=0.2,w_pad=0.05,h_pad=0.05) plt.savefig(pm.out_dir()+"/figures/disgraph/"+river[point]+"/"+pname[point]+"_disgraph_GRDC.png",dpi=500) #plt.show() return 0
def stanz_fig(point): plt.close() #print org[:,point] # standardized data sdz=nc_standz.standardize[:,ylist[point],xlist[point]] #fig=plt.figure() fig=plt.figure(figsize=(8.27,4))#11.69/3.0)) G = gridspec.GridSpec(1,1) # standadized data ax3 = fig.add_subplot(G[0,0]) # print np.log(org[:,point]) mu = np.mean(sdz.values) sigma=np.std(sdz.values) #ks=stats.kstest(sdz[:,point],"norm",stats.norm.fit(sdz[:,point])) #ks=stats.kstest(sdz[:,point],"norm",args=(mu,sigma)) #ks=stats.normaltest(sdz[:,point].T) ks=stats.shapiro(sdz) bin1=100#int(abs(np.amax(sdz[:,point])-np.amin(sdz[:,point]))) print mu, sigma, bin1 #print np.amax(sdz[:,point]),np.amin(sdz[:,point]) n, bins, patches = ax3.hist(sdz, bin1, normed=1, facecolor='green', alpha=0.75)#-np.mean(sdz[:,point] # add a 'best fit' line y = mlab.normpdf( bins, mu, sigma) l = ax3.plot(bins, y, 'r--', linewidth=1) #ms="mean:%6.2f\nstd:%6.2f\nks:%6.4f\np-val:%0.2E"%(mu,sigma,ks[0],ks[1]) ms="mean:%6.2f\nstd:%6.2f"%(mu,sigma) print ms ax3.text(0.02,0.8,ms,ha="left",va="center",fontsize=10,transform=ax3.transAxes,color="black") # Make the y-axis label, ticks and tick labels match the line color. ax3.set_ylabel('$frequency$', color='k') ax3.set_xlabel('$standardized$ $data$', color='k') #ax3.set_xlim(xmin=0,xmax=la+1) #ax3.set_ylim(ymin=0)#,xmax=ed+1) ax3.tick_params('y', colors='k') plt.savefig(pm.out_dir()+"/figures/hist_sfcelv/"+river[point]+"/"+pname[point]+"_hist_standz.png",pad_inches=0.02,dpi=800) return 0
def make_fig(point): plt.close() #print np.shape(org[:,point]) #print np.shape(fft[:,point]) fig = plt.figure(figsize=(wdt, hgt)) #fig=plt.figure() G = gridspec.GridSpec(4, 1) # standardized data sdz = nc_standz.standardize[:, ylist[point], xlist[point]] # actuall curve org = nc_sfcelv.sfcelv[:, ylist[point], xlist[point]] # seasonality removed fft = nc_rmdseson.rmdsesn[:, ylist[point], xlist[point]] # trend line tdl = nc_rmdtrend.rmdtrnd[:, ylist[point], xlist[point]] # original data ax1 = fig.add_subplot(G[3, 0]) ax1.plot(np.arange(start, last), sdz, label="standardize", color="black", linestyle="-", linewidth=0.7, zorder=101) #ax1.plot(np.arange(start,last),org-fft,label="fft",color="green",linewidth=0.7,zorder=101) # Make the y-axis label, ticks and tick labels match the line color. ax1.set_ylabel('standardized\n data', color='k') ax1.set_xlabel('$year$', color='k') #ax1.get_yaxis().set_ticklabels([]) ax1.set_xlim(xmin=0, xmax=last + 1) ax1.tick_params('y', colors='k') ax1.set_xticks(xxlist) ax1.set_xticklabels(days, fontsize=8) #--trend line ax2 = fig.add_subplot(G[1, 0]) ax2.plot(np.arange(start, last), tdl, label="trend line", color="grey", linewidth=0.7, zorder=103) # sesonality #ax2.plot(np.arange(start,last),yif,label="fft",color="r",linewidth=0.7,zorder=102) ax2.plot(np.arange(start, last), tdl - fft, label="seasonality", color="red", linewidth=0.7, zorder=101) # Make the y-axis label, ticks and tick labels match the line color. ax2.set_ylabel('WSE ($m$)', color='k') ax2.set_xlabel('$year$)', color='k') #ax2.get_yaxis().set_ticklabels([]) ax2.set_xlim(xmin=0, xmax=last + 1) ax2.tick_params('y', colors='k') ax2.set_xticks(xxlist) ax2.set_xticklabels(days, fontsize=8) ######################### #--senality removed data ax3 = fig.add_subplot(G[2, 0]) ax3.plot(np.arange(start, last), fft, label="seasonality removed", color="xkcd:ocean", linewidth=0.7, zorder=102) # ax3.scatter(np.arange(start,last),fft[:,point],marker="*",color="r") # actual curve #ax3.plot(np.arange(start,last),org-tdl,label="true",color="gray",linewidth=0.7,zorder=101) #ax3.plot(xf[1:N//2],1.0/N * np.abs(yf[0:N//2])[1:],color="blue",linewidth=0.7,zorder=101) #ax3.axhline((1.0/N) * yfmax * coeff,color="g",linestyle="--",linewidth=0.7,zorder=100) #ax3.plot(xf[1:],psd,color="r",linewidth=0.7,zorder=101) # Make the y-axis label, ticks and tick labels match the line color. ax3.set_ylabel('$m$', color='k') ax3.set_xlabel('$year$', color='k') ax3.set_xlim(xmin=0, xmax=last + 1) #ax3.set_ylim(ymin=0) #ax2.tick_params('y', colors='k') ax3.set_xticks(xxlist) ax3.set_xticklabels(days, fontsize=8) #--CaMa-Flood WSE ax4 = fig.add_subplot(G[0, 0]) # actual curve ax4.plot(np.arange(start, last), org, label="WSE", color="b", linewidth=0.7, zorder=102) # ax3.scatter(np.arange(start,last),fft[:,point],marker="*",color="r") ax4.plot(np.arange(start, last), org - tdl, label="trend line", color="g", linewidth=0.7, zorder=101) # actual curve #ax4.plot(np.arange(start,last),org-tdl,label="trend line",color="gray",linewidth=0.7,zorder=101) #ax4.plot(xf[1:N//2],1.0/N * np.abs(yf[0:N//2])[1:],color="blue",linewidth=0.7,zorder=101) #ax4.axhline((1.0/N) * yfmax * coeff,color="g",linestyle="--",linewidth=0.7,zorder=100) #ax4.plot(xf[1:],psd,color="r",linewidth=0.7,zorder=101) # Make the y-axis label, ticks and tick labels match the line color. ax4.set_ylabel('$WSE$ ($m$)', color='k') ax4.set_xlabel('$year$', color='k') ax4.set_xlim(xmin=0, xmax=last + 1) #ax4.set_ylim(ymin=0) #ax4.tick_params('y', colors='k') ax4.set_xticks(xxlist) ax4.set_xticklabels(days, fontsize=8) #------------------ #xff1.tofile("../img/fft_sfcelv/"+river[point]+"/"+pname[point]+"_fft_freq.bin") plt.savefig(pm.out_dir() + "/figures/timeseries/" + river[point] + "/" + pname[point] + "_sfcelv.png", dpi=500) print "/figures/timeseries/" + river[point] + "/" + pname[ point] + "_sfcelv.png" #plt.show() return 0
#--- #def sendslack(msg): # slack=Slacker('xoxb-316041665447-hNXDYiTz9PZMITg0gfRGzyrC') # message="` "+os.path.basename(__file__)+"` : "+msg # slack.chat.post_message('#camasim',message); # return 0 #--- #-- #slink("/hydro/covariance/CaMa_simulation/params.py", "params.py") #slink("/hydro/SWOTDA/img_code/read_grdc.py","read_grdc.py") import params as pm import read_grdc as grdc #-- mk_dir(pm.out_dir() + "/figures") mk_dir(pm.out_dir() + "/figures/timeseries") #--read outflow netCDF4-- tag = "%04d-%04d" % (pm.starttime()[0], pm.endtime()[0]) # sfcelv fname = pm.out_dir() + "/CaMa_out/" + pm.input_name() + "/sfcelv" + tag + ".nc" nc_sfcelv = xr.open_dataset(fname) # removed trend line fname = pm.out_dir() + "/CaMa_out/" + pm.input_name( ) + "/rmdtrnd" + tag + ".nc" nc_rmdtrend = xr.open_dataset(fname) # removed seasonality fname = pm.out_dir() + "/CaMa_out/" + pm.input_name( ) + "/rmdsesn" + tag + ".nc" nc_rmdseson = xr.open_dataset(fname) # standardized
nextxy = pm.CaMa_dir()+"/map/"+pm.map_name()+"/nextxy.bin" rivwth = pm.CaMa_dir()+"/map/"+pm.map_name()+"/rivwth.bin" rivhgt = pm.CaMa_dir()+"/map/"+pm.map_name()+"/rivhgt.bin" rivlen = pm.CaMa_dir()+"/map/"+pm.map_name()+"/rivlen.bin" elevtn = pm.CaMa_dir()+"/map/"+pm.map_name()+"/elevtn.bin" uparea = pm.CaMa_dir()+"/map/"+pm.map_name()+"/uparea.bin" lonlat = pm.CaMa_dir()+"/map/"+pm.map_name()+"/lonlat.bin" nextxy = np.fromfile(nextxy,np.int32).reshape(2,ny,nx) rivwth = np.fromfile(rivwth,np.float32).reshape(ny,nx) rivhgt = np.fromfile(rivhgt,np.float32).reshape(ny,nx) rivlen = np.fromfile(rivlen,np.float32).reshape(ny,nx) elevtn = np.fromfile(elevtn,np.float32).reshape(ny,nx) uparea = np.fromfile(uparea,np.float32).reshape(ny,nx) lonlat = np.fromfile(lonlat,np.float32).reshape(2,ny,nx) #---- rivnum = pm.out_dir()+"/dat/rivnum_"+pm.map_name()[-5::]+".bin" rivnum = np.fromfile(rivnum,np.int32).reshape(nYY,nXX) #---- nextx = nextxy[0]#ma.masked_where(rivwth<=500.0,nextxy[0]).filled(0) nexty = nextxy[1]#ma.masked_where(rivwth<=500.0,nextxy[1]).filled(0) #-------------- #fname = "./congo_nextxy.bin" #c_nextx= np.fromfile(fname,np.int32).reshape([2,nyYYnXX])[0] #fname = pm.CaMa_dir()+"/map/glb_15min/outclm.bin" #trueforo = np.fromfile(fname,np.float32).reshape([2,nYY,nXX]) #dis=(trueforo[0]>500.)*1 #dis=dis*((nextx>0)*1) #--major rivers and Ids rivid={} fname=pm.out_dir()+"/dat/river30_id.txt" f = open(fname,"r")
line = filter(None, re.split(" ",line)) num = line[0] river = line[1] station = line[3] d_info = line[2] print station, num grdc_id[station]=num return grdc_id #---- #slink("/hydro/covariance/CaMa_simulation/params.py", "params.py") #slink("../CaMa_simulation/params.py", "params.py") #slink("../read_grdc.py","read_grdc.py") import params as pm import read_grdc as grdc #-- mk_dir(pm.out_dir()+"/figures") mk_dir(pm.out_dir()"/figures/disgraph") #--read outflow netCDF4-- tag="%04d-%04d"%(pm.starttime()[0],pm.endtime()[0]) fname=pm.out_dir()+"/CaMa_out/"+pm.input_name()+"/outflw"+tag+".nc" nc=xr.open_dataset(fname) print pm.patch_start() syear=pm.starttime()[0] smonth=pm.starttime()[1] sdate=pm.starttime()[2] start_dt=datetime.date(syear,smonth,sdate) size=60 start=0 #last_dt=datetime.date(int(argvs[1]),int(argvs[2]),int(argvs[3]))
def semivari_fig(point): lon = xlist[point] + 1 lat = ylist[point] + 1 #-- up, dn = updn[lon, lat] print(pname[point], up, dn) #-- llat = -(lat - 1) * 0.25 + 90. llon = (lon - 1) * 0.25 - 180. lllat, urlat, lllon, urlon = max(llat - 50, -90.), min( llat + 50, 90.), max(llon - 50, -180.), min(llon + 50, 180.) #-- al = np.zeros([720, 1440], np.float32) #-- cs = cm.viridis(np.linspace(0, 1, up + 2)) xfig = int(math.ceil(up / 2.0)) + 1 print(lon, lat) #print xfig, cs[0] pathname = pm.out_dir() + "/figures/semivar/%s/%s" % (river[point], pname[point]) #-- if up + dn == 0: #continue return 0 #-- if dn > 0: print("downstream", dn) #-- lix = [] liy = [] ldis = [] lgamma = [] lstd = [] #-- fname = "%s/semivar/%04d%04d/dn%05d.svg" % (pm.out_dir(), lon, lat, 0) f = open(fname, "r") lines = f.readlines() f.close() #-- for line in lines[1::]: line = filter(None, re.split(" ", line)) #print line ix = int(line[0]) iy = int(line[1]) dis = float(line[2]) gamma = float(line[3]) std = float(line[4]) #-- print(dis, gamma) #-- lix.append(ix) liy.append(iy) ldis.append(dis) lgamma.append(gamma) lstd.append(std) #--- #print ldis,lgamma #-- mk_dir(pathname) mk_svfig(lgamma, ldis, pathname, 0) #==================================== # upstream if up > 0: for iup in np.arange(1, up): print("upstream", iup) #-- lix = [] liy = [] ldis = [] lgamma = [] lstd = [] #-- fname = "%s/semivar/%04d%04d/up%05d.svg" % (pm.out_dir(), lon, lat, iup) f = open(fname, "r") lines = f.readlines() f.close() #-- for line in lines[1::]: line = filter(None, re.split(" ", line)) ix = int(line[0]) iy = int(line[1]) dis = float(line[2]) gamma = float(line[3]) std = float(line[4]) #-- print(dis, gamma) #-- lix.append(ix) liy.append(iy) ldis.append(dis) lgamma.append(gamma) lstd.append(std) #-- #print ldis,lgamma #-- mk_svfig(lgamma, ldis, pathname, iup) return 0
# high=0 # for ix in lx: # if ix==1: # low+=1 # elif ix==1440: # high+=1 # if low==0 and high==0: # return lx,ly # elif low<high #-- #slink("/hydro/covariance/CaMa_simulation/params.py", "params.py") #slink("/hydro/covariance/img_code/read_grdc.py","read_grdc.py") import params as pm import read_grdc as grdc #-------------- mk_dir(pm.out_dir() + "/figures") mk_dir(pm.out_dir() + "/figures/semivar") #-------------- ###nextxy = pm.CaMa_dir()+"/map/glb_15min/nextxy.bin" ###rivwth = pm.CaMa_dir()+"/map/glb_15min/rivwth.bin" ###rivhgt = pm.CaMa_dir()+"/map/glb_15min/rivhgt.bin" ###rivlen = pm.CaMa_dir()+"/map/glb_15min/rivlen.bin" ###elevtn = pm.CaMa_dir()+"/map/glb_15min/elevtn.bin" ###nextxy = np.fromfile(nextxy,np.int32).reshape(2,720,1440) ###rivwth = np.fromfile(rivwth,np.float32).reshape(720,1440) ###rivhgt = np.fromfile(rivhgt,np.float32).reshape(720,1440) ###rivlen = np.fromfile(rivlen,np.float32).reshape(720,1440) ###elevtn = np.fromfile(elevtn,np.float32).reshape(720,1440) ####---- ####---- ###nextx = nextxy[0]#ma.masked_where(rivwth<=500.0,nextxy[0]).filled(0)
def make_fig(point): plt.close() #print org[:,point] # standardized data sdz=nc_standz.standardize[:,ylist[point],xlist[point]] # water surface elevation org=nc_sfcelv.sfcelv[:,ylist[point],xlist[point]] # seasonality removed fft=nc_rmdseson.rmdsesn[:,ylist[point],xlist[point]] # trend line tdl=nc_rmdtrend.rmdtrnd[:,ylist[point],xlist[point]] #fig=plt.figure() fig=plt.figure(figsize=(8.27,11.69)) G = gridspec.GridSpec(3,1) ax1 = fig.add_subplot(G[0,0]) mu = np.mean(org.values) sigma=np.std(org.values) bin1=100#int(abs(np.amax(org[:,point])-np.amin(org[:,point]))) print mu, sigma, bin1#, type(mu) #print np.amax(org[:,point]),np.amin(org[:,point]) n, bins, patches = ax1.hist(org, bin1, normed=1, facecolor='green', alpha=0.75)#-np.mean(org[:,point] # add a 'best fit' line y = mlab.normpdf( bins, mu, sigma) l = ax1.plot(bins, y, 'r--', linewidth=1) ms="mean:%6.2f\nstd:%6.2f"%(mu,sigma) ax1.text(0.02,0.9,ms,ha="left",va="center",fontsize=8,transform=ax1.transAxes,color="black") # Make the y-axis label, ticks and tick labels match the line color. ax1.set_ylabel('$frequency$', color='k') ax1.set_xlabel('WSE ($m$)', color='k') #ax1.set_xlim(xmin=0,xmax=la+1) #ax1.set_ylim(ymin=0)#,xmax=ed+1) ax1.tick_params('y', colors='k') #------------------------ # seasonality removed data ax2 = fig.add_subplot(G[1,0]) # print np.log(org[:,point]) mu = np.mean(fft.values) sigma=np.std(fft.values) bin1=100#int(abs(np.amax(org[:,point])-np.amin(org[:,point]))) print mu, sigma, bin1 #print np.amax(org[:,point]),np.amin(org[:,point]) n, bins, patches = ax2.hist(fft, bin1, normed=1, facecolor='green', alpha=0.75)#-np.mean(org[:,point] # add a 'best fit' line y = mlab.normpdf( bins, mu, sigma) l = ax2.plot(bins, y, 'r--', linewidth=1) ms="mean:%6.2f\nstd:%6.2f"%(mu,sigma) ax2.text(0.02,0.9,ms,ha="left",va="center",fontsize=8,transform=ax2.transAxes,color="black") # Make the y-axis label, ticks and tick labels match the line color. ax2.set_ylabel('$frequency$', color='k') ax2.set_xlabel('WSE - TD -SEASON', color='k') #ax2.set_xlim(xmin=0,xmax=la+1) #ax2.set_ylim(ymin=0)#,xmax=ed+1) ax2.tick_params('y', colors='k') #------------------------ # standadized data ax3 = fig.add_subplot(G[2,0]) # print np.log(org[:,point]) mu = np.mean(sdz.values) sigma=np.std(sdz.values) #ks=stats.kstest(sdz[:,point],"norm",stats.norm.fit(sdz[:,point])) #ks=stats.kstest(sdz[:,point],"norm",args=(mu,sigma)) #ks=stats.normaltest(sdz[:,point].T) ks=stats.shapiro(sdz.values) bin1=100#int(abs(np.amax(sdz[:,point])-np.amin(sdz[:,point]))) print mu, sigma, bin1 #print np.amax(sdz[:,point]),np.amin(sdz[:,point]) n, bins, patches = ax3.hist(sdz, bin1, normed=1, facecolor='green', alpha=0.75)#-np.mean(sdz[:,point] # add a 'best fit' line y = mlab.normpdf( bins, mu, sigma) l = ax3.plot(bins, y, 'r--', linewidth=1) ms="mean:%6.2f\nstd:%6.2f\nks:%6.4f\np-val:%0.2E"%(mu,sigma,ks[0],ks[1]) print ms ax3.text(0.02,0.7,ms,ha="left",va="center",fontsize=8,transform=ax3.transAxes,color="black") # Make the y-axis label, ticks and tick labels match the line color. ax3.set_ylabel('$frequency$', color='k') ax3.set_xlabel('$standardized$ $data$', color='k') #ax3.set_xlim(xmin=0,xmax=la+1) #ax3.set_ylim(ymin=0)#,xmax=ed+1) ax3.tick_params('y', colors='k') #ax.text(0.02,0.9,Nash,ha="left",va="center",transform=ax.transAxes,fontsize=8) #plt.legend(loc=1) plt.savefig(pm.out_dir()+"/figures/hist_sfcelv/"+river[point]+"/"+pname[point]+"_hist_sfcelv.png",dpi=500) #plt.show() return 0
#---- #---- def mk_dir(sdir): try: os.makedirs(sdir) except: pass #--- #-- #slink("/hydro/covariance/CaMa_simulation/params.py", "params.py") #slink("/hydro/SWOTDA/img_code/read_grdc.py","read_grdc.py") import params as pm import read_grdc as grdc #-- #-- mk_dir(pm.out_dir()+"/figures") mk_dir(pm.out_dir()+"/figures/hist_sfcelv") #--read outflow netCDF4-- tag="%04d-%04d"%(pm.starttime()[0],pm.endtime()[0]) # sfcelv fname=pm.out_dir()+"/CaMa_out/"+pm.input_name()+"/sfcelv"+tag+".nc" nc_sfcelv=xr.open_dataset(fname) # removed trend line fname=pm.out_dir()+"/CaMa_out/"+pm.input_name()+"/rmdtrnd"+tag+".nc" nc_rmdtrend=xr.open_dataset(fname) # removed seasonality fname=pm.out_dir()+"/CaMa_out/"+pm.input_name()+"/rmdsesn"+tag+".nc" nc_rmdseson=xr.open_dataset(fname) # standardized fname=pm.out_dir()+"/CaMa_out/"+pm.input_name()+"/standardized"+tag+".nc" nc_standz=xr.open_dataset(fname)