Source code for pycif.plugins.datastreams.fields.noaa_glob_avg.get_domain
import datetime
import glob
import os
import numpy as np
import xarray as xr
from .....utils.classes.setup import Setup
from .....utils.classes.domains import Domain
from .....utils.check.errclass import CifError
from .....utils.hdf5 import _hdf5_lock
[docs]
def get_domain(ref_dir, ref_file, input_interval, target_dir, tracer=None):
"""Build a global cyclic Domain from a NOAA reference file.
Looks for a reference NetCDF file in ``ref_dir``, either an exact
match for ``ref_file`` or the first file whose name matches
``ref_file`` used as a date-format pattern, then builds a global
cyclic lon-lat grid from its ``longitude``/``latitude`` variables and
vertical mid-levels from its ``pressure`` variable (with ``sigma_b``
set to zero, i.e. a purely pressure-based vertical coordinate).
Args:
ref_dir: directory to search for a reference file.
ref_file: exact file name, or a date-format pattern used to
match one of the files in ``ref_dir``.
input_interval: unused, accepted for interface compatibility.
target_dir: unused, accepted for interface compatibility.
tracer: unused, accepted for interface compatibility.
Returns:
Domain: the NOAA grid domain.
Raises:
CifError: if no reference file can be found in ``ref_dir``.
"""
# Looking for a reference file to read lon/lat in
list_file = glob.glob(f"{ref_dir}/*nc")
# Either a file is specified in the Yaml
domain_file = None
if ref_file in list_file:
domain_file = f"{ref_dir}/{ref_file}"
# Or loop over available file regarding file pattern
else:
for flx_file in list_file:
try:
date = datetime.datetime.strptime(
os.path.basename(flx_file), ref_file
)
domain_file = flx_file
break
except ValueError:
continue
if domain_file is None:
raise CifError(
"EDGARv5 domain could not be initialized as no file was found"
)
# Read lon/lat in
with _hdf5_lock:
nc = xr.open_dataset(domain_file, decode_times=False)
lonc = np.concatenate([nc["longitude"].values, [180]])
latc = np.concatenate([nc["latitude"].values, [90]])
zlonc, zlatc = np.meshgrid(lonc, latc)
lon = 0.5 * (lonc[1:] + lonc[:-1])
lat = 0.5 * (latc[1:] + latc[:-1])
zlon, zlat = np.meshgrid(lon, lat)
nlon = lon.size
nlat = lat.size
# Initializes vertical resolution
pres = nc["pressure"]
nlev = len(pres)
sigma_a_mid = pres.values
sigma_b_mid = 0. * pres.values
# Initializes domain
domain = Domain(
nlon=nlon, nlat=nlat, zlon=zlon, zlat=zlat,
zlonc=zlonc, zlatc=zlatc,
pressure_unit="Pa", lon_cyclic=True, nlev=nlev,
sigma_a_mid=sigma_a_mid, sigma_b_mid=sigma_b_mid,
)
return domain