def iceberg_meltwater(grid_path, input_dir, output_file, nc_out=None, prec=32): from plot_latlon import latlon_plot input_dir = real_dir(input_dir) file_head = 'icebergs_' file_tail = '.nc' print 'Building grids' # Read the NEMO grid from the first file # It has longitude in the range -180 to 180 file_path = input_dir + file_head + '01' + file_tail nemo_lon = read_netcdf(file_path, 'nav_lon') nemo_lat = read_netcdf(file_path, 'nav_lat') # Build the model grid model_grid = Grid(grid_path, max_lon=180) print 'Interpolating' icebergs_interp = np.zeros([12, model_grid.ny, model_grid.nx]) for month in range(12): print '...month ' + str(month + 1) # Read the data file_path = input_dir + file_head + '{0:02d}'.format(month + 1) + file_tail icebergs = read_netcdf(file_path, 'berg_total_melt', time_index=0) # Interpolate icebergs_interp_tmp = interp_nonreg_xy(nemo_lon, nemo_lat, icebergs, model_grid.lon_1d, model_grid.lat_1d, fill_value=0) # Make sure the land and ice shelf cavities don't get any iceberg melt icebergs_interp_tmp[model_grid.land_mask + model_grid.ice_mask] = 0 # Save to the master array icebergs_interp[month, :] = icebergs_interp_tmp write_binary(icebergs_interp, output_file, prec=prec) print 'Plotting' # Make a nice plot of the annual mean latlon_plot(mask_land_ice(np.mean(icebergs_interp, axis=0), model_grid), model_grid, include_shelf=False, vmin=0, title=r'Annual mean iceberg melt (kg/m$^2$/s)') if nc_out is not None: # Also write to NetCDF file print 'Writing ' + nc_out ncfile = NCfile(nc_out, model_grid, 'xyt') ncfile.add_time(np.arange(12) + 1, units='months') ncfile.add_variable('iceberg_melt', icebergs_interp, 'xyt', units='kg/m^2/s') ncfile.close()
def calc_ice_prod (file_path, out_file, monthly=True): # Build the grid from the file grid = Grid(file_path) # Add up all the terms to get sea ice production at each time index ice_prod = read_netcdf(file_path, 'SIdHbOCN') + read_netcdf(file_path, 'SIdHbATC') + read_netcdf(file_path, 'SIdHbATO') + read_netcdf(file_path, 'SIdHbFLO') # Also need time time = netcdf_time(file_path, monthly=monthly) # Set negative values to 0 ice_prod = np.maximum(ice_prod, 0) # Write a new file ncfile = NCfile(out_file, grid, 'xyt') ncfile.add_time(time) ncfile.add_variable('ice_prod', ice_prod, 'xyt', long_name='Net sea ice production', units='m/s') ncfile.close()
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_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()
def precompute_timeseries (mit_file, timeseries_file, timeseries_types=None, monthly=True, lon0=None, lat0=None): # Timeseries to compute if timeseries_types is None: timeseries_types = ['fris_mass_balance', 'eta_avg', 'seaice_area', 'fris_temp', 'fris_salt', 'fris_age'] #['fris_mass_balance', 'hice_corner', 'mld_ewed', 'eta_avg', 'seaice_area', 'fris_temp', 'fris_salt'] # Build the grid grid = Grid(mit_file) # Check if the timeseries file already exists file_exists = os.path.isfile(timeseries_file) if file_exists: # Open it id = nc.Dataset(timeseries_file, 'a') else: # Create it ncfile = NCfile(timeseries_file, grid, 't') # Define/update time # Read the time array from the MITgcm file, and its units time, time_units = netcdf_time(mit_file, return_units=True) if file_exists: # Update the units to match the old time array time_units = id.variables['time'].units # Also figure out how many time indices are in the file so far num_time = id.variables['time'].size # Convert to numeric values time = nc.date2num(time, time_units) # Append to file id.variables['time'][num_time:] = time else: # Add the time variable to the file ncfile.add_time(time, units=time_units) # Inner function to define/update non-time variables def write_var (data, var_name, title, units): if file_exists: # Append to file id.variables[var_name][num_time:] = data else: # Add the variable to the file ncfile.add_variable(var_name, data, 't', long_name=title, units=units) # Now process all the timeseries for ts_name in timeseries_types: print 'Processing ' + ts_name # Get information about the variable; only care about title and units title, units = set_parameters(ts_name)[2:4] if ts_name == 'fris_mass_balance': melt, freeze = calc_special_timeseries(ts_name, mit_file, grid=grid, monthly=monthly)[1:] # We need two titles now title_melt = 'Total melting beneath FRIS' title_freeze = 'Total refreezing beneath FRIS' # Update two variables write_var(melt, 'fris_total_melt', title_melt, units) write_var(freeze, 'fris_total_freeze', title_freeze, units) else: data = calc_special_timeseries(ts_name, mit_file, grid=grid, lon0=lon0, lat0=lat0, monthly=monthly)[1] write_var(data, ts_name, title, units) # Finished if file_exists: id.close() else: ncfile.close()