Source code for pycif.plugins.datastreams.fluxes.GFEDv4.get_domain
import numpy as np
from logging import debug
import os
import xarray as xr
import itertools
import netCDF4
import h5py
from .....utils.classes.setup import Setup
from .....utils.classes.domains import Domain
from .....utils.check.errclass import CifError
[docs]
def get_domain(ref_dir, ref_file, input_interval, target_dir, tracer=None):
"""Read information from the reference file
to define the data horizontal and, if relevant, vertical domain.
Args:
ref_dir (str): the path to the input files
ref_file (str): format of the input files
input_interval (list): simulation interval (start and end dates)
target_dir (str): where to copy
tracer: the tracer Plugin, corresponding to the paragraph
:bash:`datavect/components/fluxes/parameters/my_species` in the
configuration yaml; can be needed to fetch extra information
given by the user
Return:
domain (Domain): a domain class object, with the definition of the center grid
cells coordinates, as well as corners
"""
ref_file = list(itertools.chain.from_iterable(
tracer.input_files.values()))[0]
if not os.path.isfile(ref_file):
raise CifError(f"Could not initialize the domain as no reference file is available. Expecting the following file: {ref_file}")
# Open HDF file
with h5py.File(ref_file, "r") as f:
zlat = f["lat"][:][::-1, :]
zlon = f["lon"][:]
nlat, nlon = zlat.shape
# Grid cell corners
dlat = np.unique(np.diff(zlat, axis=0))[0]
dlon = np.unique(np.diff(zlon, axis=1))[0]
zlatc = np.concatenate((zlat - dlat / 2, zlat[[-1]] + dlat / 2), axis=0)
zlatc = np.concatenate((zlatc, zlatc[:, [-1]]), axis=1)
zlonc = np.concatenate((zlon - dlon / 2, zlon[:, [-1]] + dlon / 2), axis=1)
zlonc = np.concatenate((zlonc, zlonc[[-1]]), axis=0)
# Vertical definition (surface level)
pressure_unit = "Pa"
nlev = 1
sigma_a_mid = np.array([0])
sigma_b_mid = np.array([1])
# Put it to a domain Plugin
domain = Domain(nlon=nlon, nlat=nlat,
zlon=zlon, zlat=zlat,
zlonc=zlonc, zlatc=zlatc,
nlev=nlev, pressure_unit=pressure_unit,
sigma_b_mid=sigma_b_mid, sigma_a_mid=sigma_a_mid,
lon_cyclic=True)
return domain