Esempio n. 1
0
def main():
    print "ok"
    cg = modelgrid.ConformalMapping(-40., 140., -50., 140., 178.2, 181.42, 800,
                                    0., 80, 760, True, 0.365, False)

    la_n, lo_n = cg.oldtonew(70, -45)
    #print la_n,lo_n
    ip, jp = cg.pivotp(la_n, lo_n)
    #print ip,jp
    #print numpy.mod(-180.,360.)
    i, j = cg.ll2gind(70, -45)
    #print i,j
    lo, la = cg.gind2ll(i, j)
    #print lo,la

    tmp = modelgrid.ConformalGrid(-40., 140., -50., 140., 178.2, 181.42, 1000,
                                  0., 80, 950, True, 0.365, False)
    plat, plon = tmp.pgrid()
    #print plat.max()
    #print tmp.scpx()
    print plat.shape

    fig = tmp.plotgrid(1.5)
    fig.savefig("tst.png")
    tmp.save_to_scrip("tst.nc")

    datadict = tmp.create_datadict_hycom()
    abfile.write_regional_grid(datadict, endian="big")
Esempio n. 2
0
def main(grid):
    griddict = grid.create_datadict_hycom()
    abfile.write_regional_grid(griddict)
    abfile.write_diag_nc(griddict)
    logger.info("grid shown in grid.png")
    grid.plotgrid(2.).canvas.print_figure("grid.png")

    # Also dump cice grid file
    modeltools.cice.io.write_netcdf_grid(grid, "cice_grid.nc")
Esempio n. 3
0
def soda_to_regional_grid(fid):
    # Assume this is for t-cell centers
    plon = fid["longitude"][:]
    plat = fid["latitude"][:]
    dlon = plon[1] - plon[0]
    dlat = plat[1] - plat[0]
    ulon = plon - dlon
    ulat = numpy.copy(plat)
    vlon = numpy.copy(plon)
    vlat = plat - dlat
    qlon = plon - dlon
    qlat = plat - dlat

    plon, plat = numpy.meshgrid(plon, plat)
    ulon, ulat = numpy.meshgrid(ulon, ulat)
    vlon, vlat = numpy.meshgrid(vlon, vlat)
    qlon, qlat = numpy.meshgrid(qlon, qlat)

    # Rough estimate of grid sizes
    scpx = dlon * (110574.2727) * numpy.cos(numpy.radians(plat))
    scpy = dlat * (110574.2727) * numpy.ones(plat.shape)
    scux = dlon * (110574.2727) * numpy.cos(numpy.radians(ulat))
    scuy = dlat * (110574.2727) * numpy.ones(plat.shape)
    scvx = dlon * (110574.2727) * numpy.cos(numpy.radians(vlat))
    scvy = dlat * (110574.2727) * numpy.ones(plat.shape)
    scqx = dlon * (110574.2727) * numpy.cos(numpy.radians(qlat))
    scqy = dlat * (110574.2727) * numpy.ones(plat.shape)
    corio = numpy.sin(numpy.radians(qlat)) * 4. * numpy.pi / 86164.0
    asp = numpy.where(scpy == 0., 99.0, scpx / scpy)
    pang = numpy.zeros(plat.shape)

    # 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)
Esempio n. 4
0
def main() :
   print "ok"
   cg = modelgrid.ConformalMapping(
      -40.,140.,
      -50.,140.,
      178.2,181.42,800,
      0.,80,760,
      True,
      0.365,False)

   la_n,lo_n = cg.oldtonew(70,-45)
   #print la_n,lo_n
   ip,jp =  cg.pivotp(la_n,lo_n)
   #print ip,jp
   #print numpy.mod(-180.,360.)
   i,j= cg.ll2gind(70,-45)
   #print i,j
   lo,la = cg.gind2ll(i,j)
   #print lo,la


   tmp =  modelgrid.ConformalGrid(
      -40.,140.,
      -50.,140.,
      178.2,181.42,1000,
      0.,80,950,
      True,
      0.365,False)
   plat,plon=tmp.pgrid()
   #print plat.max()
   #print tmp.scpx()
   print plat.shape

   fig=tmp.plotgrid(1.5)
   fig.savefig("tst.png")
   tmp.save_to_scrip("tst.nc")

   datadict=tmp.create_datadict_hycom()
   abfile.write_regional_grid(datadict,endian="big")
Esempio n. 5
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")
Esempio n. 6
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")
Esempio n. 7
0
def main(grid):
    griddict = grid.create_datadict_hycom()
    abfile.write_regional_grid(griddict)
    abfile.write_diag_nc(griddict)
    logger.info("grid shown in grid.png")
    grid.plotgrid(2.).canvas.print_figure("grid.png")