def test_afile_writeread_nomask(self): idm = random.randrange(10, 5000) jdm = random.randrange(10, 5000) #print "Creating %dX%d random arrays"%(idm,jdm) scale = 1e4 wfld1 = numpy.random.rand(jdm, idm) * scale wfld2 = numpy.random.rand(jdm, idm) * scale wfld3 = numpy.random.rand(jdm, idm) * scale afile = abfile.AFile(idm, jdm, "test.a", "w") afile.writerecord(wfld1, None, record=None) afile.writerecord(wfld2, None, record=None) afile.writerecord(wfld3, None, record=None) afile.close() afile = abfile.AFile(idm, jdm, "test.a", "r") rfld1 = afile.read_record(record=0) rfld2 = afile.read_record(record=1) rfld3 = afile.read_record(record=2) afile.close() maxdiff1 = numpy.abs(numpy.amax(rfld1 - wfld1)) / scale maxdiff2 = numpy.abs(numpy.amax(rfld2 - wfld2)) / scale maxdiff3 = numpy.abs(numpy.amax(rfld3 - wfld3)) / scale #print maxdiff1,maxdiff2,maxdiff3 if all([elem < 1e-7 for elem in [maxdiff1, maxdiff2, maxdiff3]]): #print "afile io test passed for %d x %d array " %(idm,jdm) pass else: self.fail( "AFile IO failed. MAx diff between read/written: %14.7g" % max([maxdiff1, maxdiff2, maxdiff3]))
ch.setLevel(_loglevel) ch.setFormatter(formatter) logger.addHandler(ch) logger.propagate = False if __name__ == "__main__": parser = argparse.ArgumentParser(description='') parser.add_argument('idm', type=int, help='Grid dimension 1st index []') parser.add_argument('jdm', type=int, help='Grid dimension 2nd index []') parser.add_argument('filename', help="") parser.add_argument('records', nargs="+", type=int) args = parser.parse_args() #afile = modeltools.hycom.io.AFile(args.idm,args.jdm,args.filename,"r") afile = abfile.AFile(args.idm, args.jdm, args.filename, "r") cmap = matplotlib.pyplot.get_cmap("jet") #cmap=matplotlib.pyplot.get_cmap("YlOrRd") for record in args.records: fld = afile.read_record(record) figure = matplotlib.pyplot.figure(figsize=(8, 8)) ax = figure.add_subplot(111) #P=ax.pcolormesh(fld) #P=ax.pcolormesh(fld[2200:2800,3500:4500],cmap=cmap) P = ax.pcolormesh(fld, cmap=cmap) ax.figure.colorbar(P) #figure.colorbar(P,norm=matplotlib.colors.LogNorm(vmin=w5.min(), vmax=w5.max()))
def main(rmuwidth,exptid="") : #fh = Dataset('mld_dr003_l3.nc', mode='r') #fh_lons = fh.variables['lon'][:] #fh_lats = fh.variables['lat'][:] #fh_time = fh.variables['time'][:] #fh_mld_clim = fh.variables['mld_dr003_rmoutliers_smth_okrg'][:] #fh.close() grdpath='./' #gfile = abfile.ABFileGrid(jip+"regional.grid","r") gfile = abfile.ABFileGrid("regional.grid","r") idm=gfile.idm jdm=gfile.jdm #gfile = abfile.ABFileGrid("regional.grid","r") plon=gfile.read_field("plon") plat=gfile.read_field("plat") gfile.close() dpfile = abfile.ABFileBathy("regional.depth","r",idm=gfile.idm,jdm=gfile.jdm) depth=dpfile.read_field("depth") dpfile.close() # read from nc file #wdth=40 wdth=rmuwidth mnn=0.02 mxx=0.125 thkdf4_array=numpy.zeros(depth.shape) thkdf4_array[:,:]=mnn ddd_mnn_mxx=np.linspace(mnn, mxx, num=wdth) ddd_mxx_mnn=numpy.flip(ddd_mnn_mxx[:]) #print("ddd_E=",ddd_mnn_mxx[:]) ##AA thkdf4_array[:,:]=0.015 ##AA ddd=[(0.015+ii*0.0055) for ii in range(20)] #print "dd=",ddd[:] print("thkdf4_array.shape=",thkdf4_array.shape) for jjj in range(thkdf4_array.shape[0]) : thkdf4_array[jjj, 0:wdth] =ddd_mxx_mnn[:] thkdf4_array[jjj,-1*wdth:]=ddd_mnn_mxx[:] for iii in range(thkdf4_array.shape[1]): thkdf4_array[0:wdth ,iii]=ddd_mxx_mnn[:] thkdf4_array[-1*wdth:,iii]=ddd_mnn_mxx[:] # #fix lower left corner for iii in range(wdth): for jjj in range(iii,wdth): thkdf4_array[jjj,iii]=ddd_mxx_mnn[iii] #print(thkdf4_array[jjj,iii]) #fix upper left corner for iii in range(wdth): for jjj in range(jdm-iii-1,jdm-wdth-1,-1): thkdf4_array[jjj,iii]=ddd_mxx_mnn[iii] #fix lower right corner LL = range(idm-wdth,idm) LL_r=LL[::-1] for iii in LL_r[:]: for jjj in range(idm-iii-1,wdth): #print("jjj,iii=",jjj,iii) thkdf4_array[jjj,iii]=ddd_mxx_mnn[idm-iii-1] #fix upper right corner for iii in LL_r[:]: for jjj in range(jdm-1-(idm-iii-1),jdm-1-wdth,-1): thkdf4_array[jjj,iii]=ddd_mxx_mnn[idm-iii-1] #print '-----------------' #write thkdf4 to ab file af = abfile.AFile(plon.shape[1],plon.shape[0],"thkdf4.a","w") hmin,hmax = af.writerecord(thkdf4_array,None,record=0) af.close() print("thkdf4: range = %14.6e%14.6e\n"%(hmin,hmax)) bf = open("thkdf4.b","w") bf.write("thkdf4: range = %14.6e%14.6e\n"%(hmin,hmax)) bf.close() #print '-----------------' #write veldf4 to ab file af1 = abfile.AFile(plon.shape[1],plon.shape[0],"veldf4.a","w") hmin,hmax = af1.writerecord(thkdf4_array,None,record=0) af1.close() print("veldf4: range = %14.6e%14.6e\n"%(hmin,hmax)) bf1 = open("veldf4.b","w") bf1.write("veldf4: range = %14.6e%14.6e\n"%(hmin,hmax)) bf1.close() #print '-----------------' print("write to NC file------") ncfilename='thkdf4.nc' rootgrp = Dataset(ncfilename, "w", format="NETCDF4") logger.info("output to ncfile in %s"%ncfilename) #dimension lat = rootgrp.createDimension("lat", gfile.jdm) lon = rootgrp.createDimension("lon", gfile.idm) #variable thkdf4_v = rootgrp.createVariable("thkdf4","f4",("lat","lon",)) print('thkdf4_v.shape=',thkdf4_v.shape) thkdf4_v[:,:]=thkdf4_array[:,:] rootgrp.close()
def main(region,experiment,tracer,month,cmap,clim,workdir): user = getpass.getuser() if region == 'TP0' or region == 'TP2' or region == 'TP5' or region == 'NAT' : version = 'new' elif region == 'TP4' or region == 'NA2': version = 'old' else : print('wrong or not implemented domain is provided') quit() if tracer == "nitrate" or tracer == 'phosphate' or \ tracer == 'silicate' or tracer == "volume" : pass else : print('tracer name not correct') print('choose: nitrate, phosphate, silicate, volume') quit() units = "(mgC m$^{-2}$ s$^{-1}$), nutrient converted to C" if version == "new": if tracer == "nitrate": name = "ECO_no3" elif tracer == "phosphate": name = "ECO_pho" elif tracer == "silicate": name = "ECO_sil" else: name = "rivers" units = "(m s$^{-1}$)" if version == "old": if tracer == "nitrate": name = "rivnitr" elif tracer == "phosphate": name = "rivphos" elif tracer == "silicate": name = "rivsili" else: name = "rivers" units = "(m s$^{-1}$)" if region == "TP0": region = "TP0a1.00" if region == "TP2": region = "TP2a0.10" if region == "TP4": region = "TP4a0.12" if region == "TP5": region = "TP5a0.06" if region == "NAT": region = "NATa1.00" if region == "NA2": region = "NA2a0.80" abgrid = abfile.ABFileGrid(workdir + user + "/" + \ region + "/topo/regional.grid","r") plon=abgrid.read_field("plon") plat=abgrid.read_field("plat") jdm,idm=plon.shape abriver = abfile.AFile(idm,jdm,workdir + user + "/" + \ region + "/force/rivers/" + experiment + \ "/" + name + ".a","r") river = abriver.read_record(int(month)-1) abriver.close() if version == "old": if tracer == "nitrate": river = river * 12.01 * 6.625 / 14.007 elif tracer == "phosphate": river = river * 12.01 * 106. / 30.97 elif tracer == "silicate": river = river * 12.01 * 106. / 28.09 abdepth = abfile.ABFileBathy(workdir + user + "/" + \ region + "/force/rivers/SCRATCH/regional.depth.b","r",idm=idm,jdm=jdm) depthm=abdepth.read_field("depth") river = np.ma.masked_where(depthm.mask,river) # now plot fig=plt.figure(figsize=(6,5),facecolor='w') ax = fig.add_subplot(1,1,1) ax.set_position([0.01,0.02,0.865,0.96]) cmap = plt.get_cmap(cmap) ax.set_facecolor('xkcd:gray') pmesh = plt.pcolormesh(river,cmap=cmap) cb=ax.figure.colorbar(pmesh) if clim is not None : pmesh.set_clim(clim) plt.text(0.015,1.05, "%s %s %s %s" %("river", tracer, "flux month:",\ month.zfill(2)),transform=ax.transAxes,fontsize=13) plt.text(0.015,1.005, units,transform=ax.transAxes,fontsize=8) # save figure fig.canvas.print_figure(workdir + user + "/" + \ region + "/force/rivers/" + experiment + \ "/" + name + ".png",dpi=180)
def main(): # Read plon,plat gfile = abfile.ABFileGrid("regional.grid", "r") plon = gfile.read_field("plon") plat = gfile.read_field("plat") gfile.close() # Read input bathymetry bfile = abfile.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 = abfile.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()
plat=gfile.read_field("plat") # # #read lons from rivers.nc form A-Ehype #file_AEhype_river_nc="./SCRATCH_Ahype/rivers.nc" #fh_ahype = Dataset(file_AEhype_river_nc, mode='r') lons=plon lats=plat #lons = fh_ahype.variables['modlon'][:] #lats = fh_ahype.variables['modlat'][:] #fh_ahype.close() #----------------- read from ab file #---- ERAI TRIP #file_TRIP_river="./SCRATCH_ERAI-TRIP/rivers.a" file_TRIP_river=args.Trip_river_afile afile_trip = abfile.AFile(800,760,file_TRIP_river,"r") # compute monthly flux: #tripafil_discharge_month = np.NaN * np.ones([12,]) 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) #uns_discharge_month[record] = np.nansum(uns_riverflux[record,:,:]*scpx*scpy) Green_trip_total_riverflux=Green_trip_total_riverflux+Gr_trp_fld*scpx*scpy
def main(path): print(path) # coordinates for the river mouth based on the grid location on TP5 domain river_lat = 66.37281 river_lon = 71.15888 # coordinates for the original river discharge effective area #(lat,lon)_1 = 71.496376,72.00326 #(lat,lon)_2 = 70.26682,82.36286 #(lat,lon)_3 = 62.861946,67.83625 #(lat,lon)_4 = 62.23391,75.26198 abgrid = abfile.ABFileGrid(path + "../../../topo/regional.grid", "r") plon = abgrid.read_field("plon") plat = abgrid.read_field("plat") jdm, idm = plon.shape abdepth = abfile.ABFileBathy(path + "../../../topo/regional.depth.b", "r", idm=idm, jdm=jdm) depthm = abdepth.read_field("depth") # cooINDEX = abs(plat - river_lat) + abs(plon - river_lon) la, lo = np.unravel_index(cooINDEX.argmin(), cooINDEX.shape) # cooINDEX = abs(plat - 71.496376) + abs(plon - 72.00326) la1, lo1 = np.unravel_index(cooINDEX.argmin(), cooINDEX.shape) cooINDEX = abs(plat - 70.26682) + abs(plon - 82.36286) la2, lo2 = np.unravel_index(cooINDEX.argmin(), cooINDEX.shape) cooINDEX = abs(plat - 62.861946) + abs(plon - 67.83625) la3, lo3 = np.unravel_index(cooINDEX.argmin(), cooINDEX.shape) cooINDEX = abs(plat - 62.23391) + abs(plon - 75.26198) la4, lo4 = np.unravel_index(cooINDEX.argmin(), cooINDEX.shape) dist = np.zeros((plon.shape)) for j in range(plon.shape[0] - 1): for i in range(plon.shape[1] - 1): dist[j, i] = distance_on_unit_sphere(plat[j, i], plon[j, i], plat[la, lo], plon[la, lo]) dist = np.ma.masked_where(depthm < 0., dist) dist = np.ma.masked_where(plat >= 74., dist) dist = np.ma.masked_where(plon <= 70., dist) dist = np.ma.masked_where(plon >= 90., dist) dist = dist / dist.max() dist = np.abs(dist - 1.) totaldist = np.sum(dist) # now modify rivers for 3 nutrients for varib in ['no3', 'pho', 'sil']: if varib == 'no3': longname = 'nitrate' if varib == 'pho': longname = 'phosphate' if varib == 'no3': longname = 'silicate' outfile=abfile.ABFileRiver(path + "ECO_"+varib+"_new.a","w",idm=idm,jdm=jdm,\ cline1='River '+longname+' fluxes (Ob River dispersed out the bay)',\ cline2='mgC m-2 s-1') outfile.write_header() abriver = abfile.AFile(idm, jdm, path + "ECO_" + varib + ".a", "r") for month in range(12): river = abriver.read_record(month) river_modified = abriver.read_record(month) river = np.ma.masked_where(depthm < 0., river) river_modified = np.ma.masked_where(depthm < 0., river_modified) unit = np.sum(river[la1:la2, lo2:lo4]) / totaldist final = unit * dist river_modified[la1:la2, lo2:lo4][~river_modified[la1:la2, lo2:lo4].mask] = 0. river_modified[ ~final.mask] = river_modified[~final.mask] + final[~final.mask] print("original total mass: " + str(np.sum(river)) + " modified total mass: " + str(np.sum(river_modified))) outfile.write_field(river_modified, None, "river " + longname, month + 1) abriver.close() outfile.close() origAfile = path + "ECO_" + varib + ".a" origBfile = path + "ECO_" + varib + ".b" oldAfile = path + "ECO_" + varib + "_orig.a" oldBfile = path + "ECO_" + varib + "_orig.b" newAfile = path + "ECO_" + varib + "_new.a" newBfile = path + "ECO_" + varib + "_new.b" os.rename(origAfile, oldAfile) os.rename(origBfile, oldBfile) os.rename(newAfile, origAfile) os.rename(newBfile, origBfile)