Source code for pycif.plugins.domains.chimere.read_domain
import numpy as np
[docs]
def read_grid(domain, **kwargs):
"""Read the CHIMERE horizontal and vertical grid from pre-computed coordinate files.
Reads ``domainlist.nml`` to determine grid dimensions (``nzo`` × ``nme``),
then loads cell centres from ``HCOORD/COORD_{domid}`` and corners from
``HCOORD/COORDcorner_{domid}``. Vertical sigma levels are read from
``VCOORD/VCOORD_{nlev}_{p1}_{pmax}`` and stored as ``sigma_a`` /
``sigma_b`` (in hPa).
Sets on *domain*: ``nlon``, ``nlat``, ``nlev``, ``zlon``, ``zlat``,
``zlonc``, ``zlatc``, ``corners``, ``sigma_a``, ``sigma_b``.
Args:
domain: CHIMERE domain plugin instance with ``repgrid``, ``domid``,
``nlev``, ``p1``, and ``pmax`` set.
**kwargs: unused.
Raises:
AttributeError: if ``domain.domid`` is not found in ``domainlist.nml``.
"""
# Loading geometry from the list of existing domains
nzo = 0
nme = 0
with open(f"{domain.repgrid}/domainlist.nml", "r") as fgrid:
ln = fgrid.readlines()
for l in ln[1:]:
s = l.split()
if s[0] == domain.domid:
nzo = int(s[1])
nme = int(s[2])
break
if nzo == nme == 0:
raise AttributeError(f"Could not find the domain {domain.domid} in the domainlist file {domain.repgrid}")
# Saving information to domain attributes
domain.nlat = nme
domain.nlon = nzo
# Reading lat/lon and latc/lonc
file_hcoord = f"{domain.repgrid}/HCOORD/COORD_{domain.domid}"
data = np.genfromtxt(file_hcoord)
lon = data[:, 0].reshape(nme, nzo)
lat = data[:, 1].reshape(nme, nzo)
file_hcoord = f"{domain.repgrid}/HCOORD/COORDcorner_{domain.domid}"
data = np.genfromtxt(file_hcoord)
lonc = data[:, 0].reshape(nme + 1, nzo + 1)
latc = data[:, 1].reshape(nme + 1, nzo + 1)
# Putting the data into the domain
domain.zlonc = lonc
domain.zlatc = latc
domain.zlon = lon
domain.zlat = lat
domain.corners = f"{domain.repgrid}/HCOORD/COORDcorner_{domain.domid}"
# Reading vertical coordinates
file_vcoord = f"{domain.repgrid}/VCOORD/VCOORD_{domain.nlev}_{domain.p1}_{domain.pmax}"
data = np.concatenate([[[0, 1]], np.genfromtxt(file_vcoord)], axis=0)
domain.sigma_a = data[:, 0] * 1e3
domain.sigma_b = data[:, 1]
domain.nlev = len(domain.sigma_a) - 1