Exemplo n.º 1
0
def make_icebin_in_base(gridA_fname, gridI_fname, overlap_fname, pism_spinup_fname, ofname):
    # gridA_name = 'modele_ll_g2x2_5'
    # gridI_name = 'searise_g%d' % ice_dx
    # DATA_PATH = os.environ['DATA_PATH']
    #   pism_spinup_fname = os.path.join(DATA_PATH, 'searise/Greenland_5km_v1.1.nc')

#    ice_dx=20       # 20km ice grid
    #ice_dx=5       # 5km ice grid

    # ========== Set up gridA and height points
#    gridA_fname = os.path.join(grid_dir, gridA_name + '.nc')
    hpdefs = np.array(range(0,21))*200.0 - 200.0
    print('BEGIN read {}'.format(gridA_fname))
    mm = icebin.GCMRegridder(gridA_fname, 'grid', hpdefs, True)
    print('END read {}'.format(gridA_fname))


    # ========= Add each Ice Sheet

    # --- Greenland
    print('PISM spinup file: {}'.format(pism_spinup_fname))
    (elevI, maskI) = giss.pism.read_elevI_maskI(pism_spinup_fname)
    print('len(elevI) = ', elevI.shape)

#    gridI_fname = os.path.join(grid_dir, '%s.nc' % gridI_name)
#    gridI_leaf = os.path.split(gridI_fname)[1]
#    overlap_fname = os.path.join(grid_dir, '%s-%s.nc' % (gridA_name, gridI_leaf))
    
    print('overlap_fname {}'.format(overlap_fname))

    print('maskI',maskI.shape)
    mm.add_sheet('greenland',
        gridI_fname, 'grid',
        overlap_fname, 'exgrid',
        'Z_INTERP')
#        elevI, maskI)

    # ========== Finish up and write out
    print('Writing: {}'.format(ofname))
    ncio = ibmisc.NcIO(ofname, 'replace')
    mm.ncio(ncio, 'm')
    ncio.close()
Exemplo n.º 2
0
def make_icebin_in_base(grid_dir, gridA_name, gridI_name, pism_spinup_fname,
                        ofname):
    # gridA_name = 'modele_ll_g2x2_5'
    # gridI_name = 'searise_g%d' % ice_dx
    # DATA_PATH = os.environ['DATA_PATH']
    #   pism_spinup_fname = os.path.join(DATA_PATH, 'searise/Greenland_5km_v1.1.nc')

    ice_dx = 20  # 20km ice grid

    #ice_dx=5       # 5km ice grid

    #   ofname = 'modele_ll_g2x2_5-searise_g%d-40-base.nc' % ice_dx

    # Regrids data from the 5km SeaRISE grid to the 20km SeaRISE grid
    # Just sub-sample, don't do anything fancy
    def regrid_5_to_20(ff):
        print(ff.shape[0] / 4 + 1, ff.shape[1] / 4 + 1)
        ret = np.zeros((ff.shape[0] / 4 + 1, ff.shape[1] / 4 + 1),
                       dtype=ff.dtype)
        for j in range(0, ret.shape[0]):
            for i in range(0, ret.shape[1]):
                ret[j, i] = ff[j * 4, i * 4]
        print('regrid: %s to %s' % (ff.shape, ret.shape))
        return ret

    # ========== Set up gridA and height points
    gridA_fname = os.path.join(grid_dir, gridA_name + '.nc')
    hpdefs = np.array(range(0, 40)) * 100.0 - 50.0
    mm = icebin.GCMRegridder(gridA_fname, 'grid', hpdefs, True)

    # ========= Add each Ice Sheet

    # --- Greenland
    print('PISM spinup file: {}'.format(pism_spinup_fname))
    (elevI, maskI) = giss.pism.read_elevI_maskI(pism_spinup_fname)

    gridI_fname = os.path.join(grid_dir, '%s.nc' % gridI_name)
    overlap_fname = os.path.join(grid_dir,
                                 '%s-%s.nc' % (gridA_name, gridI_name))

    print('maskI', maskI.shape)
    mm.add_sheet('greenland', gridI_fname, 'grid', overlap_fname, 'exgrid',
                 'Z_INTERP', elevI, maskI)

    # ========= Compute all regridding matrices for Python use
    matrices = dict()
    for sheet_name in ('greenland', ):
        rm = mm.regrid_matrices(sheet_name)
        for mat_base in ('EvI', 'AvI', 'IvA', 'IvE', 'EvA', 'AvE'):
            for variant in ('PARTIAL_CELL', ):
                key = (sheet_name, mat_base, variant)
                print('-------- Computing', key)
                sys.stdout.flush()
                matrix = rm.regrid(mat_base + '(' + variant + ')')
                matrices[key] = matrix

    with open('matrices.pik', 'wb') as fout:
        pickle.dump(matrices, fout)

    sys.exit(0)

    # ========== Finish up and write out
    print('Writing: {}'.format(ofname))
    ncio = ibmisc.NcIO(ofname, 'replace')
    mm.ncio(ncio, 'm')
    ncio.close()
Exemplo n.º 3
0
import ibmisc
import numpy as np

# Check basic properties of the TOPOA file.

topoa_nc = 'topoa.nc'

# Check sum of fhc in TOPOA file
with netCDF4.Dataset(topoa_nc, 'r') as nc:
    fhc = nc.variables['fhc'][:]

fhc1 = sum(fhc, 0)

# Check sum of fhc from AvE matrix (mismatched)
fhc_AvE_mm = np.zeros(fhc.shape[1:])
ncio = ibmisc.NcIO('global_ec_mm.nc', 'r')
lw = ibmisc.nc_read_weighted(ncio, 'AvE')
ncio.close()

AvE_mm = lw.to_coo()

for iA, iE, val in zip(AvE_mm.row, AvE_mm.col, AvE_mm.data):
    jj = iA // fhc1.shape[1]
    ii = iA - (jj * fhc1.shape[1])
    fhc_AvE_mm[jj, ii] += val

wAvE_mm = lw.get_weights(0)

# Check sum of fhc from AvE matrix (regular)
fhc_AvE = np.zeros(fhc.shape[1:])
ncio = ibmisc.NcIO('global_ec.nc', 'r')