def rmse(assimilations,truths,add=1.0e-20): rms = [] assimilations = np.array(assimilations) truths = np.array(truths) # assimilations is 2d arry for i in range(0,len(truths)): rms.append(np.sqrt(((assimilations[:,i]-truths[i]*np.ones([pm.ens_mem()],np.float32))**2).mean())) return rms#(rms/abs(truths+add))*100.0
pname.append(station_loc) xlist.append(x_list) ylist.append(y_list) EGM08.append(egm08) EGM96.append(egm96) #----------------------- river = ([flatten for inner in river for flatten in inner]) pname = ([flatten for inner in pname for flatten in inner]) xlist = ([flatten for inner in xlist for flatten in inner]) ylist = ([flatten for inner in ylist for flatten in inner]) EGM08 = ([flatten for inner in EGM08 for flatten in inner]) EGM96 = ([flatten for inner in EGM96 for flatten in inner]) pnum = len(pname) ######################################################### pnum = len(pname) opn = np.ctypeslib.as_ctypes(np.zeros([N, pm.ens_mem(), pnum], np.float32)) shared_array_opn = sharedctypes.RawArray(opn._type_, opn) asm = np.ctypeslib.as_ctypes(np.zeros([N, pm.ens_mem(), pnum], np.float32)) shared_array_asm = sharedctypes.RawArray(asm._type_, asm) # for parallel calcualtion inputlist = [] for day in np.arange(start, last): target_dt = start_dt + datetime.timedelta(days=day) yyyy = '%04d' % (target_dt.year) mm = '%02d' % (target_dt.month) dd = '%02d' % (target_dt.day) for num in np.arange(1, pm.ens_mem() + 1): numch = '%03d' % num inputlist.append([yyyy, mm, dd, numch]) #print (yyyy,mm,dd,numch)
def make_fig(point): plt.close() #labels=["GRDC","corrupted","assimilated"] labels=["GRDC","simulated","assimilated"] # #print org[:,point] #for i in np.arange(start,last): #print opn[i,:,point] #print asm[i,:,point] # plt.plot(np.arange(start,last),org[:,point],label="true",color=colors["true"],linewidth=0.7) # # for num in np.arange(0,pm.ens_mem()): # plt.plot(np.arange(start,last),opn[:,num,point],label="corrupted",color=colors["corrupted"],linewidth=0.3,alpha=0.5) # plt.plot(np.arange(start,last),asm[:,num,point],label="assimilated",color=colors["assimilated"],linewidth=0.3,alpha=0.5) # # plt.ylim(ymin=0) fig, ax1 = plt.subplots() org=grdc.grdc_dis(staid[point],syear,eyear-1) org=np.array(org) lines=[ax1.plot(np.arange(start,last),ma.masked_less(org,0.0),label="GRDC",color="#34495e",linewidth=3.0,zorder=101)[0]] #,marker = "o",markevery=swt[point]) # ax1.plot(np.arange(start,last),hgt[:,point],label="true",color="gray",linewidth=0.7,linestyle="--",zorder=101) # plt.plot(np.arange(start,last),org[:,point],label="true",color="black",linewidth=0.7) for num in np.arange(0,pm.ens_mem()): ax1.plot(np.arange(start,last),opn[:,num,point],label="corrupted",color="blue",linewidth=0.1,alpha=0.1,zorder=102) ax1.plot(np.arange(start,last),asm[:,num,point],label="assimilated",color="red",linewidth=0.1,alpha=0.1,zorder=103) # plt.plot(np.arange(start,last),opn[:,num,point],label="corrupted",color="blue",linewidth=0.3,alpha=0.5) # plt.plot(np.arange(start,last),asm[:,num,point],label="assimilated",color="red",linewidth=0.3,alpha=0.5) # draw mean of ensembles lines.append(ax1.plot(np.arange(start,last),np.mean(ma.masked_less(opn[:,:,point],0.0),axis=1),label="corrupted",color="#4dc7ec",linewidth=1.0,alpha=1,zorder=104)[0]) lines.append(ax1.plot(np.arange(start,last),np.mean(ma.masked_less(asm[:,:,point],0.0),axis=1),label="assimilated",color="#ff8021",linewidth=1.0,alpha=1,zorder=106)[0]) # plt.ylim(ymin=) # Make the y-axis label, ticks and tick labels match the line color. ax1.set_ylabel('discharge (m$^3$/s)', color='k') ax1.set_xlim(xmin=0,xmax=last+1) ax1.tick_params('y', colors='k') # scentific notaion ax1.ticklabel_format(style="sci",axis="y",scilimits=(0,0)) ax1.yaxis.major.formatter._useMathText=True # #xxlist=np.linspace(0,N,(eyear-syear)+1) #xlab=np.arange(syear,eyear+1,1) #xxlab=[calendar.month_name[i][:3] for i in range(1,13)] if eyear-syear > 8: dtt=2 dt=int(math.ceil(((eyear-syear)+1)/5.0)) elif eyear-syear > 10: dtt=5 dt=int(math.ceil(((eyear-syear)+1)/5.0)) else: dtt=1 dt=(eyear-syear)+1 xxlist=np.linspace(0,N,dt,endpoint=True) #xxlab=[calendar.month_name[i][:3] for i in range(1,13)] xxlab=np.arange(syear,eyear+1,dtt) ax1.set_xticks(xxlist) ax1.set_xticklabels(xxlab,fontsize=10) # Nash-Sutcllf calcuation NS1=NS(np.mean(asm[:,:,point],axis=1),org) NS2=NS(np.mean(opn[:,:,point],axis=1),org) KGE1=KGE(np.mean(asm[:,:,point],axis=1),org) KGE2=KGE(np.mean(opn[:,:,point],axis=1),org) COR1=correlation(np.mean(asm[:,:,point],axis=1),org) COR2=correlation(np.mean(opn[:,:,point],axis=1),org) RSE1=RMSE(np.mean(asm[:,:,point],axis=1),org) RSE2=RMSE(np.mean(opn[:,:,point],axis=1),org) #1-((np.sum((org[:ed,point]-org_Q)**2))/(np.sum((org_Q-np.mean(org_Q))**2))) #print point,NS1,NS2 Nash1="NS (assim):%4.2f"%(NS1) Nash2="NS (open):%4.2f"%(NS2) kgeh1="KGE(assim):%4.2f"%(KGE1) kgeh2="KGE(open):%4.2f"%(KGE2) corr1="$r$(assim):%4.2f"%(COR1) corr2="$r$(open):%4.2f"%(COR2) rmse1="RMSE: %4.2f"%(RSE1) rmse2="RMSE: %4.2f"%(RSE2) # # # ax1.text(0.02,0.95,Nash1,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.85,Nash2,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.42,0.95,kgeh1,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.42,0.85,kgeh2,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.75,corr1,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.55,corr2,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.95,Nash1,ha="left",va="center",transform=ax1.transAxes,fontsize=8) ax1.text(0.02,0.90,Nash2,ha="left",va="center",transform=ax1.transAxes,fontsize=8) ax1.text(0.02,0.80,kgeh1,ha="left",va="center",transform=ax1.transAxes,fontsize=8) ax1.text(0.02,0.75,kgeh2,ha="left",va="center",transform=ax1.transAxes,fontsize=8) ax1.text(0.42,0.95,corr1,ha="left",va="center",transform=ax1.transAxes,fontsize=8) ax1.text(0.42,0.90,corr2,ha="left",va="center",transform=ax1.transAxes,fontsize=8) ax1.text(0.42,0.80,rmse1,ha="left",va="center",transform=ax1.transAxes,fontsize=8) ax1.text(0.42,0.75,rmse2,ha="left",va="center",transform=ax1.transAxes,fontsize=8) # # twin axis # ax2 = ax1.twinx() # #aiv = stat.AI(asm[:,:,point],opn[:,:,point],org[:,point]) # #aivn= stat.AI_new(asm[:,:,point],opn[:,:,point],org[:,point]) # aivn,error = stat.AI_new(asm[:,:,point],opn[:,:,point],org[:,point]) # #print aivn # #-- # pBias,pB_c = stat.pBias(asm[:,:,point],opn[:,:,point],org[:,point]) # ai_mean =np.mean(ma.masked_less_equal(aivn,0.0))# np.nanmean(ma.masked_less_equal(aivn,0.0)) # # RMSE # RootMSE=stat.RMSE(asm[:,:,point],org[:,point]) # # rRMSE # rRootMSE=stat.rRMSE(asm[:,:,point],org[:,point]) # # NRMSE # NRootMSE=stat.NRMSE(asm[:,:,point],org[:,point]) # # VE # VolEff=stat.VE(asm[:,:,point],org[:,point]) # # NSE # NSEc,NSa,NSc=stat.NSE(asm[:,:,point],opn[:,:,point],org[:,point]) # # PDRI PTRI # PDRI,PTRI=stat.PRI(asm[:,:,point],opn[:,:,point],org[:,point]) # #--- # EnsSprd=stat.EnsSpr(asm[:,:,point],opn[:,:,point],org[:,point]) # EnsSpr_mean=np.mean(ma.masked_less_equal(EnsSprd,0.0)) # #--- # AssimQlt=stat.AQ(PDRI,PTRI,ai_mean,EnsSpr_mean) # #--- # mai = "meanAI:%1.2f"%(ai_mean) # pB = "pBIAS:%1.1f%%"%(pBias) # rms = "RMSE:%8.2f"%(RootMSE) # rrms= "rRMSE:%3.2f"%(rRootMSE) # nrms= "NRMSE:%3.2f"%(NRootMSE) # veff= "VE:%3.2f"%(VolEff) # nsec= "NSE:%3.2f"%(NSEc) # pdr = "PDRI:%3.2f"%(PDRI) # ptr = "PTRI:%3.2f"%(PTRI) # ESrd= "EnsSpr:%3.2f"%(EnsSpr_mean) # AssQ= "AQ:%3.2f"%(AssimQlt) # NSac= "NSEa:%3.2f, NSEc:%3.2f"%(NSa,NSc) # print mai , pB#, "pB_c", pB_c, "%" # #pB = pB + r'{1.1f}\%'.format(pBias) # ax1.text(0.02,0.95,mai,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.85,nsec,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.75,pB,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.65,rms,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.55,rrms,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.45,nrms,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.35,veff,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.25,pdr,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.15,ptr,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # #ax1.text(0.02,0.05,AssQ,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.05,NSac,ha="left",va="center",transform=ax1.transAxes,fontsize=10) #--- # pBA_p=np.sum(ma.masked_greater((np.mean(asm[:,:,point],axis=1)-org[:,point]),0.0).filled(0.0)) # pBA_n=np.sum(ma.masked_less((np.mean(asm[:,:,point],axis=1)-org[:,point]),0.0).filled(0.0)) # #--- # pBC_p=np.sum(ma.masked_greater((np.mean(opn[:,:,point],axis=1)-org[:,point]),0.0).filled(0.0)) # pBC_n=np.sum(ma.masked_less((np.mean(opn[:,:,point],axis=1)-org[:,point]),0.0).filled(0.0)) # ax1.text(0.80,0.95,"pBa_p:%1.1f%%"%(pBA_p),ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.80,0.85,"pBa_n:%1.1f%%"%(pBA_n),ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.80,0.75,"pBc_p:%1.1f%%"%(pBC_p),ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.80,0.65,"pBc_n:%1.1f%%"%(pBC_n),ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.80,0.55,"pBa_p/pBa_n:%1.1f"%(abs(pBA_p/pBA_n+1.0e-20)),ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.80,0.45,"pBc_p/pBc_n:%1.1f"%(abs(pBC_p/pBC_n+1.0e-20)),ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax2.plot(np.arange(start,last),aiv,color="green",zorder=104,marker = "o",alpha=0.3, markevery=swt[point]) # ax2.plot(np.arange(start,last),ma.masked_less_equal(aivn,1.0e-20),color="green",zorder=104,marker = "o", alpha=0.5,linewidth=0.5,markevery=swt[point]) # ax2.plot(np.arange(start,last),ma.masked_where(error<=0.1,aivn),color=green,marker = "o",markeredgecolor =green, alpha=1,linewidth=0.5,markevery=swt[point],markersize=3,zorder=100)#,label="AI") # ax2.plot(np.arange(start,last),ma.masked_where(error>0.1,aivn),color=green2,marker = "o",markeredgecolor =green2, alpha=1,linewidth=0.5,markevery=swt[point],markersize=3,zorder=100)#,label="AI") # ax2.set_ylabel('AI', color='green') # ax2.tick_params('y', colors='green') # ax2.set_ylim(ymin=0.,ymax=1.) # ax2.set_xlim(xmin=0,xmax=last+1) # print swt[point] plt.legend(lines,labels,ncol=1,loc='upper right') #, bbox_to_anchor=(1.0, 1.0),transform=ax1.transAxes) station_loc_list=pname[point].split("/") station_name="-".join(station_loc_list) print ('save',river[point] , station_name) plt.savefig(assim_out+"/figures/disgraph/"+river[point]+"-"+station_name+".png",dpi=500) return 0
def save_statistic(): # copy mean and std of simulated WSE # for anomaly and normalized assimilations mkdir("./assim_out/mean_sfcelv/") # if pm.input()=="E2O": # iname = pm.DA_dir()+"/dat/mean_sfcelv_"+pm.input()+"_"+pm.mapname()+"_2000-2010"+ens_char+".bin # oname = "./assim_out/mean_sfcelv/meansfcelv"+ens+".bin" # os.system("cp -r "+pm.DA_dir()+"/dat/mean_sfcelv_"+pm.input()+"_"+pm.mapname()+"_2000-2014.bin ./assim_out/mean_sfcelv/mean_sfcelv.bin") # os.system("cp -r "+pm.DA_dir()+"/dat/std_sfcelv_"+pm.input()+"_"+pm.mapname()+"_2000-2014.bin ./assim_out/mean_sfcelv/std_sfcelv.bin") # print ("cp -r "+pm.DA_dir()+"/dat/mean_sfcelv_"+pm.input()+"_"+pm.mapname()+"_2000-2014.bin ./assim_out/mean_sfcelv/mean_sfcelv.bin") # if pm.input()=="VIC_BC": # os.system("cp -r "+pm.DA_dir()+"/dat/mean_sfcelv_"+pm.input()+"_"+pm.mapname()+"_1979-2013.bin ./assim_out/mean_sfcelv/mean_sfcelv.bin") # os.system("cp -r "+pm.DA_dir()+"/dat/std_sfcelv_"+pm.input()+"_"+pm.mapname()+"_1979-2013.bin ./assim_out/mean_sfcelv/std_sfcelv.bin") # print ("cp -r "+pm.DA_dir()+"/dat/mean_sfcelv_"+pm.input()+"_"+pm.mapname()+"_1979-2013.bin ./assim_out/mean_sfcelv/mean_sfcelv.bin") # #=========== # # mean # inputlist=[] # for ens in np.arange(1,pm.ens_mem(pm.mode())+1): # ens_char="%03d"%(ens) # if pm.input()=="E2O": # iname = pm.DA_dir()+"/dat/mean_"+pm.stat_name()+"_"+ens_char+".bin" # # iname = pm.DA_dir()+"/dat/mean_sfcelv_E2O_"+pm.mapname()+"_2000-2010_"+ens_char+".bin" # # iname = pm.DA_dir()+"/dat/mean_sfcelv_cal_E2O_"+pm.mapname()+"_2000-2010_"+ens_char+".bin" # oname = "./assim_out/mean_sfcelv/meansfcelvC"+ens_char+".bin" # if pm.input()=="VIC_BC": # iname = pm.DA_dir()+"/dat/mean_"+pm.stat_name()+"_"+ens_char+".bin" # # iname = pm.DA_dir()+"/dat/mean_sfcelv_VIC_BC_"+pm.mapname()+"_2000-2010_"+ens_char+".bin" # # iname = pm.DA_dir()+"/dat/mean_sfcelv_cal_VIC_BC_"+pm.mapname()+"_2000-2010_"+ens_char+".bin" # oname = "./assim_out/mean_sfcelv/meansfcelvC"+ens_char+".bin" # inputlist.append([iname,oname]) #=========== # mean inputlist = [] for ens in np.arange(1, pm.ens_mem(pm.mode()) + 1): ens_char = "%03d" % (ens) iname = pm.DA_dir() + "/dat/mean_" + pm.stat_name( ) + "_" + ens_char + ".bin" oname = "./assim_out/mean_sfcelv/meansfcelvC" + ens_char + ".bin" inputlist.append([iname, oname]) # do parallel p = Pool(pm.para_nums()) p.map(copy_stat, inputlist) p.terminate() # #=========== # # std # inputlist=[] # for ens in np.arange(1,pm.ens_mem(pm.mode())+1): # ens_char="%03d"%(ens) # if pm.input()=="E2O": # iname = pm.DA_dir()+"/dat/std_"+pm.stat_name()+"_"+ens_char+".bin" # # iname = pm.DA_dir()+"/dat/std_sfcelv_E2O_"+pm.mapname()+"_2000-2010_"+ens_char+".bin" # # iname = pm.DA_dir()+"/dat/std_sfcelv_cal_E2O_"+pm.mapname()+"_2000-2010_"+ens_char+".bin" # oname = "./assim_out/mean_sfcelv/stdsfcelvC"+ens_char+".bin" # if pm.input()=="VIC_BC": # iname = pm.DA_dir()+"/dat/std_"+pm.stat_name()+"_"+ens_char+".bin" # # iname = pm.DA_dir()+"/dat/std_sfcelv_VIC_BC_"+pm.mapname()+"_2000-2010_"+ens_char+".bin" # # iname = pm.DA_dir()+"/dat/std_sfcelv_cal_VIC_BC_"+pm.mapname()+"_2000-2010_"+ens_char+".bin" # oname = "./assim_out/mean_sfcelv/stdsfcelvC"+ens_char+".bin" # inputlist.append([iname,oname]) #=========== # std inputlist = [] for ens in np.arange(1, pm.ens_mem(pm.mode()) + 1): ens_char = "%03d" % (ens) iname = pm.DA_dir() + "/dat/std_" + pm.stat_name( ) + "_" + ens_char + ".bin" oname = "./assim_out/mean_sfcelv/stdsfcelvC" + ens_char + ".bin" inputlist.append([iname, oname]) # do parallel p = Pool(pm.para_nums()) #*cpu_nums()) p.map(copy_stat, inputlist) p.terminate() return 0
def make_fig(point): plt.close() labels = ["HydroWeb", "corrupted", "assimilated"] # print org[:,point] # print "----------------" # print np.mean(asm[:,:,point],axis=1) # for i in np.arange(start,last): # print opn[i,:,point] # print asm[i,:,point] fig, ax1 = plt.subplots() #ax1.plot(np.arange(start,last),org[:,point],label="true",color="black",linewidth=0.7,zorder=101,marker = "o",markevery=swt[point]) time, org = hweb.HydroWeb_WSE(pname[point], syear, eyear) # alti=hweb.altimetry(pname[point]) - EGM08[point] + EGM96[point] data = np.array(org) - np.array(EGM08[point]) + np.array( EGM96[point]) #- alti #==================================== if conflag == 1: data0 = data elif conflag == 2: data0 = data - np.mean( data ) #(data-mean_obs[ylist[point],xlist[point]])+mean_sfcelv[ylist[point],xlist[point]] elif conflag == 3: data0 = (data - np.mean(data)) / ( np.std(data) + 1e-20 ) #((data-mean_obs[ylist[point],xlist[point]])/(std_obs[ylist[point],xlist[point]]+1.0e-20))*std_sfcelv[ylist[point],xlist[point]]+mean_sfcelv[ylist[point],xlist[point]] #==================================== lines = [ ax1.plot(time, data0, label="obs", marker="o", color="#34495e", linewidth=0.0, zorder=101)[0] ] # ax1.plot(np.arange(start,last),org[:,point],label="true",color="black",linewidth=0.7,zorder=101) # ax1.plot(np.arange(start,last),m_sf[:,point],label="mean sfcelv",color="black",linewidth=0.7,linestyle="--",zorder=107) # plt.plot(np.arange(start,last),org[:,point],label="true",color="black",linewidth=0.7) for num in np.arange(0, int(pm.ens_mem())): if conflag == 1: opn0 = opn[:, num, point] asm0 = asm[:, num, point] elif conflag == 2: opn0 = opn[:, num, point] - mean_obss[num, ylist[point], xlist[point]] asm0 = asm[:, num, point] - mean_obss[num, ylist[point], xlist[point]] elif conflag == 3: opn0 = (opn[:, num, point] - mean_obss[num, ylist[point], xlist[point]]) / ( std_obss[num, ylist[point], xlist[point]] + 1e-20) asm0 = (asm[:, num, point] - mean_obss[num, ylist[point], xlist[point]]) / ( std_obss[num, ylist[point], xlist[point]] + 1e-20) #------------------------ ax1.plot(np.arange(start, last), opn0, label="corrupted", color="#4dc7ec", linewidth=0.2, alpha=0.5, zorder=102) ax1.plot(np.arange(start, last), asm0, label="assimilated", color="#ff8021", linewidth=0.3, alpha=0.5, zorder=103) # ax1.plot(np.arange(start,last),em_sf[:,num,point],label="mean sfcelv",color="blue",linewidth=0.3,linestyle="--",alpha=0.5,zorder=103) # plt.plot(np.arange(start,last),opn[:,num,point],label="corrupted",color="blue",linewidth=0.3,alpha=0.5) # plt.plot(np.arange(start,last),asm[:,num,point],label="assimilated",color="red",linewidth=0.3,alpha=0.5) if conflag == 1: opn1 = np.mean(opn[:, :, point], axis=1) asm1 = np.mean(asm[:, :, point], axis=1) elif conflag == 2: opn1 = np.mean(opn[:, :, point], axis=1) - np.mean( mean_obss[:, ylist[point], xlist[point]], axis=0) asm1 = np.mean(asm[:, :, point], axis=1) - np.mean( mean_obss[:, ylist[point], xlist[point]], axis=0) elif conflag == 3: opn1 = (np.mean(opn[:, :, point], axis=1) - np.mean( mean_obss[:, ylist[point], xlist[point]], axis=0)) / (np.mean( std_obss[:, ylist[point], xlist[point]], axis=0) + 1e-20) asm1 = (np.mean(asm[:, :, point], axis=1) - np.mean( mean_obss[:, ylist[point], xlist[point]], axis=0)) / (np.mean( std_obss[:, ylist[point], xlist[point]], axis=0) + 1e-20) lines.append( ax1.plot(np.arange(start, last), opn1, label="corrupted", color="#4dc7ec", linewidth=0.8, alpha=0.8, zorder=102)[0]) lines.append( ax1.plot(np.arange(start, last), asm1, label="assimilated", color="#ff8021", linewidth=0.8, alpha=0.8, zorder=103)[0]) # ax1.plot(np.arange(start,last),np.mean(em_sf[:,:,point],axis=1),label="mean sfelv",color="blue",linewidth=0.5,linestyle="--",alpha=0.5,zorder=103) # plt.ylim(ymin=) meanch = "mean (long-term): %5.2f" % (np.mean( mean_obss[:, ylist[point], xlist[point]], axis=0)) stdch = "std (long-term):%5.2f" % (np.mean( std_obss[:, ylist[point], xlist[point]], axis=0)) omch = "mean (obs): %5.2f" % (np.mean(data)) osch = "std (obs): %5.2f" % (np.std(data)) cmch = "mean (cor): %5.2f" % (np.mean(opn[:, :, point])) csch = "std (cor): %5.2f" % (np.std(opn[:, :, point])) ax1.text(0.02, 0.9, meanch, ha="left", va="center", transform=ax1.transAxes, fontsize=10) ax1.text(0.02, 0.8, stdch, ha="left", va="center", transform=ax1.transAxes, fontsize=10) ax1.text(0.02, 0.7, omch, ha="left", va="center", transform=ax1.transAxes, fontsize=10) ax1.text(0.02, 0.6, osch, ha="left", va="center", transform=ax1.transAxes, fontsize=10) ax1.text(0.02, 0.5, cmch, ha="left", va="center", transform=ax1.transAxes, fontsize=10) ax1.text(0.02, 0.4, csch, ha="left", va="center", transform=ax1.transAxes, fontsize=10) # Make the y-axis label, ticks and tick labels match the line color. ax1.set_ylabel('anomaly WSE (m)', color='k') #ax1.set_ylim(ymin=0,ymax=250.) ax1.set_xlim(xmin=0, xmax=last + 1) ax1.tick_params('y', colors='k') # xxlist=np.linspace(15,N-15,int(N/30)) # xxlab=[calendar.month_name[i][:3] for i in range(1,13)] # ax1.set_xticks(xxlist) # ax1.set_xticklabels(xxlab,fontsize=10) if eyear - syear > 5: dtt = 5 dt = int(math.ceil(((eyear - syear) + 1) / 5.0)) else: dtt = 1 dt = (eyear - syear) + 1 xxlist = np.linspace(0, N, dt, endpoint=True) #xxlab=[calendar.month_name[i][:3] for i in range(1,13)] xxlab = np.arange(syear, eyear + 1, dtt) ax1.set_xticks(xxlist) ax1.set_xticklabels(xxlab, fontsize=10) # ax2 = ax1.twinx() # aiv = stat.AI(asm[:,:,point],opn[:,:,point],org[:,point]) # aivn,error= stat.AI_new(asm[:,:,point],opn[:,:,point],org[:,point])#[0] # fmax = np.finfo(np.float32).max # aivn[aivn>fmax]=0 # aivn[aivn<-fmax]=0 # print np.mean(aivn) # #-- # pBias,pB_c = stat.pBias(asm[:,:,point],opn[:,:,point],org[:,point]) # ai_mean = np.mean(ma.masked_less_equal(aivn,0.0)) #np.nanmean(ma.masked_less_equal(aivn,0.0)) # mai = "meanAI:%1.2f"%(ai_mean) # pB = "pBIAS:%1.1f%%"%(pBias) # print mai # pB, "pB_c", pB_c, "%" # #pB = pB + r'{1.1f}\%'.format(pBias) # ax1.text(0.02,0.9,mai,ha="left",va="center",transform=ax1.transAxes,fontsize=10) # ax1.text(0.02,0.8,pB,ha="left",va="center",transform=ax1.transAxes,fontsize=10) ## ax2.plot(np.arange(start,last),aiv,color="green",zorder=104,marker = "o",alpha=0.3, markevery=swt[point]) # ax2.plot(np.arange(start,last),ma.masked_less_equal(aivn,1.0e-20),color="green",zorder=104,marker = "o", alpha=0.5,linewidth=0.5,markevery=swt[point]) # ax2.set_ylabel('AI', color='green') # ax2.tick_params('y', colors='green') # ax2.set_ylim(ymin=0.,ymax=1.) plt.legend(lines, labels, ncol=1, loc='upper right' ) #, bbox_to_anchor=(1.0, 1.0),transform=ax1.transAxes) # fig.legend(lines,labels,ncol=1,loc='lower left', bbox_to_anchor=(1.0, 1.0)) print( 'save', river[point], re.split("_", pname[point])[2] + "_" + re.split("_", pname[point])[3] ) #,ylist[point],xlist[point],mean_obs[ylist[point],xlist[point]],std_obs[ylist[point],xlist[point]] # print (asm[:,num,point]) #plt.savefig(assim_out+"/figures/sfcelv/"+river[point]+"_"+re.split("_",pname[point])[2]+"_"+re.split("_",pname[point])[3]+".png",dpi=300) plt.savefig(assim_out + "/figures/anomaly/" + pname[point][2::] + ".png", dpi=300) return 0
green2 = "greenyellow" #-------------- nextxy = pm.CaMa_dir() + "/map/" + pm.mapname() + "/nextxy.bin" rivwth = pm.CaMa_dir() + "/map/" + pm.mapname() + "/rivwth_gwdlr.bin" rivhgt = pm.CaMa_dir() + "/map/" + pm.mapname() + "/rivhgt.bin" rivlen = pm.CaMa_dir() + "/map/" + pm.mapname() + "/rivlen.bin" elevtn = pm.CaMa_dir() + "/map/" + pm.mapname() + "/elevtn.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) #------- # mean & std from previous year mean_obss = np.zeros([pm.ens_mem(), ny, nx]) std_obss = np.zeros([pm.ens_mem(), ny, nx]) for num in np.arange(1, int(pm.ens_mem()) + 1): numch = '%03d' % num fname = assim_out + "/assim_out/mean_sfcelv/meansfcelvC" + numch + ".bin" mean_corr = np.fromfile(fname, np.float32).reshape([ny, nx]) mean_obss[num - 1, :, :] = mean_corr fname = assim_out + "/assim_out/mean_sfcelv/stdsfcelvC" + numch + ".bin" std_corr = np.fromfile(fname, np.float32).reshape([ny, nx]) std_obss[num - 1, :, :] = std_corr #---- pname = [] xlist = [] ylist = [] river = []
def prepare_input_old(): # spinup start_year # simulation end_year start_year=pm.spinup_end_year() end_year,end_month,end_date=pm.endtime() #start_year=pm.start_year() #end_year=pm.end_year() start_dt=datetime.date(start_year,1,1) last_dt=datetime.date(end_year,end_month,end_date) start=0 last=int((last_dt-start_dt).days) #-------------- # E2O if pm.input()=="E2O": # Earth2Observe #print "E2O" distopen=pm.distopen(1) diststd=pm.diststd(1) true_run=pm.true_run(1) # for true ensemble runname=pm.runname(1) # get runoff name # make courrpted/assimilated runoff if(len(glob.glob(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff*"))!=0): #print "Roff_CORR available" pass # corrupted input file is not ready # need preparation # make directories mkdir(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR") #print "E2O/Roff" # # # dist std for ensembles # # distopen_ranges={} # # #open_list=np.setdiff1d(np.arange(1,7+1),[true_run]) # # open_list=np.arange(1,7+1) # # random_runs=random.sample(open_list,k=2) # # mkdir(pm.DA_dir()+"/out/"+pm.experiment()+"/assim_out/runoff_error") # # f=open(pm.DA_dir()+"/out/"+pm.experiment()+"/assim_out/runoff_error/ensemble.txt","w") # # for runens in open_list: # # ens_char="%03d"%(runens) # # diststd_num=3 # # distopen_range=rd.normal(1,diststd,diststd_num) # # distopen_range=np.sort(distopen_range) # # distopen_range=distopen_range.astype(np.float32) # # distopen_ranges[ens_char]=distopen_range#[0.25,1.00,1.25] # # line="%s %3.2f %3.2f %3.2f\n"%(ens_char,distopen_range[0],distopen_range[1],distopen_range[2]) # # f.write(line) # # f.close() # # #print "L1141" # # inputlist=[] # # for day in np.arange(start,last): # # target_dt=start_dt+datetime.timedelta(days=day) # # yyyy='%04d' % (target_dt.year) # # mm='%02d' % (target_dt.month) # # dd='%02d' % (target_dt.day) # # ens_num=1 # # for runens in open_list: #np.arange(1,7+1): # # #print runens # # #if runens!=true_run: # # run_num="%03d"%(runens) # # iname=pm.DA_dir()+"/inp/"+runname+"/Roff/Roff__"+yyyy+mm+dd+run_num+".one" # # distopens=distopen_ranges[run_num] # # for dist in distopens: # # ens_char="C%03d"%(ens_num) # # oname=pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff__"+yyyy+mm+dd+ens_char+".one" # # inputlist.append([iname,oname,str(abs(dist))]) # # ens_num=ens_num+1 distopen_ranges={} # fname="random_ensemble_E2O.txt" fname="random_ensemble_E2O_49.txt" with open(pm.DA_dir()+"/dat/"+fname,"r") as f: lines=f.readlines() for line in lines: line = filter(None,re.split(" ", line)) # print line runens = int(line[0]) rndnum = float(line[1].strip("\n")) ens_char="C%03d"%(runens) distopen_ranges[ens_char]=rndnum #print "L1141" inputlist=[] open_list=np.arange(1,7+1) for day in np.arange(start,last): target_dt=start_dt+datetime.timedelta(days=day) yyyy='%04d' % (target_dt.year) mm='%02d' % (target_dt.month) dd='%02d' % (target_dt.day) ens_num=1 for runens in open_list: #np.arange(1,7+1): #print runens #if runens!=true_run: run_num="%03d"%(runens) iname=pm.DA_dir()+"/inp/"+runname+"/Roff/Roff__"+yyyy+mm+dd+run_num+".one" # distopens=distopen_ranges[run_num] # for dist in distopens: for _ in np.arange(1,7+1): ens_char="C%03d"%(ens_num) dist=distopen_ranges[ens_char] oname=pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff__"+yyyy+mm+dd+ens_char+".one" inputlist.append([iname,oname,str(abs(dist))]) ens_num=ens_num+1 # do parallel p=Pool(pm.para_nums()) p.map(copy_runoff,inputlist) p.terminate() #--------- # ERA20CM if pm.input()=="ERA20CM": #ECMWF ERA20CM distopen=pm.distopen(2) diststd=pm.diststd(2) true_run=pm.true_run(2) # for true ensemble runname=pm.runname(2) # get runoff name # make courrpted runoff if(len(glob.glob(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff*"))!=0): pass # corrupted input file is not ready # need preparation # make directories mkdir(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR") #-- # dist std for ensembles distopen_ranges={} #open_list=np.setdiff1d(np.arange(1,10+1),[true_run]) open_list=np.arange(1,10+1) random_runs=random.sample(open_list,k=2) for runens in open_list: ens_char="%03d"%(runens) diststd_num=2 distopen_range=rd.normal(1,diststd,diststd_num) distopen_range=np.sort(distopen_range) distopen_range=distopen_range.astype(np.float32) distopen_ranges[ens_char]=distopen_range#[0.25,1.00,1.25] # inputlist=[] for day in np.arange(start,last): target_dt=start_dt+datetime.timedelta(days=day) yyyy='%04d' % (target_dt.year) mm='%02d' % (target_dt.month) dd='%02d' % (target_dt.day) ens_num=1 for runens in open_list: #np.arange(1,10+1): run_num="%03d"%(runens) iname=pm.DA_dir()+"/inp/"+runname+"/Roff/Roff__"+yyyy+mm+dd+run_num+".one" distopens=distopen_ranges[run_num] for dist in distopens: ens_char="C%03d"%(ens_num) oname=pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff__"+yyyy+mm+dd+ens_char+".one" inputlist.append([iname,oname,str(dist)]) ens_num=ens_num+1 # do parallel p=Pool(pm.para_nums()) p.map(copy_runoff,inputlist) p.terminate() #--------- # VIC BC if pm.input()=="VIC_BC": #VIC BC nXX,nYY=1440,720 distopen=1.0 #pm.distopen(3) diststd=0.25 #pm.diststd(3) true_run=pm.true_run(3) # for true ensemble runname=pm.runname(3) # get runoff name # make courrpted runoff if(len(glob.glob(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff*"))!=0): pass # corrupted input file is not ready # need preparation # make directories mkdir(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR") #-- # dist std for ensembles #distopen_ranges={} # for 12 months distopen_range=np.zeros([12,pm.ens_mem(),nYY,nXX],np.float32) # fname="../../dat/std_runoff_E2O_1980-2000.bin" # #print fname # std_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) # fname="../../dat/mean_runoff_E2O_1980-2000.bin" # #print "L1481",fname # mean_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) # #print mean_runoff # std_runoff=ma.masked_where(std_runoff==-9999.0,std_runoff).filled(0.0) # mean_runoff=ma.masked_where(mean_runoff==-9999.0,mean_runoff).filled(0.0) for mon in range(1,12+1): # for 12 months mm="%02d"%(mon) fname=pm.DA_dir()+"/dat/std_month_runoff_E2O_"+mm+".bin" #print fname std_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) fname=pm.DA_dir()+"/dat/mean_month_runoff_E2O_"+mm+".bin" #print "L1481",fname mean_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) #print mean_runoff std_runoff=ma.masked_where(std_runoff==-9999.0,std_runoff).filled(0.0) mean_runoff=ma.masked_where(mean_runoff==-9999.0,mean_runoff).filled(0.0) for iXX in range(nXX): for iYY in range(nYY): #distopen_range[:,iYY,iXX]=np.sort(rd.normal(distopen,std_runoff[iYY,iXX],pm.ens_mem())) #Log-normal model #sk=np.sort(rd.normal(distopen,diststd,pm.ens_mem())) sk=np.sort(rd.normal(distopen,diststd,pm.ens_mem())) beta=0.0 #E=std_runoff[iYY,iXX]/(mean_runoff[iYY,iXX]+1.0e-20) E=diststd #distopen_range[mon,:,iYY,iXX]=((1+beta)/math.sqrt(E**2+1))*np.exp(math.sqrt(math.log(E**2+1))*sk) distopen_range[mon-1,:,iYY,iXX]=np.sort(rd.normal(distopen,E,pm.ens_mem())) # distopen_range[mon-1,:,iYY,iXX]=rd.normal(distopen,E,pm.ens_mem()) #distopen_range[:,iYY,iXX]=np.sort(rd.normal(distopen,diststd,pm.ens_mem())) #---------- for day in np.arange(start,last): target_dt=start_dt+datetime.timedelta(days=day) mon=int(target_dt.month) yyyy='%04d' % (target_dt.year) mm='%02d' % (target_dt.month) dd='%02d' % (target_dt.day) iname=pm.DA_dir()+"/inp/"+runname+"/Roff/Roff____"+yyyy+mm+dd+".one" #print iname roff=np.fromfile(iname,np.float32).reshape(nYY,nXX) #roff=np.ones([nYY,nXX],np.float32)*-9999.0 fname=pm.DA_dir()+"/dat/std_runoff_E2O_1980-2000.bin" #print fname std_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) fname=pm.DA_dir()+"/dat/mean_runoff_E2O_1980-2000.bin" #print "L1481",fname mean_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) #print mean_runoff std_runoff=ma.masked_where(std_runoff==-9999.0,std_runoff).filled(0.0) mean_runoff=ma.masked_where(mean_runoff==-9999.0,mean_runoff).filled(0.0) for ens_num in np.arange(pm.ens_mem()): ens_char="C%03d"%(ens_num+1) roffc=roff+distopen_range[mon-1,ens_num,:,:]*mean_runoff #*std_runoff # roffc=roff*distopen_range[mon-1,ens_num,:,:] #*mean_runoff oname=pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff__"+yyyy+mm+dd+ens_char+".one" roffc.tofile(oname) #-------------- # -25% biased runoff experiment if pm.input()=="ELSE": # Kim 2009/E2O/ERA20CM #print "-25% biased runoff experiment", pm.true_run(3), pm.runname(3) distopen=pm.distopen(3) diststd=pm.diststd(3) true_run=pm.true_run(3) # for true ensemble runname=pm.runname(3) # get runoff name # copy for TRUE simulation if(len(glob.glob(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_TRUE/Roff*"))!=0): pass # true input file is not ready # need preparation # make directories mkdir(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_TRUE") #print true_run inputlist=[] for day in np.arange(start,last): target_dt=start_dt+datetime.timedelta(days=day) yyyy='%04d' % (target_dt.year) mm='%02d' % (target_dt.month) dd='%02d' % (target_dt.day) ens_char="T000" true_char="%03d"%(true_run) if runname=="ELSE_KIM2009": nx,ny=360,180 prefix="Roff____" suffix=".one" if runname=="E2O": nx,ny=1440,720 prefix="Roff__" suffix="%03d.one"%(true_run) if runname=="ERA20CM": nx,ny=360,180 prefix="Roff__" suffix="%03d.one"%(true_run) iname=pm.DA_dir()+"/inp/"+runname+"/Roff/"+prefix+yyyy+mm+dd+suffix oname=pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_TRUE/Roff__"+yyyy+mm+dd+ens_char+".one" inputlist.append([iname,oname,"1.00"]) # do parallel p=Pool(pm.para_nums()) p.map(copy_runoff,inputlist) p.terminate() # calculate mothly mean cal_monthly_mean(start_year,end_year,24) # calculate monthly total cal_monthly_total(start_year,end_year,24) # make courrpted runoff if(len(glob.glob(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff*"))!=0): pass # corrupted input file is not ready # need preparation # make directories mkdir(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR") inputlist=[] std=rd.normal(0,diststd,pm.ens_mem()) std=std.astype(np.float32) for day in np.arange(start,last): target_dt=start_dt+datetime.timedelta(days=day) yyyy='%04d' % (target_dt.year) mm='%02d' % (target_dt.month) dd='%02d' % (target_dt.day) # make random values #std=np.fromfile("./CaMa_out/randlist.bin",np.float32) #std=rd.normal(0,diststd,pm.ens_mem()) #std=std.astype(np.float32) #print std ifile=np.fromfile(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_TRUE/Roff__"+yyyy+mm+dd+"T000.one",np.float32).reshape(ny,nx) roff_mean=np.fromfile(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/mean_month/mean_"+yyyy+mm+".bin",np.float32).reshape(ny,nx) roff_total=np.fromfile(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/total_month/total_"+yyyy+mm+".bin",np.float32).reshape(ny,nx) for ens in np.arange(1,pm.ens_mem()+1): ens_char="C%03d"%(ens) #print pm.distopen(),std[ens-1] ofile=ifile*distopen + roff_mean*std[ens-1]#*10.0 #ofile=ifile*(distopen + std[ens-1]) #ofile=ifile*distopen + roff_total*std[ens-1] #ofile=ifile*distopen + roff_total*std[ens-1]*0.75 ofile.astype(np.float32) ofile.tofile(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff__"+yyyy+mm+dd+ens_char+".one") # do parallel #p=Pool(pm.para_nums()) #p.map(copy_runoff,inputlist) #p.terminate() #-------------- # blind runoff experiment if pm.input()=="ELSE_KIM2009": # differnt yeaer #distopen=1.0 #0.75 #pm.distopen() #diststd=0.5 #pm.diststd() distopen=pm.distopen(4) diststd=pm.diststd(4) true_run=pm.true_run(4) # for true ensemble runname=pm.runname(4) # get runoff name # copy for TRUE simulation if(len(glob.glob(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_TRUE/Roff*"))!=0): pass # true input file is not ready # need preparation # make directories mkdir(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_TRUE") inputlist=[] for day in np.arange(start,last): target_dt=start_dt+datetime.timedelta(days=day) yyyy='%04d' % (target_dt.year) mm='%02d' % (target_dt.month) dd='%02d' % (target_dt.day) ens_char="T000" iname=pm.DA_dir()+"/inp/"+runname+"/Roff/Roff____"+yyyy+mm+dd+".one" oname=pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_TRUE/Roff__"+yyyy+mm+dd+ens_char+".one" inputlist.append([iname,oname,"1.00"]) # do parallel p=Pool(pm.para_nums()) p.map(copy_runoff,inputlist) p.terminate() # calculate mothly mean cal_monthly_mean(start_year,end_year,24) # calculate monthly total cal_monthly_total(start_year,end_year,24) # make courrpted runoff if(len(glob.glob(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff*"))!=0): pass # corrupted input file is not ready # need preparation # make directories mkdir(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR") inputlist=[] for day in np.arange(start,last): target_dt=start_dt+datetime.timedelta(days=day) yyyy='%04d' % (target_dt.year) mm='%02d' % (target_dt.month) dd='%02d' % (target_dt.day) # make random values #std=np.fromfile("./CaMa_out/randlist.bin",np.float32) std=rd.normal(0,diststd,pm.ens_mem()) std=std.astype(np.float32) #print std ifile=np.fromfile(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff/Roff____"+yyyy+mm+dd+".one",np.float32).reshape(ny,nx) roff_mean=np.fromfile(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/mean_month/mean_"+yyyy+mm+".bin",np.float32).reshape(ny,nx) roff_total=np.fromfile(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/total_month/total_"+yyyy+mm+".bin",np.float32).reshape(ny,nx) for ens in np.arange(1,pm.ens_mem()+1): ens_char="C%03d"%(ens) #print pm.distopen(),std[ens-1] ofile=ifile*distopen + roff_mean*std[ens-1]*10.0 #ofile=ifile*(distopen + std[ens-1]) #ofile=ifile*distopen + roff_total*std[ens-1] #ofile=ifile*distopen + roff_total*std[ens-1]*0.75 ofile.astype(np.float32) ofile.tofile(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff__"+yyyy+mm+dd+ens_char+".one") # do parallel #p=Pool(pm.para_nums()) #p.map(copy_runoff,inputlist) #p.terminate() #--------- # ERA5 if pm.input()=="ERA5": #ERA5-Land nXX,nYY=3600,1800 distopen=pm.distopen(5) diststd=pm.diststd(5) true_run=pm.true_run(3) # for true ensemble runname=pm.runname(3) # get runoff name # make courrpted runoff if(len(glob.glob(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff*"))!=0): pass # corrupted input file is not ready # need preparation # make directories mkdir(pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR") #-- # dist std for ensembles #distopen_ranges={} # for 12 months distopen_range=np.zeros([12,pm.ens_mem(),nYY,nXX],np.float32) # fname="../../dat/std_runoff_E2O_1980-2000.bin" # #print fname # std_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) # fname="../../dat/mean_runoff_E2O_1980-2000.bin" # #print "L1481",fname # mean_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) # #print mean_runoff # std_runoff=ma.masked_where(std_runoff==-9999.0,std_runoff).filled(0.0) # mean_runoff=ma.masked_where(mean_runoff==-9999.0,mean_runoff).filled(0.0) for mon in range(1,12+1): # for 12 months mm="%02d"%(mon) fname=pm.DA_dir()+"/dat/std_month_runoff_E2O_"+mm+".bin" #print fname std_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) fname=pm.DA_dir()+"/dat/mean_month_runoff_E2O_"+mm+".bin" #print "L1481",fname mean_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) #print mean_runoff std_runoff=ma.masked_where(std_runoff==-9999.0,std_runoff).filled(0.0) mean_runoff=ma.masked_where(mean_runoff==-9999.0,mean_runoff).filled(0.0) for iXX in range(nXX): for iYY in range(nYY): #distopen_range[:,iYY,iXX]=np.sort(rd.normal(distopen,std_runoff[iYY,iXX],pm.ens_mem())) #Log-normal model #sk=np.sort(rd.normal(distopen,diststd,pm.ens_mem())) sk=np.sort(rd.normal(distopen,diststd,pm.ens_mem())) beta=0.0 #E=std_runoff[iYY,iXX]/(mean_runoff[iYY,iXX]+1.0e-20) E=diststd #distopen_range[mon,:,iYY,iXX]=((1+beta)/math.sqrt(E**2+1))*np.exp(math.sqrt(math.log(E**2+1))*sk) distopen_range[mon-1,:,iYY,iXX]=np.sort(rd.normal(distopen,E,pm.ens_mem())) # distopen_range[mon-1,:,iYY,iXX]=rd.normal(distopen,E,pm.ens_mem()) #distopen_range[:,iYY,iXX]=np.sort(rd.normal(distopen,diststd,pm.ens_mem())) #---------- for day in np.arange(start,last): target_dt=start_dt+datetime.timedelta(days=day) mon=int(target_dt.month) yyyy='%04d' % (target_dt.year) mm='%02d' % (target_dt.month) dd='%02d' % (target_dt.day) iname=pm.DA_dir()+"/inp/"+runname+"/Roff/Roff____"+yyyy+mm+dd+".sixmin" #print iname roff=np.fromfile(iname,np.float32).reshape(nYY,nXX) #roff=np.ones([nYY,nXX],np.float32)*-9999.0 fname=pm.DA_dir()+"/dat/std_runoff_E2O_1980-2000.bin" #print fname std_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) fname=pm.DA_dir()+"/dat/mean_runoff_E2O_1980-2000.bin" #print "L1481",fname mean_runoff=np.fromfile(fname,np.float32).reshape(nYY,nXX) #print mean_runoff std_runoff=ma.masked_where(std_runoff==-9999.0,std_runoff).filled(0.0) mean_runoff=ma.masked_where(mean_runoff==-9999.0,mean_runoff).filled(0.0) for ens_num in np.arange(pm.ens_mem()): ens_char="C%03d"%(ens_num+1) roffc=roff+distopen_range[mon-1,ens_num,:,:]*mean_runoff #*std_runoff # roffc=roff*distopen_range[mon-1,ens_num,:,:] #*mean_runoff oname=pm.DA_dir()+"/out/"+pm.experiment()+"/CaMa_in/"+runname+"/Roff_CORR/Roff__"+yyyy+mm+dd+ens_char+".one" roffc.tofile(oname) return 0
os.remove(dst) os.symlink(src,dst) else: raise #-- def make_yearlist(syear,smon,sday,N=365): start_dt=datetime.date(syear,smon,sday) fname="year_day.txt" f=open(fname,"w") for day in np.arange(0,N): nw_dt = start_dt + datetime.timedelta(days=day) yyyy,mm,dd = nw_dt.year, nw_dt.month, nw_dt.day yyyymmdd = "%04d%02d%02d\n"%(yyyy,mm,dd) f.write(yyyymmdd) f.close() #-- slink("../gosh/params.py", "params.py") import params as pm # define values N=366 #2004 exp="E2O_womc_anomalyDA_if3.0" outdir=pm.DA_dir()+"/out/"+exp # mkdir stat mk_dir(outdir+"/assim_out/stat") # make list of days #make_yearlist(2004,1,1,N=366) # compile #os.system("ifort calc_stat.f90 -o calc_stat -O3 -assume byterecl -heap-arrays -g -traceback -free -parallel") # run calc_stat os.system("./calc_stat "+str(N)+" "+str(pm.ens_mem())+" "+outdir+" "+pm.CaMa_dir())