gfile = abf.ABFileGrid("regional.grid","r") scpx=gfile.read_field("scpx") scpy=gfile.read_field("scpy") plon=gfile.read_field("plon") plat=gfile.read_field("plat") # lons=plon lats=plat #----------------- read from ab file #---- ERAI TRIP file_TRIP_river=args.Trip_river_afile afile_trip = abf.AFile(800,760,file_TRIP_river,"r") # compute monthly flux: Greenland_trip_discharge_month = np.NaN * np.ones([12,]) Greenland_trip_riverflux=np.zeros((12,760,800)) Green_trip_total_riverflux=np.zeros(lons.shape) countr=0 for record in range(12) : trp_fld = afile_trip.read_record(record) Gr_trp_fld=greenlandmask(trp_fld,lats,lons) Greenland_trip_riverflux[record,:,:]=Gr_trp_fld ## convert from m^3/s km^3/year Gr_trp_fld=Gr_trp_fld*(365*86400.)*1e-9/12 Greenland_trip_discharge_month[record] = np.nansum(Gr_trp_fld*scpx*scpy) Green_trip_total_riverflux=Green_trip_total_riverflux+Gr_trp_fld*scpx*scpy countr=countr+1
def main(path, source_file): print(path) print(source_file) nc = NetCDFFile(source_file, "r") wetNOy = nc.variables["WDEP_NOy"][:, :] wetNHx = nc.variables["WDEP_NHx"][:, :] dryNOy = nc.variables["DDEP_NOy_m2Grid"][:, :] dryNHx = nc.variables["DDEP_NHx_m2Grid"][:, :] lat = nc.variables["lat"][:] lon = nc.variables["lon"][:] Nread = wetNOy + wetNHx + dryNOy + dryNHx abgrid = abf.ABFileGrid(path + "../../../topo/regional.grid", "r") plon = abgrid.read_field("plon") plat = abgrid.read_field("plat") jdm, idm = plon.shape xi, yi = np.meshgrid(lon, lat) N = griddata((xi.flatten(), yi.flatten()), Nread.flatten(), (plon, plat), method='linear') N[np.isnan(N)] = 0. N = N / 365. / 86400. # year --> second N = N / 14.01 * 6.625 * 12.01 # mgN m-2 s-1 --> mgC m-2 s-1 outfile=abf.ABFileRiver(path + "ECO_no3_new.a","w",idm=idm,jdm=jdm,\ cline1='River nitrate fluxes + Atmospheric N deposition',\ cline2='mgC m-2 s-1') outfile.write_header() Nriver = abf.AFile(idm, jdm, path + "ECO_no3.a", "r") for month in range(12): river = Nriver.read_record(month) outfile.write_field(river + N, None, "river nitrate", month + 1) Nriver.close() outfile.close() origAfile = path + "ECO_no3.a" origBfile = path + "ECO_no3.b" oldAfile = path + "ECO_no3_noATM.a" oldBfile = path + "ECO_no3_noATM.b" newAfile = path + "ECO_no3_new.a" newBfile = path + "ECO_no3_new.b" os.rename(origAfile, oldAfile ) # river without atmpspheric deposition is kept as noATM file. os.rename(origBfile, oldBfile ) # river without atmpspheric deposition is kept as noATM file. os.rename( newAfile, origAfile ) # river with atmospheric deposition is renamed to the original file used by hycom os.rename( newBfile, origBfile ) # river with atmospheric deposition is renamed to the original file used by hycom # according to Okin et al, 2011 - doi:10.1029/2010GB003858 # for North Atlantic (northern section) # Ndep = 7.4 TgN/yr # Pdep = 0.02 TgP/yr # ratio --> ( 7.4 / 14.01 * 6.625 ) / ( 0.02 / 31. * 106. ) = 51.2 P = N / 51.2 outfile=abf.ABFileRiver(path + "ECO_pho_new.a","w",idm=idm,jdm=jdm,\ cline1='River phosphate fluxes + Atmospheric P deposition',\ cline2='mgC m-2 s-1') outfile.write_header() Priver = abf.AFile(idm, jdm, path + "ECO_pho.a", "r") for month in range(12): river = Priver.read_record(month) outfile.write_field(river + P, None, "river phosphate", month + 1) Priver.close() outfile.close() origAfile = path + "ECO_pho.a" origBfile = path + "ECO_pho.b" oldAfile = path + "ECO_pho_noATM.a" oldBfile = path + "ECO_pho_noATM.b" newAfile = path + "ECO_pho_new.a" newBfile = path + "ECO_pho_new.b" os.rename(origAfile, oldAfile ) # river without atmpspheric deposition is kept as noATM file. os.rename(origBfile, oldBfile ) # river without atmpspheric deposition is kept as noATM file. os.rename( newAfile, origAfile ) # river with atmospheric deposition is renamed to the original file used by hycom os.rename( newBfile, origBfile ) # river with atmospheric deposition is renamed to the original file used by hycom '''
def main(): # Read plon,plat gfile = abf.ABFileGrid("regional.grid", "r") plon = gfile.read_field("plon") plat = gfile.read_field("plat") gfile.close() # Read input bathymetry bfile = abf.ABFileBathy("regional.depth", "r", idm=gfile.idm, jdm=gfile.jdm, mask=True) in_depth_m = bfile.read_field("depth") bfile.close() #in_depth=numpy.ma.filled(in_depth_m,bathy_threshold) # Starting point (State 1 for Atlantic) kapref = numpy.ones(plat.shape) * 1.0 print kapref.min(), kapref.max() # Find regions north of northern limit. Assumes for segment in range(len(northern_limit_longitudes)): ind1 = segment ind2 = (segment + 1) % len(northern_limit_longitudes) lo1 = northern_limit_longitudes[ind1] la1 = northern_limit_latitudes[ind1] lo2 = northern_limit_longitudes[ind2] la2 = northern_limit_latitudes[ind2] tmp1 = numpy.mod(plon + 360 - lo1, 360.) tmp2 = numpy.mod(lo2 + 360 - lo1, 360.) J = tmp1 <= tmp2 #print numpy.count_nonzero(J) # Linear weights and latitude in selected points w2 = tmp1 / tmp2 w1 = 1. - w2 la = la2 * w2 + la1 * w1 kapref[J] = numpy.where(plat[J] > la[J], 2.0, kapref[J]) import scipy.ndimage kapref = scipy.ndimage.gaussian_filter(kapref, sigma=20) #print in_depth_m.min(),type(in_depth_m) kaprefplot = numpy.ma.masked_where(in_depth_m.mask, kapref) figure = matplotlib.pyplot.figure() ax = figure.add_subplot(111) P = ax.pcolormesh(kaprefplot) figure.colorbar(P, ax=ax) figure.canvas.print_figure("kapref.png") af = abf.AFile(plon.shape[1], plon.shape[0], "tbaric.a", "w") hmin, hmax = af.writerecord(kapref, None, record=0) af.close() bf = open("tbaric.b", "w") bf.write("tbaric.b\n") bf.write("\n") bf.write("\n") bf.write("\n") bf.write("i/jdm = %5d %5d\n" % (plon.shape[1], plon.shape[0])) bf.write("tbaric: range = %14.6e%14.6e\n" % (hmin, hmax)) bf.close()
def main(lon1,lat1,lon2,lat2,variable,files,filetype="archive",clim=None,sectionid="", ijspace=False,xaxis="distance",section_map=False,dpi=180) : TP4Grd='/cluster/work/users/aal069/TP4a0.12/mfile/' logger.info("Filetype is %s"% filetype) gfile = abf.ABFileGrid("regional.grid","r") plon=gfile.read_field("plon") plat=gfile.read_field("plat") # Set up section info if ijspace : sec = gridxsec.SectionIJSpace([lon1,lon2],[lat1,lat2],plon,plat) else : sec = gridxsec.Section([lon1,lon2],[lat1,lat2],plon,plat) I,J=sec.grid_indexes dist=sec.distance print('dit.shae=',dist.shape) slon=sec.longitude slat=sec.latitude # In testing #J,I,slon,slat,case,dist=sec.find_intersection(qlon,qlat) #print I,J #raise NameError,"test" logger.info("Min max I-index (starts from 0):%d %d"%(I.min(),I.max())) logger.info("Min max J-index (starts from 0):%d %d"%(J.min(),J.max())) # # if section_map : ll_lon=slon.min()-10. ur_lon=slon.max()+10. ll_lat=np.maximum(-90.,slat.min()-10.) ur_lat=np.minimum(90. ,slat.max()+10.) proj=ccrs.Stereographic(central_latitude=90.0,central_longitude=-40.0) #pxy = proj.transform_points(ccrs.PlateCarree(), plon, plat) #px=pxy[:,:,0] #py=pxy[:,:,1] #x,y=np.meshgrid(np.arange(slon.shape[0]),np.arange(slat.shape[0])) figure =plt.figure(figsize=(8,8)) ax=figure.add_subplot(111,projection=proj) #ax = plt.axes(projection=ccrs.PlateCarree()) ax.set_extent([-179, 179, 53, 85],ccrs.PlateCarree()) #ax = plt.axes(projection=ccrs.Stereographic()) ax.add_feature(cfeature.GSHHSFeature('auto', edgecolor='grey')) ax.add_feature(cfeature.GSHHSFeature('auto', facecolor='grey')) ax.gridlines() #ax.coastlines(resolution='110m') ax.plot(slon,slat,"r-",lw=1,transform=ccrs.PlateCarree()) pos = ax.get_position() asp=pos.height/pos.width w=figure.get_figwidth() h=asp*w figure.set_figheight(h) if sectionid : figure.canvas.print_figure("map_%s.png"%sectionid,dpi=dpi,bbox_inches='tight') else : figure.canvas.print_figure("map.png",dpi=dpi,bbox_inches='tight') # Get layer thickness variable used in hycom dpname = modeltools.hycom.layer_thickness_variable[filetype] logger.info("Filetype %s: layer thickness variable is %s"%(filetype,dpname)) if xaxis == "distance" : x=dist/1000. xlab="Distance along section[km]" elif xaxis == "i" : x=I xlab="i-index" elif xaxis == "j" : x=J xlab="j-index" elif xaxis == "lon" : x=slon xlab="longitude" elif xaxis == "lat" : x=slat xlab="latitude" else : logger.warning("xaxis must be i,j,lo,lat or distance") x=dist/1000. xlab="Distance along section[km]" # get kdm from the first file: # Remove [ab] ending if present myfile0=files[0] print( 'myfile0', myfile0) m=re.match("(.*)\.[ab]",myfile0) print('m=',m.group(1)) if m : myfile=m.group(1) else : myfile=myfile0 dta_afile = abf.AFile(gfile.idm,gfile.jdm,myfile0,"r") #intfl='../../../relax/010/relax_int.a' intfl=myfile[:-3] + 'int.a' int_afile = abf.AFile(gfile.idm,gfile.jdm,intfl,"r") # lyr=1 #record_num,xmn,xmx=dta_afile.get_record_number(variable,lyr) #print 'record_num, variable, layer ===', record_num-1, variable, lyr #print 'mn,mx=',xmn,xmx # for record in record_num : record_num=1 record_var=record_num-1 fld = dta_afile.read_record(record_var) print('mn,mx data=',fld.min(),fld.max()) #np.testing.assert_approx_equal(xmn,fld.min(),significant=8) #np.testing.assert_approx_equal(xmx,fld.max(),significant=8) # presure interfc #record_num,xmn,xmx=int_afile.get_record_number('int',lyr) #print 'record_num, variable, layer ===', record_num-1, 'int', lyr #print 'mn,mx=',xmn,xmx # for record in record_num : record_prs=record_num-1 fld = int_afile.read_record(record_prs) print('mn,mx intface=',fld.min(),fld.max()) #np.testing.assert_approx_equal(xmn,fld.min(),significant=8) #np.testing.assert_approx_equal(xmx,fld.max(),significant=8) # #kdm=max(fi_abfile.fieldlevels) kdm=50 # Loop over archive files figure = plt.figure() ax=figure.add_subplot(111) pos = ax.get_position() count_sum=0 intfsec_sum=np.zeros((kdm+1,I.size)) datasec_sum=np.zeros((kdm+1,I.size)) # for mnth in range(12) : count_sum=count_sum+1 logger.info("Reading data and presure interface for month %s"%(mnth+1)) record_var_pnt=record_var + mnth*kdm record_prs_pnt=record_prs + mnth*kdm print('pointing at record num: record_var_pnt', record_var_pnt) print('pointing at record num: record_prs_pnt', record_prs_pnt) # Set up interface and daat arrays xx=np.zeros((kdm+1,I.size)) intfsec=np.zeros((kdm+1,I.size)) datasec=np.zeros((kdm+1,I.size)) # Loop over layers in file. logger.info("File %s"%(myfile)) logger.info("intfac_File %s"%(intfl)) #loop over 50 records for k in range(kdm) : logger.debug("File %s, layer %03d/%03d"%(myfile,k,kdm)) r_var=record_var_pnt+k r_prs=record_prs_pnt+k # Get 2D fields dp2d = int_afile.read_record(r_prs) data2d=dta_afile.read_record(r_var) #print('reading rec num: r_var,r_dp=', r_var, r_prs,' ','data.min,data.max=',data2d.min(),data2d.max()) print('data: month,layer, range', (mnth+1),'',(r_var)%kdm,data2d.min(),data2d.max()) #dp2d=i_abfile.read_field(dpname,k+1) #data2d=i_abfile.read_field(variable,k+1) if ((r_var)%kdm==49): print('mn,mx intface=',dp2d.min(),dp2d.max()) print('mn,mx data=', data2d.min(),data2d.max()) print( "Reach bottom layer" ) dp2d=np.ma.filled(dp2d,0.)/modeltools.hycom.onem data2d=np.ma.filled(data2d,1e30) # Place data into section arrays #intfsec[k+1,:] = intfsec[k,:] + dp2d[J,I] #print("data2d.shape=",data2d.shape) #print("data2d[J,I].size=",data2d[J,I].size) intfsec[k+1,:] = dp2d[J,I] if k==0 : datasec[k,:] = data2d[J,I] datasec[k+1,:] = data2d[J,I] intfsec_sum=intfsec_sum + intfsec datasec_sum=datasec_sum + datasec #print 'prs_intafce=', np.transpose(intfsec[:,15]) dta_afile.close() int_afile.close() # end loop over files print ('count_sum=',count_sum) intfsec_avg=intfsec_sum/count_sum datasec_avg=datasec_sum/count_sum # i_maxd=np.argmax(np.abs(intfsec_avg[kdm,:])) #print i_maxd for k in range(kdm+1) : xx[k,:] = x[:] # Set up section plot #datasec = np.ma.masked_where(datasec==1e30,datasec) datasec_avg = np.ma.masked_where(datasec_avg>0.5*1e30,datasec_avg) #print datasec.min(),datasec.max() #P=ax.pcolormesh(dist/1000.,-intfsec,datasec) #print i_maxd for k in range(kdm+1) : xx[k,:] = x[:] if clim is not None : lvls = MaxNLocator(nbins=30).tick_values(clim[0], clim[1]) #print 'levels=', lvls mf='sawtooth_0-1.txt' LinDic=mod_hyc2plot.cmap_dict(mf) my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',LinDic) cmap=my_cmap #cmap = plt.get_cmap('gist_rainbow_r') norm = BoundaryNorm(lvls, ncolors=cmap.N, clip=True) print('x.shape=' , x.shape) print('x.min,xmax=' , x.min(),x.max()) print('xx.shape=' , xx.shape) print('xx.min,xxmax=' , xx.min(),xx.max()) print('intfsec_avg.shape=', intfsec_avg.shape) print('datasec_avg.shape=', datasec_avg.shape) #P=ax.pcolormesh(x,-intfsec,datasec,cmap=cmap) P=ax.contourf(xx,-intfsec_avg,datasec_avg,extend='both',cmap=cmap,levels=lvls) if 'sal' in variable: P1=ax.contour(xx,-intfsec_avg,datasec_avg,levels=[32.0,33.0,34.0,35.0,35.5], colors=('k',),linestyles=('-',),linewidths=(1.5,)) else: P1=ax.contour(xx,-intfsec_avg,datasec_avg,levels=[-1.0,0.0,2.0], colors=('k',),linestyles=('-',),linewidths=(1.5,)) plt.clabel(P1, fmt = '%2.1d', colors = 'k', fontsize=10) #contour line labels # Plot layer interfaces for k in range(1,kdm+1) : if k%100 == 0 : PL=ax.plot(x,-intfsec_avg[k,:],"-",color="k") elif k%5 == 0 and k <= 10: PL=ax.plot(x,-intfsec_avg[k,:],"--",color="k", linewidth=0.5) textx = x[i_maxd] texty = -0.5*(intfsec_avg[k-1,i_maxd] + intfsec_avg[k,i_maxd]) ax.text(textx,texty,str(k),verticalalignment="center",horizontalalignment="center",fontsize=6) elif k%2 == 0 and k > 10: PL=ax.plot(x,-intfsec_avg[k,:],"--",color="k", linewidth=0.5) textx = x[i_maxd] texty = -0.5*(intfsec_avg[k-1,i_maxd] + intfsec_avg[k,i_maxd]) ax.text(textx,texty,str(k),verticalalignment="center",horizontalalignment="center",fontsize=6) ### else : ### PL=ax.plot(x,-intfsec_avg[k,:],"-",color=".5") # Print figure and remove wite space. aspect = 50 pad_fraction = 0.25 divider = make_axes_locatable(ax) width = axes_size.AxesY(ax, aspect=1./aspect) pad = axes_size.Fraction(pad_fraction, width) cax = divider.append_axes("right", size=width, pad=pad) cb=ax.figure.colorbar(P,cax=cax,extend='both') #cb=ax.figure.colorbar(P,extend='both') if clim is not None : P.set_clim(clim) #cb=ax.figure.colorbar(P,extend='both') ax.set_title(variable+':'+myfile+'AVG-') ax.set_ylabel('Depth [m]') ax.set_xlabel(xlab) #ax.set_position(pos) #plt.tight_layout() # Print in different y-lims suff=os.path.basename(myfile) if sectionid : suff=suff+"_"+sectionid figure.canvas.print_figure("sec_AVG_%s_full_%s.png"%(variable,suff),dpi=dpi) if 'Fram' in sectionid or 'Svin' in sectionid: ax.set_ylim(-600,0) figure.canvas.print_figure("sec_AVG_%s_600m_%s.png"%(variable,suff),dpi=dpi) else : ax.set_ylim(-3000,0) figure.canvas.print_figure("sec_AVG_%s_3000m_%s.png"%(variable,suff),dpi=dpi) #ax.set_ylim(-600,0) #figure.canvas.print_figure("sec_AVG_%s_600m_%s.png"%(variable,suff),dpi=dpi) # Close input file #i_abfile.close() # ax.clear() cb.remove()
def main(myfiles, fieldname, fieldlevel, idm=None, jdm=None, clim=None, filetype="archive", window=None, cmap="jet", datetime1=None, datetime2=None, vector="", tokml=False, masklim=None, filename2='', dpi=180): cmap = matplotlib.pyplot.get_cmap("jet") ab = abf.ABFileGrid("regional.grid", "r") plon = ab.read_field("plon") plat = ab.read_field("plat") scpx = ab.read_field("scpx") scpy = ab.read_field("scpy") target_lonlats = [plon, plat] abdpth = abf.ABFileBathy('regional.depth', "r", idm=ab.idm, jdm=ab.jdm) mdpth = abdpth.read_field('depth') maskd = mdpth.data maskd[maskd > 1e29] = np.nan #Region_mask=True Region_mask = False if Region_mask: maskd[plat > 80] = np.nan maskd[plat < 50] = np.nan maskd[plon > 60] = np.nan maskd[plon < -50] = np.nan Nordic_mask = maskd proj = ccrs.Stereographic(central_latitude=90.0, central_longitude=-40.0) pxy = proj.transform_points(ccrs.PlateCarree(), plon, plat) px = pxy[:, :, 0] py = pxy[:, :, 1] x, y = np.meshgrid(np.arange(plon.shape[1]), np.arange(plon.shape[0])) if vector: logger.info("Vector component 1:%s" % fieldname) logger.info("Vector component 2:%s" % vector) #---------------first read and compute clim Err_map = 1 freezp = -1.8 sum_fld1 = maskd sum_fld1[~np.isnan(sum_fld1)] = 0.0 Clim_arr = np.zeros((plon.shape[0], plon.shape[1], 12)) #--------------- # compute for TP6 files #----------------------------------------- #--------------------------------------------------------------------------------- #--------------------------------------------------------------------------------- # filename2 onemm = 9.806 counter = 0 file_count = 0 sum_fld1 = maskd sum_fld1[~np.isnan(sum_fld1)] = 0.0 dt_cnl = np.zeros(len(myfiles)) diff_dt_cnl = np.zeros(len(myfiles)) rmse_dt_cnl = np.zeros(len(myfiles)) Labl1 = "CNTL SST" Labl1 = myfiles[0][:28] yyyy1 = myfiles[0][-9:-5] if "archv." in myfiles[0]: yyyy1 = myfiles[0][-13:-9] print("myfiles[0]=", myfiles[0]) print("yyy1=", yyyy1) if filename2: dt_2 = np.zeros(len(filename2)) diff_dt_2 = np.zeros(len(filename2)) rmse_dt_2 = np.zeros(len(filename2)) tid_2=np.array([datetime.datetime(int(yyyy1), 1, 15) \ + relativedelta(months=i) for i in range(len(filename2))]) Labl2 = filename2[0][:28] counter = 0 file_count = 0 sum_fld1 = maskd sum_fld1[~np.isnan(sum_fld1)] = 0.0 if "srfhgt" in fieldname: fieldname = "srfhgt" elif "temp" in fieldname: fieldname = "temp" for fil0 in filename2: logger.info("Now processing %s" % fil0) n_intloop,ab,ab2,rdtimes = open_file(fil0,filetype,fieldname,fieldlevel,\ datetime1=datetime1,datetime2=datetime2,vector=vector,idm=idm,jdm=jdm) # Intloop used to read more fields in one file. Only for forcing for now for i_intloop in range(n_intloop): # Read ab file of different types if filetype == "archive": fld1 = ab.read_field(fieldname, fieldlevel) elif filetype == "forcing": fld1 = ab.read_field(fieldname, rdtimes[i_intloop]) if vector: fld2 = ab2.read_field(vector, rdtimes[i_intloop]) logger.info("Processing time %.2f" % rdtimes[i_intloop]) else: raise NotImplementedError("Filetype %s not implemented" % filetype) # Create scalar field for vectors print('---------mn,mx data=', fld1.min(), fld1.max()) #if "srfhgt" in fieldname: # fld1= fld1/9.806 print("fld1.shpe", fld1.shape) print('mn,mx=', fld1.min(), fld1.max(), 'count=', counter) dt_2[counter] = np.nanmean(fld1) counter = counter + 1 sum_fld1 = sum_fld1 + fld1 del fld1 #--------------------------------------------------------------------------------- #--------------------------------------------------------------------------------- base = datetime.datetime(int(yyyy1), 1, 15) tid = np.array( [base + relativedelta(months=i) for i in range(len(myfiles))]) if "archv." in myfiles[0]: tid = np.array( [base + relativedelta(days=i) for i in range(len(myfiles))]) nmexp = 1 if filename2: nmexp = nmexp + 1 print( 'processing data from No runs ==##############>>>>>>>>>>>>>>>>>>>>>>>', nmexp) whole_domain = True whole_domain = False # counter = 0 file_count = 0 sum_fld1 = maskd sum_fld1[~np.isnan(sum_fld1)] = 0.0 logger.info( ">>>>>--------------------------Processing the first files= myfiles") for myfile0 in myfiles: logger.info("Now processing %s" % myfile0) n_intloop,ab,ab2,rdtimes = open_file(myfile0,filetype,fieldname,fieldlevel,\ datetime1=datetime1,datetime2=datetime2,vector=vector,idm=idm,jdm=jdm) # Intloop used to read more fields in one file. Only for forcing for now for i_intloop in range(n_intloop): # Read ab file of different types if filetype == "archive": fld1 = ab.read_field(fieldname, fieldlevel) if ('temp' in fieldname) and whole_domain: vert_fld_sum = 0 for lvl in range(50): print('lvl=', lvl, fieldlevel) fld_lvl = ab.read_field(fieldname, lvl + 1) vert_fld_sum = vert_fld_sum + np.nanmean(fld_lvl) vert_fld_avg = vert_fld_sum / 50.0 elif filetype == "forcing": fld1 = ab.read_field(fieldname, rdtimes[i_intloop]) if vector: fld2 = ab2.read_field(vector, rdtimes[i_intloop]) logger.info("Processing time %.2f" % rdtimes[i_intloop]) else: raise NotImplementedError("Filetype %s not implemented" % filetype) # Create scalar field for vectors print('---------mn,mx data=', fld1.min(), fld1.max()) #if "srfhgt" in fieldname: # fld1= fld1/9.806 print("fld1.shpe", fld1.shape) print('mn,mx=', fld1.min(), fld1.max(), 'count=', counter) if ('temp' in fieldname) and whole_domain: dt_cnl[counter] = vert_fld_avg else: dt_cnl[counter] = np.nanmean(fld1) counter = counter + 1 sum_fld1 = sum_fld1 + fld1 del fld1 # End i_intloop print('Computing the avearge of file_counter= ', file_count, 'counter=', counter) #--------------------------------------- #--------------------------------------- #plot_climatology Clim_arr = np.zeros((plon.shape[0], plon.shape[1], 12)) if 'tem' in fieldname: counter = 0 rlxfile0 = "/cluster/work/users/achoth/TP5a0.06/relax/050/relax_tem.a" rlx_afile = abf.AFile(ab.idm, ab.jdm, rlxfile0, "r") lyr = fieldlevel record_num = lyr record_var = record_num - 1 fld = rlx_afile.read_record(record_var) print('mn,mx data=', fld.min(), fld.max()) kdm = 50 dt_clim = np.zeros(12) for mnth in range(12): fld1 = rlx_afile.read_record(mnth * kdm + lyr - 1) logger.debug("File %s, record_var/mnth*kdm %03d/%03d" % (rlxfile0, record_var, mnth * kdm)) print('record, mn,mx data=', kdm * mnth, fld1.min(), fld1.max()) print('record, mn,mx data=', kdm * mnth, fld1.min(), fld1.max()) # Intloop used to read more fields in one file. Only for forcing for now dt_clim[mnth] = np.nanmean(fld1) #Clim_arr[:,:,mnth]=fld1[:,:] counter = counter + 1 print('counter=', counter) del fld1 # tid_clim = np.array( [base + relativedelta(months=i) for i in range(12)]) #figure, ax = matplotlib.pyplot.figure() rpt = len(dt_cnl) / 12 dt_clim_cat = dt_clim for ii in range(int(rpt - 1)): print("concatenate ") dt_clim_cat = np.concatenate([dt_clim_cat, dt_clim]) # #--------------------------------------- #--------------------------------------- figure, ax = plt.subplots() years = YearLocator() # every year months = MonthLocator() # every month yearsFmt = DateFormatter('%Y') #ax=figure.add_subplot(111) nplts = 1 ax.plot_date(tid, dt_cnl, '-o', color='g', ms=3, label=Labl1) if 'tem' in fieldname: ax.plot_date(tid[0:len(dt_cnl)], dt_clim_cat[:], ':', color='black', label='Phc-Clim.') if filename2: ax.plot_date(tid_2, dt_2, '-v', color='orange', ms=3, label=Labl2) ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) ax.xaxis.set_minor_locator(months) ax.autoscale_view() # format the coords message box def price(x): return '$%1.2f' % x ax.fmt_xdata = DateFormatter('%Y-%m-%d') ax.fmt_ydata = price ax.grid(True) figure.autofmt_xdate() legend = plt.legend(loc='upper left', fontsize=8) plt.title("Area-averaged: %s(%d)" % (fieldname, fieldlevel)) plt.ylabel("%s(%d)" % (fieldname, fieldlevel)) if "k.e" in fieldname: fieldname = "KE" if "u-vel" in fieldname: fieldname = "u-vel" ts_fil = "Time_series_cntl%s_%02d_%02d" % (fieldname, fieldlevel, counter) if Region_mask: ts_fil = 'Region_' + ts_fil figure.canvas.print_figure(ts_fil, bbox_inches='tight', dpi=dpi) logger.info("Successfull printing: %s" % ts_fil)
def main(myfiles, fieldname, fieldlevel, idm=None, jdm=None, clim=None, filetype="archive", window=None, cmap="jet", datetime1=None, datetime2=None, vector="", tokml=False, masklim=None, filename2='', filename5='', dpi=180): LinDic = mod_hyc2plot.cmap_dict('sawtooth_fc100.txt') if 'temp' or 'sal' in fieldname: cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', LinDic) else: cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', LinDic) if tokml: ab = abf.ABFileGrid("regional.grid", "r") plon = ab.read_field("plon") plat = ab.read_field("plat") ab.close() ab = abf.ABFileGrid("regional.grid", "r") plon = ab.read_field("plon") plat = ab.read_field("plat") scpx = ab.read_field("scpx") scpy = ab.read_field("scpy") target_lonlats = [plon, plat] abdpth = abf.ABFileBathy('regional.depth', "r", idm=ab.idm, jdm=ab.jdm) mdpth = abdpth.read_field('depth') maskd = mdpth.data maskd[maskd > 1e29] = np.nan Region_mask = True Region_mask = False if Region_mask: maskd[plat > 70] = np.nan #maskd[plat<50]=np.nan maskd[plon > 20] = np.nan maskd[plon < -30] = np.nan Nordic_mask = maskd proj = ccrs.Stereographic(central_latitude=90.0, central_longitude=-40.0) pxy = proj.transform_points(ccrs.PlateCarree(), plon, plat) px = pxy[:, :, 0] py = pxy[:, :, 1] x, y = np.meshgrid(np.arange(plon.shape[1]), np.arange(plon.shape[0])) if vector: logger.info("Vector component 1:%s" % fieldname) logger.info("Vector component 2:%s" % vector) #---------------first read and compute clim Err_map = 1 sum_fld1 = maskd sum_fld1[~np.isnan(sum_fld1)] = 0.0 Clim_arr = np.zeros((plon.shape[0], plon.shape[1], 12)) if 'tem' or 'sal' in fieldname: counter = 0 if 'tem' in fieldname: rlxfile0 = "/cluster/work/users/achoth/TP5a0.06/relax/050/relax_tem.a" if 'sal' in fieldname: rlxfile0 = "/cluster/work/users/achoth/TP5a0.06/relax/050/relax_sal.a" rlx_afile = abf.AFile(ab.idm, ab.jdm, rlxfile0, "r") lyr = fieldlevel record_num = 1 record_var = record_num - 1 fld = rlx_afile.read_record(record_var) print('mn,mx data='), fld.min(), fld.max() kdm = 50 dt_clim = np.zeros(12) for mnth in range(12): fld1 = rlx_afile.read_record(mnth * kdm + lyr - 1) print('record, mn,mx data='), kdm * mnth, fld1.min(), fld1.max() # Intloop used to read more fields in one file. Only for forcing for now dt_clim[mnth] = mod_hyc2plot.spatiomean(fld1, maskd) sum_fld1 = sum_fld1 + fld1 Clim_arr[:, :, mnth] = fld1[:, :] counter = counter + 1 print('counter='), counter del fld1 Clim_Avg = sum_fld1 / counter del sum_fld1 #---------------filename figure = matplotlib.pyplot.figure(figsize=(8, 8)) ax = figure.add_subplot(111) onemm = 9.806 counter = 0 file_count = 0 sum_fld1 = maskd sum_fld1[~np.isnan(sum_fld1)] = 0.0 dt_cnl = np.zeros(len(myfiles)) diff_dt_cnl = np.zeros(len(myfiles)) rmse_dt_cnl = np.zeros(len(myfiles)) Labl1 = "Model: " + fieldname if "SPRBAS_0" in myfiles[0]: Labl1 = "CNTL: prsbas=0 " if filename2: dt_2 = np.zeros(len(filename2)) diff_dt_2 = np.zeros(len(filename2)) rmse_dt_2 = np.zeros(len(filename2)) yyyy1 = filename2[0][-9:-5] print("filename2[0]="), filename2[0][-9:-5] print("filename2[0]="), filename2[0] print("yyy1="), yyyy1 tid_2=np.array([datetime.datetime(int(yyyy1), 1, 15) \ + relativedelta(months=i) for i in range(len(filename2))]) Labl2 = "filename2" Labl2 = "Corrected" if "erai" in filename2[0]: Labl2 = "CNTL: prsbas=1e5 " yyyy1cnt = myfiles[0][-8:-5] print("myfiles[0]="), myfiles[0][-9:-5] print("myfiles[0]="), myfiles[0] print("yyy1cnt="), print(yyyy1cnt) base = datetime.datetime(int(yyyy1cnt), 1, 15) tid = np.array( [base + relativedelta(months=i) for i in range(len(myfiles))]) if len(myfiles) == 36: base = datetime.datetime(int(yyyy1cnt), 1, 15) tid = np.array( [base + relativedelta(months=i) for i in range(len(myfiles))]) nmexp = 1 if filename2: nmexp = nmexp + 1 print( 'processing data from No runs ==##############>>>>>>>>>>>>>>>>>>>>>>>' ), nmexp for iii in range(nmexp): counter = 0 file_count = 0 sum_fld1 = maskd sum_fld1[~np.isnan(sum_fld1)] = 0.0 if iii == 1 and filename2: myfiles = filename2 else: logger.info( ">>>>>--------------------------Processing the first files= %d<<<<" % iii) logger.info( ">>>>>--------------------------Processing the first files= %d<<<<" % iii) for myfile0 in myfiles: # Open files, and return some useful stuff. # ab2 i used in case of vector # rdtimes is used for plotting forcing fields n_intloop,ab,ab2,rdtimes = open_file(myfile0,filetype,fieldname,fieldlevel,\ datetime1=datetime1,datetime2=datetime2,vector=vector,idm=idm,jdm=jdm) # Intloop used to read more fields in one file. Only for forcing for now for i_intloop in range(n_intloop): # Read ab file of different types if filetype == "archive": fld1 = ab.read_field(fieldname, fieldlevel) elif filetype == "forcing": fld1 = ab.read_field(fieldname, rdtimes[i_intloop]) if vector: fld2 = ab2.read_field(vector, rdtimes[i_intloop]) logger.info("Processing time %.2f" % rdtimes[i_intloop]) else: raise NotImplementedError("Filetype %s not implemented" % filetype) if not window: J, I = np.meshgrid(np.arange(fld1.shape[0]), np.arange(fld1.shape[1])) else: J, I = np.meshgrid(np.arange(window[1], window[3]), np.arange(window[0], window[2])) # Create scalar field for vectors if vector: fld = np.sqrt(fld1**2 + fld2**2) else: fld = fld1 print('---------mn,mx data='), fld.min(), fld.max() sum_fld1 = sum_fld1 + fld cindx = np.remainder(counter, 12) print("counter"), counter, print("cindx="), cindx if iii == 0: dt_cnl[counter] = mod_hyc2plot.spatiomean(fld, Nordic_mask) diff_dt_cnl[counter] = mod_hyc2plot.spatiomean( fld[:, :] - Clim_arr[:, :, cindx], Nordic_mask) rmse_dt_cnl[counter] = np.sqrt( mod_hyc2plot.spatiomean( (fld[:, :] - Clim_arr[:, :, cindx])**2, Nordic_mask)) Labl = Labl1 if iii == 1 and filename2: dt_2[counter] = mod_hyc2plot.spatiomean(fld, Nordic_mask) diff_dt_2[counter] = mod_hyc2plot.spatiomean( fld[:, :] - Clim_arr[:, :, cindx], Nordic_mask) rmse_dt_2[counter] = np.sqrt( mod_hyc2plot.spatiomean( (fld[:, :] - Clim_arr[:, :, cindx])**2, Nordic_mask)) Labl = Labl2 # Apply mask if requested counter = counter + 1 file_count = file_count + 1 del fld # End i_intloop print('Computing the avearge of file_counter= '), print( file_count), print('counter='), print(counter), print( 'cindx='), print(cindx) if file_count > 0: fld_Avg = sum_fld1 / file_count if Err_map: cmap = cmocean.cm.balance fld_diff = fld_Avg - Clim_Avg if fieldname == 'k.e.': P = ax.pcolormesh(x[J, I], y[J, I], np.log10(fld_Avg[J, I]), cmap=cmap) elif fieldname == 'srfhgt': P = ax.pcolormesh(x[J, I], y[J, I], (fld_Avg[J, I] / onemm), cmap=cmap) else: P = ax.pcolormesh(x[J, I], y[J, I], fld_diff[J, I], cmap=cmap) if 'temp' in fieldname: P1 = ax.contour(x[J, I], y[J, I], fld_diff[J, I], levels=[-1., 1, 4.0, 8], colors=('w', ), linestyles=('-', ), linewidths=(1.5, )) matplotlib.pyplot.clabel(P1, fmt='%2.1d', colors='w', fontsize=10) #contour line labels # Print figure. aspect = 40 pad_fraction = 0.25 divider = make_axes_locatable(ax) width = axes_size.AxesY(ax, aspect=1. / aspect) pad = axes_size.Fraction(pad_fraction, width) cax = divider.append_axes("right", size=width, pad=pad) cb = ax.figure.colorbar(P, cax=cax, extend='both') if clim is not None: P.set_clim(clim) ax.set_title("Diff:%s(%d)" % (fieldname, fieldlevel) + ' :( Model - Clim )') # Print figure. fnamepng_template = myfiles[0][-20:-5].replace( "/", '') + "_Avg_TP6_%s_%d_%03d_iii%03d_Avg.png" if Region_mask: fnamepng_template='Region_'+yyyy1cnt+myfiles[0][1:11].replace("/",'') \ +"Avg_TP5_%s_%d_%03d_iii%03d_Avg.png" fnamepng = fnamepng_template % (fieldname, fieldlevel, counter, iii) logger.info("output in %s" % fnamepng) figure.canvas.print_figure(fnamepng, bbox_inches='tight', dpi=dpi) ax.clear() cb.remove() datmen = np.nanmean(fld_diff) spatiodatmen = mod_hyc2plot.spatiomean(fld_diff, Nordic_mask) print('-----------mean diff data, spatio='), datmen, spatiodatmen del sum_fld1 #--------------------------------------- print('tid len='), print(tid.shape) if filename2: print('dt_2='), print(dt_2.shape) tid_clim = np.array([base + relativedelta(months=i) for i in range(12)]) figure, ax = plt.subplots() rpt = len(dt_cnl) / 12 dt_clim_cat = dt_clim for ii in range(int(rpt - 1)): print("concatenate ") dt_clim_cat = np.concatenate([dt_clim_cat, dt_clim]) years = YearLocator() # every year months = MonthLocator() # every month yearsFmt = DateFormatter('%Y') nplts = 1 ax.plot_date(tid, dt_cnl, '-o', color='g', ms=3, label=Labl1) if filename2: ax.plot_date(tid_2, dt_2, '-v', color='orange', ms=3, label=Labl2) nplts = nplts + 1 if filename5: ax.plot_date(tid_5, dt_5, '--', color='m', label=Labl5) nplts = nplts + 1 if 'tem' or 'sal' in fieldname: ax.plot_date(tid[0:len(dt_cnl)], dt_clim_cat[:], ':', color='black', label='Clim:' + fieldname) nplts = nplts + 1 ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) ax.xaxis.set_minor_locator(months) ax.autoscale_view() # format the coords message box def price(x): return '$%1.2f' % x ax.fmt_xdata = DateFormatter('%Y-%m-%d') ax.fmt_ydata = price ax.grid(True) figure.autofmt_xdate() legend = plt.legend(loc='upper left', fontsize=8) plt.title("Area-averaged: %s(%d)" % (fieldname, fieldlevel)) plt.ylabel("%s(%d)" % (fieldname, fieldlevel)) ts_fil = "time_series_cntl_flx_%s_%02d_%02d" % (fieldname, fieldlevel, nplts) if Region_mask: ts_fil = 'Region_' + ts_fil figure.canvas.print_figure(ts_fil, bbox_inches='tight', dpi=dpi) logger.info("Successfull printing: %s" % ts_fil) #----------------- # plot short mean error figure, ax = plt.subplots() print("diff_dt_cnl[:]="), diff_dt_cnl[:] nplts = 1 ll = -1 * len(tid) if filename2: ll = -1 * len(tid) ax.plot_date(tid[ll:], diff_dt_cnl[ll:], '-o', color='g', ms=3) if filename2: ax.plot_date(tid_2, diff_dt_2, '-v', color='orange', ms=3, label=Labl2) nplts = nplts + 1 ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) ax.xaxis.set_minor_locator(months) ax.autoscale_view() # format the coords message box def price(x): return '$%1.2f' % x ax.fmt_xdata = DateFormatter('%Y-%m-%d') ax.fmt_ydata = price ax.grid(True) figure.autofmt_xdate() legend = plt.legend(loc='upper left', fontsize=8) plt.title("Mean diff:Model-Clim: %s(%d)" % (fieldname, fieldlevel)) plt.ylabel("diff:%s(%d)" % (fieldname, fieldlevel)) ts_fil = 'Mdiff' + "ST_cntl_flx_%s_%02d_%02d" % (fieldname, fieldlevel, nplts) if Region_mask: ts_fil = 'Region_Mdiff' + "ST_cntl_flx_%s_%02d_%02d" % ( fieldname, fieldlevel, nplts) figure.canvas.print_figure(ts_fil, bbox_inches='tight', dpi=dpi) logger.info("Successfull printing: %s" % ts_fil) # plot rooot mean square RMSE error figure, ax = plt.subplots() nplts = 1 ll = -1 * len(tid) if filename2: ll = -1 * len(tid_2) ax.plot_date(tid[ll:], rmse_dt_cnl[ll:], '-o', color='g', ms=3) if filename2: ax.plot_date(tid_2, rmse_dt_2, '-v', color='orange', ms=3, label=Labl2) nplts = nplts + 1 ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) ax.xaxis.set_minor_locator(months) ax.autoscale_view() # format the coords message box def price(x): return '$%1.2f' % x ax.fmt_xdata = DateFormatter('%Y-%m-%d') ax.fmt_ydata = price ax.grid(True) figure.autofmt_xdate() legend = plt.legend(loc='upper left', fontsize=8) plt.title("RMSE: (Model-Clim) %s(%d)" % (fieldname, fieldlevel)) plt.ylabel("RMSE:%s(%d)" % (fieldname, fieldlevel)) ts_fil = 'RMSE' + "ST_cntl_flx_%s_%02d_%02d" % (fieldname, fieldlevel, nplts) if Region_mask: ts_fil = 'Region_RMSE' + "ST2007_cntl_flx_%s_%02d_%02d" % ( fieldname, fieldlevel, nplts) figure.canvas.print_figure(ts_fil, bbox_inches='tight', dpi=dpi) logger.info("Successfull printing: %s" % ts_fil) logger.info( "End --------------------------------------------------------------- printing: %s" % ts_fil)