Beispiel #1
0
def main(args):
    #FIXME: Pick projection based on input file!
    proj_epsg3413, proj_eigen_gl04c = projections.greenland()
    if args.projection == 'epsg':
        proj = proj_epsg3413
        scrip_title = "CISM EPSG:3413 Grid"
    elif args.projection == 'bamber':
        proj = proj_eigen_gl04c
        scrip_title = "CISM Bamber Grid"

    # load the dataset
    nc_base = Dataset(args.input, 'r')
    base = projections.DataGrid()
    base.y = nc_base.variables[args.dims[0]]
    base.x = nc_base.variables[args.dims[1]]
    base.dy = base.y[1]-base.y[0]
    base.dx = base.x[1]-base.x[0]
    base.ny = base.y[:].shape[0]
    base.nx = base.x[:].shape[0]
    base.N = base.ny*base.nx
    base.make_grid()

    lon_grid, lat_grid = proj(base.x_grid.ravel(), base.y_grid.ravel(), inverse=True)
    lon_grid.shape = base.x_grid.shape 
    lat_grid.shape = base.x_grid.shape
    base.lon_grid = lon_grid
    base.lat_grid  = lat_grid

    base.ll_y = base.y_grid.flatten(order='C') - base.dy/2.  
    base.ll_x = base.x_grid.flatten(order='C') - base.dx/2. 
    base.lr_y = base.y_grid.flatten(order='C') - base.dy/2.  
    base.lr_x = base.x_grid.flatten(order='C') + base.dx/2. 
    base.ur_y = base.y_grid.flatten(order='C') + base.dy/2.  
    base.ur_x = base.x_grid.flatten(order='C') + base.dx/2. 
    base.ul_y = base.y_grid.flatten(order='C') + base.dy/2.  
    base.ul_x = base.x_grid.flatten(order='C') - base.dx/2. 

    base.ll_lon, base.ll_lat = proj(base.ll_x, base.ll_y, inverse=True)
    base.lr_lon, base.lr_lat = proj(base.lr_x, base.lr_y, inverse=True)
    base.ur_lon, base.ur_lat = proj(base.ur_x, base.ur_y, inverse=True)
    base.ul_lon, base.ul_lat = proj(base.ul_x, base.ul_y, inverse=True)

    base.corner_lat = numpy.column_stack((base.ll_lat, base.lr_lat, base.ur_lat, base.ul_lat))
    base.corner_lon = numpy.column_stack((base.ll_lon, base.lr_lon, base.ur_lon, base.ul_lon))

    min_lat = numpy.amin(base.corner_lat)
    max_lat = numpy.amax(base.corner_lat)

    min_lon = numpy.amin(base.corner_lon)
    max_lon = numpy.amax(base.corner_lon)

    proj_aea = projections.equal_area(min_lat, max_lat, (max_lon+min_lon)/2.)

    # get the area for each grid cell
    sys.stdout.write("   [%-60s] %d%%" % ('='*0, 0.))
    sys.stdout.flush()
    base.area = numpy.zeros(base.N)
    for ii in range(base.N):
        ctr = (ii*60)/base.N
        if not (ii % 100): 
            sys.stdout.write("\r   [%-60s] %d%%" % ('='*ctr, ctr/60.*100.))
            sys.stdout.flush()
        
        lat = base.corner_lat[ii,:]
        lon = base.corner_lon[ii,:]
        x, y = proj_aea(lon, lat)
       
        points = {'type':'polygon', 'coordinates':[zip(x,y)]}
        base.area[ii] = shape(points).area #m^2

    sys.stdout.write("\r   [%-60s] %d%%\n" % ('='*60, 100.))

    path_scrip, name_scrip = os.path.split(args.input)
    lc_scrip = os.path.join(path_scrip, 'SCRIPgrid_'+name_scrip)

    nc_scrip = Dataset(lc_scrip, 'w', format='NETCDF4') 
    nc_scrip.createDimension('grid_size', base.N)
    nc_scrip.createDimension('grid_corners', 4)
    nc_scrip.createDimension('grid_rank', 2)
    nc_scrip.title = scrip_title 
    nc_scrip.source = 'Joseph H. Kennedy, ORNL'

    scrip = projections.DataGrid()
    scrip.dims = nc_scrip.createVariable('grid_dims','i4', ('grid_rank'))
    #NOTE: SCRIP is a Fortran program and as such assumes data is stored in column-major order. 
    #      Because (1) netCDF stores data 'C-style' with row-major order and (2) the scrip grid 
    #      formate uses flattened arrays, it's easiest to flatten the data 'C-syle', which is the 
    #      default for numpy, and flip the dimensions. SCRIP will then read in the array structure
    #      correctly, and the expected ordering within the netCDF file we are creating will be 
    #      maintained.
    #scrip.dims[:] = numpy.array(base.dims)[::-1]
    #NOTE: Now dumping everything 'F-style' (column-major) order. 
    #scrip.dims[:] = numpy.array(base.dims)[::]
    #NOTE:  I don't freaking know man; just do it normal like. 
    scrip.dims[:] = numpy.array(base.dims)[:]

    scrip.imask = nc_scrip.createVariable('grid_imask','i4', ('grid_size'))
    scrip.imask[:] = numpy.ones(base.N) 
    scrip.imask.units = 'unitless'

    scrip.center_lat = nc_scrip.createVariable('grid_center_lat','f4', ('grid_size'))
    scrip.center_lat[:] = base.lat_grid[:,:].flatten(order='C')
    scrip.center_lat.setncattr('units', 'degrees')

    scrip.center_lon = nc_scrip.createVariable('grid_center_lon','f4', ('grid_size'))
    scrip.center_lon[:] = base.lon_grid[:,:].flatten(order='C')
    scrip.center_lon.setncattr('units', 'degrees')

    scrip.corner_lat = nc_scrip.createVariable('grid_corner_lat','f4', ('grid_size','grid_corners',))
    scrip.corner_lat[:,:] = base.corner_lat[:,:]
    scrip.corner_lat.units = 'degrees'

    scrip.corner_lon = nc_scrip.createVariable('grid_corner_lon','f4', ('grid_size','grid_corners',))
    scrip.corner_lon[:,:] = base.corner_lon[:,:]
    scrip.corner_lon.units = 'degrees'

    scrip.area = nc_scrip.createVariable('grid_area','f4', ('grid_size',))
    scrip.area[:] = (base.area[:]/SURFACE_AREA_EARTH)*SQR_DEG_ON_SPHERE
    scrip.area.units = 'square degrees'

    nc_base.close()
    nc_scrip.close()
    os.chmod(lc_scrip, 0o644)   # uses an octal number!
Beispiel #2
0
#=======================
speak.verbose(args, "\nBuilding the base dataset: " + f_base)

speak.notquiet(args, "\nCreating the base grid."),

nc_base, base = bamberdem.build_base(f_base, nc_bamber)

speak.notquiet(args, "   Done!")

#==== Projections ====
# All the projections
# needed for the data
#=====================
speak.notquiet(args, "\nGetting the projections.")

proj_epsg3413, proj_eigen_gl04c = projections.greenland()

speak.notquiet(args, "   Done!")

# transform meshes.
speak.verbose(
    args, "   Creating the transform meshes: base Bamber grid to EPSG-3413.")

trans = projections.transform(base, proj_eigen_gl04c, proj_epsg3413)

speak.notquiet(args, "   Done!")

#==== SeaRise Data =====
# this is a 1km dataset
#=======================
speak.notquiet(args, "\nGetting bheatflx and presartm from the SeaRise data.")
Beispiel #3
0
base.y0 = nc_base.variables['y0']
base.ny = base.y[:].shape[0]
base.ny0 = base.y0[:].shape[0]

base.x = nc_base.variables['x1']
base.x0 = nc_base.variables['x0']
base.nx = base.x[:].shape[0]
base.nx0 = base.x0[:].shape[0]

base.make_grid()
base.make_stg_grid()

base.get_grid_corners()
base.get_stg_grid_corners()

bamber_proj, latlon_proj = projections.greenland(lc_bamber)

class latlon():
    pass

latlon.x_grid, latlon.y_grid = pyproj.transform(bamber_proj, latlon_proj, base.x_grid.flatten(), base.y_grid.flatten())
latlon.x0_grid, latlon.y0_grid = pyproj.transform(bamber_proj, latlon_proj, base.x0_grid.flatten(), base.y0_grid.flatten())

latlon.x_corners, latlon.y_corners = pyproj.transform(bamber_proj, latlon_proj, base.x_corners.flatten(), base.y_corners.flatten())
latlon.y_corners = latlon.y_corners.reshape((base.ny*base.nx, 4))
latlon.x_corners = latlon.x_corners.reshape((base.ny*base.nx, 4))

latlon.x0_corners, latlon.y0_corners = pyproj.transform(bamber_proj, latlon_proj, base.x0_corners.flatten(), base.y0_corners.flatten())
latlon.y0_corners = latlon.y0_corners.reshape((base.ny0*base.nx0, 4))
latlon.x0_corners = latlon.x0_corners.reshape((base.ny0*base.nx0, 4))