From 5d21c11965618139274744d96d2f78d49ee9e817 Mon Sep 17 00:00:00 2001 From: ctena Date: Wed, 11 May 2022 16:28:54 +0200 Subject: [PATCH] Added level selection --- nes/load_nes.py | 25 +++++++++---------------- nes/nc_projections/default_nes.py | 20 +++++++++++++++++--- nes/nc_projections/latlon_nes.py | 16 ++++++---------- nes/nc_projections/rotated_nes.py | 9 +++++---- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/nes/load_nes.py b/nes/load_nes.py index e9bdcb1..c5e8240 100644 --- a/nes/load_nes.py +++ b/nes/load_nes.py @@ -9,8 +9,8 @@ from nes.nc_projections.rotated_nes import RotatedNes from nes.nc_projections.latlon_nes import LatLonNes -def open_netcdf(path, comm=None, xarray=False, info=False, - avoid_first_hours=0, avoid_last_hours=0, parallel_method='Y'): +def open_netcdf(path, comm=None, xarray=False, info=False, parallel_method='Y', + avoid_first_hours=0, avoid_last_hours=0, first_level=0, last_level=None): """ Open a netCDF file @@ -52,21 +52,14 @@ def open_netcdf(path, comm=None, xarray=False, info=False, # Rotated if __is_rotated(dataset): - nessy = RotatedNes(comm=comm, - dataset=dataset, - xarray=xarray, - info=info, - avoid_first_hours=avoid_first_hours, - avoid_last_hours=avoid_last_hours, - parallel_method=parallel_method) + nessy = RotatedNes(comm=comm, dataset=dataset, xarray=xarray, info=info, parallel_method=parallel_method, + avoid_first_hours=avoid_first_hours, avoid_last_hours=avoid_last_hours, + first_level=first_level, last_level=last_level + ) else: - nessy = LatLonNes(comm=comm, - dataset=dataset, - xarray=xarray, - info=info, - avoid_first_hours=avoid_first_hours, - avoid_last_hours=avoid_last_hours, - parallel_method=parallel_method) + nessy = LatLonNes(comm=comm, dataset=dataset, xarray=xarray, info=info, parallel_method=parallel_method, + avoid_first_hours=avoid_first_hours, avoid_last_hours=avoid_last_hours, + first_level=first_level, last_level=last_level) return nessy diff --git a/nes/nc_projections/default_nes.py b/nes/nc_projections/default_nes.py index 9ca9c5f..6e4163a 100644 --- a/nes/nc_projections/default_nes.py +++ b/nes/nc_projections/default_nes.py @@ -80,8 +80,8 @@ class Nes(object): _lon_dim : None, tuple Tuple with the name of the dimensions of the Longitude values """ - def __init__(self, comm=None, path=None, info=False, dataset=None, xarray=False, avoid_first_hours=0, - avoid_last_hours=0, parallel_method='Y'): + def __init__(self, comm=None, path=None, info=False, dataset=None, xarray=False, parallel_method='Y', + avoid_first_hours=0, avoid_last_hours=0, first_level=0, last_level=None): """ Initialize the Nes class @@ -118,8 +118,12 @@ class Nes(object): self.print_info = info self.is_xarray = xarray self.__ini_path = path + + # Selecting info self.hours_start = avoid_first_hours self.hours_end = avoid_last_hours + self.first_level = first_level + self.last_level = last_level # NetCDF object if dataset is not None: @@ -358,6 +362,16 @@ class Nes(object): else: raise NotImplementedError("Parallel method '{meth}' is not implemented. Use one of these: {accept}".format( meth=self.parallel_method, accept=['Y'])) + + # Verical levels selection: + axis_limits['z_min'] = self.first_level + if self.last_level == -1 or self.last_level is None: + self.last_level = None + elif self.last_level +1 == len(self._lev['data']): + self.last_level = None + else: + self.last_level += 1 + axis_limits['z_max'] = self.last_level return axis_limits def set_write_axis_limits(self): @@ -780,7 +794,7 @@ class Nes(object): netcdf4-python opened Dataset """ netcdf.createDimension('time', None) - netcdf.createDimension('lev', len(self._lev['data'])) + netcdf.createDimension('lev', len(self.lev['data'])) if self._time_bnds is not None: netcdf.createDimension('time_nv', 2) diff --git a/nes/nc_projections/latlon_nes.py b/nes/nc_projections/latlon_nes.py index 609d801..d5ec6fb 100644 --- a/nes/nc_projections/latlon_nes.py +++ b/nes/nc_projections/latlon_nes.py @@ -18,8 +18,8 @@ class LatLonNes(Nes): Tuple with the name of the dimensions of the Longitude values. ('lon',) for a regular latitude-longitude projection. """ - def __init__(self, comm=None, path=None, info=False, dataset=None, xarray=False, - avoid_first_hours=0, avoid_last_hours=0, parallel_method='Y'): + def __init__(self, comm=None, path=None, info=False, dataset=None, xarray=False, parallel_method='Y', + avoid_first_hours=0, avoid_last_hours=0, first_level=0, last_level=None): """ Initialize the LatLonNes class @@ -43,14 +43,10 @@ class LatLonNes(Nes): Indicates the parallelization method that you want. Default over Y axis accepted values: ['Y', 'T'] """ - super(LatLonNes, self).__init__(comm=comm, - path=path, - info=info, - dataset=dataset, - xarray=xarray, - avoid_first_hours=avoid_first_hours, - avoid_last_hours=avoid_last_hours, - parallel_method=parallel_method) + super(LatLonNes, self).__init__(comm=comm, path=path, info=info, dataset=dataset, + xarray=xarray, parallel_method=parallel_method, + avoid_first_hours=avoid_first_hours, avoid_last_hours=avoid_last_hours, + first_level=first_level, last_level=last_level) self._var_dim = ('lat', 'lon') self._lat_dim = ('lat',) diff --git a/nes/nc_projections/rotated_nes.py b/nes/nc_projections/rotated_nes.py index 812d521..0698405 100644 --- a/nes/nc_projections/rotated_nes.py +++ b/nes/nc_projections/rotated_nes.py @@ -30,8 +30,8 @@ class RotatedNes(Nes): Tuple with the name of the dimensions of the Longitude values. ('rlat', 'rlon') for a rotated projection. """ - def __init__(self, comm=None, path=None, info=False, dataset=None, xarray=False, - avoid_first_hours=0, avoid_last_hours=0, parallel_method='Y'): + def __init__(self, comm=None, path=None, info=False, dataset=None, xarray=False, parallel_method='Y', + avoid_first_hours=0, avoid_last_hours=0, first_level=0, last_level=None): """ Initialize the RotatedNes class @@ -57,8 +57,9 @@ class RotatedNes(Nes): """ super(RotatedNes, self).__init__(comm=comm, path=path, info=info, dataset=dataset, - xarray=xarray, avoid_first_hours=avoid_first_hours, - avoid_last_hours=avoid_last_hours, parallel_method=parallel_method) + xarray=xarray, parallel_method=parallel_method, + avoid_first_hours=avoid_first_hours, avoid_last_hours=avoid_last_hours, + first_level=first_level, last_level=last_level) self._rlat = self._get_coordinate_dimension('rlat') self._rlon = self._get_coordinate_dimension('rlon') -- GitLab