def make_obcs (location, grid_path, input_path, output_dir, source='SOSE', use_seaice=True, nc_out=None, prec=32, split=180): from grid import SOSEGrid from file_io import NCfile, read_netcdf from interpolation import interp_bdry if source == 'SOSE': input_path = real_dir(input_path) output_dir = real_dir(output_dir) # Fields to interpolate # Important: SIarea has to be before SIuice and SIvice so it can be used for masking fields = ['THETA', 'SALT', 'UVEL', 'VVEL', 'SIarea', 'SIheff', 'SIuice', 'SIvice', 'ETAN'] # Flag for 2D or 3D dim = [3, 3, 3, 3, 2, 2, 2, 2, 2] # Flag for grid type gtype = ['t', 't', 'u', 'v', 't', 't', 'u', 'v', 't'] if source == 'MIT': # Also consider snow thickness fields += ['SIhsnow'] dim += [2] gtype += ['t'] # End of filenames for input infile_tail = '_climatology.data' # End of filenames for output outfile_tail = '_'+source+'.OBCS_'+location print 'Building MITgcm grid' if source == 'SOSE': model_grid = grid_check_split(grid_path, split) elif source == 'MIT': model_grid = Grid(grid_path) # Figure out what the latitude or longitude is on the boundary, both on the centres and outside edges of those cells if location == 'S': lat0 = model_grid.lat_1d[0] lat0_e = model_grid.lat_corners_1d[0] print 'Southern boundary at ' + str(lat0) + ' (cell centre), ' + str(lat0_e) + ' (cell edge)' elif location == 'N': lat0 = model_grid.lat_1d[-1] lat0_e = 2*model_grid.lat_corners_1d[-1] - model_grid.lat_corners_1d[-2] print 'Northern boundary at ' + str(lat0) + ' (cell centre), ' + str(lat0_e) + ' (cell edge)' elif location == 'W': lon0 = model_grid.lon_1d[0] lon0_e = model_grid.lon_corners_1d[0] print 'Western boundary at ' + str(lon0) + ' (cell centre), ' + str(lon0_e) + ' (cell edge)' elif location == 'E': lon0 = model_grid.lon_1d[-1] lon0_e = 2*model_grid.lon_corners_1d[-1] - model_grid.lon_corners_1d[-2] print 'Eastern boundary at ' + str(lon0) + ' (cell centre), ' + str(lon0_e) + ' (cell edge)' else: print 'Error (make_obcs): invalid location ' + str(location) sys.exit() if source == 'SOSE': print 'Building SOSE grid' source_grid = SOSEGrid(input_path+'grid/', model_grid=model_grid, split=split) elif source == 'MIT': print 'Building grid from source model' source_grid = Grid(input_path) else: print 'Error (make_obcs): invalid source ' + source sys.exit() # Calculate interpolation indices and coefficients to the boundary latitude or longitude if location in ['N', 'S']: # Cell centre j1, j2, c1, c2 = interp_slice_helper(source_grid.lat_1d, lat0) # Cell edge j1_e, j2_e, c1_e, c2_e = interp_slice_helper(source_grid.lat_corners_1d, lat0_e) else: # Pass lon=True to consider the possibility of boundary near 0E i1, i2, c1, c2 = interp_slice_helper(source_grid.lon_1d, lon0, lon=True) i1_e, i2_e, c1_e, c2_e = interp_slice_helper(source_grid.lon_corners_1d, lon0_e, lon=True) # Set up a NetCDF file so the user can check the results if nc_out is not None: ncfile = NCfile(nc_out, model_grid, 'xyzt') ncfile.add_time(np.arange(12)+1, units='months') # Process fields for n in range(len(fields)): if fields[n].startswith('SI') and not use_seaice: continue print 'Processing ' + fields[n] if source == 'SOSE': in_file = input_path + fields[n] + infile_tail out_file = output_dir + fields[n] + outfile_tail # Read the monthly climatology at all points if source == 'SOSE': if dim[n] == 3: source_data = source_grid.read_field(in_file, 'xyzt') else: source_data = source_grid.read_field(in_file, 'xyt') else: source_data = read_netcdf(input_path, fields[n]) if fields[n] == 'SIarea' and source == 'SOSE': # We'll need this field later for SIuice and SIvice, as SOSE didn't mask those variables properly print 'Interpolating sea ice area to u and v grids for masking of sea ice velocity' source_aice_u = interp_grid(source_data, source_grid, 't', 'u', time_dependent=True, mask_with_zeros=True, periodic=True) source_aice_v = interp_grid(source_data, source_grid, 't', 'v', time_dependent=True, mask_with_zeros=True, periodic=True) # Set sea ice velocity to zero wherever sea ice area is zero if fields[n] in ['SIuice', 'SIvice'] and source == 'SOSE': print 'Masking sea ice velocity with sea ice area' if fields[n] == 'SIuice': index = source_aice_u==0 else: index = source_aice_v==0 source_data[index] = 0 # Choose the correct grid for lat, lon, hfac source_lon, source_lat = source_grid.get_lon_lat(gtype=gtype[n], dim=1) source_hfac = source_grid.get_hfac(gtype=gtype[n]) model_lon, model_lat = model_grid.get_lon_lat(gtype=gtype[n], dim=1) model_hfac = model_grid.get_hfac(gtype=gtype[n]) # Interpolate to the correct grid and choose the correct horizontal axis if location in ['N', 'S']: if gtype[n] == 'v': source_data = c1_e*source_data[...,j1_e,:] + c2_e*source_data[...,j2_e,:] # Multiply hfac by the ceiling of hfac on each side, to make sure we're not averaging over land source_hfac = (c1_e*source_hfac[...,j1_e,:] + c2_e*source_hfac[...,j2_e,:])*np.ceil(source_hfac[...,j1_e,:])*np.ceil(source_hfac[...,j2_e,:]) else: source_data = c1*source_data[...,j1,:] + c2*source_data[...,j2,:] source_hfac = (c1*source_hfac[...,j1,:] + c2*source_hfac[...,j2,:])*np.ceil(source_hfac[...,j1,:])*np.ceil(source_hfac[...,j2,:]) source_haxis = source_lon model_haxis = model_lon if location == 'S': model_hfac = model_hfac[:,0,:] else: model_hfac = model_hfac[:,-1,:] else: if gtype[n] == 'u': source_data = c1_e*source_data[...,i1_e] + c2_e*source_data[...,i2_e] source_hfac = (c1_e*source_hfac[...,i1_e] + c2_e*source_hfac[...,i2_e])*np.ceil(source_hfac[...,i1_e])*np.ceil(source_hfac[...,i2_e]) else: source_data = c1*source_data[...,i1] + c2*source_data[...,i2] source_hfac = (c1*source_hfac[...,i1] + c2*source_hfac[...,i2])*np.ceil(source_hfac[...,i1])*np.ceil(source_hfac[...,i2]) source_haxis = source_lat model_haxis = model_lat if location == 'W': model_hfac = model_hfac[...,0] else: model_hfac = model_hfac[...,-1] if source == 'MIT' and model_haxis[0] < source_haxis[0]: # Need to extend source data to the west or south. Just add one row. source_haxis = np.concatenate(([model_haxis[0]-0.1], source_haxis)) source_data = np.concatenate((np.expand_dims(source_data[:,...,0], -1), source_data), axis=-1) source_hfac = np.concatenate((np.expand_dims(source_hfac[:,0], 1), source_hfac), axis=1) # For 2D variables, just need surface hfac if dim[n] == 2: source_hfac = source_hfac[0,:] model_hfac = model_hfac[0,:] # Now interpolate each month to the model grid if dim[n] == 3: data_interp = np.zeros([12, model_grid.nz, model_haxis.size]) else: data_interp = np.zeros([12, model_haxis.size]) for month in range(12): print '...interpolating month ' + str(month+1) data_interp_tmp = interp_bdry(source_haxis, source_grid.z, source_data[month,:], source_hfac, model_haxis, model_grid.z, model_hfac, depth_dependent=(dim[n]==3)) if fields[n] not in ['THETA', 'SALT']: # Zero in land mask is more physical than extrapolated data index = model_hfac==0 data_interp_tmp[index] = 0 data_interp[month,:] = data_interp_tmp write_binary(data_interp, out_file, prec=prec) if nc_out is not None: print '...adding to ' + nc_out # Construct the dimension code if location in ['S', 'N']: dimension = 'x' else: dimension = 'y' if dim[n] == 3: dimension += 'z' dimension += 't' ncfile.add_variable(fields[n] + '_' + location, data_interp, dimension) if nc_out is not None: ncfile.close()
def sose_ics (grid_path, sose_dir, output_dir, nc_out=None, constant_t=-1.9, constant_s=34.4, split=180, prec=64): from grid import SOSEGrid from file_io import NCfile from interpolation import interp_reg sose_dir = real_dir(sose_dir) output_dir = real_dir(output_dir) # Fields to interpolate fields = ['THETA', 'SALT', 'SIarea', 'SIheff'] # Flag for 2D or 3D dim = [3, 3, 2, 2] # Constant values for ice shelf cavities constant_value = [constant_t, constant_s, 0, 0] # End of filenames for input infile_tail = '_climatology.data' # End of filenames for output outfile_tail = '_SOSE.ini' print 'Building grids' # First build the model grid and check that we have the right value for split model_grid = grid_check_split(grid_path, split) # Now build the SOSE grid sose_grid = SOSEGrid(sose_dir+'grid/', model_grid=model_grid, split=split) # Extract land mask sose_mask = sose_grid.hfac == 0 print 'Building mask for SOSE points to fill' # Figure out which points we need for interpolation # Find open cells according to the model, interpolated to SOSE grid model_open = np.ceil(interp_reg(model_grid, sose_grid, np.ceil(model_grid.hfac), fill_value=1)) # Find ice shelf cavity points according to model, interpolated to SOSE grid model_cavity = np.ceil(interp_reg(model_grid, sose_grid, xy_to_xyz(model_grid.ice_mask, model_grid), fill_value=0)).astype(bool) # Select open, non-cavity cells fill = model_open*np.invert(model_cavity) # Extend into the mask a few times to make sure there are no artifacts near the coast fill = extend_into_mask(fill, missing_val=0, use_3d=True, num_iters=3) # Set up a NetCDF file so the user can check the results if nc_out is not None: ncfile = NCfile(nc_out, model_grid, 'xyz') # Process fields for n in range(len(fields)): print 'Processing ' + fields[n] in_file = sose_dir + fields[n] + infile_tail out_file = output_dir + fields[n] + outfile_tail print '...reading ' + in_file # Just keep the January climatology if dim[n] == 3: sose_data = sose_grid.read_field(in_file, 'xyzt')[0,:] else: # Fill any missing regions with zero sea ice, as we won't be extrapolating them later sose_data = sose_grid.read_field(in_file, 'xyt', fill_value=0)[0,:] # Discard the land mask, and extrapolate slightly into missing regions so the interpolation doesn't get messed up. print '...extrapolating into missing regions' if dim[n] == 3: sose_data = discard_and_fill(sose_data, sose_mask, fill) # Fill cavity points with constant values sose_data[model_cavity] = constant_value[n] else: # Just care about surface layer sose_data = discard_and_fill(sose_data, sose_mask[0,:], fill[0,:], use_3d=False) print '...interpolating to model grid' data_interp = interp_reg(sose_grid, model_grid, sose_data, dim=dim[n]) # Fill the land mask with zeros if dim[n] == 3: data_interp[model_grid.hfac==0] = 0 else: data_interp[model_grid.hfac[0,:]==0] = 0 write_binary(data_interp, out_file, prec=prec) if nc_out is not None: print '...adding to ' + nc_out if dim[n] == 3: ncfile.add_variable(fields[n], data_interp, 'xyz') else: ncfile.add_variable(fields[n], data_interp, 'xy') if nc_out is not None: ncfile.close()
def sose_sss_restoring (grid_path, sose_dir, output_salt_file, output_mask_file, nc_out=None, h0=-1250, obcs_sponge=0, split=180, prec=64): sose_dir = real_dir(sose_dir) print 'Building grids' # First build the model grid and check that we have the right value for split model_grid = grid_check_split(grid_path, split) # Now build the SOSE grid sose_grid = SOSEGrid(sose_dir+'grid/', model_grid=model_grid, split=split) # Extract surface land mask sose_mask = sose_grid.hfac[0,:] == 0 print 'Building mask' mask_surface = np.ones([model_grid.ny, model_grid.nx]) # Mask out land and ice shelves mask_surface[model_grid.hfac[0,:]==0] = 0 # Save this for later mask_land_ice = np.copy(mask_surface) # Mask out continental shelf mask_surface[model_grid.bathy > h0] = 0 # Smooth, and remask the land and ice shelves mask_surface = smooth_xy(mask_surface, sigma=2)*mask_land_ice if obcs_sponge > 0: # Also mask the cells affected by OBCS and/or its sponge mask_surface[:obcs_sponge,:] = 0 mask_surface[-obcs_sponge:,:] = 0 mask_surface[:,:obcs_sponge] = 0 mask_surface[:,-obcs_sponge:] = 0 # Make a 3D version with zeros in deeper layers mask_3d = np.zeros([model_grid.nz, model_grid.ny, model_grid.nx]) mask_3d[0,:] = mask_surface print 'Reading SOSE salinity' # Just keep the surface layer sose_sss = sose_grid.read_field(sose_dir+'SALT_climatology.data', 'xyzt')[:,0,:,:] # Figure out which SOSE points we need for interpolation # Restoring mask interpolated to the SOSE grid fill = np.ceil(interp_reg(model_grid, sose_grid, mask_3d[0,:], dim=2, fill_value=1)) # Extend into the mask a few times to make sure there are no artifacts near the coast fill = extend_into_mask(fill, missing_val=0, num_iters=3) # Process one month at a time sss_interp = np.zeros([12, model_grid.nz, model_grid.ny, model_grid.nx]) for month in range(12): print 'Month ' + str(month+1) print '...filling missing values' sose_sss_filled = discard_and_fill(sose_sss[month,:], sose_mask, fill, use_3d=False) print '...interpolating' # Mask out land and ice shelves sss_interp[month,0,:] = interp_reg(sose_grid, model_grid, sose_sss_filled, dim=2)*mask_land_ice write_binary(sss_interp, output_salt_file, prec=prec) write_binary(mask_3d, output_mask_file, prec=prec) if nc_out is not None: print 'Writing ' + nc_out ncfile = NCfile(nc_out, model_grid, 'xyzt') ncfile.add_time(np.arange(12)+1, units='months') ncfile.add_variable('salinity', sss_interp, 'xyzt', units='psu') ncfile.add_variable('restoring_mask', mask_3d, 'xyz') ncfile.close()