Beispiel #1
0
def get_ROMS_vgrid(gridid, zeta=None):
    """
    vgrid = get_ROMS_vgrid(gridid)

    Load ROMS vertical grid object. vgrid is a s_coordinate or
    a z_coordinate object, depending on gridid.grdtype.
    vgrid.z_r and vgrid.z_w (vgrid.z for a z_coordinate object)
    can be indexed in order to retreive the actual depths. The
    free surface time serie zeta can be provided as an optional
    argument. Note that the values of zeta are not calculated
    until z is indexed, so a netCDF variable for zeta may be passed,
    even if the file is large, as only the values that are required
    will be retrieved from the file.
    """

    gridinfo = ROMS_gridinfo(gridid)
    grdfile = gridinfo.grdfile

    nc = io.Dataset(grdfile)

    #Get vertical grid
    try:
        h = nc.variables['h'][:]
    except:
        raise ValueError('NetCDF file must contain the bathymetry h')

    try:
        hraw = nc.variables['hraw'][:]
    except:
        hraw = None

    if gridinfo.grdtype == 'roms':
        Vtrans = gridinfo.Vtrans
        theta_b = gridinfo.theta_b
        theta_s = gridinfo.theta_s
        Tcline = gridinfo.Tcline
        N = gridinfo.N
        if Vtrans == 1:
            vgrid = s_coordinate(h, theta_b, theta_s, Tcline, N, hraw=hraw, zeta=zeta)
        elif Vtrans == 2:
            vgrid = s_coordinate_2(h, theta_b, theta_s, Tcline, N, hraw=hraw, zeta=zeta)
        elif Vtrans == 4:
            vgrid = s_coordinate_4(h, theta_b, theta_s, Tcline, N, hraw=hraw, zeta=zeta)
        elif Vtrans == 5:
            vgrid = s_coordinate_5(h, theta_b, theta_s, Tcline, N, hraw=hraw, zeta=zeta)
        else:
            raise Warning('Unknown vertical transformation Vtrans')

    elif  gridinfo.grdtype == 'z':
        N = gridinfo.N
        depth = gridinfo.depth
        vgrid = z_coordinate(h, depth, N)

    else:
        raise ValueError('Unknown grid type')

    return vgrid
Beispiel #2
0
def get_ROMS_hgrid(gridid):
    """
    hgrid = get_ROMS_hgrid(gridid)

    Load ROMS horizontal grid object
    """

    gridinfo = ROMS_gridinfo(gridid)
    grdfile = gridinfo.grdfile

    nc = io.Dataset(grdfile)

    #Check for cartesian or geographical grid
    spherical = nc.variables['spherical'][:]

    #Get horizontal grid
    if spherical == 'F':
        #cartesian grid
        print 'Load cartesian grid from file'
        if 'x_vert' in nc.variables.keys() and 'y_vert' in nc.variables.keys():
            x_vert = nc.variables['x_vert'][:]
            y_vert = nc.variables['y_vert'][:]
        elif 'x_rho' in nc.variables.keys() and 'y_rho' in nc.variables.keys() \
                 and 'pm' in nc.variables.keys() and 'pn' in nc.variables.keys():
            x_rho = nc.variables['x_rho'][:]
            y_rho = nc.variables['y_rho'][:]
            pm = nc.variables['pm'][:]
            pn = nc.variables['pn'][:]
            try:
                angle = nc.variables['angle'][:]
            except:
                angle = np.zeros(x_rho.shape)
            #compute verts from rho point, pm, pn, angle
            x_vert, y_vert = rho_to_vert(x_rho, y_rho, pm, pn, angle)
        else:
            raise ValueError, 'NetCDF file must contain x_vert and y_vert \
                     or x_rho, y_rho, pm, pn and angle for a cartesian grid'

        if 'x_rho' in nc.variables.keys() and 'y_rho' in nc.variables.keys() and \
             'x_u' in nc.variables.keys() and 'y_u' in nc.variables.keys() and \
             'x_v' in nc.variables.keys() and 'y_v' in nc.variables.keys() and \
             'x_psi' in nc.variables.keys() and 'y_psi' in nc.variables.keys():
            x_rho = nc.variables['x_rho'][:]
            y_rho = nc.variables['y_rho'][:]
            x_u = nc.variables['x_u'][:]
            y_u = nc.variables['y_u'][:]
            x_v = nc.variables['x_v'][:]
            y_v = nc.variables['y_v'][:]
            x_psi = nc.variables['x_psi'][:]
            y_psi = nc.variables['y_psi'][:]
        else:
            x_rho = None
            y_rho = None
            x_u = None
            y_u = None
            x_v = None
            y_v = None
            x_psi = None
            y_psi = None

        if 'pm' in nc.variables.keys() and 'pn' in nc.variables.keys():
            pm = nc.variables['pm'][:]
            dx = 1. / pm
            pn = nc.variables['pn'][:]
            dy = 1. / pn
        else:
            dx = None
            dy = None

        if 'dndx' in nc.variables.keys() and 'dmde' in nc.variables.keys():
            dndx = nc.variables['dndx'][:]
            dmde = nc.variables['dmde'][:]
        else:
            dndx = None
            dmde = None

        if 'angle' in nc.variables.keys():
            angle = nc.variables['angle'][:]
        else:
            angle = None

        #Get cartesian grid
        hgrd = CGrid(x_vert, y_vert, x_rho=x_rho, y_rho=y_rho, \
                     x_u=x_u, y_u=y_u, x_v=x_v, y_v=y_v, \
                     x_psi=x_psi, y_psi=y_psi, dx=dx, dy=dy, \
                     dndx=dndx, dmde=dmde, angle_rho=angle)

    else:
        #geographical grid
        print 'Load geographical grid from file'
        proj = Basemap(projection='merc', resolution=None, lat_0=0, lon_0=0)
        if 'lon_vert' in nc.variables.keys(
        ) and 'lat_vert' in nc.variables.keys():
            lon_vert = nc.variables['lon_vert'][:]
            lat_vert = nc.variables['lat_vert'][:]
        elif 'lon_rho' in nc.variables.keys() and 'lon_rho' in nc.variables.keys() \
                and 'lon_psi' in nc.variables.keys() and 'lat_psi' in nc.variables.keys():
            lon_rho = nc.variables['lon_rho'][:]
            lat_rho = nc.variables['lat_rho'][:]
            lon_psi = nc.variables['lon_psi'][:]
            lat_psi = nc.variables['lat_psi'][:]
            #compute verts from rho and psi point
            lon_vert, lat_vert = rho_to_vert_geo(lon_rho, lat_rho, lon_psi,
                                                 lat_psi)
        else:
            raise ValueError, 'NetCDF file must contain lon_vert and lat_vert \
                  or lon_rho, lat_rho, lon_psi, lat_psi for a geographical grid'

        if 'lon_rho' in nc.variables.keys() and 'lat_rho' in nc.variables.keys() and \
              'lon_u' in nc.variables.keys() and 'lat_u' in nc.variables.keys() and \
              'lon_v' in nc.variables.keys() and 'lat_v' in nc.variables.keys() and \
              'lon_psi' in nc.variables.keys() and 'lat_psi' in nc.variables.keys():
            lon_rho = nc.variables['lon_rho'][:]
            lat_rho = nc.variables['lat_rho'][:]
            lon_u = nc.variables['lon_u'][:]
            lat_u = nc.variables['lat_u'][:]
            lon_v = nc.variables['lon_v'][:]
            lat_v = nc.variables['lat_v'][:]
            lon_psi = nc.variables['lon_psi'][:]
            lat_psi = nc.variables['lat_psi'][:]
        else:
            lon_rho = None
            lat_rho = None
            lon_u = None
            lat_u = None
            lon_v = None
            lat_v = None
            lon_psi = None
            lat_psi = None

        if 'pm' in nc.variables.keys() and 'pn' in nc.variables.keys():
            pm = nc.variables['pm'][:]
            dx = 1. / pm
            pn = nc.variables['pn'][:]
            dy = 1. / pn
        else:
            dx = None
            dy = None

        if 'dndx' in nc.variables.keys() and 'dmde' in nc.variables.keys():
            dndx = nc.variables['dndx'][:]
            dmde = nc.variables['dmde'][:]
        else:
            dndx = None
            dmde = None

        if 'angle' in nc.variables.keys():
            angle = nc.variables['angle'][:]
        else:
            angle = None

        #Get geographical grid
        hgrd = CGrid_geo(lon_vert, lat_vert, proj, \
                         lon_rho=lon_rho, lat_rho=lat_rho, \
                         lon_u=lon_u, lat_u=lat_u, lon_v=lon_v, lat_v=lat_v, \
                         lon_psi=lon_psi, lat_psi=lat_psi, dx=dx, dy=dy, \
                         dndx=dndx, dmde=dmde, angle_rho=angle)

    #load the mask
    try:
        hgrd.mask_rho = np.array(nc.variables['mask_rho'][:])
    except:
        hgrd.mask_rho = np.ones(hgrd.mask_rho.shape)

    return hgrd
Beispiel #3
0
def get_Stations_hgrid(gridid, sta_file):
    """
    hgrid = get_Stations_hgrid(gridid, sta_file)

    Load Stations horizontal grid object
    """

    gridinfo = ROMS_gridinfo(gridid)
    grdfile = gridinfo.grdfile

    nc = io.Dataset(sta_file)

    #Check for cartesian or geographical grid
    spherical = nc.variables['spherical'][:]

    #Get horizontal grid
    if ((spherical == 0) or (spherical == 'F')):
        #cartesian grid
        print('Load cartesian grid from file')
        if 'x_rho' in list(nc.variables.keys()) and 'y_rho' in list(
                nc.variables.keys()):
            x_rho = nc.variables['x_rho'][:]
            y_rho = nc.variables['y_rho'][:]
            try:
                angle = nc.variables['angle'][:]
            except:
                angle = np.zeros(x_rho.shape)
        else:
            raise ValueError('NetCDF file must contain x_rho and y_rho \
                     and possibly angle for a cartesian grid')

        x_rho = nc.variables['x_rho'][:]
        y_rho = nc.variables['y_rho'][:]

        if 'angle' in list(nc.variables.keys()):
            angle = nc.variables['angle'][:]
        else:
            angle = None

        #Get cartesian grid
        hgrd = Sta_CGrid(x_rho, y_rho, angle_rho=angle)

    else:
        #geographical grid
        print('Load geographical grid from file')
        proj = Basemap(projection='merc', resolution=None, lat_0=0, lon_0=0)
        if 'lon_rho' in list(nc.variables.keys()) and 'lat_rho' in list(
                nc.variables.keys()):
            lon_rho = nc.variables['lon_rho'][:]
            lat_rho = nc.variables['lat_rho'][:]
        else:
            raise ValueError('NetCDF file must contain lon_rho and lat_rho \
                  for a geographical grid')

        lon_rho = nc.variables['lon_rho'][:]
        lat_rho = nc.variables['lat_rho'][:]

        if 'angle' in list(nc.variables.keys()):
            angle = nc.variables['angle'][:]
        else:
            angle = None

        #Get geographical grid
        hgrd = Sta_CGrid_geo(lon_rho, lat_rho, proj, \
                         angle_rho=angle)

    return hgrd