Source code for pycif.plugins.datastreams.fluxes.edgar_v5.get_domain
import datetime
import glob
import os
import numpy as np
import xarray as xr
from netCDF4 import Dataset
from .....utils.classes.domains import Domain
from logging import info
[docs]
def get_domain(ref_dir, ref_file, input_interval, target_dir, tracer=None):
# Looking for a reference file to read lon/lat in
list_file = glob.glob("{}/*nc".format(ref_dir))
domain_file = None
# Either a file is specified in the Yaml
if ref_file in list_file:
domain_file = "{}/{}".format(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 Exception(
"EDGARv5 domain could not be initialized as no file was found"
)
# Read lon/lat in
# WARNING: Force reading from -180 to 180 or choose truncated option
if tracer.truncated:
with Dataset(domain_file, "r") as f:
lon = f.variables["lon"][:]
lat = f.variables["lat"][:]
zlon, zlat = np.meshgrid(lon, lat)
# Grid cell corners
dlat = np.unique(np.diff(lat))[0]
dlon = np.unique(np.diff(lon))[0]
latc = np.append(lat - dlat * 0.5, lat[-1] + dlat * 0.5)
lonc = np.append(lon - dlon * 0.5, lon[-1] + dlon * 0.5)
zlonc, zlatc = np.meshgrid(lonc, latc)
nlon = lon.size
nlat = lat.size
domain = Domain(nlon=nlon, nlat=nlat,
zlon=zlon, zlat=zlat,
zlonc=zlonc, zlatc=zlatc,
nlev=1, pressure_unit="Pa",
sigma_b_mid=np.array([1]), sigma_a_mid=np.array([0]))
else:
lonc = np.arange(-180, 180.1, 0.1)
latc = np.arange(-90, 90.1, 0.1)
lon = 0.5 * (lonc[1:] + lonc[:-1])
lat = 0.5 * (latc[1:] + latc[:-1])
zlon, zlat = np.meshgrid(lon, lat)
zlonc, zlatc = np.meshgrid(lonc, latc)
nlat, nlon = zlat.shape
domain = Domain(nlon=nlon, nlat=nlat,
zlon=zlon, zlat=zlat,
zlonc=zlonc, zlatc=zlatc,
nlev=1, pressure_unit="Pa",
sigma_b_mid=np.array([1]), sigma_a_mid=np.array([0]),
lon_cyclic=True)
return domain