Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _read_forcing(self, vs, var):
with h5netcdf.File(DATA_FILES['forcing'], 'r') as infile:
var_obj = infile.variables[var]
return np.array(var_obj, dtype=str(var_obj.dtype)).T
def set_initial_conditions(self, vs):
with h5netcdf.File(DATA_FILES['forcing'], 'r') as forcing_file:
t_hor = (vs.xt[2:-2], vs.yt[2:-2])
t_grid = (vs.xt[2:-2], vs.yt[2:-2], vs.zt)
forc_coords = [self._get_data(vs, forcing_file, k) for k in ('xt', 'yt', 'zt')]
forc_coords[0][...] += -360
forc_coords[2][...] = -0.01 * forc_coords[2][::-1]
temp_raw = self._get_data(vs, forcing_file, 'temp_ic')[..., ::-1]
temp = veros.tools.interpolate(
forc_coords, temp_raw, t_grid, missing_value=-1e20
)
vs.temp[2:-2, 2:-2, :, vs.tau] = vs.maskT[2:-2, 2:-2, :] * temp
salt_raw = self._get_data(vs, forcing_file, 'salt_ic')[..., ::-1]
salt = 35. + 1000 * veros.tools.interpolate(
forc_coords, salt_raw, t_grid, missing_value=-1e20
def set_initial_conditions(self, vs):
# initial conditions for T and S
temp_data = self._read_forcing(vs, 'temperature')[:, :, ::-1]
vs.temp[2:-2, 2:-2, :, :2] = temp_data[:, :, :, np.newaxis] * \
vs.maskT[2:-2, 2:-2, :, np.newaxis]
salt_data = self._read_forcing(vs, 'salinity')[:, :, ::-1]
vs.salt[2:-2, 2:-2, :, :2] = salt_data[..., np.newaxis] * vs.maskT[2:-2, 2:-2, :, np.newaxis]
# use Trenberth wind stress from MITgcm instead of ECMWF (also contained in ecmwf_4deg.cdf)
vs.taux[2:-2, 2:-2, :] = self._read_forcing(vs, 'tau_x')
vs.tauy[2:-2, 2:-2, :] = self._read_forcing(vs, 'tau_y')
# heat flux
with h5netcdf.File(DATA_FILES['ecmwf'], 'r') as ecmwf_data:
qnec_var = ecmwf_data.variables['Q3']
vs.qnec[2:-2, 2:-2, :] = np.array(qnec_var, dtype=str(qnec_var.dtype)).transpose()
vs.qnec[vs.qnec <= -1e10] = 0.0
q = self._read_forcing(vs, 'q_net')
vs.qnet[2:-2, 2:-2, :] = -q
vs.qnet[vs.qnet <= -1e10] = 0.0
fxa = np.sum(vs.qnet[2:-2, 2:-2, :] * vs.area_t[2:-2, 2:-2, np.newaxis]) \
/ 12 / np.sum(vs.area_t[2:-2, 2:-2])
print(' removing an annual mean heat flux imbalance of %e W/m^2' % fxa)
vs.qnet[...] = (vs.qnet - fxa) * vs.maskT[:, :, -1, np.newaxis]
# SST and SSS
vs.sst_clim[2:-2, 2:-2, :] = self._read_forcing(vs, 'sst')
vs.sss_clim[2:-2, 2:-2, :] = self._read_forcing(vs, 'sss')
def _read_forcing(self, vs, var):
with h5netcdf.File(DATA_FILES['forcing'], 'r') as infile:
var = infile.variables[var]
return np.array(var, dtype=str(var.dtype)).T
def _read_forcing(self, vs, var):
with h5netcdf.File(DATA_FILES['forcing'], 'r') as infile:
var_obj = infile.variables[var]
return np.array(var_obj, dtype=str(var_obj.dtype)).T
If using IO threads, start a new thread to write the netCDF data to disk.
"""
import h5netcdf
if vs.use_io_threads:
_wait_for_disk(vs, filepath)
_io_locks[filepath].clear()
kwargs = {}
if runtime_state.proc_num > 1:
kwargs.update(
driver='mpio',
comm=rs.mpi_comm
)
nc_dataset = h5netcdf.File(filepath, mode, **kwargs)
try:
yield nc_dataset
finally:
if vs.use_io_threads:
threading.Thread(target=_write_to_disk, args=(vs, nc_dataset, filepath)).start()
else:
_write_to_disk(vs, nc_dataset, filepath)
def _get_data(self, vs, var, idx=None):
if idx is None:
idx = Ellipsis
else:
idx = idx[::-1]
kwargs = {}
if rst.proc_num > 1:
kwargs.update(
driver='mpio',
comm=rs.mpi_comm,
)
with h5netcdf.File(DATA_FILES['forcing'], 'r', **kwargs) as forcing_file:
var_obj = forcing_file.variables[var]
return np.array(var_obj[idx].astype(str(var_obj.dtype))).T
def _get_data(self, vs, var):
with h5netcdf.File(DATA_FILES['forcing'], 'r') as forcing_file:
var_obj = forcing_file.variables[var]
return np.array(var_obj, dtype=str(var_obj.dtype)).T
def dtype(self):
dt = self._h5ds.dtype
if h5py.check_dtype(vlen=dt) is unicode:
return str
return dt
class Group(core.Group, HasAttributesMixin):
_cls_name = 'h5netcdf.legacyapi.Group'
_variable_cls = Variable
@property
def _group_cls(self):
return Group
createGroup = core.Group.create_group
createDimension = core.Group._create_dimension
def createVariable(self, varname, datatype, dimensions=(), zlib=False,
complevel=4, shuffle=True, fletcher32=False,
chunksizes=None, fill_value=None):
if len(dimensions) == 0: # it's a scalar
# rip off chunk and filter options for consistency with netCDF4-python
chunksizes = None
zlib = False
fletcher32 = False
shuffle = False
if datatype is str:
datatype = h5py.special_dtype(vlen=unicode)
dt = self._h5ds.dtype
if h5py.check_dtype(vlen=dt) is unicode:
return str
return dt
class Group(core.Group, HasAttributesMixin):
_cls_name = 'h5netcdf.legacyapi.Group'
_variable_cls = Variable
@property
def _group_cls(self):
return Group
createGroup = core.Group.create_group
createDimension = core.Group._create_dimension
def createVariable(self, varname, datatype, dimensions=(), zlib=False,
complevel=4, shuffle=True, fletcher32=False,
chunksizes=None, fill_value=None):
if len(dimensions) == 0: # it's a scalar
# rip off chunk and filter options for consistency with netCDF4-python
chunksizes = None
zlib = False
fletcher32 = False
shuffle = False
if datatype is str:
datatype = h5py.special_dtype(vlen=unicode)
kwds = {}