Source code for pycif.plugins.domains.dummy.read_domain


import numpy as np

from logging import info


[docs] def read_grid(domain, **kwargs): """Read the dummy Cartesian domain from text files or fall back to ``create_domain``. Loads 1-D coordinate arrays from ``domain.filelon`` and ``domain.filelat``, derives half-step corners, and builds 2-D meshgrids. Cell areas are computed as the Cartesian product of zonal and meridional cell sizes. If the files cannot be read (``IOError``) or the attributes are absent (``AttributeError``), delegates to ``domain.create_domain()`` to build the grid from YAML parameters. Sets on *domain*: ``nlon``, ``nlat``, ``zlon``, ``zlat``, ``zlonc``, ``zlatc``, ``areas``, ``projection``. Args: domain: dummy domain plugin instance. **kwargs: unused. """ # Tries open filelon, filelat try: zlon = np.loadtxt(domain.filelon) zlat = np.loadtxt(domain.filelat) nlon = zlon.size nlat = zlat.size # Corner coordinates dlon = np.ptp(zlon) / (nlon - 1) / 2.0 zlonc = zlon - dlon zlonc = np.append(zlonc, zlonc[-1] + 2 * dlon) dlat = np.ptp(zlat) / (nlat - 1) / 2.0 zlatc = zlat - dlat zlatc = np.append(zlatc, zlatc[-1] + 2 * dlat) # Meshgrids zlon, zlat = np.meshgrid(zlon, zlat) zlonc, zlatc = np.meshgrid(zlonc, zlatc) # Saving information to domain attributes domain.nlon = nlon domain.nlat = nlat domain.zlon = zlon domain.zlat = zlat domain.zlonc = zlonc domain.zlatc = zlatc except (IOError, AttributeError): info( "Couldn't read longitudes and latitudes.\n" "Make them from given coordinates" ) domain.create_domain() # Compute areas in m2 domain.areas = ( np.diff(domain.zlatc, axis=0)[:, :-1] * np.diff(domain.zlonc, axis=1)[:-1] ) # Projection not as GPS domain.projection = "xy"