def heat_content_freezing(temp, salt, grid, eosType='MDJWF', rhoConst=None, Tref=None, Sref=None, tAlpha=None, sBeta=None, time_dependent=False): dV = grid.dV # Get 3D z (for freezing point) z = z_to_xyz(grid.z, grid) # Add time dimensions if needed if time_dependent: num_time = temp.shape[0] dV = add_time_dim(dV, num_time) z = add_time_dim(z, num_time) # Calculate freezing temperature Tf = tfreeze(salt, z) # Calculate potential density rho = potential_density(eosType, salt, temp, rhoConst=rhoConst, Tref=Tref, Sref=Sref, tAlpha=tAlpha, sBeta=sBeta) # Now calculate heat content relative to Tf, in J return (temp - Tf) * rho * Cp_sw * dV
def vertical_average_column(data, hfac, grid, gtype='t', time_dependent=False): if gtype == 'w': dz = grid.dz_t else: dz = grid.dz if time_dependent: # Add time dimension to dz and hfac dz = add_time_dim(dz, data.shape[0]) hfac = add_time_dim(hfac, data.shape[0]) return np.sum(data * dz * hfac, axis=-1) / np.sum(dz * hfac, axis=-1)
def prepare_dz_hfac(data, grid, gtype='t', time_dependent=False): # Choose the correct integrand of depth if gtype == 'w': dz = grid.dz_t else: dz = grid.dz # Make it 3D dz = z_to_xyz(dz, grid) # Get the correct hFac hfac = grid.get_hfac(gtype=gtype) if time_dependent: # There's also a time dimension dz = add_time_dim(dz, data.shape[0]) hfac = add_time_dim(hfac, data.shape[0]) return dz, hfac
def t_minus_tf(temp, salt, grid, time_dependent=False): # Tile the z coordinates to be the same size as temp and salt # First assume 3D arrays z = z_to_xyz(grid.z, grid) if time_dependent: # 4D arrays z = add_time_dim(z, temp.shape[0]) return in_situ_temp(temp, salt, z) - tfreeze(salt, z)
def transport_transect(u, v, grid, point0, point1, shore='S', time_dependent=False): # Calculate normal velocity u_norm = normal_vector(u, v, grid, point0, point1, time_dependent=time_dependent) # Extract the transect u_norm_trans, left, right, below, above = get_transect( u_norm, grid, point0, point1, time_dependent=time_dependent) # Calculate integrands dh = (right - left) * 1e3 # Convert from km to m dz = above - below if time_dependent: # Make them 3D num_time = u.shape[0] dh = add_time_dim(dh, num_time) dz = add_time_dim(dh, num_time) # Integrate and convert to Sv trans_S = np.sum(np.minimum(u_norm_trans, 0) * dh * dz * 1e-6, axis=(-2, -1)) trans_N = np.sum(np.maximum(u_norm_trans, 0) * dh * dz * 1e-6, axis=(-2, -1)) # Retrn onshore, then offshore transport if shore == 'S': return trans_S, trans_N elif shore == 'N': return trans_N, trans_S else: print 'Error (transport_transect): invalid shore ' + shore sys.exit()
def prepare_integrand_mask(option, data, grid, gtype='t', time_dependent=False): if option in ['dA', 'dV'] and gtype != 't': print 'Error (prepare_integrand_mask): non-tracer grids not yet supported' sys.exit() elif option == 'dx' and gtype == 'u': print 'Error (prepare_integrand_mask): u-grid not yet supported for dx' sys.exit() elif option == 'dy' and gtype == 'v': print 'Error (prepare_integrand_mask): v-grid not yet supported for dy' sys.exit() # Get the mask as 1s and 0s if isinstance(data, np.ma.MaskedArray): mask = np.invert(data.mask).astype(float) else: # No mask, just use 1s everywhere mask = np.ones(data.shape) # Get the integrand if option == 'dA': integrand = grid.dA elif option == 'dV': integrand = grid.dV elif option == 'dx': integrand = grid.dx_s elif option == 'dy': integrand = grid.dy_w else: print 'Error (prepare_integrand_mask): invalid option ' + option if (len(integrand.shape) == 2) and ((time_dependent and len(data.shape) == 4) or (not time_dependent and len(data.shape) == 3)): # There's also a depth dimension; tile in z integrand = xy_to_xyz(integrand, grid) if time_dependent: # Tile in time integrand = add_time_dim(integrand, data.shape[0]) return integrand, mask
def prepare_coord(shape, grid, option, gtype='t', time_dependent=False): if option == 'lon': # Get 2D lon coordinates = grid.get_lon_lat(gtype=gtype)[0] elif option == 'lat': # Get 2D lat coordinates = grid.get_lon_lat(gtype=gtype)[1] elif option == 'depth': # Get 3D z if gtype == 'w': print 'Error (prepare_coord): w-grid not yet supported for depth derivatives' sys.exit() coordinates = z_to_xyz(grid.z, z) if option in ['lon', 'lat'] and ((len(shape) == 3 and not time_dependent) or (len(shape) == 4 and time_dependent)): # Add depth dimension coordinates = xy_to_xyz(coordinates, grid) if time_dependent: # Add time dimension coordinates = add_time_dim(coordinates, shape[0]) return coordinates