def test_abfilebathy_writeread_nomask(self): idm = random.randrange(10, 5000) jdm = random.randrange(10, 5000) scale = 1e4 #print "Creating %dX%d bathy array"%(idm,jdm) wfldout = scale + numpy.random.rand(jdm, idm) * scale bathyfile = abfile.ABFileBathy("testbathy", "w") bathyfile.write_field(wfldout, None) bathyfile.close() #print "Reading %dX%d bathy array"%(idm,jdm) bathyfile = abfile.ABFileBathy("testbathy", "r", idm=idm, jdm=jdm) wfldin = bathyfile.read_field("depth", None) bathyfile.close() fldmaxdiff = numpy.abs(numpy.amax(wfldin - wfldout)) / scale bmin, bmax = bathyfile.bminmax("depth") amax = numpy.amax(wfldin) amin = numpy.amin(wfldin) abmindiff = numpy.abs(amin - bmin) / scale abmaxdiff = numpy.abs(amax - bmax) / scale if not (fldmaxdiff > 1e-7 and abmindiff > 1e-5 and abmaxdiff > 1e-5): pass else: self.fail( "AFile IO failed. MAx diff between read/written: %14.7g" % max([fldmaxdiff, abmaxdiff, abmindiff]))
def main(archv_files,regress_file,opath,depth_file,header_line1=cline1,header_line2=cline2,header_line3=cline3 ) : regr = open(regress_file,'rb') slope,intercept,nemomeandt = pickle.load(regr,encoding="latin1") bp=modeltools.hycom.BlkdatParser("blkdat.input") idm = bp["idm"] jdm = bp["jdm"] kdm = bp["kdm"] iversn = bp["iversn"] yrflag = bp["yrflag"] iexpt = bp["iexpt"] # get domain depth abdepth = abfile.ABFileBathy(depth_file, \ "r",idm=idm,jdm=jdm) depthm=abdepth.read_field("depth") for archv_file in archv_files : logger.debug("Processing %s"%(archv_file)) arcfile=abfile.ABFileArchv(archv_file,"r") srfhgt=arcfile.read_field("srfhgt",0) montg1=arcfile.read_field("montg1",0) montg1=(srfhgt-nemomeandt) * slope + intercept montg1=np.where(np.isnan(montg1), 0, montg1) dummy = arcfile.read_field("temp",1) dummy[~depthm.mask] = montg1[~depthm.mask] # your fixed montg1 logger.info("Montg Minimum") print(dummy.min()) logger.info("Montg Maximum") print(dummy.max()) # logger.info("Estimated montg1 ") fnameout = opath+str(archv_file[-19:]) arcfile_out=abfile.ABFileArchv(fnameout,"w",iversn=iversn,yrflag=yrflag,iexpt=iexpt,mask=False,cline1=header_line1,cline2=header_line2,cline3=header_line3) for keys in sorted( arcfile.fields.keys() ) : fieldname = arcfile.fields[keys]["field"] time_step = arcfile.fields[keys]["step"] model_day = arcfile.fields[keys]["day"] k = arcfile.fields[keys]["k"] dens = arcfile.fields[keys]["dens"] fld = arcfile.read_field(fieldname,k) if fieldname == "montg1" : logger.info("Writing field %10s at level %3d to %s (modified)"%(fieldname,k,fnameout)) arcfile_out.write_field(dummy,None,fieldname,time_step,model_day,k,dens) else : arcfile_out.write_field(fld ,None,fieldname,time_step,model_day,k,dens) logger.info("Finished writing to %s"%fnameout) arcfile_out.close() arcfile.close() archv_file_b = archv_file[0:-1] + 'b' fnameout_b = fnameout[0:-1] + 'b' os.rename(fnameout,archv_file) # replaces the new archive files with the original ones in nest folder os.rename(fnameout_b,archv_file_b)
def open_file(myfile0, filetype, fieldname, fieldlevel, datetime1=None, datetime2=None, vector="", idm=None, jdm=None): logger.info("Now processing %s" % myfile0) m = re.match("(.*)\.[ab]", myfile0) if m: myfile = m.group(1) else: myfile = myfile0 ab2 = None rdtimes = [] if filetype == "archive": ab = abfile.ABFileArchv(myfile, "r") n_intloop = 1 #elif filetype == "restart" : # tmp = abfile.ABFileRestart(myfile,"r",idm=gfile.idm,jdm=gfile.jdm) elif filetype == "regional.depth": ab = abfile.ABFileBathy(myfile, "r", idm=idm, jdm=jdm) n_intloop = 1 elif filetype == "forcing": ab = abfile.ABFileForcing(myfile, "r", idm=idm, jdm=jdm) if vector: file2 = myfile.replace(fieldname, vector) logger.info("Opening file %s for vector component nr 2" % file2) ab2 = abfile.ABFileForcing(file2, "r", idm=idm, jdm=jdm) if datetime1 is None or datetime2 is None: raise NameError, "datetime1 and datetime2 must be specified when plotting forcing files" else: iday1, ihour1, isec1 = modeltools.hycom.datetime_to_ordinal( datetime1, 3) rdtime1 = modeltools.hycom.dayfor(datetime1.year, iday1, ihour1, 3) # iday2, ihour2, isec2 = modeltools.hycom.datetime_to_ordinal( datetime2, 3) rdtime2 = modeltools.hycom.dayfor(datetime2.year, iday2, ihour2, 3) rdtimes = sorted([ elem for elem in ab.field_times if elem > rdtime1 and elem < rdtime2 ]) n_intloop = len(rdtimes) else: raise NotImplementedError, "Filetype %s not implemented" % filetype # Check that fieldname is actually in file if fieldname not in ab.fieldnames: logger.error("Unknown field %s at level %d" % (fieldname, fieldlevel)) logger.error("Available fields : %s" % (" ".join(ab.fieldnames))) raise ValueError, "Unknown field %s at level %d" % (fieldname, fieldlevel) return n_intloop, ab, ab2, rdtimes
def main(intopo) : # 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(intopo,"r",idm=gfile.idm,jdm=gfile.jdm,mask=True) in_depth_m=bfile.read_field("depth") bfile.close() # Print to CICE mask files kmt=numpy.where(~in_depth_m.mask,1.,0.) modeltools.cice.io.write_netcdf_kmt(kmt,"cice_kmt.nc")
def main(region,experiment,tracer,month,axis,layer,cmap,clim,workdir,\ section,ijspace): 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 axis == 'horizontal' or axis == 'vertical': pass else: print('provide arguments "horizontal" or "vertical"') quit() 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" if tracer == "nitrate" or tracer == 'phosphate' or \ tracer == 'silicate' or tracer == "oxygen" or \ tracer == 'dic' or tracer == "alkalinity" or \ tracer == 'temperature' or tracer == "salinity" : pass else: print('tracer name not correct') print('choose: nitrate, phosphate, silicate, \ oxygen, dic, alkalinity, temperature, salinity') quit() units = "(mgC m$^{-2}$ s$^{-1}$), tracer converted to C" key = "trc" # trc for tracers, below tem and sal is set if necessary if version == "new": if tracer == "nitrate": name = "relax.ECO_no3" elif tracer == "phosphate": name = "relax.ECO_pho" elif tracer == "silicate": name = "relax.ECO_sil" elif tracer == "oxygen": name = "relax.ECO_oxy" units = "mmol m$^{-3}$" elif tracer == "dic": name = "relax.CO2_dic" units = "mmol m$^{-3}$" elif tracer == "alkalinity": name = "relax.CO2_alk" units = "mEq m$^{-3}$" elif tracer == "temperature": name = "relax_tem" key = "tem" units = "$^{o}C$" elif tracer == "salinity": name = "relax_sal" key = "sal" units = "psu" if version == "old": if tracer == "nitrate": name = "relax_nit" elif tracer == "phosphate": name = "relax_pho" elif tracer == "silicate": name = "relax_sil" elif tracer == "oxygen": name = "relax_oxy" units = "mmol m$^{-3}$" elif tracer == "dic": name = "relax_dic" units = "mmol m$^{-3}$" elif tracer == "alkalinity": name = "relax_alk" units = "mEq m$^{-3}$" elif tracer == "temperature": name = "relax_tem" key = "tem" units = "$^{o}C$" elif tracer == "salinity": name = "relax_sal" key = "sal" units = "psu" abgrid = abfile.ABFileGrid(workdir + user + "/" + \ region + "/topo/regional.grid","r") plon = abgrid.read_field("plon") plat = abgrid.read_field("plat") jdm, idm = plon.shape abdepth = abfile.ABFileBathy(workdir + user + "/" + \ region + "/relax/" + experiment + "/SCRATCH/regional.depth.b", \ "r",idm=idm,jdm=jdm) depthm = abdepth.read_field("depth") abrelax = abfile.ABFileRelax(workdir + user + "/" + \ region + "/relax/" + experiment + \ "/" + name + ".a","r") # 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') if axis == 'horizontal': if layer is None: print(" ") print("provide the layer number to be plotted, e.g. --layer=1") print("quitting ...") print(" ") quit() else: relax = abrelax.read_field(key, np.int(layer), np.int(month) - 1) abrelax.close() pmesh = plt.pcolormesh(relax, cmap=cmap) cb = ax.figure.colorbar(pmesh) if clim is not None: pmesh.set_clim(clim) if axis == 'vertical': if section is None: print(" ") print("provide the section to be plotted") print("--section='lon1,lon2,lat1,lat2'") print("--section='-30.1,2.5,50.0,75.5'") print("quitting ...") print(" ") quit() else: lon1 = section[0] lon2 = section[1] lat1 = section[2] lat2 = section[3] # pick up indexes 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 slon = sec.longitude slat = sec.latitude dpname = modeltools.hycom.layer_thickness_variable["archive"] # get arbitrary relaxation thicknesses dummy = sorted(fnmatch.filter(\ os.listdir(workdir + user + "/" + \ region + "/relax/" + experiment), \ 'relax.0000_[012]*_00.a')) dummyarch = workdir + user + "/" + \ region + "/relax/" + experiment + \ "/" + dummy[np.int(month)-1] dummyfile = abfile.ABFileArchv(dummyarch, "r") kdm = max(dummyfile.fieldlevels) intfsec = np.zeros((kdm + 1, I.size)) datasec = np.zeros((kdm + 1, I.size)) for k in range(kdm): dp2d = dummyfile.read_field(dpname, k + 1) data2d = abrelax.read_field(key, k + 1, np.int(month) - 1) dp2d = np.ma.filled(dp2d, 0.) dp2d = dp2d / modeltools.hycom.onem data2d = np.ma.filled(data2d, 1e30) intfsec[k + 1, :] = intfsec[k, :] + dp2d[J, I] if k == 0: datasec[k, :] = data2d[J, I] datasec[k + 1, :] = data2d[J, I] datasec = np.ma.masked_where(datasec > 0.5 * 1e30, datasec) x = dist / 1000. # km pmesh = ax.pcolormesh(x, -intfsec, datasec, cmap=cmap) cb = ax.figure.colorbar(pmesh) if clim is not None: pmesh.set_clim(clim) axm = fig.add_subplot(212) axm.set_position([0.85, 0.85, 0.15, 0.15]) axm.set_facecolor('xkcd:gray') pmesh2 = plt.pcolormesh(dummyfile.read_field(dpname, 1) * 0., cmap=cmap) pltsec = plt.plot(I, J, 'r', lw=2) plt.xticks([]) plt.yticks([]) plt.text(0.015,1.05, "%s %s %s" %(tracer, "relaxation month:",\ month.zfill(2)),transform=ax.transAxes,FontSize=13) if axis == 'horizontal': plt.text(0.015, 1.005, units + " @layer=" + layer, transform=ax.transAxes, FontSize=8) else: plt.text(0.015, 1.005, units, transform=ax.transAxes, FontSize=8) # save figure counter = 0 plottemp = workdir + user + "/" + \ region + "/relax/" + experiment + \ "/" + name + "_" + axis + "_%s" + ".png" plotname = plottemp % (np.str(counter).zfill(3)) while os.path.isfile(plotname): counter += 1 plotname = plottemp % (np.str(counter).zfill(3)) fig.canvas.print_figure(plotname, dpi=180) print(" ") print("figure: " + plotname) print(" ")
def main(infile, rmu_width, rmu_efold, dpi=180): bathy_threshold = 0. # TODO # Read plon,plat gfile = abfile.ABFileGrid("regional.grid", "r") plon = gfile.read_field("plon") plat = gfile.read_field("plat") gfile.close() # Read input bathymetri m = re.match("^(.*)(\.[ab])", infile) if m: infile = m.group(1) bfile = abfile.ABFileBathy(infile, "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) #print in_depth.min(),in_depth.max() ip = ~in_depth_m.mask ip = numpy.copy(ip) iu = numpy.copy(ip) iv = numpy.copy(ip) iu[:, 1:] = numpy.logical_and(ip[:, 1:], ip[:, :-1]) iv[1:, :] = numpy.logical_and(ip[1:, :], ip[:-1, :]) ifports = [] ilports = [] jfports = [] jlports = [] kdports = [] process_south = True process_north = True process_west = True process_east = True fatal = False rmumask = numpy.zeros(in_depth.shape) labels = numpy.zeros(in_depth.shape) # Test ocean mask in 2nd grid cell from edge. If ocean, mark as nesting boundary. # When written to ports.input, we write # NB: All diag output is "Fortran" indexes (Starting from 1) - thats why we add 1 here and there for kdport in [1, 2, 3, 4]: t_ifports, t_ilports, t_jfports, t_jlports, t_kdports, t_labels, = port_setup( kdport, in_depth_m) labels[t_labels > 0] = t_labels[t_labels > 0] + labels.max() ifports.extend(t_ifports) ilports.extend(t_ilports) jfports.extend(t_jfports) jlports.extend(t_jlports) kdports.extend(t_kdports) # Build mask for i in range(len(ifports)): rmumask = relaxation_mask(rmumask, ifports[i], ilports[i], jfports[i], jlports[i], kdports[i], rmu_width) #print rmumask.min(),rmumask.max() rmumask = numpy.minimum(rmumask, 1.) * 1. / (rmu_efold * 86400.) rmumask_m = numpy.ma.masked_where(in_depth_m.mask, rmumask) # Check consistency fatal = False for i in range(len(ifports)): fatal = fatal or check_consistency(ifports[i], ilports[i], jfports[i], jlports[i], kdports[i], iu, iv, i + 1) # Open port output file logger.info("Writing to ports.input.tmp") fid = open("ports.input.tmp", "w") fid.write("%6d 'nports' = Number of ports \n" % len(kdports)) for i in range(len(kdports)): write_port_location(fid, kdports[i], ifports[i] + 1, ilports[i] + 1, jfports[i] + 1, jlports[i] + 1) fid.close() # Write rmu file rmufile = abfile.ABFileRmu( "rmu", "w", cline1="Relaxation mask", cline2= "Relaxation mask created by topo_ports.py. rel zone width=%d, efold time=%d days" % (rmu_width, rmu_efold), mask=True) rmufile.write_field(rmumask, in_depth_m.mask, "rmu") rmufile.close() # Plot rmu with pcolormesh figure = matplotlib.pyplot.figure(figsize=(8, 8)) ax = figure.add_subplot(111) cmap = matplotlib.pyplot.get_cmap("Greys_r") cmap2 = matplotlib.pyplot.get_cmap("jet") ax.add_patch( matplotlib.patches.Rectangle((1, 1), in_depth.shape[1], in_depth.shape[0], color=".5", alpha=.5)) P = ax.pcolormesh(in_depth_m, cmap=cmap) P = ax.pcolormesh(rmumask_m, cmap=cmap2) CB = ax.figure.colorbar(P) figure.canvas.print_figure("rmu.png", dpi=dpi) # Plot ports with pcolormesh figure = matplotlib.pyplot.figure(figsize=(8, 8)) ax = figure.add_subplot(111) cmap = matplotlib.pyplot.get_cmap("Greys_r") ax.add_patch( matplotlib.patches.Rectangle((1, 1), in_depth.shape[1], in_depth.shape[0], color=".5", alpha=.5)) P = ax.pcolormesh(in_depth_m, cmap=cmap) I, J = numpy.where(labels > 0) S = ax.scatter(J, I, 50, labels[I, J], edgecolor='none') CB = ax.figure.colorbar(S) ax.set_xlim(0, in_depth.shape[1]) ax.set_ylim(0, in_depth.shape[0]) CB.ax.set_title("Port number") logger.info("Writing to ports_all.png") figure.canvas.print_figure("ports_all.png", dpi=dpi) # Port diagnostics plot figure2 = matplotlib.pyplot.figure(figsize=(8, 8)) ax2 = figure2.add_subplot(111) cmap = matplotlib.pyplot.get_cmap("Greys_r") P = ax2.pcolormesh(in_depth_m, cmap=cmap, edgecolor=".4", alpha=.5, linewidth=.05) ax2.hold() Ps = [] Ls = [] for i in range(len(kdports)): iwidth = ilports[i] - ifports[i] + 1 jwidth = jlports[i] - jfports[i] + 1 #print ifports[i],jfports[i],iwidth,jwidth d = 1 if kdports[i] == 1: xy = (ifports[i], jfports[i]) jwidth = d c = "r" elif kdports[i] == 2: xy = (ifports[i], jfports[i]) jwidth = d c = "g" elif kdports[i] == 3: xy = (ifports[i], jfports[i]) iwidth = d c = "b" elif kdports[i] == 4: xy = (ifports[i], jfports[i]) iwidth = d c = "m" figure.clf() ax = figure.add_subplot(111) P = ax.pcolormesh(in_depth_m, cmap=cmap, edgecolor=".4", alpha=.5, linewidth=.05) ax.add_patch( matplotlib.patches.Rectangle(xy, iwidth, jwidth, color=c, alpha=.5)) ax.grid() ax.set_xlim(xy[0] - 20, xy[0] + iwidth + 20) ax.set_ylim(xy[1] - 20, xy[1] + jwidth + 20) ax.set_title("Port number %d - kdport=%d" % (i + 1, kdports[i])) R = ax2.add_patch( matplotlib.patches.Rectangle(xy, iwidth, jwidth, color=c, alpha=.5)) Ps.append(R) Ls.append("Port %d" % (i + 1)) fname = "port_%03d.png" % (i + 1) logger.info("Writing Diagnostics to %s" % fname) figure.canvas.print_figure(fname, bbox_inches='tight', dpi=dpi) fname = "ports_all_2.png" logger.info("Writing Diagnostics to %s" % fname) ax2.legend(Ps, Ls) figure2.canvas.print_figure(fname, bbox_inches='tight', dpi=dpi) if fatal: logger.error( "Errors were encountered - see errors above, and consult diag files. You may need to modify your topo file" ) raise NameError, "fatal exit"
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()
def main(infile_coarse,infile_fine,ncells_linear=20,ncells_exact=3) : bathy_threshold=0. # TODO # Read plon,plat gfile=abfile.ABFileGrid("regional.grid","r") plon=gfile.read_field("plon") plat=gfile.read_field("plat") gfile.close() # Read input bathymetry - fine version bfile=abfile.ABFileBathy(infile_fine,"r",idm=gfile.idm,jdm=gfile.jdm) fine_depth_m=bfile.read_field("depth") fine_depth_m=numpy.ma.masked_where(fine_depth_m<=bathy_threshold,fine_depth_m) fine_depth=numpy.ma.filled(fine_depth_m,bathy_threshold) bfile.close() # Read input bathymetry - coarse version bfile=abfile.ABFileBathy(infile_coarse,"r",idm=gfile.idm,jdm=gfile.jdm) coarse_depth_m=bfile.read_field("depth") coarse_depth_m=numpy.ma.masked_where(coarse_depth_m<=bathy_threshold,coarse_depth_m) coarse_depth=numpy.ma.filled(coarse_depth_m,bathy_threshold) bfile.close() # create relaxation mask (rmu) tmp=numpy.linspace(0.,1.,ncells_linear) tmp=numpy.concatenate((numpy.zeros((ncells_exact,)),tmp)) # ie: hree first cells will match outer bathymetry ncells=ncells_linear+ncells_exact rmu=numpy.ones(coarse_depth.shape) rmu[:,0:ncells] = numpy.minimum(tmp,rmu[:,0:ncells]) rmu[0:ncells,:] = numpy.minimum(tmp,rmu[0:ncells,:].transpose()).transpose() rmu[:,-ncells:] = numpy.minimum(tmp[::-1],rmu[:,-ncells:]) rmu[-ncells:,:] = numpy.minimum(tmp[::-1],rmu[-ncells:,:].transpose()).transpose() ## Only allow points where both models are defined in the boundaruy rmumask=fine_depth_m.mask rmumask[:,0:ncells] = numpy.logical_or(rmumask[:,0:ncells],coarse_depth_m.mask[:,0:ncells]) rmumask[0:ncells,:] = numpy.logical_or(rmumask[0:ncells,:],coarse_depth_m.mask[0:ncells,:]) rmumask[:,-ncells:] = numpy.logical_or(rmumask[:,-ncells:],coarse_depth_m.mask[:,-ncells:]) rmumask[-ncells:,:] = numpy.logical_or(rmumask[-ncells:,:],coarse_depth_m.mask[-ncells:,:]) figure = matplotlib.pyplot.figure(figsize=(8,8)) ax=figure.add_subplot(111) P=ax.pcolormesh(rmu) figure.colorbar(P)#,norm=matplotlib.colors.LogNorm(vmin=mask.min(), vmax=mask.max())) figure.canvas.print_figure("tst.png") # Modify bathy in mask region newbathy = (1.-rmu) * coarse_depth + rmu * fine_depth newbathy[rmumask] = bathy_threshold newbathy[:,0]=bathy_threshold newbathy[:,-1]=bathy_threshold newbathy[0,:]=bathy_threshold newbathy[-1,:]=bathy_threshold #print newbathy.min(),newbathy.max() # Mask data where depth below threshold newbathy_m=numpy.ma.masked_where(newbathy<=bathy_threshold,newbathy) # Create netcdf file with all stages for analysis logger.info("Writing bathymetry to diagnostic file bathy_merged.nc") ncid = netCDF4.Dataset("bathy_merged.nc","w") ncid.createDimension("idm",newbathy.shape[1]) ncid.createDimension("jdm",newbathy.shape[0]) ncid.createVariable("lon","f8",("jdm","idm")) ncid.createVariable("lat","f8",("jdm","idm")) ncid.createVariable("coarse","f8",("jdm","idm")) ncid.createVariable("coarse_masked","f8",("jdm","idm")) ncid.createVariable("fine","f8",("jdm","idm")) ncid.createVariable("fine_masked","f8",("jdm","idm")) ncid.createVariable("final","f8",("jdm","idm")) ncid.createVariable("final_masked","f8",("jdm","idm")) ncid.createVariable("rmu","f8",("jdm","idm")) ncid.createVariable("modified","f8",("jdm","idm")) ncid.variables["lon"][:]=plon ncid.variables["lat"][:]=plat ncid.variables["coarse"][:]=coarse_depth ncid.variables["coarse_masked"][:]=coarse_depth_m ncid.variables["fine"][:]=fine_depth ncid.variables["fine_masked"][:]=fine_depth_m ncid.variables["final"][:]=newbathy ncid.variables["final_masked"][:]=newbathy_m modmask=newbathy-fine_depth ncid.variables["modified"][:] = modmask ncid.variables["rmu"][:] = rmu ncid.close() logger.info("Writing bathymetry plot to file newbathy.png") figure = matplotlib.pyplot.figure(figsize=(8,8)) ax=figure.add_subplot(111) P=ax.pcolormesh(newbathy) figure.colorbar(P,norm=matplotlib.colors.LogNorm(vmin=newbathy.min(), vmax=newbathy.max())) I,J=numpy.where(numpy.abs(modmask)>.1) ax.scatter(J,I,20,"r") figure.canvas.print_figure("newbathy.png") # Print to HYCOM abfile.write_bathymetry("MERGED",0,newbathy,bathy_threshold)
def main(intopo): bathy_threshold = 0. # TODO # Read plon,plat gfile = abfile.ABFileGrid("regional.grid", "r") plon = gfile.read_field("plon") plat = gfile.read_field("plat") gfile.close() # Read input bathymetri bfile = abfile.ABFileBathy(intopo, "r", idm=gfile.idm, jdm=gfile.jdm, mask=True) in_depth_m = bfile.read_field("depth") bfile.close() # Modify basin in_depth = numpy.ma.filled(in_depth_m, bathy_threshold) depth = numpy.copy(in_depth) # Open stdin logger.info("Reading input data") for line in sys.stdin.readlines(): logger.info(line.strip()) ifirst, ilast, jfirst, jlast, d = line.split() ifirst = int(ifirst) - 1 # Fortran indexing -> C ilast = int(ilast) # Fortran indexing -> C jfirst = int(jfirst) - 1 # Fortran indexing -> C jlast = int(jlast) # Fortran indexing -> C d = float(d) logger.info( "ifirst=%5d, ilast=%5d, jfirst=%5d, jlast=%5d : depth=%6.2f" % (ifirst, ilast, jfirst, jlast, d)) depth[jfirst:jlast, ifirst:ilast] = d # Mask data where depth below thresholddata = sys.stdin.readlines() depth_m = numpy.ma.masked_where(depth <= bathy_threshold, depth) # Create netcdf file with all stages for analysis logger.info("Writing bathymetry to file bathy_modify.nc") ncid = netCDF4.Dataset("bathy_modify.nc", "w") ncid.createDimension("idm", depth.shape[1]) ncid.createDimension("jdm", depth.shape[0]) ncid.createVariable("lon", "f8", ("jdm", "idm")) ncid.createVariable("lat", "f8", ("jdm", "idm")) ncid.createVariable("old", "f8", ("jdm", "idm")) ncid.createVariable("old_masked", "f8", ("jdm", "idm")) ncid.createVariable("new", "f8", ("jdm", "idm")) ncid.createVariable("new_masked", "f8", ("jdm", "idm")) ncid.createVariable("modified", "i4", ("jdm", "idm")) ncid.variables["lon"][:] = plon ncid.variables["lat"][:] = plat ncid.variables["old"][:] = in_depth ncid.variables["old_masked"][:] = in_depth_m ncid.variables["new"][:] = depth ncid.variables["new_masked"][:] = depth_m modmask = numpy.abs(in_depth - depth) > .1 ncid.variables["modified"][:] = modmask.astype("i4") ncid.close() # Print to HYCOM and CICE bathymetry files abfile.write_bathymetry("MODIFIED", 0, depth, bathy_threshold)
def main(infile, blo, bla, remove_isolated_basins=True, remove_one_neighbour_cells=True, remove_islets=True): bathy_threshold = 0. # TODO # Read plon,plat gfile = abfile.ABFileGrid("regional.grid", "r") plon = gfile.read_field("plon") plat = gfile.read_field("plat") gfile.close() # Read input bathymetri bfile = abfile.ABFileBathy(infile, "r", idm=gfile.idm, jdm=gfile.jdm, mask=True) in_depth_m = bfile.read_field("depth") print "in_depth_m type, min, max:", type( in_depth_m), in_depth_m.min(), in_depth_m.max() bfile.close() # Modify basin in_depth = numpy.ma.filled(in_depth_m, bathy_threshold) depth = numpy.copy(in_depth) print "depth min max", depth.min(), depth.max() it = 1 while it == 1 or numpy.count_nonzero(numpy.abs(depth - depth_old)) > 0: depth_old = numpy.copy(depth) logger.info("Basin modifications ... pass %d" % (it)) if remove_isolated_basins: depth = modeltools.tools.remove_isolated_basins( plon, plat, depth, blo, bla, threshold=bathy_threshold) if remove_islets: depth = modeltools.tools.remove_islets(depth, threshold=bathy_threshold) if remove_one_neighbour_cells: depth = modeltools.tools.remove_one_neighbour_cells( depth, threshold=bathy_threshold) logger.info("Modified %d points " % numpy.count_nonzero(depth - depth_old)) it += 1 w5 = numpy.copy(depth) w5[:, 0] = bathy_threshold w5[:, -1] = bathy_threshold w5[0, :] = bathy_threshold w5[-1, :] = bathy_threshold print "w5 type min max", type(w5), w5.min(), w5.max() # Mask data where depth below threshold w5_m = numpy.ma.masked_where(w5 <= bathy_threshold, w5) # Create netcdf file with all stages for analysis logger.info("Writing bathymetry to file bathy_consistency.nc") ncid = netCDF4.Dataset("bathy_consistency.nc", "w") ncid.createDimension("idm", w5.shape[1]) ncid.createDimension("jdm", w5.shape[0]) ncid.createVariable("lon", "f8", ("jdm", "idm")) ncid.createVariable("lat", "f8", ("jdm", "idm")) ncid.createVariable("old", "f8", ("jdm", "idm")) ncid.createVariable("old_masked", "f8", ("jdm", "idm")) ncid.createVariable("new", "f8", ("jdm", "idm")) ncid.createVariable("new_masked", "f8", ("jdm", "idm")) ncid.createVariable("modified", "i4", ("jdm", "idm")) ncid.variables["lon"][:] = plon ncid.variables["lat"][:] = plat ncid.variables["old"][:] = in_depth ncid.variables["old_masked"][:] = in_depth_m ncid.variables["new"][:] = w5 ncid.variables["new_masked"][:] = w5_m modmask = numpy.abs(in_depth - depth) > .1 ncid.variables["modified"][:] = modmask.astype("i4") ncid.close() logger.info("Writing bathymetry plot to file newbathy.png") figure = matplotlib.pyplot.figure(figsize=(8, 8)) ax = figure.add_subplot(111) P = ax.pcolormesh(w5_m) figure.colorbar(P, norm=matplotlib.colors.LogNorm(vmin=w5_m.min(), vmax=w5_m.max())) I, J = numpy.where(numpy.abs(modmask) > .1) ax.scatter(J, I, 20, "r") figure.canvas.print_figure("newbathy.png") # Print to HYCOM and CICE bathymetry files abfile.write_bathymetry("CONSISTENT", 0, w5, bathy_threshold)
def main(region, experiment, year, day, workdir, satdir, debug): experiment = 'expt_' + experiment[0:2] + '.' + experiment[2] 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" # get domain dimensions abgrid = abfile.ABFileGrid(workdir + user + "/" + \ region + "/topo/regional.grid","r") plon = abgrid.read_field("plon") plat = abgrid.read_field("plat") scpx = abgrid.read_field("scpx") scpy = abgrid.read_field("scpy") jdm, idm = plon.shape # get domain depth abdepth = abfile.ABFileBathy(workdir + user + "/" + \ region + "/" + experiment + "/data/regional.depth.b", \ "r",idm=idm,jdm=jdm) depthm = abdepth.read_field("depth") # read in satellite data timecal = (datetime.datetime(int(year), 1, 1) + datetime.timedelta(int(day) - 1)) globchl = satdir+year+'/'+year+'-'+\ str(timecal.month).zfill(2)+'-'+str(timecal.day).zfill(2)+'.nc' nc = NetCDFFile(globchl) satlat = nc.variables['lat'][:] satlon = nc.variables['lon'][:] satchl = nc.variables['CHL'][0, :, :] # satlon_2d, satlat_2d = np.meshgrid(satlon, satlat) if debug: kdfile = satdir[:-4]+'kd/'+year+'/'+year+'-'+\ str(timecal.month).zfill(2)+'-'+str(timecal.day).zfill(2)+'.nc' nckd = NetCDFFile(kdfile) kd = nckd.variables['KD490'][0, :, :] # mask out high latitudes ( >=70 ) from mid September to avoid artificial high CHL due to angle of the sun if int(day) >= 259: satchl = np.ma.masked_where(satlat_2d >= 70., satchl) if debug: kd = np.ma.masked_where(satlat_2d >= 70., kd) # load restart file oldfile = workdir + user + "/" + \ region + "/" + experiment + "/data/"+region[0:3]+"restart."+year+"_"+day.zfill(3)+"_00_0000.a" f = abfile.ABFileRestart(oldfile,"r",\ idm=idm,jdm=jdm) kdm = max(f.fieldlevels) prok = 21 dia3D = np.zeros((kdm, jdm, idm)) fla3D = np.zeros((kdm, jdm, idm)) diachl3D = np.zeros((kdm, jdm, idm)) flachl3D = np.zeros((kdm, jdm, idm)) dp3D = np.zeros((kdm, jdm, idm)) depth3D = np.zeros((kdm, jdm, idm)) dianew = np.zeros((kdm, jdm, idm)) diachlnew = np.zeros((kdm, jdm, idm)) flanew = np.zeros((kdm, jdm, idm)) flachlnew = np.zeros((kdm, jdm, idm)) estimated = np.zeros((prok, jdm, idm)) d_estimated = np.zeros((prok, jdm, idm)) for k in range(kdm): dia3D[k, :, :] = f.read_field('ECO_dia', k + 1) fla3D[k, :, :] = f.read_field('ECO_fla', k + 1) diachl3D[k, :, :] = f.read_field('ECO_diac', k + 1) flachl3D[k, :, :] = f.read_field('ECO_flac', k + 1) dp3D[k, :, :] = f.read_field('dp', k + 1) / modeltools.hycom.onem if k == 0: depth3D[k, :, :] = dp3D[k, :, :] / 2. else: depth3D[k, :, :] = depth3D[k - 1, :, :] + dp3D[k, :, :] / 2. dianew[k, :, :] = dia3D[k, :, :] flanew[k, :, :] = fla3D[k, :, :] diachlnew[k, :, :] = diachl3D[k, :, :] flachlnew[k, :, :] = flachl3D[k, :, :] depthmf = np.asfortranarray(depthm) satchlf = np.asfortranarray(satchl) platf = np.asfortranarray(plat) plonf = np.asfortranarray(plon) satlatf = np.asfortranarray(satlat) satlonf = np.asfortranarray(satlon) scpxf = np.asfortranarray(scpx) scpyf = np.asfortranarray(scpy) if debug: kdf = np.asfortranarray(kd) icefile = workdir + user + "/" + \ region + "/" + experiment + "/data/cice/iced." + year + "-"+ \ str(timecal.month).zfill(2)+'-'+str(timecal.day).zfill(2)+'-00000.nc' ic = NetCDFFile(icefile) icemask = ic.variables["iceumask"][:, :] icf = np.asfortranarray(icemask) satout = _FRAM_the_mapping_loop_with_ice.main(depthmf, scpxf, scpyf, platf, plonf, icf, satlatf, satlonf, satchlf) satout = np.ma.masked_where(satout > 1000., satout) satout = np.ma.masked_where(satout < 0., satout) satout = np.ma.masked_array(satout, depthm.mask) if debug: kdout = _the_mapping_loop.main(depthmf, scpxf, scpyf, platf, plonf, icf, satlatf, satlonf, kdf) kdout = np.ma.masked_where(kdout > 1E4, kdout) kdout = np.ma.masked_array(kdout, depthm.mask) kdout = 1. / kdout ############################## # NOW BEGINS THE CHL PROFILING # ############################## # read mld climatology ############################################# ncmld = NetCDFFile('./mld_DR003_c1m_reg2.0.nc') ncmld.set_auto_mask(False) mldlat = ncmld.variables['lat'][:] mldlon = ncmld.variables['lon'][:] mld3D = ncmld.variables['mld'][:, :, :] ncmld.close() for l in range( mldlon.shape[0]): # make longitudes compatible with hycom model if mldlon[l] > 180.: mldlon[l] = -(360. - mldlon[l]) # following two lines will be used to interpolate mld between months dayofyear = int(timecal.strftime('%-j')) mldt = np.array( [-15, 15, 46, 74, 105, 135, 166, 196, 227, 258, 288, 319, 349, 380]) # day of year every 15th #################################################################### # read mixed water profile curves ################################# # below are parameters taken from Uitz et al., 2006 # they are stored in a text file and here we read them # Uitz classify profiles are 'mixed' and 'stratified' # this chunk simply reads in for 'mixed' profiles # we will determine whether the profile is mixed later Mchlsigma = np.zeros((20, 5)) Msigma = np.zeros((20, 5)) k = 0 with open("./M_profiles", "r") as filestream: for line in filestream: currentline = line.split(",") Msigma[k, 0] = float(currentline[1]) Msigma[k, 1] = float(currentline[3]) Msigma[k, 2] = float(currentline[5]) Msigma[k, 3] = float(currentline[7]) Msigma[k, 4] = float(currentline[9]) Mchlsigma[k, 0] = float(currentline[0]) Mchlsigma[k, 1] = float(currentline[2]) Mchlsigma[k, 2] = float(currentline[4]) Mchlsigma[k, 3] = float(currentline[6]) Mchlsigma[k, 4] = float(currentline[8]) k = k + 1 Mchlave = np.array([0.244, 0.592, 0.885, 1.881, 6.32]) zeu_ave = np.array( [77.1, 53.2, 44., 31.5, 16.9] ) # average EZ depth for the mixing conditions - taken from Uitz et al., 2006 #################################################################### # NOW WE BEGIN THE LOOP FOR EACH MODEL POINT print 'adjusting profiles' for j in range(jdm): for i in range(idm): # print j,i ################################################################## # get mld from climatology JJ = np.abs(plon[j, i] - mldlon).argmin() II = np.abs(plat[j, i] - mldlat).argmin() # for each model point, interpolate MLD values to the exact dayofyear # for continuity, last month is concatenated to the front of the year # and first month to the back of the year mld = np.interp(dayofyear,mldt,\ np.concatenate((mld3D[11,II,JJ],mld3D[:,II,JJ],mld3D[0,II,JJ]),axis=None)) # masked mld have very high values, I keep it that way, # so later water column is assigned "mixed" to avoid code malfunction ################################################################### ################################################################## # true/false whether mixed or stratified surf = satout[j, i] # copy the local satellite chl if np.ma.is_masked(surf): pass else: eup = 4.61 / ( 0.041 + 0.04 * surf ) # taken from ECOSMO - kw=0.041 kb=0.04 ln(1%)=4.61 stratified = 'false' # initial estimation of euphotic depth to determine whether mixed or not # later on in the code, Morel and Maritorena,2001 estimation will be used if eup > mld: stratified = 'true' if stratified: # if stratified, we need to assign different parameters (from Uitz et al., 2006) # I suggest at this point, it is worth to look at the paper and have an idea on # what is happening below. Basically a different gaussian shaped curve is assigned # based on the surface CHL value. From what I understand, it is safe to interpolate # between curves, so any surface CHL value from satellite will be fit to an individual # interpolated profile if surf <= 1.0: A10 = 36.1 B10 = 0.357 A15 = 42.0 B15 = 0.248 else: A10 = 37.7 B10 = 0.615 A15 = 43.5 B15 = 0.847 Cbc = np.zeros((9)) sc = np.zeros((9)) Cmaxc = np.zeros((9)) sigma_maxc = np.zeros((9)) deltac = np.zeros((9)) sigma_avec = np.zeros((9)) zeu_ave = np.zeros((9)) Cbc[0] = 0.471 sc[0] = 0.135 Cmaxc[0] = 1.572 sigma_maxc[0] = 0.969 deltac[0] = 0.393 sigma_avec[0] = 0.032 zeu_ave[0] = 119.1 Cbc[1] = 0.533 sc[1] = 0.172 Cmaxc[1] = 1.194 sigma_maxc[1] = 0.921 deltac[1] = 0.435 sigma_avec[1] = 0.062 zeu_ave[1] = 99.9 Cbc[2] = 0.428 sc[2] = 0.138 Cmaxc[2] = 1.015 sigma_maxc[2] = 0.905 deltac[2] = 0.630 sigma_avec[2] = 0.098 zeu_ave[2] = 91.0 Cbc[3] = 0.570 sc[3] = 0.173 Cmaxc[3] = 0.766 sigma_maxc[3] = 0.814 deltac[3] = 0.586 sigma_avec[3] = 0.158 zeu_ave[3] = 80.2 Cbc[4] = 0.611 sc[4] = 0.214 Cmaxc[4] = 0.676 sigma_maxc[4] = 0.663 deltac[4] = 0.539 sigma_avec[4] = 0.244 zeu_ave[4] = 70.3 Cbc[5] = 0.390 sc[5] = 0.109 Cmaxc[5] = 0.788 sigma_maxc[5] = 0.521 deltac[5] = 0.681 sigma_avec[5] = 0.347 zeu_ave[5] = 63.4 Cbc[6] = 0.569 sc[6] = 0.183 Cmaxc[6] = 0.608 sigma_maxc[6] = 0.452 deltac[6] = 0.744 sigma_avec[6] = 0.540 zeu_ave[6] = 54.4 Cbc[7] = 0.835 sc[7] = 0.298 Cmaxc[7] = 0.382 sigma_maxc[7] = 0.512 deltac[7] = 0.625 sigma_avec[7] = 1.235 zeu_ave[7] = 39.8 Cbc[8] = 0.188 sc[8] = 0. Cmaxc[8] = 0.885 sigma_maxc[8] = 0.378 deltac[8] = 1.081 sigma_avec[8] = 2.953 zeu_ave[8] = 26.1 if surf <= sigma_avec[0]: Cb = Cbc[0] s = sc[0] Cmax = Cmaxc[0] sigma_max = sigma_maxc[0] delta = deltac[0] sigma_ave = sigma_avec[0] zeu = zeu_ave[0] elif surf >= sigma_avec[8]: Cb = Cbc[8] s = sc[8] Cmax = Cmaxc[8] sigma_max = sigma_maxc[8] delta = deltac[8] sigma_ave = sigma_avec[8] zeu = zeu_ave[8] else: Cb = np.interp(surf, sigma_avec, Cbc) s = np.interp(surf, sigma_avec, sc) sigma_max = np.interp(surf, sigma_avec, sigma_maxc) Cmax = np.interp(surf, sigma_avec, Cmaxc) delta = np.interp(surf, sigma_avec, deltac) zeu = np.interp(surf, sigma_avec, zeu_ave) sigma = np.arange(prok) / 10. # the shape of the dimensionless profile for stratified waters based on surface chl c_sigma = Cb - s * sigma + Cmax * np.exp(-( (sigma - sigma_max) / delta)**2) ''' plt.plot(c_sigma,-sigma) plt.scatter(c_sigma,-sigma) ''' chl_int_eup = A10 * surf**B10 # integrated chl within the euphotic depth for stratified waters #chl_ave_eup = chl_int_eup / eup # mean chl within the euphotic depth for stratified waters chl_ave_eup = chl_int_eup / zeu chl_profile = chl_ave_eup * c_sigma # chl profile estimated from satellite chl for stratified waters #chl_profile_depth = sigma * eup # chl profile sample depth for stratified waters chl_profile_depth = sigma * zeu ''' plt.plot(chl_profile,-chl_profile_depth) plt.scatter(chl_profile,-chl_profile_depth) ''' else: # if water column is mixed we use below CHL profile shapes A10 = 42.1 B10 = 0.538 A15 = 58.5 B15 = 0.546 sigma = np.zeros((20)) c_sigma = zeros((20)) if surf <= Mchlave[0]: sigma = Msigma[:, 0] c_sigma = Mchlsigma[:, 0] zeu = zeu_ave[0] elif surf >= Mchlave[4]: sigma = Msigma[:, 4] c_sigma = Mchlsigma[:, 4] zeu = zeu_ave[4] else: for k in range(20): sigma[k] = np.interp(surf, Mchlave, Msigma[k, :]) c_sigma[k] = np.interp( surf, Mchlave, Mchlsigma[k, :] ) # the shape of the dimensionless profile for mixed waters based on surface chl zeu = np.interp(surf, Mchlave, zeu_ave) ''' plt.plot(c_sigma,-sigma) plt.scatter(c_sigma,-sigma) ''' chl_int_eup = A10 * surf**B10 # integrated chl within the euphotic depth for mixed waters #chl_ave_eup = chl_int_eup / eup # mean chl within the euphotic depth for stratified waters chl_ave_eup = chl_int_eup / zeu chl_profile = chl_ave_eup * c_sigma # chl profile estimated from satellite chl for mixed waters #chl_profile_depth = sigma * eup # chl profile sample depth for mixed waters chl_profile_depth = sigma * zeu ''' plt.plot(chl_profile,-chl_profile_depth) plt.scatter(chl_profile,-chl_profile_depth) ''' ################################################ # now we fit the model towards the estimated profile # I used a 30% fit towards the estimated profile # take model data in 1D dia = dia3D[:, j, i] fla = fla3D[:, j, i] diachl = diachl3D[:, j, i] flachl = flachl3D[:, j, i] chl = diachl + flachl d1D = depth3D[:, j, i] # interpolate estimated profile to model depth chl_estimated = np.interp(d1D, chl_profile_depth, chl_profile) chl_adjusted = np.zeros((kdm)) flachl_adjsuted = np.zeros((kdm)) diachl_adjsuted = np.zeros((kdm)) fla_adjsuted = np.zeros((kdm)) dia_adjsuted = np.zeros((kdm)) for k in range(kdm): if d1D[k] <= np.max( chl_profile_depth ): # profile estimation is limited to the euphotic depth # we need to keep model values below chl_adjusted[k] = chl_estimated[k] * 0.3 + chl[k] * 0.7 else: chl_adjusted[k] = chl[k] flachl_adjsuted[k] = ( flachl[k] / (flachl[k] + diachl[k])) * chl_adjusted[k] diachl_adjsuted[k] = ( diachl[k] / (flachl[k] + diachl[k])) * chl_adjusted[k] fla_adjsuted[k] = flachl_adjsuted[k] * (fla[k] / flachl[k]) dia_adjsuted[k] = diachl_adjsuted[k] * (dia[k] / diachl[k]) dianew[:, j, i] = dia_adjsuted flanew[:, j, i] = fla_adjsuted diachlnew[:, j, i] = diachl_adjsuted flachlnew[:, j, i] = flachl_adjsuted estimated[:, j, i] = chl_profile d_estimated[:, j, i] = chl_profile_depth # dianew[dianew>1E25] = 0.; dianew[dianew<-1E15] = 0. # dianew[diachlnew>1E25] = 0.; dianew[diachlnew<-1E15] = 0. # flanew[flanew>1E25] = 0.; flanew[flanew<-1E15] = 0. # flanew[flachlnew>1E25] = 0.; flanew[flachlnew<-1E15] = 0. # dianew = np.ma.masked_array(dianew,np.repeat(depthm[np.newaxis,:], kdm, 0).mask) # flanew = np.ma.masked_array(flanew,np.repeat(depthm[np.newaxis,:], kdm, 0).mask) # flachlnew = np.ma.masked_array(flachlnew,np.repeat(depthm[np.newaxis,:], kdm, 0).mask) # diachlnew = np.ma.masked_array(diachlnew,np.repeat(depthm[np.newaxis,:], kdm, 0).mask) # finally replace the old model values with the estimated one in the restart file newfile = workdir + user + "/" + \ region + "/" + experiment + "/data/"+region[0:3]+"restart."+year+"_"+day.zfill(3)+"_00_0000_NEW" new_abfile = abfile.ABFileRestart(newfile, "w", idm=idm, jdm=jdm) new_abfile.write_header(f._iexpt, f._iversn, f._yrflag, f._sigver, f._nstep, f._dtime, f._thbase) for keys in sorted(f.fields.keys()): fieldname = f.fields[keys]["field"] k = f.fields[keys]["k"] t = f.fields[keys]["tlevel"] field = f.read_field(fieldname, k, t) if fieldname == "ECO_dia": print("MODIFYING %10s at level %3d at time=%d" % (fieldname, k, t)) dummy = dianew[k - 1, :, :] field[~depthm.mask] = dummy[~depthm.mask] # new_abfile.write_field(dianew[k-1,:,:],None,fieldname,k,t) new_abfile.write_field(field, None, fieldname, k, t) elif fieldname == "ECO_fla": print("MODIFYING %10s at level %3d at time=%d" % (fieldname, k, t)) # new_abfile.write_field(flanew[k-1,:,:],None,fieldname,k,t) dummy = flanew[k - 1, :, :] field[~depthm.mask] = dummy[~depthm.mask] new_abfile.write_field(field, None, fieldname, k, t) elif fieldname == "ECO_flac": print("MODIFYING %10s at level %3d at time=%d" % (fieldname, k, t)) # new_abfile.write_field(flachlnew[k-1,:,:],None,fieldname,k,t) dummy = flachlnew[k - 1, :, :] field[~depthm.mask] = dummy[~depthm.mask] new_abfile.write_field(field, None, fieldname, k, t) elif fieldname == "ECO_diac": print("MODIFYING %10s at level %3d at time=%d" % (fieldname, k, t)) # new_abfile.write_field(diachlnew[k-1,:,:],None,fieldname,k,t) dummy = diachlnew[k - 1, :, :] field[~depthm.mask] = dummy[~depthm.mask] new_abfile.write_field(field, None, fieldname, k, t) else: print("Copying %10s to level %3d at time=%d" % (fieldname, k, t)) new_abfile.write_field(field, None, fieldname, k, t) f.close() new_abfile.close() # now replace restart files os.rename(oldfile[:-2] + '.a', oldfile[:-2] + '_OLD.a') os.rename(oldfile[:-2] + '.b', oldfile[:-2] + '_OLD.b') os.rename(newfile + '.a', newfile[:-4] + '.a') os.rename(newfile + '.b', newfile[:-4] + '.b') if debug: namencout = workdir + user + "/" + \ region + "/" + experiment + "/data/"+region[0:3]+"restart."+year+"_"+day.zfill(3)+"_00_0000_NEW.nc" ncout = NetCDFFile(namencout, "w", format="NETCDF4") ncout.createDimension("JJ", jdm) ncout.createDimension("II", idm) ncout.createDimension("dpth", kdm) ncout.createDimension("dpro", prok) sat_mapped = ncout.createVariable('sat_mapped', dtype('double').char, ("JJ", "II")) kd_mapped = ncout.createVariable('kd_mapped', dtype('double').char, ("JJ", "II")) latitude = ncout.createVariable('latitude', dtype('double').char, ("JJ", "II")) longitude = ncout.createVariable('longitude', dtype('double').char, ("JJ", "II")) chlold = ncout.createVariable('chlold', dtype('double').char, ("dpth", "JJ", "II")) chlnew = ncout.createVariable('chlnew', dtype('double').char, ("dpth", "JJ", "II")) profile = ncout.createVariable('profile', dtype('double').char, ("dpro", "JJ", "II")) depth = ncout.createVariable('depth', dtype('double').char, ("dpth", "JJ", "II")) profile_depth = ncout.createVariable('profile_depth', dtype('double').char, ("dpro", "JJ", "II")) depth3D = np.ma.masked_array( depth3D, np.repeat(depthm[np.newaxis, :], kdm, 0).mask) old = diachl3D + flachl3D old = np.ma.masked_array(old, np.repeat(depthm[np.newaxis, :], kdm, 0).mask) new = diachlnew + flachlnew new = np.ma.masked_array(new, np.repeat(depthm[np.newaxis, :], kdm, 0).mask) estimated = np.ma.masked_array( estimated, np.repeat(depthm[np.newaxis, :], prok, 0).mask) d_estimated = np.ma.masked_array( d_estimated, np.repeat(depthm[np.newaxis, :], prok, 0).mask) ncout.variables["longitude"][:] = plon ncout.variables["latitude"][:] = plat ncout.variables["sat_mapped"][:] = satout ncout.variables["chlold"][:] = old ncout.variables["chlnew"][:] = new ncout.variables["depth"][:] = depth3D ncout.variables["profile"][:] = estimated ncout.variables["profile_depth"][:] = d_estimated ncout.variables["kd_mapped"][:] = kdout ncout.sync() ncout.close()
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)
def main(tide_file,archv_files,include_uv=False): # 1) If this routine is called without any archive files (empty list), then # Files suitable for barotropic nesting only are created. The new archive files are then # chosen to match times in tide file. # 2) If routines are called with archive files, then times matching the archive file times are # sought from the tide file. It they are found, srfhgt and montg1 are adjusted # to match the new tidal data. # Read plon,plat and depth from regional files. Mainly used to check that # grid is ok ... logger.info("Opening regional.grid.[ab]") gfile=abfile.ABFileGrid("regional.grid","r") plon=gfile.read_field("plon") plat=gfile.read_field("plat") pang=gfile.read_field("pang") # For rotation of tidal current gfile.close() logger.info("Opening regional.depth.[ab]") bathyfile=abfile.ABFileBathy("regional.depth","r",idm=gfile.idm,jdm=gfile.jdm,mask=True) depth=bathyfile.read_field("depth") bathyfile.close() depth = depth.filled(0.) ip=depth>0.0 iu=numpy.copy(ip) iu[:,1:] = numpy.logical_and(iu[:,1:],iu[:,0:-1]) iv=numpy.copy(ip) iv[1:,:] = numpy.logical_and(iv[1:,:],iv[0:-1,:]) # Open netcdf file, get time variable and some basic stuff print os.getcwd(),tide_file logger.info("Opening %s"%tide_file) nc_h = netCDF4.Dataset(tide_file,"r") plon_h=nc_h.variables["longitude"][:] plat_h=nc_h.variables["latitude"][:] depth_h=nc_h.variables["depth"][:] check_grids(plon,plon_h,plat,plat_h) check_depths(depth,depth_h) # Time processing for tidal elevations time_h=nc_h.variables["time"][:] tunit = nc_h.variables["time"].units mydt_h = cf_time_to_datetime(time_h,tunit) if include_uv : m=re.match("^(.*)_h.nc$",tide_file) if m : tide_file_u = m.group(1)+"_u.nc" else : msg="Unable to guesstimate tidal u component from tidsl heights file %s "%tide_file_h logger.error(msg) raise ValueError,msg m=re.match("^(.*)_h.nc$",tide_file) if m : tide_file_v = m.group(1)+"_v.nc" else : msg="Unable to guesstimate tidal u component from tidsl heights file %s "%tide_file_h logger.error(msg) raise ValueError,msg logger.info("Opening %s"%tide_file_u) nc_u = netCDF4.Dataset(tide_file_u,"r") plon_u=nc_u.variables["longitude"][:] plat_u=nc_u.variables["latitude"][:] depth_u=nc_u.variables["depth"][:] check_grids(plon,plon_u,plat,plat_u) check_depths(depth,depth_u) # Time processing for tidal elevations time_u=nc_u.variables["time"][:] tunit = nc_u.variables["time"].units mydt_u = cf_time_to_datetime(time_u,tunit) logger.info("Opening %s"%tide_file_v) nc_v = netCDF4.Dataset(tide_file_v,"r") plon_v=nc_v.variables["longitude"][:] plat_v=nc_v.variables["latitude"][:] depth_v=nc_v.variables["depth"][:] check_grids(plon,plon_v,plat,plat_v) check_depths(depth,depth_v) # Time processing for tidal elevations time_v=nc_v.variables["time"][:] tunit = nc_v.variables["time"].units mydt_v = cf_time_to_datetime(time_v,tunit) # restriction for now, u and v must have same time steps as h # TODO: Loosen restriction try : difftu=[abs(diff_in_seconds(elem[0]-elem[1])) for elem in zip(mydt_h,mydt_u)] difftv=[abs(diff_in_seconds(elem[0]-elem[1])) for elem in zip(mydt_h,mydt_v)] except: # Probably due to size mismatch, but could be more descriptive. # TODO: Add more descriptive error message msg="Error when subtracting times from u/v from h. Check your data" logger.error(msg) raise ValueError,msg #print difftu #print difftv if any([ elem > 10. for elem in difftu]) or any([ elem > 10. for elem in difftv]): msg="Times in tidal u/v vs tidal h mismatch. Time series must be estimated at the same times" logger.error(msg) raise ValueError,msg # Create output dir. path0=os.path.join(".","archv_with_tide") if os.path.exists(path0) and os.path.isdir(path0) : pass else : os.mkdir(path0) # Open blkdat files. Get some properties bp=modeltools.hycom.BlkdatParser("blkdat.input") idm = bp["idm"] jdm = bp["jdm"] kdm = bp["kdm"] thflag = bp["thflag"] thbase = bp["thbase"] kapref = bp["kapref"] iversn = bp["iversn"] iexpt = bp["iexpt"] yrflag = bp["yrflag"] thref=1e-3 if kapref == -1 : kapnum = 2 msg="Only kapref>=0 is implemented for now" logger.error(msg) raise ValueError,msg else : kapnum = 1 if kapnum > 1 : msg="Only kapnum=1 is implemented for now" logger.error(msg) raise ValueError,msg # hycom sigma and kappa, written in python. NB: sigver is not used here. # Modify to use other equations of state. For now we assume sigver is: # 1 (7-term eqs referenced to 0 bar) # 2 (7-term eqs referenced to 2000 bar) if thflag == 0 : sigver=1 else : sigver=2 sig = modeltools.hycom.Sigma(thflag) if kapref > 0 : kappa = modeltools.hycom.Kappa(kapref,thflag*1000.0e4) # # Now loop through tide_times for rec,tide_time in enumerate(mydt_h) : # Construct archive file name to create iy = tide_time.year id,ih,isec = modeltools.hycom.datetime_to_ordinal(tide_time,yrflag) archv_file_in_string = "archv.%04d_%03d_%02d"%(iy,id,ih) # Is there match for this file name in list of archive files? I=[elem for elem in archv_files if os.path.basename(elem)[:17] == archv_file_in_string ] state_from_archv=len(I)>0 if state_from_archv : archv_file_in =I[0] # Output file name fnameout = os.path.join(path0,os.path.basename(archv_file_in_string)) arcfile_out=abfile.ABFileArchv(fnameout,"w", iversn=iversn, yrflag=yrflag, iexpt=iexpt,mask=False, cline1="TIDAL data has been added") tide_h=numpy.copy(nc_h.variables["h"][rec,:,:]) tide_h=numpy.where(tide_h==nc_h.variables["h"]._FillValue,0.,tide_h) #print tide_h.min(),tide_h.max() if include_uv : tide_u=numpy.copy(nc_u.variables["u"][rec,:,:]) tide_v=numpy.copy(nc_v.variables["v"][rec,:,:]) #print tide_u.min(),tide_u.max() #print tide_v.min(),tide_u.max() tide_u=numpy.where(tide_u==nc_u.variables["u"]._FillValue,0.,tide_u) tide_v=numpy.where(tide_v==nc_v.variables["v"]._FillValue,0.,tide_v) # Rotate vectors to align with grid tide_u= tide_u*numpy.cos(pang) + tide_v*numpy.sin(pang) tide_v=-tide_u*numpy.sin(pang) + tide_v*numpy.cos(pang) #tide_v=tide_u*numpy.cos(pang+.5*numpy.pi) + tide_v*numpy.sin(pang+.5*numpy.pi) # From P-point to u. 2nd dim in python = 1st dim in Fortran tide_u[:,1:] =.5*(tide_u[:,1:] + tide_u[:,0:-1]) tide_u=numpy.where(iu,tide_u,0.) # From P-point to v. 1st dim in python = 2nd dim in Fortran tide_v[1:,:] =.5*(tide_v[1:,:] + tide_v[0:-1,:]) tide_v=numpy.where(iv,tide_v,0.) if state_from_archv : logger.info("Adding tidal values to existing state:%s"%arcfile_out.basename) arcfile=abfile.ABFileArchv(archv_file_in,"r") if arcfile.idm <> plon.shape[1] or arcfile.jdm <> plon.shape[0] : msg="Grid size mismatch between %s and %s "%(tide_file,archv_file_in) # Read all layers .. (TODO: If there are memory problems, read and estimate sequentially) temp = numpy.ma.zeros((jdm,idm)) # Only needed when calculating density saln = numpy.ma.zeros((jdm,idm)) # Only needed when calculating density th3d =numpy.ma.zeros((kdm,jdm,idm)) thstar=numpy.ma.zeros((kdm,jdm,idm)) dp =numpy.ma.zeros((jdm,idm)) p =numpy.ma.zeros((kdm+1,jdm,idm)) logger.info("Reading layers to get thstar and p") for k in range(kdm) : logger.debug("Reading layer %d from %s"%(k,archv_file_in)) temp =arcfile.read_field("temp",k+1) saln =arcfile.read_field("salin",k+1) #dp [k ,:,:]=arcfile.read_field("thknss",k+1) dp [:,:]=arcfile.read_field("thknss",k+1) th3d [k ,:,:]=sig.sig(temp,saln) - thbase p [k+1,:,:]= p[k,:,:] + dp[:,:] thstar[k ,:,:]=numpy.ma.copy(th3d [k ,:,:]) if kapref > 0 : thstar[k ,:,:]=thstar [k ,:,:] + kappa.kappaf( temp[:,:], saln[:,:], th3d[k,:,:]+thbase, p[k,:,:]) elif kapref < 0 : msg="Only kapref>=0 is implemented for now" logger.error(msg) raise ValueError,msg # Read montg1 and srfhgt, and set new values # ... we have ... # montg1 = montgc + montgpb * pbavg # srfhgt = montg1 + thref*pbavg # ... montg1 = arcfile.read_field("montg1",thflag) srfhgt = arcfile.read_field("srfhgt",0) # New surface height - montg1pb=modeltools.hycom.montg1_pb(thstar,p) montg1 = montg1 + montg1pb * modeltools.hycom.onem * tide_h srfhgt = montg1 + thref*tide_h*modeltools.hycom.onem # Barotrpic velocities if include_uv : ubavg = arcfile.read_field("u_btrop",0) vbavg = arcfile.read_field("v_btrop",0) ubavg = ubavg + tide_u vbavg = vbavg + tide_v # Loop through original fields and write for key in sorted(arcfile.fields.keys()) : fieldname = arcfile.fields[key]["field"] time_step = arcfile.fields[key]["step"] model_day = arcfile.fields[key]["day"] k = arcfile.fields[key]["k"] dens = arcfile.fields[key]["dens"] fld =arcfile.read_field(fieldname,k) if fieldname == "montg1" : logger.info("Writing field %10s at level %3d to %s (modified)"%(fieldname,k,fnameout)) arcfile_out.write_field(montg1,None,fieldname,time_step,model_day,sigver,thbase) elif fieldname == "srfhgt" : logger.info("Writing field %10s at level %3d to %s (modified)"%(fieldname,k,fnameout)) arcfile_out.write_field(srfhgt,None,fieldname,time_step,model_day,sigver,thbase) elif fieldname == "u_btrop" and include_uv : logger.info("Writing field %10s at level %3d to %s (modified)"%(fieldname,k,fnameout)) arcfile_out.write_field(ubavg,None,fieldname,time_step,model_day,sigver,thbase) elif fieldname == "v_btrop" and include_uv : logger.info("Writing field %10s at level %3d to %s (modified)"%(fieldname,k,fnameout)) arcfile_out.write_field(vbavg,None,fieldname,time_step,model_day,sigver,thbase) else : arcfile_out.write_field(fld ,None,fieldname,time_step,model_day,k,dens) #logger.info("Writing field %10s at level %3d to %s (copy from original)"%(fieldname,k,fnameout)) arcfile.close() else : logger.info("Crating archv file with tidal data :%s"%arcfile_out.basename) montg1=numpy.zeros((jdm,idm,)) srfhgt=tide_h*modeltools.hycom.onem*thref arcfile_out.write_field(montg1,None,"montg1",0,0.,sigver,thbase) arcfile_out.write_field(srfhgt,None,"srfhgt",0,0.,0,0.0) # Write 9 empty surface fields so that forfun.F can understand these files .... TODO: Fix in hycom arcfile_out.write_field(montg1,None,"surflx",0,0.,0,0.0) arcfile_out.write_field(montg1,None,"salflx",0,0.,0,0.0) arcfile_out.write_field(montg1,None,"bl_dpth",0,0.,0,0.0) arcfile_out.write_field(montg1,None,"mix_dpth",0,0.,0,0.0) if include_uv : ubavg = tide_u vbavg = tide_v arcfile_out.write_field(ubavg ,None,"u_btrop" ,0,0.,0,0.0) arcfile_out.write_field(vbavg ,None,"v_btrop" ,0,0.,0,0.0) logger.info("Finished writing to %s"%fnameout) arcfile_out.close() logger.info("Files containing tidal data in directory %s"%path0) logger.warning("Sigver assumed to be those of 7 term eqs") logger.warning(" 1 for sigma-0/thflag=0, 2 for sigma-2/thflag=2")