def bheatflx_artm_bamber(args, nc_seaRise, nc_base, base): """Get bheatflx and artm from the sea rise data. This function pulls in the `bheatflx` and `presartm` variables from the Sea Rise dataset and writes them to the base dataset as `bheatflx` and `artm`. NetCDF attributes are preserved. Parameters ---------- args : Namespace() object holding parsed command line arguments. nc_seaRise : An opened netCDF Dataset containing the Sea Rise data. nc_base : The created netCDF Dataset that will contain the base data. base : A DataGrid() class instance that holds the base data grid information. """ seaRise_y = nc_seaRise.variables['y'] seaRise_ny = seaRise_y[:].shape[0] seaRise_x = nc_seaRise.variables['x'] seaRise_nx = seaRise_x[:].shape[0] # to convert to Bamber 1km grid seaRise_data = np.ndarray((base.ny, base.nx)) seaRise_y_equal_base = 100 seaRise_x_equal_base = 500 # get basal heat flux #-------------------- seaRise_data[:, :] = 0. seaRise_bheatflx = nc_seaRise.variables['bheatflx'] seaRise_data[seaRise_y_equal_base:seaRise_y_equal_base + seaRise_ny, seaRise_x_equal_base:seaRise_x_equal_base + seaRise_nx] = -seaRise_bheatflx[0, :, :] # invert sign! speak.verbose(args, " Writing bheatflx to base.") base_bheatflx = nc_base.createVariable('bheatflx', 'f4', ( 'y', 'x', )) base_bheatflx[:, :] = seaRise_data[:, :] copy_atts(seaRise_bheatflx, base_bheatflx) # get annual mean air temperature (2m) #------------------------------------- seaRise_data[:, :] = 0. seaRise_presartm = nc_seaRise.variables['presartm'] seaRise_data[seaRise_y_equal_base:seaRise_y_equal_base + seaRise_ny, seaRise_x_equal_base:seaRise_x_equal_base + seaRise_nx] = seaRise_presartm[0, :, :] speak.verbose(args, " Writing artm to base.") base_artm = nc_base.createVariable('artm', 'f4', ( 'y', 'x', )) base_artm[:, :] = seaRise_data[:, :] copy_atts(seaRise_presartm, base_artm)
def velocity_bamber(args, nc_insar, nc_base, trans): """Get the velocities from the insar data. This function pulls in the `vx`, `vy`, `ex` and `ey` variables from the InSAR dataset and writes them to the base dataset. NetCDF attributes are preserved. Parameters ---------- args : Namespace() object holding parsed command line arguments. nc_insar : An opened netCDF Dataset containing the InSAR data. nc_base : The created netCDF Dataset that will contain the base data. trans : A DataGrid() class instance that holds the base data grid transformed to the EPSG:3413 projection. """ insar_y = nc_insar.variables['y'] insar_ny = insar_y[:].shape[0] insar_x = nc_insar.variables['x'] insar_nx = insar_x[:].shape[0] base_data = np.ndarray((trans.ny, trans.nx)) for vv in ['vy', 'vx', 'ey', 'ex']: insar_data[:, :] = 0. base_data[:, :] = 0. insar_var = nc_insar.variables[vv] insar_data = np.ma.masked_values( nc_bamber.variables[var_list[1]][:, :], -2.e9) data_min = insar_data.min() data_max = insar_data.max() speak.verbose(args, " Interpolating " + vv + ".") insar_to_base = scipy.interpolate.RectBivariateSpline( insar_y[:], insar_x[:], insar_data, kx=1, ky=1, s=0) # regular 2d linear interp. but faster for ii in range(0, trans.nx): base_data[:, ii] = insar_to_base.ev(trans.y_grid[:, ii], trans.x_grid[:, ii]) base_data[base_data < data_min] = -2.e9 base_data[base_data > data_max] = -2.e9 speak.verbose(args, " Writing " + vv + " to base.") base_var = nc_base.createVariable(vv, 'f4', ( 'y', 'x', )) base_var[:, :] = base_data[:, :] copy_atts(insar_var, base_var)
def build_base(f_base, nc_bamber): """Build the Bamber base grid. This function opens the Bamber DEM dataset, pulls in the `projection_y_coordinate` and `projection_x_coordinate` variables, creates a new base dataset, creates a DataGrid() class instance to hold the base grid, and sets up the base coordinate dimension and variables `y` and `x` in the new base dataset. NetCDF attributes are preserved. Parameters ---------- f_base : Filename for the created netCDF Dataset that will contain the base data. nc_bamber : An opened netCDF Dataset containing the Bamber dataset. """ nc_base = Dataset(f_base, 'w', format='NETCDF4') bamber_y = nc_bamber.variables['projection_y_coordinate'] bamber_ny = bamber_y[:].shape[0] # number of y points for 1km grid bamber_x = nc_bamber.variables['projection_x_coordinate'] bamber_nx = bamber_x[:].shape[0] # number of x points for 1km grid # make bamber 1km grid for base base = DataGrid() base.ny = bamber_ny base.nx = bamber_nx # create our base dimensions nc_base.createDimension('y', base.ny) nc_base.createDimension('x', base.nx) # create some base variables base.y = nc_base.createVariable('y', 'f4', 'y') base.y[:] = bamber_y[:] copy_atts(bamber_y, base.y) #FIXME: units say km, but it's actuall in m base.x = nc_base.createVariable('x', 'f4', 'x') base.x[:] = bamber_x[:] copy_atts(bamber_x, base.x) #FIXME: units say km, but it's actuall in m # create some grids for interpolation base.make_grid() return (nc_base, base)
def velocity_epsg3413(args, nc_insar, nc_base, base): insar = projections.DataGrid() insar.y = nc_insar.variables['y'] insar.x = nc_insar.variables['x'] insar.ny = insar.y[:].shape[0] insar.nx = insar.x[:].shape[0] insar.make_grid() for var in ['vy', 'vx', 'ey', 'ex']: speak.verbose(args, ' Interpolating ' + var + ' and writing to base.') sys.stdout.write(" [%-60s] %d%%" % ('=' * 0, 0.)) sys.stdout.flush() insar_data = np.ma.masked_values(nc_insar.variables[var][:, :], -2.e9) data_min = insar_data.min() data_max = insar_data.max() insar_to_base = scipy.interpolate.RectBivariateSpline( insar.y[:], insar.x[:], insar_data, kx=1, ky=1, s=0) # regular 2d linear interp. but faster base_data = np.zeros(base.dims) for ii in range(0, base.nx): ctr = (ii * 60) / base.nx sys.stdout.write("\r [%-60s] %d%%" % ('=' * ctr, ctr / 60. * 100.)) sys.stdout.flush() base_data[:, ii] = insar_to_base.ev(base.y_grid[:, ii], base.x_grid[:, ii]) sys.stdout.write("\r [%-60s] %d%%\n" % ('=' * 60, 100.)) sys.stdout.flush() base_data[base_data < data_min] = -2.e9 base_data[base_data > data_max] = -2.e9 base.var = nc_base.createVariable(var, 'f4', ( 'y', 'x', )) base.var[:] = base_data[:] copy_atts(nc_insar.variables[var], base.var) base.var.grid_mapping = 'epsg_3413' base.var.coordinates = 'lon lat'
def acab_bamber(args, nc_racmo2p0, nc_base, base): """Get acab from the RACMO 2.0 data. This function pulls in the `smb` variable from the RACMO 2.0 dataset and writes it to the base dataset as `acab`. NetCDF attributes are preserved. Parameters ---------- args : Namespace() object holding parsed command line arguments. nc_racmo2p0 : An opened netCDF Dataset containing the RACMO 2.0 data. nc_base : The created netCDF Dataset that will contain the base data. base : A DataGrid() class instance that holds the base data grid information. """ racmo2p0_data = np.ndarray((base.ny, base.nx)) racmo2p0_data[:, :] = 0. racmo2p0_smb = nc_racmo2p0.variables['smb'] racmo2p0_data[:, :] = racmo2p0_smb[:, ::-1].transpose() / 910. racmo2p0_data = np.ma.masked_invalid( racmo2p0_data) # find invalid data and create a mask racmo2p0_data = racmo2p0_data.filled(0.) # fill invalid data with zeros speak.verbose(args, " Writing acab to base.") base_acab = nc_base.createVariable('acab', 'f4', ( 'y', 'x', )) base_acab[:, :] = racmo2p0_data[:, :] copy_atts( racmo2p0_smb, base_acab) #FIXME: check atribute units -- divided by 910 earlier
def add_time(args, f_base, f_1km, f_template): """Add the time dimension to a Bamber 1km DEM dataset and write config files. This function opens the base dataset, creates a new 1km dataset with the time dimension that has been shrunken to just around the ice sheet, and creates a CISM config file. NetCDF attributes are preserved. Parameters ---------- args : Namespace() object holding parsed command line arguments. f_base : Filename for the created netCDF Dataset that contains the base data. f_1km : Filename for the created netCDF Dataset that will contain the 1 km dataset. f_template : Filename for the template used to create the CISM config file. """ # shrink dataset to the ice sheet nc_base = Dataset(f_base, 'r') y_shrink = [100, 2900 + 1] #NOTE: python stop exclusive, nco stop inclusive! x_shrink = [500, 2000 + 1] #NOTE: python stop exclusive, nco stop inclusive! base = DataGrid() base.y = nc_base.variables['y'] base.x = nc_base.variables['x'] base.ny = base.y[y_shrink[0]:y_shrink[1]].shape[0] base.nx = base.x[x_shrink[0]:x_shrink[1]].shape[0] speak.verbose(args, " Writing " + f_1km) nc_1km = Dataset(f_1km, 'w', format='NETCDF4') nc_1km.createDimension('time', None) nc_1km.createDimension('y1', base.ny) nc_1km.createDimension('x1', base.nx) time = nc_1km.createVariable('time', 'f4', ('time', )) y1 = nc_1km.createVariable('y1', 'f4', ('y1', )) x1 = nc_1km.createVariable('x1', 'f4', ('x1', )) copy_atts(base.y, y1) copy_atts(base.x, x1) y1[:] = base.y[y_shrink[0]:y_shrink[1]] x1[:] = base.x[x_shrink[0]:x_shrink[1]] time[0] = 0. for var_name, var_data in nc_base.variables.iteritems(): if var_name not in ['x', 'y', 'lat', 'lon']: var_1km = nc_1km.createVariable(var_name, 'f4', ( 'time', 'y1', 'x1', )) var_1km[0, :, :] = var_data[y_shrink[0]:y_shrink[1], x_shrink[0]:x_shrink[1]] copy_atts(var_data, var_1km) elif var_name not in ['x', 'y']: var_1km = nc_1km.createVariable(var_name, 'f4', ( 'y1', 'x1', )) copy_atts(var_data, var_1km) var_1km[:, :] = var_data[y_shrink[0]:y_shrink[1], x_shrink[0]:x_shrink[1]] nc_base.close() nc_1km.close() os.chmod(f_1km, 0o644) # uses an octal number! speak.verbose(args, " Writing the 1km config file.") config.write(f_1km, f_template, base, 1)
def coarsen(args, f_base, f_template, coarse_list): """Coarsen an base dataset. This function opens the 1 km dataset and creates coarser resolution datasets. All NetCDF data attributes are preserved. Parameters ---------- args : Namespace() object holding parsed command line arguments. f_base : Filename for the base dataset to coarsen. f_template : Filename for the template used to create the CISM config files. coarse_list : List of resolutions to coarsen to. """ nc_base = Dataset(f_base, 'r') base = DataGrid() base.y = nc_base.variables['y1'] base.ny = base.y[:].shape[0] base.x = nc_base.variables['x1'] base.nx = base.x[:].shape[0] base.proj = nc_base.variables['epsg_3413'] coarse_names = [] for skip in coarse_list: coarse = DataGrid() coarse.ny = base.y[::skip].shape[0] coarse.nx = base.x[::skip].shape[0] idx = f_base.find('km') f_coarse = f_base[:idx - 1] + str(skip) + f_base[idx:] speak.verbose(args, " Writing " + f_coarse) nc_coarse = Dataset(f_coarse, 'w', format='NETCDF4') nc_coarse.createDimension('time', None) nc_coarse.createDimension('y1', coarse.ny) nc_coarse.createDimension('x1', coarse.nx) coarse.time = nc_coarse.createVariable('time', 'f4', ('time', )) coarse.y = nc_coarse.createVariable('y1', 'f4', ('y1', )) coarse.x = nc_coarse.createVariable('x1', 'f4', ('x1', )) copy_atts(base.y, coarse.y) copy_atts(base.x, coarse.x) coarse.y[:] = base.y[::skip] coarse.x[:] = base.x[::skip] coarse.time[0] = 0. coarse.proj = nc_coarse.createVariable('epsg_3413', 'b') copy_atts(base.proj, coarse.proj) for var_name, var_data in nc_base.variables.iteritems(): if var_name not in ['time', 'x1', 'y1', 'lat', 'lon', 'epsg_3413']: var_coarse = nc_coarse.createVariable(var_name, 'f4', ( 'time', 'y1', 'x1', )) var_coarse[0, :, :] = var_data[0, ::skip, ::skip] copy_atts(var_data, var_coarse) elif var_name not in ['time', 'x1', 'y1', 'epsg_3413']: var_coarse = nc_coarse.createVariable(var_name, 'f4', ( 'y1', 'x1', )) var_coarse[:, :] = var_data[::skip, ::skip] copy_atts(var_data, var_coarse) nc_coarse.close() # set file permissions os.chmod(f_coarse, 0o644) # uses an Octal number! # write config files speak.verbose(args, " Writing the " + str(skip) + " km config file.") config.write(f_coarse, f_template, coarse, skip)
nc_coarse.createDimension('lithoz', base_lithoz) nc_coarse.createDimension('staglevel', base_staglevel) nc_coarse.createDimension('stagwbndlevel', base_stagwbndlevel) coarse.time = nc_coarse.createVariable('time', 'd', ('time',)) coarse.y = nc_coarse.createVariable('y1', 'd', ('y1',) ) coarse.x = nc_coarse.createVariable('x1', 'd', ('x1',) ) coarse.y0 = nc_coarse.createVariable('y0', 'd', ('y0',) ) coarse.x0 = nc_coarse.createVariable('x0', 'd', ('x0',) ) coarse.level = nc_coarse.createVariable('level', 'd', ('level',) ) coarse.lithoz = nc_coarse.createVariable('lithoz', 'd', ('lithoz',) ) coarse.staglevel = nc_coarse.createVariable('staglevel', 'd', ('staglevel',) ) coarse.stagwbndlevel = nc_coarse.createVariable('stagwbndlevel', 'd', ('stagwbndlevel',) ) ncfunc.copy_atts(base.y, coarse.y) ncfunc.copy_atts(base.x, coarse.x) ncfunc.copy_atts(base.y0, coarse.y0) ncfunc.copy_atts(base.x0, coarse.x0) ncfunc.copy_atts(base.level, coarse.level) ncfunc.copy_atts(base.lithoz, coarse.lithoz) ncfunc.copy_atts(base.staglevel, coarse.staglevel) ncfunc.copy_atts(base.stagwbndlevel, coarse.stagwbndlevel) coarse.time[0] = 0. coarse.y[:] = base.y[::skip] coarse.x[:] = base.x[::skip] coarse.y0[:] = base.y0[::skip] coarse.x0[:] = base.x0[:1-skip:skip] coarse.level[:] = base.level[:] coarse.lithoz[:] = base.lithoz[:]
def add_time(args, f_base, f_1km, f_template, f_epsg_shrunk): """Add the time dimension to a EPSG:3413 1km dataset and write config files. This function opens the base dataset, creates a new 1km dataset with the time dimension that has been shrunken to just around the ice sheet, and creates a CISM config file. NetCDF attributes are preserved. Parameters ---------- args : Namespace() object holding parsed command line arguments. f_base : Filename for the created netCDF Dataset that contains the base data. f_1km : Filename for the created netCDF Dataset that will contain the 1 km dataset. f_template : Filename for the template used to create the CISM config file. """ # shrink dataset to the ice sheet nc_base = Dataset(f_base, 'r') base = DataGrid() base.y = nc_base.variables['y'] base.x = nc_base.variables['x'] with open(f_epsg_shrunk, 'r') as f: epsg = json.load(f) idx_left = (np.abs(base.x[:] - epsg['ll'][0])).argmin() idx_right = (np.abs(base.x[:] - epsg['ur'][0])).argmin() idx_lower = (np.abs(base.y[:] - epsg['ll'][1])).argmin() idx_upper = (np.abs(base.y[:] - epsg['ur'][1])).argmin() y_shrink = [idx_lower, idx_upper + 1 ] #NOTE: python stop exclusive, nco stop inclusive! x_shrink = [idx_left, idx_right + 1 ] #NOTE: python stop exclusive, nco stop inclusive! base.ny = base.y[y_shrink[0]:y_shrink[1]].shape[0] base.nx = base.x[x_shrink[0]:x_shrink[1]].shape[0] speak.verbose(args, " Writing " + f_1km) nc_1km = Dataset(f_1km, 'w', format='NETCDF4') nc_1km.createDimension('time', None) nc_1km.createDimension('y1', base.ny) nc_1km.createDimension('x1', base.nx) time = nc_1km.createVariable('time', 'f4', ('time', )) time.long_name = 'time' time.units = 'common_years since 2009-01-01 00:00:00' time.calendar = '365_day' time.comment = "The initial time here is an estimate of the nominal date for Joughin's 2015 "+ \ "InSAR data. Because this is a synthesis of datasets across many time periods, "+ \ "the inital date is inherently fuzzy and should be changed to suit your purposes." y1 = nc_1km.createVariable('y1', 'f4', ('y1', )) x1 = nc_1km.createVariable('x1', 'f4', ('x1', )) copy_atts(base.y, y1) copy_atts(base.x, x1) y1[:] = base.y[y_shrink[0]:y_shrink[1]] x1[:] = base.x[x_shrink[0]:x_shrink[1]] time[0] = 0. base.proj = nc_base.variables['epsg_3413'] proj = nc_1km.createVariable('epsg_3413', 'b') copy_atts(base.proj, proj) for var_name, var_data in nc_base.variables.iteritems(): if var_name not in ['x', 'y', 'lat', 'lon', 'epsg_3413']: var_1km = nc_1km.createVariable(var_name, 'f4', ( 'time', 'y1', 'x1', )) copy_atts(var_data, var_1km) var_1km[0, :, :] = var_data[y_shrink[0]:y_shrink[1], x_shrink[0]:x_shrink[1]] elif var_name not in ['x', 'y', 'epsg_3413']: var_1km = nc_1km.createVariable(var_name, 'f4', ( 'y1', 'x1', )) copy_atts(var_data, var_1km) var_1km[:, :] = var_data[y_shrink[0]:y_shrink[1], x_shrink[0]:x_shrink[1]] nc_base.close() nc_1km.close() os.chmod(f_1km, 0o644) # uses an octal number! speak.verbose(args, " Writing the 1km config file.") config.write(f_1km, f_template, base, 1)