Source code for pycif.plugins.datastreams.fields.gridded_NetCDF_inicond.read
from logging import warning
import xarray as xr
from ....domains.gridded_NetCDF.utils import find_coord
[docs]
def read(self, name, varnames, dates, files, interpol_flx=False, comp_type=None, **kwargs):
if len(files) > 1:
warning("multiple inicond files to read, "
"reading the first one and ignoring all others")
inicond_file = files[0]
initial_date = dates[0][0]
# Getting inicond dataarray
varnames = varnames if varnames else name
xmod = xr.open_dataset(inicond_file)[varnames]
# Getting coordinates names
lev_coord_name = self.vertical_dim_name
lat_coord_name = find_coord(xmod, 'latitude', 'lat')
lon_coord_name = find_coord(xmod, 'longitude', 'lon')
# Renaming spatial coordinates to 'lev', 'lat' and 'lon'
xmod = xmod.rename({lat_coord_name: 'lat',
lon_coord_name: 'lon',
lev_coord_name: 'lev'})
# TODO: need to also rename the dimensions associated with lat, lon and time
# if they have a different name than the coordinates.
# Sorting latitudes and longitudes in ascending order
dims_to_sort = []
if self.sort_lat:
dims_to_sort.append('lat')
if self.sort_lon:
dims_to_sort.append('lon')
if dims_to_sort:
xmod = xmod.sortby(dims_to_sort)
# Adding 'time' dimension and coordinate
if 'time' in xmod.dims:
xmod = xmod.isel(time=0)
xmod = xmod.expand_dims(['time'])
xmod['time'] = (['time'], [initial_date])
# Reordering dimensions
xmod = xmod.transpose('time', 'lev', 'lat', 'lon')
return xmod