def test_remap_runoff(self, input_dir, output_dir): """ Remapping runoff and check that it is conservative. It will be necessary to use a destination mask. We want to move all points from src into unmasked parts of the destination. """ mom_hgrid = os.path.join(input_dir, 'ocean_hgrid.nc') mom_mask = os.path.join(input_dir, 'ocean_mask.nc') filename = os.path.join(input_dir, 'runoff_from_iaf_16MAR2016_jragrid_1984.nc') with nc.Dataset(filename) as f: runoff = f.variables['runof'][0, :] src, dest, weights = remap_atm_to_ocean(input_dir, output_dir, mom_hgrid, mom_mask, src=runoff, core2_or_jra='JRA55') rel_err = calc_regridding_err(weights, src, dest) print('ESMF relative error {}'.format(rel_err)) assert rel_err < 1e-15
def test_remap_tenth_deg(self, input_dir, oasis_dir): """ Use OASIS for a tenth degree remapping. """ # Delete all netcdf files in oasis dir this will include the OASIS # configuration. for f in glob.glob(oasis_dir + '/*.nc'): try: os.remove(f) except FileNotFoundError as e: pass mom_hgrid = os.path.join(input_dir, 'ocean_01_hgrid.nc') mom_mask = os.path.join(input_dir, 'ocean_01_mask.nc') src, dest, weights, runtime = remap_to_mom(oasis_dir, input_dir, mom_hgrid, mom_mask) rel_err = calc_regridding_err(weights, src, dest) print('OASIS relative error {}'.format(rel_err)) print('OASIS time to make weights and remap one field {}'.format( runtime)) assert rel_err < 1e-9
def test_core2_to_mom_one_remapping(self, input_dir, output_dir): mom_hgrid = os.path.join(input_dir, 'grid_spec.nc') mom_mask = os.path.join(input_dir, 'grid_spec.nc') src, dest, weights, time = remap_atm_to_ocean(input_dir, output_dir, mom_hgrid, mom_mask) rel_err = calc_regridding_err(weights, src, dest) print('ESMF relative error {}'.format(rel_err)) print('ESMF time to make CORE2 to 1 degree weights {}'.format(time)) assert rel_err < 1e-15
def test_core_to_mom_quarter_remapping(self, input_dir, output_dir): """ Do a test remapping between core and MOM 0.25 grid. """ mom_hgrid = os.path.join(input_dir, 'ocean_hgrid.nc') mom_mask = os.path.join(input_dir, 'ocean_mask.nc') src, dest, weights, time = remap_atm_to_ocean(input_dir, output_dir, mom_hgrid, mom_mask) rel_err = calc_regridding_err(weights, src, dest) print('ESMF relative error {}'.format(rel_err)) print('ESMF time to make CORE2 to 0.25 degree weights {}'.format(time)) assert rel_err < 1e-13
def remap_to_output(input_file, output_file, weights_file, method='conserve', varname='runoff', verbose=False, clobber=False, check=False): """ Remapping and check that it is conservative. """ if verbose: print("Opening input file: {}".format(input_file)) with nc.Dataset(input_file) as f: var = f.variables[varname] ntime = create_output_ncfile(input, args.varname, weights_file, output_file, None) if ntime is None: ntime = 1 time_axis = False else: time_axis = True for idx in range(ntime): if time_axis: src = var[idx, :] else: src = var[:] if verbose: print("Remapping using weights for index: {}".format(idx)) dest = remap(src, weights_file) if check: rel_err = calc_regridding_err(weights_file, src, dest) print('ESMF relative error {}'.format(rel_err)) if time_axis: write_output(output_file, varname, dest, idx) else: write_output(output_file, varname, dest)
def test_jra55_to_mom_tenth_remapping(self, input_dir, output_dir): """ Do a test remapping between jra55 and MOM 0.1 grid. """ mom_hgrid = os.path.join(input_dir, 'ocean_01_hgrid.nc') mom_mask = os.path.join(input_dir, 'ocean_01_mask.nc') src, dest, weights, time = remap_atm_to_ocean(input_dir, output_dir, mom_hgrid, mom_mask, core2_or_jra='JRA55') rel_err = calc_regridding_err(weights, src, dest) print('ESMF relative error {}'.format(rel_err)) print('ESMF time to make 0.1 degree weights and remap {}'.format(time)) assert rel_err < 1e-13
def test_remap_runoff(self, input_dir, output_dir): """ Remapping runoff from JRA55 1440x720 to 1deg, 0.25 and 0.1 deg ocean grids. It will be necessary to use a destination mask. We want to move all points from src into unmasked parts of the destination. This is very slow and not suitable for regridding a timeseries. """ filename = os.path.join(input_dir, 'RYF.runoff_all.1984_1985.nc') with nc.Dataset(filename) as f: runoff = f.variables['friver'][0, :] ocn_grids = [('025deg_river', 'ocean_hgrid.nc', 'ocean_mask.nc'), ('01deg_river', 'ocean_01_hgrid.nc', 'ocean_01_mask.nc'), ('1deg_river', 'grid_spec.nc', 'grid_spec.nc')] for name, hgrid, mask in ocn_grids: mom_hgrid = os.path.join(input_dir, hgrid) mom_mask = os.path.join(input_dir, mask) src, dest, weights = remap_atm_to_ocean(input_dir, output_dir, mom_hgrid, mom_mask, src=runoff, core2_or_jra='JRA55_river') rel_err = calc_regridding_err(weights, src, dest) print('ESMF {} relative error {}'.format(name, rel_err)) assert rel_err < 1e-15 # Write out results. with nc.Dataset(os.path.join(output_dir, name + '.nc'), 'w') as fd: fd.createDimension('ny', dest.shape[0]) fd.createDimension('nx', dest.shape[1]) vd = fd.createVariable('friver', 'f8', ('ny', 'nx')) vd[:] = dest[:]
def remap_to_mom(input_file, mom_hgrid, mom_mask, output_file, method='conserve', varname='runoff', verbose=False, clobber=False): """ Remapping JRA and check that it is conservative. It will be necessary to use a destination mask. We want to move all points from src into unmasked parts of the destination. """ if verbose: print("Opening input file: {}".format(input_file)) oasis_grids_dir = os.environ["OASIS_GRIDS_DIR"] cmd = [os.path.join(oasis_grids_dir, 'remapweights.py')] # input_grid = Jra55Grid(input_file) output_dir = os.path.dirname(output_file) if verbose: print("Output directory: {}".format(output_dir)) weights = os.path.join(output_dir, 'JRA_MOM_conserve.nc') if not os.path.exists(weights): args = [ 'JRA55', 'MOM', '--src_grid', input_file, '--dest_grid', mom_hgrid, '--dest_mask', mom_mask, '--method', method, '--output', weights ] if verbose: print("Generating weights file : {} using\n{}".format( weights, " ".join(cmd + args))) ret = sp.call(cmd + args) assert ret == 0 assert os.path.exists(weights) # Only use these to pull out the dimensions of the grids. mom = MomGrid.fromfile(mom_hgrid, mask_file=mom_mask) with nc.Dataset(input_file) as f: var = f.variables[varname] if "time" in f.variables: time = f.variables["time"] time_units = time.units else: time = numpy.arange(0, var.shape[0]) time_units = "days since 1900-01-01" create_mom_output(mom, output_file, time_units) for idx in range(var.shape[0]): src = var[idx, :] if verbose: print("Remapping using weights for index: {}".format(idx)) dest = remap(src, weights, (mom.num_lat_points, mom.num_lon_points)) rel_err = calc_regridding_err(weights, src, dest) print('ESMF relative error {}'.format(rel_err)) write_mom_output(output_file, varname, var.long_name, var.units, dest, idx, time[idx])