예제 #1
0
def make_grid(filemesh, idm=4320, jdm=1188):

    meshfile_hgr = filemesh[:-6] + "hgr.nc"
    meshfile_zgr = filemesh[:-6] + "zgr.nc"
    maskfile = filemesh[:-11] + "mask.nc"

    ncidh = netCDF4.Dataset(meshfile_hgr, "r")
    ncidz = netCDF4.Dataset(meshfile_zgr, "r")
    ncidm = netCDF4.Dataset(maskfile, "r")

    # Now acquire the data. P-cell data
    plon = np.squeeze(ncidh.variables["glamt"][0, :, :])
    plat = np.squeeze(ncidh.variables["gphit"][0, :, :])
    scpx = np.squeeze(ncidh.variables["e1t"][0, :, :])
    scpy = np.squeeze(ncidh.variables["e2t"][0, :, :])

    # U-cell data.
    ulon = np.squeeze(u_to_hycom_u(ncidh.variables["glamu"][0, :, :]))
    ulat = np.squeeze(u_to_hycom_u(ncidh.variables["gphiu"][0, :, :]))
    scux = np.squeeze(u_to_hycom_u(ncidh.variables["e1u"][0, :, :]))
    scuy = np.squeeze(u_to_hycom_u(ncidh.variables["e2u"][0, :, :]))

    # V-cell data.
    # TODO: Proper extrapolation of data on grid edges (mainly bottom row)
    vlon = np.squeeze(v_to_hycom_v(ncidh.variables["glamv"][0, :, :]))
    vlat = np.squeeze(v_to_hycom_v(ncidh.variables["gphiu"][0, :, :]))
    scvx = np.squeeze(v_to_hycom_v(ncidh.variables["e1v"][0, :, :]))
    scvy = np.squeeze(v_to_hycom_v(ncidh.variables["e2v"][0, :, :]))

    # Q-cell data
    # TODO: Proper extrapolation of data on grid edges (mainly bottom row)
    qlon = np.squeeze(f_to_hycom_q(ncidh.variables["glamf"][0, :, :]))
    qlat = np.squeeze(f_to_hycom_q(ncidh.variables["gphif"][0, :, :]))
    scqx = np.squeeze(f_to_hycom_q(ncidh.variables["e1f"][0, :, :]))
    scqy = np.squeeze(f_to_hycom_q(ncidh.variables["e2f"][0, :, :]))

    # Angle used for rotation
    ulon_rgt = numpy.copy(ulon)
    ulat_rgt = numpy.copy(ulat)
    ulon_lft = numpy.roll(ulon, 1, axis=1)
    ulat_lft = numpy.roll(ulat, 1, axis=1)
    pang = modeltools.tools.p_azimuth(ulon_lft, ulat_lft, ulon_rgt, ulat_rgt)

    # Aspect ratio
    asp = numpy.where(scpy == 0., 99.0, scpx / scpy)

    # Coriolis
    corio = numpy.sin(
        numpy.radians(qlat)) * 4. * numpy.pi / 86164.0  # Sidereal day

    # Put inside datadict for abfile writing
    ddict = {}
    ddict["plon"] = plon
    ddict["plat"] = plat
    ddict["ulon"] = ulon
    ddict["ulat"] = ulat
    ddict["vlon"] = vlon
    ddict["vlat"] = vlat
    ddict["qlon"] = qlon
    ddict["qlat"] = qlat
    #
    ddict["scpx"] = scpx
    ddict["scpy"] = scpy
    ddict["scux"] = scux
    ddict["scuy"] = scuy
    ddict["scvx"] = scvx
    ddict["scvy"] = scvy
    ddict["scqx"] = scqx
    ddict["scqy"] = scqy
    #
    ddict["cori"] = corio
    ddict["pang"] = pang
    ddict["pasp"] = asp
    abfile.write_regional_grid(ddict)

    # Bathymetry
    hdepw = np.squeeze(ncidz.variables["hdepw"][0, :, :])
    mbathy = np.squeeze(ncidz.variables["mbathy"][0, :, :])

    ncidt = netCDF4.Dataset(maskfile, "r")
    tmask = np.squeeze(ncidt.variables["tmaskutil"][0, :, :])
    tmp2 = numpy.where(mbathy > 0., hdepw, 0.)
    abfile.write_bathymetry("bathy", 1, tmp2, 0.)

    shutil.move("depth_bathy_01.a", './dummped_depth_NMOa0.08_01.a')
    shutil.move("depth_bathy_01.b", './dummped_depth_NMOa0.08_01.b')
    shutil.move("regional.grid.a", "./dummped_regional.grid.a")
    shutil.move("regional.grid.b", "./dummped_regional.grid.b")
예제 #2
0
def main(meshfile, first_j=0):

    nemo_mesh = modeltools.nemo.NemoMesh(meshfile, first_j=first_j)

    #   ncid =netCDF4.Dataset(meshfile,"r")
    #   plon =ncid.variables["glamt"][0,:,:]
    #   plat =ncid.variables["gphit"][0,:,:]
    #   ulon =ncid.variables["glamu"][0,:,:]
    #   ulat =ncid.variables["gphiu"][0,:,:]
    #   vlon =ncid.variables["glamv"][0,:,:]
    #   vlat =ncid.variables["gphiv"][0,:,:]
    #   jdm,idm=plon.shape
    #
    #   # These tests are on full grid
    #   periodic_i = modeltools.nemo.periodic_i(plon,plat)
    #   arctic_patch=modeltools.nemo.arctic_patch(plon,plat)
    #   logger.info("Grid periodic in i  :%s"%str(periodic_i))
    #   logger.info("Arctic Patch        :%s"%str(arctic_patch))
    #
    #   # Not difficult to extend, but for now...
    #   if not periodic_i and arctic_patch:
    #      msg="Only periodic in i  and arctic patchsupported for now"
    #      logger.error(msg)
    #      raise ValueError,msg

    #  HYCOM stagger with same i,j index:
    #  -------------
    #
    #  x----x----x
    #  |    |    |
    #  |    |    |
    #  U----P----x
    #  |    |    |
    #  |    |    |
    #  Q----V----x
    #
    # Procedure:
    #  - Shift u points 1 cell to left
    #  - Shift v points 1 cell down
    #  - Shift q points 1 cell to left and 1 cell down

    # Get slices for the unique domain of the nemo mesh
    #si = nemo_mesh.slicei
    #sj = nemo_mesh.slicej

    # Now acquire the data. P-cell data
    plon = nemo_mesh.sliced(nemo_mesh["glamt"][0, :, :])
    plat = nemo_mesh.sliced(nemo_mesh["gphit"][0, :, :])
    scpx = nemo_mesh.sliced(nemo_mesh["e1t"][0, :, :])
    scpy = nemo_mesh.sliced(nemo_mesh["e2t"][0, :, :])

    # U-cell data.
    ulon = nemo_mesh.sliced(nemo_mesh.u_to_hycom_u(
        nemo_mesh["glamu"][0, :, :]))
    ulat = nemo_mesh.sliced(nemo_mesh.u_to_hycom_u(
        nemo_mesh["gphiu"][0, :, :]))
    scux = nemo_mesh.sliced(nemo_mesh.u_to_hycom_u(nemo_mesh["e1u"][0, :, :]))
    scuy = nemo_mesh.sliced(nemo_mesh.u_to_hycom_u(nemo_mesh["e2u"][0, :, :]))

    # V-cell data.
    # TODO: Proper extrapolation of data on grid edges (mainly bottom row)
    vlon = nemo_mesh.sliced(nemo_mesh.v_to_hycom_v(
        nemo_mesh["glamv"][0, :, :]))
    vlat = nemo_mesh.sliced(nemo_mesh.v_to_hycom_v(
        nemo_mesh["gphiu"][0, :, :]))
    scvx = nemo_mesh.sliced(nemo_mesh.v_to_hycom_v(nemo_mesh["e1v"][0, :, :]))
    scvy = nemo_mesh.sliced(nemo_mesh.v_to_hycom_v(nemo_mesh["e2v"][0, :, :]))

    # Q-cell data
    # TODO: Proper extrapolation of data on grid edges (mainly bottom row)
    qlon = nemo_mesh.sliced(nemo_mesh.f_to_hycom_q(
        nemo_mesh["glamf"][0, :, :]))
    qlat = nemo_mesh.sliced(nemo_mesh.f_to_hycom_q(
        nemo_mesh["gphif"][0, :, :]))
    scqx = nemo_mesh.sliced(nemo_mesh.f_to_hycom_q(nemo_mesh["e1f"][0, :, :]))
    scqy = nemo_mesh.sliced(nemo_mesh.f_to_hycom_q(nemo_mesh["e2f"][0, :, :]))

    # Angle used for rotation
    ulon_rgt = numpy.copy(ulon)
    ulat_rgt = numpy.copy(ulat)
    ulon_lft = numpy.roll(ulon, 1, axis=1)
    ulat_lft = numpy.roll(ulat, 1, axis=1)
    pang = modeltools.tools.p_azimuth(ulon_lft, ulat_lft, ulon_rgt, ulat_rgt)

    # Aspect ratio
    asp = numpy.where(scpy == 0., 99.0, scpx / scpy)

    # Coriolis
    corio = numpy.sin(
        numpy.radians(qlat)) * 4. * numpy.pi / 86164.0  # Sidereal day

    # Put inside datadict for abfile writing
    ddict = {}
    ddict["plon"] = plon
    ddict["plat"] = plat
    ddict["ulon"] = ulon
    ddict["ulat"] = ulat
    ddict["vlon"] = vlon
    ddict["vlat"] = vlat
    ddict["qlon"] = qlon
    ddict["qlat"] = qlat
    #
    ddict["scpx"] = scpx
    ddict["scpy"] = scpy
    ddict["scux"] = scux
    ddict["scuy"] = scuy
    ddict["scvx"] = scvx
    ddict["scvy"] = scvy
    ddict["scqx"] = scqx
    ddict["scqy"] = scqy
    #
    ddict["cori"] = corio
    ddict["pang"] = pang
    ddict["pasp"] = asp
    abfile.write_regional_grid(ddict)

    # Bathymetry
    hdepw = nemo_mesh.sliced(nemo_mesh["hdepw"][0, :, :])
    mbathy = nemo_mesh.sliced(nemo_mesh["mbathy"][0, :, :])
    tmp2 = numpy.where(mbathy > 0, hdepw, 0)
    abfile.write_bathymetry("bathy", 1, tmp2, 0.)

    # Total depth calc
    hdept = nemo_mesh.sliced(nemo_mesh["hdept"][0, :, :])
    hdepw = nemo_mesh.sliced(nemo_mesh["hdepw"][0, :, :])
    e3t_ps = nemo_mesh.sliced(nemo_mesh["e3t_ps"][0, :, :])
    e3w_ps = nemo_mesh.sliced(nemo_mesh["e3w_ps"][0, :, :])
    nav_lev = nemo_mesh["nav_lev"][:]
    gdept_0 = nemo_mesh["gdept_0"][0, :]
    gdepw_0 = nemo_mesh["gdepw_0"][0, :]
    e3t_0 = nemo_mesh["e3t_0"][0, :]
    tmp1 = numpy.where(mbathy > 0, gdepw_0[mbathy - 1] + e3t_ps, 0)
    itest = 0
    jtest = 300

    # KAL - my conclusions  (Double check with Gilles)
    #   hdepw = Total water depth in t-cell
    #   gdepw_0[mbathy-1] Gives Water depth of full vertical cells 1 up to and including cell mbathy-1
    #   e3t_ps            Is the fraction of cell "mbathy" that is used in the calculations'
    #   hdepw = gdepw_0[mbathy-1] + e3t_ps

    # Diag plot of depths
    figure = matplotlib.pyplot.figure(figsize=(8, 8))
    ax = figure.add_subplot(111)
    ax.hold(True)
    ax.plot(nav_lev, "r-", label="nav_lev")
    ax.plot(gdept_0, "b.", label="gdept_0")
    ax.plot(gdepw_0, "g.", label="gdepw_0")
    ax.plot(numpy.arange(nav_lev.size),
            numpy.ones((nav_lev.size, )) * hdepw[jtest, itest],
            "c",
            lw=2,
            label="hdepw")
    ax.plot(numpy.arange(nav_lev.size),
            numpy.ones((nav_lev.size, )) * hdept[jtest, itest],
            "m",
            lw=2,
            label="hdept")
    ax.plot(numpy.arange(nav_lev.size),
            numpy.ones((nav_lev.size, )) * gdepw_0[mbathy[jtest, itest] - 1] +
            e3t_ps[jtest, itest],
            "m.",
            lw=2,
            label="gdepw_0[mbathy[jtest,itest]-1]+e3t_ps")
    ax.legend()
    ax.set_ylim(hdepw[jtest, itest] - 600, hdepw[jtest, itest] + 600)
    figure.canvas.print_figure("tst.png")

    # Diag plot of depths
    figure = matplotlib.pyplot.figure(figsize=(8, 8))
    ax = figure.add_subplot(111)
    P = ax.pcolormesh(tmp2 - tmp1, vmin=-1e-2, vmax=1e-2)
    figure.colorbar(P)
    figure.canvas.print_figure("tst2.png")
예제 #3
0
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")
예제 #4
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)
예제 #5
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)
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)
예제 #7
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)
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)
예제 #9
0
def main(startdate, enddate, first_j=0):

    soda_template = "/work/shared/nersc/msc/SODA/3.3.1/monthly/soda3.3.1_mn_ocean_reg_%Y.nc"

    # open blkdat.input. Get nesting frequency
    bp = modeltools.hycom.BlkdatParser("blkdat.input")
    nestfq = bp["nestfq"]
    bnstfq = bp["bnstfq"]

    # Read soda-grid and topo from first file
    fid = netCDF4.Dataset(startdate.strftime(soda_template), "r")
    depth = fid["depth"][:]
    soda_to_regional_grid(fid)

    # Get bathymetry. Set to 0 wherever salinit in top layer is undefined.
    bathy = numpy.zeros(fid["salt"].shape[-2:])
    for k in range(depth.size):
        bathy[~numpy.squeeze(fid["salt"][0, k, :, :].mask)] = depth[k]
    abfile.write_bathymetry("SODA", 22, bathy, 0.)
    fid.close()

    # TODO:
    ip = bathy == 0.
    iu = bathy == 0.
    iv = bathy == 0.

    onem = 9806

    #if mean_file :
    #   fnametemplate_out="archm.%Y_%j_%H"
    #else :
    hycom_template = "archv.%Y_%j_%H"

    # Loop over nestfq, bnstfq.
    deltat = enddate - startdate
    dsec = deltat.days * 86400 + deltat.seconds
    baroclinic_nest_times = [
        startdate + datetime.timedelta(seconds=s)
        for s in numpy.arange(0, dsec, nestfq * 86400)
    ]
    barotropic_nest_times = [
        startdate + datetime.timedelta(seconds=s)
        for s in numpy.arange(0, dsec, bnstfq * 86400)
    ]
    tmp = sorted(set(barotropic_nest_times + baroclinic_nest_times))
    for dt in tmp:

        logger.info("Processing time %s" % str(dt))

        # Get "mid-month" dates
        if dt.day >= 15:
            nm = 1 + dt.month % 12
            ny = dt.year + dt.month / 12
            mm0 = datetime.datetime(dt.year, dt.month, 15, 0, 0, 0)
            mm1 = datetime.datetime(ny, nm, 15, 0, 0, 0)
        else:
            lm = 1 + (12 + dt.month - 2) % 12
            ly = dt.year - lm / 12
            print dt.month, lm, ly
            mm0 = datetime.datetime(ly, lm, 15, 0, 0, 0)
            mm1 = datetime.datetime(dt.year, dt.month, 15, 0, 0, 0)

        # Linear interpolation weights
        deltat = mm1 - mm0
        deltat = deltat.days + deltat.seconds / 86400.
        w1 = dt - mm0
        w1 = w1.days + w1.seconds / 86400.
        w1 = w1 / deltat
        w0 = 1. - w1
        flnm0 = mm0.strftime(soda_template)
        flnm1 = mm1.strftime(soda_template)
        logger.info("Time %s, file %s at %s(w=%.4f) , file %s at %s(w=%.4f)" %
                    (str(dt), flnm0, str(mm0), w0, flnm1, str(mm1), w1))

        # Open files
        # TODO: reuse pointers/fields
        fid0 = netCDF4.Dataset(flnm0, "r")
        fid1 = netCDF4.Dataset(flnm1, "r")

        # Calculate temperature, velocity
        temp = w0 * fid0["temp"][0, :, :, :] + w1 * fid1["temp"][0, :, :, :]
        salt = w0 * fid0["salt"][0, :, :, :] + w1 * fid1["salt"][0, :, :, :]
        utot = w0 * fid0["u"][0, :, :, :] + w1 * fid1["u"][0, :, :, :]
        vtot = w0 * fid0["v"][0, :, :, :] + w1 * fid1["v"][0, :, :, :]

        #NB: No checks for missing values yet !
        ubaro = numpy.sum(utot, 0)
        vbaro = numpy.sum(vtot, 0)
        u = utot - ubaro
        v = vtot - vbaro

        # 2D vars
        anompb = w0 * fid0["anompb"][0, :, :] + w1 * fid1["anompb"][0, :, :]
        ssh = w0 * fid0["ssh"][0, :, :] + w1 * fid1["ssh"][0, :, :]
        salflx = w0 * fid0["salt_flux_total"][
            0, :, :] + w1 * fid1["salt_flux_total"][0, :, :]
        surflx = w0 * fid0["net_heating"][
            0, :, :] + w1 * fid1["salt_flux_total"][0, :, :]
        montg1 = numpy.zeros(ssh.shape)

        # Write to abfile
        outfile = abfile.ABFileArchv(dt.strftime(hycom_template),
                                     "w",
                                     iexpt=10,
                                     iversn=22,
                                     yrflag=3)
        logger.info("Writing 2D variables")
        outfile.write_field(montg1, ip, "montg1", 0, 0, 1, 0)
        outfile.write_field(ssh, ip, "srfhgt", 0, 0, 0, 0)
        outfile.write_field(surflx, ip, "surflx", 0, 0, 0, 0)
        outfile.write_field(salflx, ip, "salflx", 0, 0, 0, 0)
        outfile.write_field(numpy.zeros(ssh.shape), ip, "bl_dpth", 0, 0, 0, 0)
        outfile.write_field(numpy.zeros(ssh.shape), ip, "mix_dpth", 0, 0, 0, 0)
        outfile.write_field(ubaro, iu, "u_btrop", 0, 0, 0, 0)
        outfile.write_field(vbaro, iv, "v_btrop", 0, 0, 0, 0)
        #outfile.close() ; raise NameError,"test"
        for k in numpy.arange(u.shape[0]):
            if k % 10 == 0:
                logger.info("Writing 3D variables, level %d of %d" %
                            (k + 1, u.shape[0]))

            if k == 0:
                dtl = depth[0] * numpy.where(bathy >= depth[k], 1, 0)
            else:
                dtl = (depth[k] - depth[k - 1]) * numpy.where(
                    bathy >= depth[k], 1, 0)
            print dtl.min(), dtl.max()

            templ = temp[k, :, :]
            saltl = salt[k, :, :]

            # Set to layer above if undefined
            templ[dtl <= 0.] = 20.
            saltl[dtl <= 0.] = 35.

            outfile.write_field(u[k, :, :], iu, "u-vel.", 0, 0, k + 1, 0)
            outfile.write_field(v[k, :, :], iv, "v-vel.", 0, 0, k + 1, 0)
            outfile.write_field(dtl * onem, ip, "thknss", 0, 0, k + 1, 0)
            outfile.write_field(saltl, ip, "salin", 0, 0, k + 1, 0)
            outfile.write_field(templ, ip, "temp", 0, 0, k + 1, 0)

            oldsaltl = saltl
            oldtempl = templ

        # TODO: reuse pointers/fields
        outfile.close()
        fid0.close()
        fid1.close()
        raise NameError, "check vals"

    raise NameError, "test"

    itest = 1
    jtest = 200
    logger.info("Mean file:%s" % str(mean_file))
    logger.info("Output file template:%s" % str(fnametemplate))

    # Write regional files
    nemo_mesh_to_hycom.main(filemesh, first_j=first_j)

    nemo_mesh = modeltools.nemo.NemoMesh(filemesh, first_j=first_j)

    #ncidmesh=netCDF4.Dataset(filemesh,"r")
    gdept = nemo_mesh["gdept_0"][0, :]  # Depth of t points
    gdepw = nemo_mesh["gdepw_0"][0, :]  # Depth of w points
    e3t_ps = nemo_mesh.sliced(
        nemo_mesh["e3t_ps"][0, :, :])  # Partial steps of t cell
    e3w_ps = nemo_mesh.sliced(
        nemo_mesh["e3w_ps"][0, :, :])  # Partial steps of w cell
    mbathy = nemo_mesh.sliced(nemo_mesh["mbathy"][0, :, :])  # bathy index
    hdepw = nemo_mesh.sliced(
        nemo_mesh["hdepw"][0, :, :])  # Total depth of w points
    mbathy = mbathy - 1  # python indexing starts from 0
    nlev = gdept.size

    mbathy_u, e3u_ps, depthu = nemo_mesh.depth_u_points()
    mbathy_v, e3v_ps, depthv = nemo_mesh.depth_v_points()
    #
    mbathy_u = nemo_mesh.sliced(nemo_mesh.u_to_hycom_u(mbathy_u))
    e3u_ps = nemo_mesh.sliced(nemo_mesh.u_to_hycom_u(e3u_ps))
    depthu = nemo_mesh.sliced(nemo_mesh.u_to_hycom_u(depthu))
    #
    mbathy_v = nemo_mesh.sliced(nemo_mesh.v_to_hycom_v(mbathy_v))
    e3v_ps = nemo_mesh.sliced(nemo_mesh.v_to_hycom_v(e3v_ps))
    depthv = nemo_mesh.sliced(nemo_mesh.v_to_hycom_v(depthv))

    # Thickness of t layers (NB: 1 less than gdepw dimension)
    dt = gdepw[1:] - gdepw[:-1]

    # Loop over input files. All must be in same directory
    for file2d in grid2dfiles:

        # See if actually a grid2D file
        dirname = os.path.dirname(file2d)
        m = re.match("(.*_)(grid2D)(_.*\.nc)", os.path.basename(file2d))
        if not m:
            msg = "File %s is not a grid2D file, aborting" % file2d
            logger.error(msg)
            raise ValueError, msg

        # Construct remaining files
        filet = os.path.join(dirname, m.group(1) + "gridT" + m.group(3))
        files = os.path.join(dirname, m.group(1) + "gridS" + m.group(3))
        fileu = os.path.join(dirname, m.group(1) + "gridU" + m.group(3))
        filev = os.path.join(dirname, m.group(1) + "gridV" + m.group(3))
        filew = os.path.join(dirname, m.group(1) + "gridW" + m.group(3))
        fileice = os.path.join(dirname, m.group(1) + "icemod" + m.group(3))
        logger.info("grid2D file: %s" % file2d)

        # P-points
        logger.info("gridS  file: %s" % files)
        logger.info("gridT  file: %s" % filet)
        ncids = netCDF4.Dataset(files, "r")
        s = numpy.zeros((nlev, mbathy.shape[0], mbathy.shape[1]))
        for k in range(nlev):  # Dont include lowest layer
            s[k, :, :] = nemo_mesh.sliced(ncids.variables["vosaline"][0,
                                                                      k, :, :])
        s = numpy.where(s < 1e30, s, 0.)
        s = numpy.where(s == ncids.variables["vosaline"]._FillValue, 0., s)
        ncidt = netCDF4.Dataset(filet, "r")
        t = numpy.zeros((nlev, mbathy.shape[0], mbathy.shape[1]))
        for k in range(nlev):  # Dont include lowest layer
            t[k, :, :] = nemo_mesh.sliced(ncidt.variables["votemper"][0,
                                                                      k, :, :])
        t = numpy.where(t == ncidt.variables["votemper"]._FillValue, 0., t)
        t = numpy.where(t < 1e30, t, 0.)

        # time from gridT file.
        time = ncidt.variables["time_counter"][0]
        tunit = ncidt.variables["time_counter"].units
        tmp = cfunits.Units(tunit)
        refy, refm, refd = (1958, 1, 1)
        tmp2 = cfunits.Units("seconds since %d-%d-%d 00:00:00" %
                             (refy, refm, refd))  # Units from CF convention
        tmp3 = cfunits.Units.conform(time, tmp,
                                     tmp2)  # Transform to new new unit
        mydt = datetime.datetime(refy, refm,
                                 refd, 0, 0, 0) + datetime.timedelta(
                                     seconds=tmp3)  # Then calculate dt. Phew!

        # Read and calculculate U in hycom U-points.
        logger.info("gridU  file: %s" % fileu)
        ncidu = netCDF4.Dataset(fileu, "r")
        u = numpy.zeros((nlev, mbathy.shape[0], mbathy.shape[1]))
        for k in range(nlev):
            u[k, :, :] = nemo_mesh.sliced(
                nemo_mesh.u_to_hycom_u(ncidu.variables["vozocrtx"][
                    0, k, :, :]))  # Costly, make more efficient if needed
        u = numpy.where(numpy.abs(u) < 1e10, u, 0.)

        #Calculate barotropic and baroclinic u
        usum = numpy.zeros(u.shape[-2:])
        dsum = numpy.zeros(u.shape[-2:])
        for k in range(u.shape[0] - 1):  # Dont include lowest layer
            # TODO: Mid-layer depths seem to be undefined - figure out why ...
            logger.debug(
                "k=%3d, u=%10.3g, mbathy_u[jtest,itest]=%3d,gdepw[k]=%8.2f, depthu[jtest,itest]=%8.2f"
                % (k, u[k, jtest, itest], mbathy_u[jtest, itest], gdepw[k],
                   depthu[jtest, itest]))
            J, I = numpy.where(mbathy_u > k)
            usum[J, I] = usum[J, I] + u[k, J, I] * dt[k]
            dsum[J, I] = dsum[J, I] + dt[k]
        J, I = numpy.where(mbathy >= 0)
        usum[J, I] = usum[J, I] + u[mbathy_u[J, I], J, I] * e3u_ps[J, I]
        dsum[J, I] = dsum[J, I] + e3u_ps[J, I]
        ubaro = numpy.where(dsum > 0.1, usum / dsum, 0.)

        # Read and calculculate V in hycom V-points.
        logger.info("gridV  file: %s" % filev)
        ncidv = netCDF4.Dataset(filev, "r")
        v = numpy.zeros((nlev, mbathy.shape[0], mbathy.shape[1]))
        for k in range(nlev):
            v[k, :, :] = nemo_mesh.sliced(
                nemo_mesh.v_to_hycom_v(ncidv.variables["vomecrty"][
                    0, k, :, :]))  # Costly, make more efficient if needed
        v = numpy.where(numpy.abs(v) < 1e10, v, 0.)

        #Calculate barotropic and baroclinic v
        vsum = numpy.zeros(v.shape[-2:])
        dsum = numpy.zeros(v.shape[-2:])
        for k in range(v.shape[0] - 1):  # Dont include lowest layer
            logger.debug(
                "k=%3d, v=%10.3g, mbathy_v[jtest,itest]=%3d,gdepw[k]=%8.2f, depthv[jtest,itest]=%8.2f"
                % (k, v[k, jtest, itest], mbathy_v[jtest, itest], gdepw[k],
                   depthv[jtest, itest]))
            J, I = numpy.where(mbathy_v > k)
            vsum[J, I] = vsum[J, I] + v[k, J, I] * dt[k]
            dsum[J, I] = dsum[J, I] + dt[k]
        J, I = numpy.where(mbathy_u >= 0)
        vsum[J, I] = vsum[J, I] + v[mbathy_u[J, I], J, I] * e3v_ps[J, I]
        dsum[J, I] = dsum[J, I] + e3v_ps[J, I]
        vbaro = numpy.where(dsum > .1, vsum / dsum, 0.)

        # Masks (land:True)
        #print mbathy.min(),mbathy.max()
        ip = mbathy == -1
        iu = mbathy_u == -1
        iv = mbathy_v == -1
        #iu = nemo_mesh.periodic_i_shift_right(iu,1)   # u: nemo in cell i is hycom in cell i+1
        #iv = nemo_mesh.arctic_patch_shift_up(iu,1)    # v: nemo in cell j is hycom in cell j+1
        #ip = nemo_mesh.sliced(ip)
        #iu = nemo_mesh.sliced(iu)
        #iv = nemo_mesh.sliced(iv)
        #raise NameError,"test"

        # 2D data
        ncid2d = netCDF4.Dataset(file2d, "r")
        ssh = nemo_mesh.sliced(ncid2d.variables["sossheig"][0, :, :])
        ssh = numpy.where(ssh == ncid2d.variables["sossheig"]._FillValue, 0.,
                          ssh)
        ssh = numpy.where(ssh > 1e30, 0., ssh)  # Hmmmmm
        #bar_height   = nemo_mesh.sliced(ncid2d.variables["sobarhei"][0,:,:] )
        #dyn_height   = nemo_mesh.sliced(ncid2d.variables["sodynhei"][0,:,:]
        montg1 = ssh * 9.81  #* 1e-3  # Approx
        logger.warning("TODO:montg pot calculation must be checked...")

        # Write to abfile
        outfile = abfile.ABFileArchv(
            mydt.strftime(fnametemplate),
            "w",
            iexpt=10,
            iversn=22,
            yrflag=3,
        )
        logger.info("Writing 2D variables")
        outfile.write_field(montg1, ip, "montg1", 0, 0, 1, 0)
        outfile.write_field(ssh, ip, "srfhgt", 0, 0, 0, 0)
        outfile.write_field(numpy.zeros(ssh.shape), ip, "surflx", 0, 0, 0,
                            0)  # Not used
        outfile.write_field(numpy.zeros(ssh.shape), ip, "salflx", 0, 0, 0,
                            0)  # Not used
        outfile.write_field(numpy.zeros(ssh.shape), ip, "bl_dpth", 0, 0, 0,
                            0)  # Not used
        outfile.write_field(numpy.zeros(ssh.shape), ip, "mix_dpth", 0, 0, 0,
                            0)  # Not used
        outfile.write_field(ubaro, iu, "u_btrop", 0, 0, 0,
                            0)  # u: nemo in cell i is hycom in cell i+1
        outfile.write_field(vbaro, iv, "v_btrop", 0, 0, 0,
                            0)  # v: nemo in cell j is hycom in cell j+1
        #outfile.close() ; raise NameError,"test"
        for k in numpy.arange(u.shape[0]):
            if k % 10 == 0:
                logger.info("Writing 3D variables, level %d of %d" %
                            (k + 1, u.shape[0]))
            ul = numpy.squeeze(u[k, :, :]) - ubaro  # Baroclinic velocity
            vl = numpy.squeeze(v[k, :, :]) - vbaro  # Baroclinic velocity
            sl = numpy.squeeze(s[k, :, :])
            tl = numpy.squeeze(t[k, :, :])

            # Layer thickness
            dtl = numpy.zeros(ul.shape)
            if k < u.shape[0] - 1:
                J, I = numpy.where(mbathy > k)
                dtl[J, I] = dt[k]
                J, I = numpy.where(mbathy == k)
                dtl[J, I] = e3t_ps[J, I]
            else:
                J, I = numpy.where(mbathy == k)
                dtl[J, I] = e3t_ps[J, I]

            onem = 9806.
            outfile.write_field(ul, iu, "u-vel.", 0, 0, k + 1,
                                0)  # u: nemo in cell i is hycom in cell i+1
            outfile.write_field(vl, iv, "v-vel.", 0, 0, k + 1,
                                0)  # v: nemo in cell j is hycom in cell j+1
            outfile.write_field(dtl * onem, ip, "thknss", 0, 0, k + 1, 0)
            outfile.write_field(sl, ip, "salin", 0, 0, k + 1, 0)
            outfile.write_field(tl, ip, "temp", 0, 0, k + 1, 0)

        # TODO: Process ice data
        ncid2d.close()
        ncids.close()
        ncidt.close()
        ncidu.close()
        ncidv.close()
        outfile.close()

        logger.info("Finished writing %s.[ab] " % mydt.strftime(fnametemplate))
    nemo_mesh = []