Example #1
0
 def test_abfilegrid_write(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
     regfile = abfile.ABFileGrid("test.grid", "w")
     plon = regfile.write_field(wfldout, None, "plon")
     regfile.close()
Example #2
0
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")
  # parser.add_argument('--masklim',    action=ClimParseAction,default=None,help="mask limits")
  # parser.add_argument('--window',     action=WindowParseAction, help='firsti,firstj,lasti,lastj', default=None)
   parser.add_argument('Trip_river_afile',  help='Trip_river_afile')
   parser.add_argument('Hype_river_afile',  help='Hype_river_afile')
   parser.add_argument('griver_afile',  help='greenland_glacier_river_afile')
   #parser.add_argument('filename', help="")
   #parser.add_argument('records',  nargs="+",    type=int)
   # The first input is from ETRIP and the second one is from HYPE
   # we have added the third input corresponding to griver (Green land Glacier from /cluster/projects/nn2993k/TRIP/triver_Roshin/)
   #example
   #python ./merge_TRIP_Hype_abfile_4Greenlnad.py ./SCRATCH_ERAI-TRIP/rivers.a ./SCRATCH_hype_rev2/rev2_rivers.a ./SCRATCH_greenlanice/rivers.a

   args = parser.parse_args()


   gfile = abfile.ABFileGrid("regional.grid","r")
   scpx=gfile.read_field("scpx")
   scpy=gfile.read_field("scpy")
   plon=gfile.read_field("plon")
   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 
Example #4
0
def atmfor(start,end,af,grid_file="regional.grid",blkdat_file="blkdat.input",plot_diag=False,
      nersc_forcing=False) :

   if nersc_forcing :
      logger.info("Using old NERSC-HYCOM forcing fields")
      
      # Modify names used by hycom
      mynames = dict(modeltools.hycom.variable_names)
      mynames["10u"] = "uwind"
      mynames["10v"] = "vwind"
      mynames["msl"] = "slp"
      mynames["tcc"] = "clouds"
      #mynames["wspd"] = "wndspd"
      mynames["relhum"] = "relhum"
      mynames["taux"] = "tauewd"
      mynames["tauy"] = "taunwd"

      # Modify output units needed
      myunits = dict(modeltools.hycom.variable_units)
      myunits["msl"]  = "hPa"
      myunits["tcc"]  = "1"
      #myunits["wspd"] = "m s-1"
      myunits["relhum"] = "1"

      mylimits = dict(modeltools.hycom.variable_limits)
      mylimits["tcc"] = [0.,1.]
      mylimits["relhum"] = [0.,1.]

      forcingpropertyset = modeltools.forcing.atmosphere.ForcingPropertySet(
            mynames,myunits, mylimits,
            modeltools.forcing.atmosphere.known_vectors)

   # Standard case, use names and units directly from hycom module
   else :

      forcingpropertyset = modeltools.forcing.atmosphere.ForcingPropertySet(
         modeltools.hycom.variable_names,
         modeltools.hycom.variable_units,
         modeltools.hycom.variable_limits,
         modeltools.forcing.atmosphere.known_vectors)

   # Open hycom grid file, read longitude and latitude@
   # TODO: HYCOM-specific
   #za = modeltools.hycom.io.ABFileRegionalGrid(grid_file,"r")
   za = abfile.ABFileGrid(grid_file,"r")
   mlon = za.read_field("plon")
   mlat = za.read_field("plat")
   Nx=mlon.shape[1]
   Ny=mlon.shape[0]
   za.close()

   # parse blkdat to get yearflag
   # TODO: HYCOM-specific
   blkd = modeltools.hycom.BlkdatParser(blkdat_file)
   yrflag = blkd["yrflag"]
   wndflg = blkd["wndflg"]
   lwflag  = blkd["lwflag"]

   # Main loop 
   always_calculate_interpolator = False
   always_calculate_rotator = False
   field_interpolator={}
   vector_rotator={}
   ffiles={}
   dt = start
   while dt <= end :
       
       logger.info("Reading at %s"%str(dt))
       #print af.known_names

       # Read variables
       af.get_timestep(dt)

       # Estimate dependent variable on native grid
       # radflx is downwelling longwave radiation
       # TODO: HYCOM-specific
       if wndflg in [1,2,3] :
           af.calculate_windstress()
           af.calculate_windspeed()
           af.calculate_ustar()

       #  Forcing used by old NERSC-HYCOM
       if nersc_forcing :
          if "relhum" not in af.known_names_explicit : af.calculate_relhum()
       #  Forcing used by new version 
       else :
          if "vapmix" not in af.known_names_explicit : af.calculate_vapmix()
          if "ssrd"   not in af.known_names_explicit : af.calculate_ssrd()
          if lwflag == -1 :
              if "strd"   not in af.known_names_explicit : af.calculate_strd()
          else :
              raise ValueError,"TODO: lwflag<>-1 not supported"

       # Open output files. Dict uses "known name" when mapping to file object
       # TODO: HYCOM-specific
       if dt == start :
          # Open files
          for k,v in forcingpropertyset.items() :
              if k in af.known_names :
                 ffiles[k]=abfile.ABFileForcing(
                       "forcing.%s"%v.name,"w",idm=Nx, jdm=Ny, 
                       cline1=af.name,
                       cline2="%s (%s)"%(v.name,v.cfunit))

       # Interpolation of all fields and unit conversion
       newfld={}
       for kn in [elem for elem in af.known_names if elem in ffiles.keys()] :

          # Read and convert field to units used by HYCOM
          # TODO: HYCOM-specific
          fld=af[kn].data_to_unit(forcingpropertyset[kn].cfunit)

          # Calculate fieldintepolator object
          if kn not in field_interpolator.keys() or always_calculate_interpolator:
             lo,la=af[kn].coords
             field_interpolator[kn]=modeltools.tools.FieldInterpolatorBilinear(lo,la,mlon,mlat)

          #Actual interpolation
          newfld[kn]=field_interpolator[kn].interpolate(fld)

       # Do rotation of u and v components if this the first component of a vector field
       for kn in af.known_names :
           if kn in modeltools.forcing.atmosphere.known_vectors.keys() and kn in ffiles.keys() :
               knu,knv = modeltools.forcing.atmosphere.known_vectors[kn]
               logger.info("Rotating %s,%s "%(knu,knv))

               # Calculate rotateVector object
               if kn not in vector_rotator.keys() or always_calculate_rotator:
                  vector_rotator[kn] = modeltools.tools.rotateVector(mlon,mlat)
               ur,vr=vector_rotator[kn].rotate(newfld[knu],newfld[knv])
               newfld[knu]=ur
               newfld[knv]=vr

       # Apply limits if specified
       for kn in [elem for elem in af.known_names if elem in ffiles.keys()] :
          newfld[kn] = forcingpropertyset[kn].apply_limit(newfld[kn])

       # Loop over open files and write
       # TODO: HYCOM specific
       for kn in ffiles.keys() :
               
           # Variable name used by hycom
           vname=forcingpropertyset[kn].name

           # Write to hycom file
           newdt=af[kn].time
           ord_day,hour,isec=modeltools.hycom.datetime_to_ordinal(newdt,yrflag)
           dtime=modeltools.hycom.dayfor(newdt.year,ord_day,hour,yrflag)
           ffiles[kn].write_field(newfld[kn],newfld[kn],vname,dtime,af.timestep_in_days)

           # Write diagnostics, if requested
           if plot_diag :
              tmp="forcing.%s."%vname 
              tmp=tmp+"%Y%m%d%H.png"
              tmp=newdt.strftime(tmp)
              logger.info( "plotting %s"%tmp)
              plot_fig(newfld[kn],newdt,vname,tmp)

       # Increase time
       dt = dt + af.timestep
       
   # CLose files            
   for kn in ffiles.keys() :
       ffiles[kn].close()
   af=[]
Example #5
0
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(myglobs):

    # Read cice files, plot volume
    reg = abfile.ABFileGrid("regional.grid", "r")
    plon = reg.read_field("plon")
    plat = reg.read_field("plat")
    scpx = reg.read_field("scpx")
    scpy = reg.read_field("scpy")

    figure1 = matplotlib.pyplot.figure(figsize=(8, 8))
    ax1 = figure1.add_subplot(111)
    ax1.set_title("Total Ice Area [ 1.000.000 km^2 ]")
    ax1.grid(True)

    figure2 = matplotlib.pyplot.figure(figsize=(8, 8))
    ax2 = figure2.add_subplot(111)
    ax2.set_title("Total Ice Volume [ km^3 ]")
    ax2.grid(True)

    for myglob in myglobs:
        print myglob
        files = glob.glob(myglob)

        l_area = []
        l_vol = []
        l_time = []

        for file in files:

            print file

            nc = netCDF4.Dataset(file, "r")
            aice = nc.variables["aice"][0, :, :]
            hice = nc.variables["hi"][0, :, :]
            newt = nc.variables["time"][0]

            vol = numpy.sum(aice * hice * scpx * scpy)
            area = numpy.sum(aice * scpx * scpy)

            t_unit = cfunits.Units(nc.variables["time"].units)
            my_t_unit = cfunits.Units('days since 1900-1-1')
            newt = cfunits.Units.conform(newt, t_unit, my_t_unit)
            newt = int(newt * 86400.)
            newdt = datetime.datetime(1900, 1, 1, 0, 0,
                                      0) + datetime.timedelta(seconds=newt)
            #print file, "%14.6gm**3    %14.6gm**2" % (vol,area)
            nc.close()

            l_area.append(area)
            l_vol.append(vol)
            l_time.append(newdt)

        #  Sort
        I = sorted(range(len(l_time)), key=lambda x: l_time[x])
        l_area = [l_area[i] for i in I]
        l_vol = [l_vol[i] for i in I]
        l_time = [l_time[i] for i in I]

        ax1.plot(l_time, numpy.array(l_area) * 1e-12, label=myglob, lw=3)
        ax2.plot(l_time, numpy.array(l_vol) * 1e-12, label=myglob, lw=3)

    ax1.legend()
    figure1.canvas.print_figure("icearea.png")

    ax2.legend()
    figure2.canvas.print_figure("icevolume.png")
def main(path):
    print path

    fnametemplate = "woa13_decav_%s%02d_04v2.nc"  # 0.25 degrees
    #fnametemplate="woa13_decav_%s%02d_01v2.nc" # 1.00 degrees

    dump_netcdf = True

    # Open seasonal files
    ncid = {}
    ncid["month"] = {"t": [], "s": []}
    ncid["season"] = {"t": [], "s": []}
    for i in range(4):
        fname = os.path.join(path, fnametemplate % ("s", i + 13))
        logger.debug("Opening %s" % fname)
        ncid["season"]["s"].append(netCDF4.Dataset(fname, "r"))

        fname = os.path.join(path, fnametemplate % ("t", i + 13))
        logger.debug("Opening %s" % fname)
        ncid["season"]["t"].append(netCDF4.Dataset(fname, "r"))

    # Open monthly files
    seasonal_ncid = {"t": [], "s": []}
    for i in range(12):
        fname = os.path.join(path, fnametemplate % ("s", i + 1))
        ncid["month"]["s"].append(netCDF4.Dataset(fname, "r"))
        logger.debug("Opening %s" % fname)

        fname = os.path.join(path, fnametemplate % ("t", i + 1))
        ncid["month"]["t"].append(netCDF4.Dataset(fname, "r"))
        logger.debug("Opening %s" % fname)

    # Open regional grid file
    gridfile = abfile.ABFileGrid("regional.grid", "r")
    plat = gridfile.read_field("plat")
    plon = gridfile.read_field("plon")
    gridfile.close()

    # Get lon_bnds from one of the files
    lon_bnds = ncid["month"]["s"][0].variables["lon_bnds"][:]
    lat_bnds = ncid["month"]["s"][0].variables["lat_bnds"][:]
    #print lon_bnds
    #print lat_bnds

    # Get grid spacing from bnds
    #print lon_bnds.shape
    dlon = lon_bnds[:, 1] - lon_bnds[:, 0]
    dlat = lat_bnds[:, 1] - lat_bnds[:, 0]
    if numpy.any(numpy.abs(dlon - dlon[0]) > numpy.abs(dlon[0] * 1e-7)):
        logger.error("longitude spacing not uniform")
    if numpy.any(numpy.abs(dlat - dlat[0]) > numpy.abs(dlat[0] * 1e-7)):
        logger.error("latgitude spacing not uniform")

    # Consistency checks for bnds
    for mkey in ncid.keys():
        for vkey in ncid[mkey].keys():
            for (i, tmpnc) in enumerate(ncid[mkey][vkey]):
                #print mkey,vkey,i,tmpnc.filepath()
                tmplo = tmpnc.variables["lon_bnds"]
                tmpla = tmpnc.variables["lat_bnds"]

                if any([
                        elem[1] - elem[0] <> 0
                        for elem in zip(lon_bnds.shape, tmplo.shape)
                ]):
                    loggear.error("longitude shapes differ between files")

                if any([
                        elem[1] - elem[0] <> 0
                        for elem in zip(lat_bnds.shape, tmpla.shape)
                ]):
                    logger.error("latitude shapes differ between files")

                if numpy.any(numpy.abs(lon_bnds - tmplo) > 1e-7):
                    logger.error("longitudes differ between files")

                if numpy.any(numpy.abs(lat_bnds - tmpla) > 1e-7):
                    logger.error("latitudes differ between files")

    # Bounds ok, Find pivot points for hycom on woa grid
    lon0 = lon_bnds[0, 0]
    lat0 = lat_bnds[0, 0]
    dlon = dlon[0]
    dlat = dlat[0]
    tmp = numpy.fmod(720. + plon - lon0, 360.)
    ipiv = tmp / dlon
    jpiv = (plat - lat0) / dlat
    plot_test(ipiv, "ipiv.png")
    plot_test(jpiv, "jpiv.png")

    # Find bilinear interpolation weights and cornerpoints
    s = ipiv - numpy.floor(ipiv)
    t = jpiv - numpy.floor(jpiv)
    wll = (1. - s) * (1. - t)
    wlr = s * (1. - t)
    wur = s * t
    wul = (1. - s) * t
    tmpsum = wll + wlr + wur + wul
    #print tmpsum.min(),tmpsum.max()
    plot_test(wll, "wll.png")
    ipiv = numpy.floor(ipiv).astype(int)
    jpiv = numpy.floor(jpiv).astype(int)
    print lat_bnds[-1, :]
    ipib = numpy.mod(ipiv + 1, lon_bnds.shape[0])
    jpib = numpy.minimum(jpiv + 1, lat_bnds.shape[0] - 1)

    ## Test on actual field
    #fld = ncid["month"]["s"][0].variables["s_an"][0,0,:]
    #print fld.shape
    #newfld = fld[jpiv,ipiv]*wll + fld[jpiv,ipib]*wlr + fld[jpib,ipib]*wur + fld[jpib,ipiv]*wul
    #plot_test(newfld,"newfld.png")

    sig = modeltools.hycom.Sigma(0)

    kkseason = ncid["season"]["s"][0].variables["s_an"].shape[1]
    kkmonth = ncid["month"]["s"][0].variables["s_an"].shape[1]
    logger.info("kkseason=%3d, kkmonth=%3d" % (kkseason, kkmonth))

    #NB: Not general
    def month_weights(month):
        i0 = ((month + 1 - 2) / 3) % 4
        i1 = (i0 + 1) % 4
        month0 = i0 * 3 + 2
        w1 = ((month + 1 - month0) % 12) / 3.
        w0 = 1. - w1
        return i0, i1, w0, w1

    # Loop over months
    for month in range(12):
        # season index and weights. Hardcoded, but possible to estimate from clim_bnds - file 1 is Jan, Feb, March, File 2 is April, MAy, June, etc...
        i0, i1, w0, w1 = month_weights(month + 1)

        if dump_netcdf:
            fname_out_nc = "extrapolated_WOA2013_modelgrid_m%02d.nc" % (month +
                                                                        1)
            ds_out = netCDF4.Dataset(fname_out_nc, "w", format="NETCDF4")
            ds_out.createDimension("depth", kkseason)
            ds_out.createDimension("idm", ipiv.shape[1])
            ds_out.createDimension("jdm", ipiv.shape[0])
            ds_out.createVariable("depth", "f4", ("depth", ))
            ds_out.createVariable("latitude", "f4", (
                "jdm",
                "idm",
            ))
            ds_out.createVariable("longitude", "f4", (
                "jdm",
                "idm",
            ))
            ds_out.createVariable("temperature", "f4", (
                "depth",
                "jdm",
                "idm",
            ))
            ds_out.createVariable("salinity", "f4", (
                "depth",
                "jdm",
                "idm",
            ))
            ds_out.createVariable("temperature_e", "f4", (
                "depth",
                "jdm",
                "idm",
            ))
            ds_out.createVariable("salinity_e", "f4", (
                "depth",
                "jdm",
                "idm",
            ))
            ds_out.createVariable("density_e", "f4", (
                "depth",
                "jdm",
                "idm",
            ))
            ds_out.variables["latitude"] = plat
            ds_out.variables["longitude"] = plon
            ds_out.variables["depth"][:] = ncid["season"]["s"][0].variables[
                "depth"][:]

        # Open HYCOM .a files
        t_abfile = abfile.ABFileRelaxZ("temp_sig%d_m%02d" %
                                       (sig.sigma, month + 1),
                                       "w",
                                       cline1="WOA2013 monthly",
                                       cline2="Potential temperature")
        s_abfile = abfile.ABFileRelaxZ("saln_sig%d_m%02d" %
                                       (sig.sigma, month + 1),
                                       "w",
                                       cline1="WOA2013 monthly",
                                       cline2="Salinity")
        d_abfile = abfile.ABFileRelaxZ(
            "dens_sig%d_m%02d" % (sig.sigma, month + 1),
            "w",
            cline1="WOA2013 monthly",
            cline2="Potential density(Sigma-%d)" % sig.sigma)

        logger.info("month = %2d/%2d, i0=%d, i1=%d, w0=%6.2f, w1=%6.2f" %
                    (month, 12, i0, i1, w0, w1))
        d_over = numpy.zeros(ipiv.shape)
        t_over = numpy.zeros(ipiv.shape)
        s_over = numpy.zeros(ipiv.shape)
        first = True
        for k in range(kkseason):
            #for k in range(1,kkseason,10) :
            #for k in range(kkseason-10,kkseason) :

            # Read 10 levels at a time

            logger.debug("Reading netcdf data")
            if k < kkmonth:
                myfile = "month"
                depth = ncid["month"]["s"][month].variables["depth"][k]

                s_in = ncid["month"]["s"][month].variables["s_an"][0, k, :]
                t_in = ncid["month"]["t"][month].variables["t_an"][0, k, :]
            else:
                myfile = "season"
                depth = ncid["season"]["s"][i0].variables["depth"][k]

                s_in_0 = ncid["season"]["s"][i0].variables["s_an"][0, k, :]
                s_in_1 = ncid["season"]["s"][i1].variables["s_an"][0, k, :]

                t_in_0 = ncid["season"]["t"][i0].variables["t_an"][0, k, :]
                t_in_1 = ncid["season"]["t"][i1].variables["t_an"][0, k, :]
                s_in = s_in_0 * w0 + s_in_1 * w1
                t_in = t_in_0 * w0 + t_in_1 * w1

            logger.info("%s file, level %3d/%3d, depth=%5.0f" %
                        (myfile, k, kkseason, depth))
            s_out = s_in[jpiv, ipiv] * wll + s_in[jpiv, ipib] * wlr + s_in[
                jpib, ipib] * wur + s_in[jpib, ipiv] * wul
            t_out = t_in[jpiv, ipiv] * wll + t_in[jpiv, ipib] * wlr + t_in[
                jpib, ipib] * wur + t_in[jpib, ipiv] * wul
            t_out = numpy.maximum(-0.055 * s_out, t_out)

            # write nc file
            if dump_netcdf:
                logger.info("Writing to netcdf file %s" % fname_out_nc)
                ds_out.variables["salinity"][k, :, :] = s_out
                ds_out.variables["temperature"][k, :, :] = t_out
                ds_out.sync()

            # unmask using nearest neighbour
            if first:
                method = "nearest"
            else:
                method = "linear"
            logger.debug("Unmasking salinity using %s" % method)
            s_out = modeltools.tools.extrapolate_data(s_out, method)
            logger.debug("Unmasking temperature using %s" % method)
            t_out = modeltools.tools.extrapolate_data(t_out, method)

            # This will fill the remainder with values from above. This will happen if
            # we move to climatology layers deeper than we have in the region
            if numpy.count_nonzero(s_out.mask) > 0 or numpy.count_nonzero(
                    t_out.mask) > 0:
                logger.debug("Unmasking salinity using layer above")
                s_out[s_out.mask] = s_over[s_out.mask]
                logger.debug("Unmasking temperature using layer above")
                t_out[t_out.mask] = t_over[t_out.mask]

            d_out = sig.SIG(t_out, s_out)

            I = numpy.where(d_out < d_over)
            if I and len(I[0]) <> 0:
                d_out[I] = d_over[I]
                s_out[I] = sig.SOFSIG(d_out[I], t_out[I])
                logger.info("Corrected %d points for density" % len(I[0]))
            if first:
                plot_test(s_out, "s_out.png")
                plot_test(t_out, "t_out.png")
                plot_test(d_out, "d_out.png")
                first = False

            d_over = d_out
            t_over = t_out
            s_over = s_out

            # Write hycom abfile
            logger.info("Writing to hycom files")
            s_abfile.write_field(s_out, s_out, "salinity             ", depth)
            t_abfile.write_field(t_out, t_out, "potential temperature", depth)
            d_abfile.write_field(d_out, d_out, "sigma-%d" % sig.sigma, depth)

            # write nc file
            if dump_netcdf:
                logger.info("Writing to netcdf file %s" % fname_out_nc)
                ds_out.variables["salinity_e"][k, :, :] = s_out
                ds_out.variables["temperature_e"][k, :, :] = t_out
                ds_out.variables["density_e"][k, :, :] = d_out
                ds_out.sync()

            # Some stats
            logger.info(
                "**Salinity    min=%10.3f, max=%10.3f, ave=%14.3f, rms=%10.3f"
                % (s_out.min(), s_out.max(), s_out.mean(),
                   numpy.sqrt(numpy.mean(s_out**2))))
            logger.info(
                "**Temperature min=%10.3f, max=%10.3f, ave=%14.3f, rms=%10.3f"
                % (t_out.min(), t_out.max(), t_out.mean(),
                   numpy.sqrt(numpy.mean(t_out**2))))
            logger.info(
                "**Density     min=%10.3f, max=%10.3f, ave=%14.3f, rms=%10.3f"
                % (d_out.min(), d_out.max(), d_out.mean(),
                   numpy.sqrt(numpy.mean(d_out**2))))

            first = False

        s_abfile.close()
        d_abfile.close()
        t_abfile.close()

        if dump_netcdf: ds_out.close()
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()
Example #9
0
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()
Example #10
0
def main(lon1,
         lat1,
         lon2,
         lat2,
         variable,
         files,
         filetype="archive",
         clim=None):

    print filetype
    gfile = abfile.ABFileGrid("regional.grid", "r")
    plon = gfile.read_field("plon")
    plat = gfile.read_field("plat")
    sec = modeltools.tools.Section([lon1, lon2], [lat1, lat2], plon, plat)

    I, J = sec.grid_indexes
    dist = sec.distance
    slon = sec.longitude
    slat = sec.latitude

    m = Basemap(projection='mill',
                llcrnrlon=-180.,
                llcrnrlat=-90.,
                urcrnrlon=180.,
                urcrnrlat=90.,
                resolution='l')
    (x, y) = m(slon, slat)
    figure = matplotlib.pyplot.figure()
    ax = figure.add_subplot(111)
    m.drawcoastlines()
    m.fillcontinents(color='coral', lake_color='aqua')
    m.drawparallels(numpy.arange(-90., 120., 30.),
                    labels=[1, 0, 0, 0])  # draw parallels
    m.drawmeridians(numpy.arange(0., 420., 60.), labels=[0, 0, 0,
                                                         1])  # draw meridians
    m.drawmapboundary()  # draw a line around the map region
    #m.plot(x,y,"r",lw=3)
    m.scatter(x, y, s=20, c=dist)
    figure.canvas.print_figure("map.png")

    dpname = modeltools.hycom.layer_thickness_variable[filetype]
    logger.info("Filetype %s: layer thickness variable is %s" %
                (filetype, dpname))

    for fcnt, myfile0 in enumerate(files):

        m = re.match("(.*)\.[ab]", myfile0)
        if m:
            myfile = m.group(1)
        else:
            myfile = myfile0

        if filetype == "archive":
            tmp = abfile.ABFileArchv(myfile, "r")
        elif filetype == "restart":
            tmp = abfile.ABFileRestart(myfile,
                                       "r",
                                       idm=gfile.idm,
                                       jdm=gfile.jdm)
        else:
            raise NotImplementedError, "Filetype %s not implemented" % filetype

        kdm = max(tmp.fieldlevels)

        intfsec = numpy.zeros((kdm + 1, I.size))
        datasec = numpy.zeros((kdm + 1, I.size))

        for k in range(kdm):
            logger.info("File %s, layer %03d/%03d" % (myfile, k, kdm))

            dp2d = tmp.read_field(dpname, k + 1)
            data2d = tmp.read_field(variable, k + 1)

            dp2d = numpy.ma.filled(dp2d, 0.) / modeltools.hycom.onem

            data2d = numpy.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 = numpy.ma.masked_where(datasec == 1e30, datasec)
        figure = matplotlib.pyplot.figure()
        ax = figure.add_subplot(111)
        P = ax.pcolormesh(dist / 1000., -intfsec, datasec)
        if clim is not None: P.set_clim(clim)
        for k in range(1, kdm + 1):
            if k % 10 == 0:
                PL = ax.plot(dist / 1000., -intfsec[k, :], "-", color="k")
            elif k % 5 == 0:
                PL = ax.plot(dist / 1000., -intfsec[k, :], "--", color="k")
            else:
                PL = ax.plot(dist / 1000., -intfsec[k, :], "-", color=".5")

            textx = dist[dist.size / 2] / 1000.
            texty = -0.5 * (intfsec[k - 1, dist.size / 2] +
                            intfsec[k, dist.size / 2])
            #print "textx,texty",textx,texty
            ax.text(textx,
                    texty,
                    str(k),
                    verticalalignment="center",
                    horizontalalignment="center",
                    fontsize=6)
        ax.figure.colorbar(P)
        ax.set_title(myfile)
        ax.set_ylabel(variable)
        ax.set_xlabel("distance along section [km]")
        matplotlib.pyplot.tight_layout()
        figure.canvas.print_figure("sec_%s_full_%s.png" %
                                   (variable, os.path.basename(myfile)))

        ax.set_ylim(-1000, 0)
        figure.canvas.print_figure("sec_%s_1000m_%s.png" %
                                   (variable, os.path.basename(myfile)))

        ax.set_ylim(-300, 0)
        figure.canvas.print_figure("sec_%s_300m_%s.png" %
                                   (variable, os.path.basename(myfile)))

        tmp.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(myfiles,
         fieldname,
         fieldlevel,
         idm=None,
         jdm=None,
         clim=None,
         filetype="archive",
         window=None,
         cmap="jet",
         datetime1=None,
         datetime2=None,
         vector="",
         tokml=False,
         masklim=None,
         dpi=180):

    #cmap=matplotlib.pyplot.get_cmap("jet")
    cmap = matplotlib.pyplot.get_cmap(cmap)

    if tokml:
        ab = abfile.ABFileGrid("regional.grid", "r")
        plon = ab.read_field("plon")
        plat = ab.read_field("plat")
        ab.close()

    # Prelim support for projections. import basemap only if needed since its usually not needed
    # aaaand installation can sometimes be a bit painful .... bmn is None now, define it if needed
    bm = None
    #from mpl_toolkits.basemap import Basemap
    #ab = abfile.ABFileGrid("regional.grid","r")
    #plon=ab.read_field("plon")
    #plat=ab.read_field("plat")
    #bm = Basemap(width=9000000,height=9000000,
    #      resolution='i',projection='stere',\
    #      lat_ts=70,lat_0=90,lon_0=-40.)
    #x,y=bm(plon,plat)

    if vector:
        logger.info("Vector component 1:%s" % fieldname)
        logger.info("Vector component 2:%s" % vector)

    figure = matplotlib.pyplot.figure(figsize=(8, 8))
    ax = figure.add_subplot(111)

    counter = 0
    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)
                if vector: fld2 = ab.read_field(vector, fieldlevel)
            elif filetype == "regional.depth":
                fld1 = ab.read_field(fieldname)
            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 = numpy.meshgrid(numpy.arange(fld1.shape[0]),
                                      numpy.arange(fld1.shape[1]))
            else:
                J, I = numpy.meshgrid(numpy.arange(window[1], window[3]),
                                      numpy.arange(window[0], window[2]))

            # Create scalar field for vectors
            if vector:
                fld = numpy.sqrt(fld1**2 + fld2**2)
            else:
                fld = fld1

            # Apply mask if requested
            if masklim:
                fld = numpy.ma.masked_where(fld <= masklim[0], fld)
                fld = numpy.ma.masked_where(fld >= masklim[1], fld)

            if tokml:
                #NB: Window not used
                if vector:
                    logger.warning(
                        "Vector plots in kml not implemented, only plottign vector speed"
                    )
                to_kml(plon, plat, fld, fieldname, cmap)
            else:

                if bm is not None:
                    P = bm.pcolormesh(x[J, I], y[J, I], fld[J, I], cmap=cmap)
                    bm.drawcoastlines()
                    bm.fillcontinents(color='.5', lake_color='aqua')
                    # draw parallels and meridians.
                    bm.drawparallels(numpy.arange(-80., 81., 20.))
                    bm.drawmeridians(numpy.arange(-180., 181., 20.))
                    bm.drawmapboundary()  #fill_color='aqua')
                else:
                    P = ax.pcolormesh(I, J, fld[J, I], cmap=cmap)
                cb = ax.figure.colorbar(P)
                if clim is not None: P.set_clim(clim)
                ax.set_title("%s:%s(%d)" % (myfile0, fieldname, fieldlevel))
                if vector:
                    skip = 10
                    I2 = I[::skip, ::skip]
                    J2 = J[::skip, ::skip]
                    ax.quiver(I2, J2, fld1[J2, I2], fld2[J2, I2])

                # Print figure.
                fnamepng_template = "%s_%d_%03d.png"
                fnamepng = fnamepng_template % (fieldname, fieldlevel, counter)
                logger.info("output in  %s" % fnamepng)
                figure.canvas.print_figure(fnamepng, dpi=dpi)
                ax.clear()
                cb.remove()

            counter = counter + 1
def main(lon1,lat1,lon2,lat2,variable,files,filetype="archive",clim=None,sectionid="",
      ijspace=False,xaxis="distance",section_map=False,dpi=180) :

   logger.info("Filetype is %s"% filetype)
   gfile = abfile.ABFileGrid("regional.grid","r")
   plon=gfile.read_field("plon")
   plat=gfile.read_field("plat")
   qlon=gfile.read_field("qlon")
   qlat=gfile.read_field("qlat")

   # 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
   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=numpy.maximum(-90.,slat.min()-10.)
      ur_lat=numpy.minimum(90. ,slat.max()+10.)
      m = Basemap(projection='mill', llcrnrlon=ll_lon, llcrnrlat=ll_lat, urcrnrlon=ur_lon, urcrnrlat=ur_lat, resolution='l')
      (x,y) = m(slon,slat)
      figure = matplotlib.pyplot.figure()
      ax=figure.add_subplot(111)
      m.drawcoastlines()
      #m.fillcontinents(color='coral',lake_color='aqua')
      m.drawparallels(numpy.arange(-90.,120.,30.),labels=[1,0,0,0]) # draw parallels
      m.drawmeridians(numpy.arange(0.,420.,60.),labels=[0,0,0,1]) # draw meridians
      m.drawmapboundary() # draw a line around the map region
      m.plot(x,y,"r",lw=3)
      m.etopo()
      #m.scatter(x,y,s=20,c=dist)
      pos = ax.get_position()
      #print pos
      asp=pos.height/pos.width
      #print asp
      w=figure.get_figwidth()
      #print w
      h=asp*w
      figure.set_figheight(h)
      if sectionid :
         figure.canvas.print_figure("map_%s.png"%sectionid,dpi=dpi)
      else :
         figure.canvas.print_figure("map.png",dpi=dpi)

   # 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]"

   # Loop over archive files
   figure = matplotlib.pyplot.figure()
   ax=figure.add_subplot(111)
   pos = ax.get_position()
   for fcnt,myfile0 in enumerate(files) :

      # Remove [ab] ending if present
      m=re.match("(.*)\.[ab]",myfile0)
      if m :
         myfile=m.group(1)
      else :
         myfile=myfile0

      # Add more filetypes if needed. By def we assume archive
      if filetype == "archive" :
         i_abfile = abfile.ABFileArchv(myfile,"r")
      elif filetype == "restart" :
         i_abfile = abfile.ABFileRestart(myfile,"r",idm=gfile.idm,jdm=gfile.jdm)
      else :
         raise NotImplementedError,"Filetype %s not implemented"%filetype

      # kdm assumed to be max level in ab file
      kdm=max(i_abfile.fieldlevels)

      # Set up interface and daat arrays
      intfsec=numpy.zeros((kdm+1,I.size))
      datasec=numpy.zeros((kdm+1,I.size))

      # Loop over layers in file. 
      logger.info("File %s"%(myfile))
      for k in range(kdm) :
         logger.debug("File %s, layer %03d/%03d"%(myfile,k,kdm))

         # Get 2D fields
         dp2d=i_abfile.read_field(dpname,k+1)
         data2d=i_abfile.read_field(variable,k+1)
         dp2d=numpy.ma.filled(dp2d,0.)/modeltools.hycom.onem
         data2d=numpy.ma.filled(data2d,1e30)

         # Place data into section arrays
         intfsec[k+1,:] = intfsec[k,:] + dp2d[J,I]
         if k==0 : datasec[k,:] = data2d[J,I]
         datasec[k+1,:] = data2d[J,I]

      i_maxd=numpy.argmax(numpy.abs(intfsec[kdm,:]))
      #print i_maxd
      
      # Set up section plot
      #datasec = numpy.ma.masked_where(datasec==1e30,datasec)
      datasec = numpy.ma.masked_where(datasec>0.5*1e30,datasec)
      #print datasec.min(),datasec.max()
      #figure = matplotlib.pyplot.figure()
      #ax=figure.add_subplot(111)
      #P=ax.pcolormesh(dist/1000.,-intfsec,datasec)
      P=ax.pcolormesh(x,-intfsec,datasec,cmap="jet")
      if clim is not None : P.set_clim(clim)

      # Plot layer interfaces
      for k in range(1,kdm+1) :
         if k%10 == 0 : 
            PL=ax.plot(x,-intfsec[k,:],"--",color="k",lw=.5)
         elif k%5 == 0 : 
            PL=ax.plot(x,-intfsec[k,:],"--",color="k",lw=.5)
         else :
            PL=ax.plot(x,-intfsec[k,:],"--",color=".5",lw=.5)

         textx = x[i_maxd]
         texty = -0.5*(intfsec[k-1,i_maxd] + intfsec[k,i_maxd])
         ax.text(textx,texty,str(k),verticalalignment="center",horizontalalignment="center",fontsize=6)
      cb=ax.figure.colorbar(P)
      ax.set_title(myfile)
      ax.set_ylabel(variable)
      ax.set_xlabel(xlab)
      #ax.set_position(pos)
      #matplotlib.pyplot.tight_layout()

      # Print in different y-lims 
      suff=os.path.basename(myfile)
      if sectionid : suff=suff+"_"+sectionid
      figure.canvas.print_figure("sec_%s_full_%s.png"%(variable,suff),dpi=dpi)
      ax.set_ylim(-1000,0)
      figure.canvas.print_figure("sec_%s_1000m_%s.png"%(variable,suff),dpi=dpi)
      ax.set_ylim(-300,0)
      figure.canvas.print_figure("sec_%s_300m_%s.png"%(variable,suff),dpi=dpi)

      # Close input file
      i_abfile.close()

      #
      ax.clear()
      cb.remove()
Example #14
0
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")
Example #15
0
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()
Example #16
0
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(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(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"
Example #19
0
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)
Example #20
0
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,blo,bla,shapiro_passes,resolution,cutoff,input_bathymetry) :

   gfile=abfile.ABFileGrid("regional.grid","r")
   plon=gfile.read_field("plon")
   plat=gfile.read_field("plat")
   scpx=gfile.read_field("scpx")
   scpy=gfile.read_field("scpy")
   width=numpy.median(scpx)/1000.0
   logger.info("Grid median resolution:%8.2f km "%(width))
   # give the bathy directory here
   if input_bathymetry=="GEBCO_2021":
     bathyDir="/cluster/projects/nn2993k/ModelInput/bathymetry/GEBCO_2021/"
     # GEBCO only - TODO: move logic to gebco set
     if resolution is None  :
        if width  > 20 :
          dfile=bathyDir+"GEBCO_2021_2D_median20km.nc"
        elif width > 8 :
          dfile=bathyDir+"GEBCO_2021_2D_median8km.nc"
        elif width > 4 :
          dfile=bathyDir+"GEBCO_2021_2D_median4km.nc"
        elif width > 2 :
          dfile=bathyDir+"GEBCO_2021_2D_median2km.nc"
        else :
          dfile=bathyDir+"GEBCO_2021_sub_ice_topo.nc"
        logger.info ("Source resolution not set - choosing datafile %s"%dfile)
     else :
       dfile=bathyDir+"GEBCO_2021_2D_median%dkm.nc" % resolution
       print(dfile)
       logger.info ("Source resolution set to %d km - trying to use datafile %s"%(resolution,dfile))
   elif input_bathymetry=="GEBCO_2014":
     bathyDir="/cluster/projects/nn2993k/ModelInput/bathymetry/GEBCO_2014/"
     # GEBCO only - TODO: move logic to gebco set
     if resolution is None  :
       if width  > 20 :
          dfile=bathyDir+"GEBCO_2014_2D_median20km.nc"
       elif width > 8 :
          dfile=bathyDir+"GEBCO_2014_2D_median8km.nc"
       elif width > 4 :
          dfile=bathyDir+"GEBCO_2014_2D_median4km.nc"
       else :
          dfile=bathyDir+"GEBCO_2014_2D.nc"
       logger.info ("Source resolution not set - choosing datafile %s"%dfile)
     else :
       dfile=bathyDir+"GEBCO_2014_2D_median%dkm.nc" % resolution
       logger.info ("Source resolution set to %d km - trying to use datafile %s"%(resolution,dfile))
   else:
     print("Input grid not defined, cuttetn opotion GEBCO_2014 and GEBCO_2021")

   gebco = modeltools.forcing.bathy.GEBCO(filename=dfile)
   w2=gebco.regrid(plon,plat,width=width)

   print(w2.min(),w2.max())

   # Run shapiro filter on interpolated data to remove 2 DeltaX noise
   w3=numpy.copy(w2)
   for i in range(shapiro_passes):
      logger.info("Shapiro filter ... pass %d"%(i+1))
      w3=modeltools.tools.shapiro_filter(w3,threshold=cutoff)
   #print w3.min(),w3.max()

   # Modify basin 
   w4=numpy.copy(-w3)
   it=1
   while it==1 or numpy.count_nonzero(w4-w4old) > 0 :
      w4old = numpy.copy(w4)
      logger.info("Basin modifications ... pass %d"%(it))
      w4=modeltools.tools.remove_isolated_basins(plon,plat,w4,blo,bla,threshold=cutoff)
      w4=modeltools.tools.remove_islets(w4,threshold=cutoff)
      w4=modeltools.tools.remove_one_neighbour_cells(w4,threshold=cutoff)
      logger.info("Modified %d points "%numpy.count_nonzero(w4-w4old) )
      it+=1
   w5=numpy.copy(w4)
   #print w5.min(),w5.max()

   # Mask data where depth below threshold
   w5=numpy.ma.masked_where(w5<=cutoff,w5)

   # Create netcdf file with all  stages for analysis
   logger.info("Writing bathymetry to file hycom_bathymetry.nc")
   ncid = netCDF4.Dataset("hycom_bathymetry.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("h_1","f8",("jdm","idm"))
   ncid.createVariable("h_2","f8",("jdm","idm"))
   ncid.createVariable("h_3","f8",("jdm","idm"))
   ncid.variables["lon"][:]=plon
   ncid.variables["lat"][:]=plat
   ncid.variables["h_1"][:]=w2
   ncid.variables["h_2"][:]=w3
   ncid.variables["h_3"][:]=w5
   ncid.close()

   # Print to HYCOM and CICE bathymetry files
   abfile.write_bathymetry("bathy",1,w5,cutoff)
   
   # Show some grid statistics 
   sx = (w2[1:,:-1]-w2[:-1,:-1])/scpy[1:,:-1]
   sy = (w2[:-1,1:]-w2[:-1,:-1])/scpx[:-1,1:]
   grad = sx + sy
   slopefac=numpy.sqrt(sx**2+sy**2)
   logger.info("Maximum slope factor after interpolation =%.4f"%slopefac.max())

   sx = (w5[1:,:-1]-w5[:-1,:-1])/scpy[1:,:-1]
   sy = (w5[:-1,1:]-w5[:-1,:-1])/scpx[:-1,1:]
   grad = sx + sy
   slopefac=numpy.sqrt(sx**2+sy**2)
   logger.info("Maximum slope factor after smoothing    =%.4f"%slopefac.max())

   
   figure = matplotlib.pyplot.figure(figsize=(8,8))
   ax=figure.add_subplot(111)
   P=ax.pcolor(slopefac)
   figure.colorbar(P,norm=matplotlib.colors.LogNorm(vmin=w5.min(), vmax=w5.max()))
   #ax.contour(w5)#,[-10.,-100.,-500.,-1000.])
   #ax.set_title("Slope fac in color, depth contours in black")
   logger.info("Slope factor in slopefac.png")
   figure.canvas.print_figure("slopefac.png")