Skip to content

Commit

Permalink
Merge pull request #124 from ec-jrc/development
Browse files Browse the repository at this point in the history
Coords from netcdf template
  • Loading branch information
doc78 authored Mar 22, 2023
2 parents 83e8fc7 + e37608e commit 4d1f2ed
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.1
4.1.2
4 changes: 2 additions & 2 deletions src/lisflood/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

__version__ = version
__authors__ = "Ad de Roo, Emiliano Gelati, Peter Burek, Johan van der Knijff, Niko Wanders"
__date__ = "09/06/2022"
__copyright__ = "Copyright 2019-2022, European Commission - Joint Research Centre"
__date__ = "22/03/2023"
__copyright__ = "Copyright 2019-2023, European Commission - Joint Research Centre"
__maintainer__ = "Stefania Grimaldi, Cinzia Mazzetti, Carlo Russo, Damien Decremer, Corentin Carton De Wiart, Valerio Lorini, Ad de Roo"
__status__ = "Operation"
33 changes: 14 additions & 19 deletions src/lisflood/global_modules/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,14 @@
from pyproj import Proj

from .settings import (calendar_inconsistency_warning, get_calendar_type, calendar, MaskAttrs, CutMap, NetCDFMetadata,
LisSettings, MaskInfo)
get_core_dims, LisSettings, MaskInfo)
from .errors import LisfloodWarning, LisfloodError
from .decorators import Cache, cached
from .zusatz import iterOpenNetcdf
# from .add1 import *
from .add1 import nanCheckMap, decompress


def get_core_dims(dims):
if 'x' in dims and 'y' in dims:
core_dims = ('y', 'x')
elif 'lat' in dims and 'lon' in dims:
core_dims = ('lat', 'lon')
else:
msg = 'Core dimension in netcdf file not recognised! Expecting (y, x) or (lat, lon), have '+str(dims)
LisfloodError(msg)
return core_dims


def mask_array_np(data, mask, crop):
data_cut = data[:, crop[2]:crop[3], crop[0]:crop[1]]
return data_cut[:, mask]
Expand Down Expand Up @@ -331,18 +320,24 @@ def read_lat_from_template(binding):
return lat_deg


def get_space_coords(nrow, ncol, dim_lat_y, dim_lon_x):
def get_space_coords(template, dim_lat_y, dim_lon_x):
cutmap = CutMap.instance()
crop = cutmap.cuts
coordinates = {}
coordinates[dim_lat_y] = template.coords[dim_lat_y][crop[2]:crop[3]]
coordinates[dim_lon_x] = template.coords[dim_lon_x][crop[0]:crop[1]]
return coordinates


def get_space_coords_pcraster(nrow, ncol, dim_lat_y, dim_lon_x):
cell = pcraster.clone().cellSize()
xl = pcraster.clone().west() + cell / 2
xr = xl + ncol * cell
yu = pcraster.clone().north() - cell / 2
yd = yu - nrow * cell
#lats = np.arange(yu, yd, -cell)
#lons = np.arange(xl, xr, cell)
coordinates = {}
coordinates[dim_lat_y] = np.linspace(yu, yd, nrow, endpoint=False)
coordinates[dim_lon_x] = np.linspace(xl, xr, ncol, endpoint=False)

return coordinates


Expand Down Expand Up @@ -386,18 +381,18 @@ def write_header(var_name, netfile, DtDay,
ncol = np.abs(cutmap.cuts[1] - cutmap.cuts[0])

dim_lat_y, dim_lon_x = get_core_dims(meta_netcdf.data)
latlon_coords = get_space_coords(nrow, ncol, dim_lat_y, dim_lon_x)
latlon_coords = get_space_coords(meta_netcdf, dim_lat_y, dim_lon_x)

if dim_lon_x in meta_netcdf.data:
lon = nf1.createDimension(dim_lon_x, ncol) # x 1000
nf1.createDimension(dim_lon_x, ncol) # x 1000
longitude = nf1.createVariable(dim_lon_x, 'f8', (dim_lon_x,))
valid_attrs = [i for i in meta_netcdf.data[dim_lon_x] if i not in not_valid_attrs]
for i in valid_attrs:
setattr(longitude, i, meta_netcdf.data[dim_lon_x][i])
longitude[:] = latlon_coords[dim_lon_x]

if dim_lat_y in meta_netcdf.data:
lat = nf1.createDimension(dim_lat_y, nrow) # x 950
nf1.createDimension(dim_lat_y, nrow) # x 950
latitude = nf1.createVariable(dim_lat_y, 'f8', (dim_lat_y,))
valid_attrs = [i for i in meta_netcdf.data[dim_lat_y] if i not in not_valid_attrs]
for i in valid_attrs:
Expand Down
20 changes: 20 additions & 0 deletions src/lisflood/global_modules/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from .decorators import cached
from .default_options import default_options


project_dir = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../..'))


Expand Down Expand Up @@ -189,6 +190,17 @@ def __iter__(self):
return iter(self.info)


def get_core_dims(dims):
if 'x' in dims and 'y' in dims:
core_dims = ('y', 'x')
elif 'lat' in dims and 'lon' in dims:
core_dims = ('lat', 'lon')
else:
msg = 'Core dimension in netcdf file not recognised! Expecting (y, x) or (lat, lon), have '+str(dims)
LisfloodError(msg)
return core_dims


@nine
class NetCDFMetadata(with_metaclass(Singleton)):
def __init__(self, uid):
Expand All @@ -203,6 +215,10 @@ def __init__(self, uid):
nf1 = iterOpenNetcdf(filename, "Trying to get metadata from netcdf template \n", 'r')
for var in nf1.variables:
self.data[var] = {k: v for k, v in iteritems(nf1.variables[var].__dict__) if k != '_FillValue'}
core_dims = get_core_dims(self.data)
self.coords = {}
for dim in core_dims:
self.coords[dim] = nf1.variables[dim][:]
nf1.close()
return
except (KeyError, IOError, IndexError, Exception):
Expand All @@ -212,6 +228,10 @@ def __init__(self, uid):
nf1 = iterOpenNetcdf(filename, "Trying to get metadata from E0 maps \n", 'r')
for var in nf1.variables:
self.data[var] = {k: v for k, v in iteritems(nf1.variables[var].__dict__) if k != '_FillValue'}
core_dims = get_core_dims(self.data)
self.coords = {}
for dim in core_dims:
self.coords[dim] = nf1.variables[dim]
nf1.close()

@nine
Expand Down

0 comments on commit 4d1f2ed

Please sign in to comment.