Skip to content

Commit 48ed689

Browse files
committed
Allow to remove field before loading
1 parent 16aa035 commit 48ed689

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/py_eddy_tracker/observations/observation.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,32 +361,38 @@ def index(self, index):
361361
return eddies
362362

363363
@classmethod
364-
def load_from_netcdf(cls, filename, raw_data=False):
364+
def load_from_netcdf(cls, filename, raw_data=False, remove_vars=None):
365365
array_dim = "NbSample"
366366
if not isinstance(filename, str):
367367
filename = filename.astype(str)
368368
with Dataset(filename) as h_nc:
369+
var_list = list(h_nc.variables.keys())
370+
if remove_vars is not None:
371+
print(var_list)
372+
print(remove_vars)
373+
var_list = [i for i in var_list if i not in remove_vars]
374+
369375
nb_obs = len(h_nc.dimensions[cls.obs_dimension(h_nc)])
370376
logging.debug('%d observations will be load', nb_obs)
371377
kwargs = dict()
372378
if array_dim in h_nc.dimensions:
373379
kwargs["track_array_variables"] = len(h_nc.dimensions[array_dim])
374380
kwargs["array_variables"] = list()
375-
for variable in h_nc.variables:
381+
for variable in var_list:
376382
if array_dim in h_nc.variables[variable].dimensions:
377383
var_inv = VAR_DESCR_inv[variable]
378384
kwargs["array_variables"].append(var_inv)
379385
array_variables = kwargs.get("array_variables", list())
380386
kwargs["track_extra_variables"] = []
381-
for variable in h_nc.variables:
387+
for variable in var_list:
382388
var_inv = VAR_DESCR_inv[variable]
383389
if var_inv == "type_cyc":
384390
continue
385391
if var_inv not in cls.ELEMENTS and var_inv not in array_variables:
386392
kwargs["track_extra_variables"].append(var_inv)
387393
kwargs["raw_data"] = raw_data
388394
eddies = cls(size=nb_obs, **kwargs)
389-
for variable in h_nc.variables:
395+
for variable in var_list:
390396
var_inv = VAR_DESCR_inv[variable]
391397
if var_inv == "type_cyc":
392398
continue
@@ -397,6 +403,8 @@ def load_from_netcdf(cls, filename, raw_data=False):
397403
factor = 1
398404
if not raw_data:
399405
input_unit = getattr(h_nc.variables[variable], 'unit', None)
406+
if input_unit is None:
407+
input_unit = getattr(h_nc.variables[variable], 'units', None)
400408
output_unit = VAR_DESCR[var_inv]['nc_attr'].get('units', None)
401409
if output_unit is not None and input_unit is not None and output_unit != input_unit:
402410
units = UnitRegistry()
@@ -411,14 +419,14 @@ def load_from_netcdf(cls, filename, raw_data=False):
411419
factor = input_unit.to(output_unit).to_tuple()[0]
412420
# If we are able to find a conversion
413421
if factor != 1:
414-
logging.info('%s will be multiply by %f to take care of units', variable, factor)
422+
logging.info('%s will be multiply by %f to take care of units(%s->%s)',
423+
variable, factor, input_unit, output_unit)
415424
if factor != 1:
416425
eddies.obs[var_inv] = h_nc.variables[variable][:] * factor
417426
else:
418427
eddies.obs[var_inv] = h_nc.variables[variable][:]
419428

420-
421-
for variable in h_nc.variables:
429+
for variable in var_list:
422430
var_inv = VAR_DESCR_inv[variable]
423431
if var_inv == "type_cyc":
424432
eddies.sign_type = h_nc.variables[variable][0]

src/scripts/EddySubSetter

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def id_parser():
2424
metavar=('llcrnrlon', 'llcrnrlat', 'urcrnrlon', 'urcrnrlat'),
2525
help='Coordinates of bounding to extract'
2626
)
27+
parser.add_argument('--remove_var', nargs='+', type=str, help='remove all listed variable')
2728
parser.add_argument('-i', '--ids', nargs='+', type=int, help='List of tracks which will be extract')
2829
parser.add_argument('-n', '--no_raw_mode', action='store_true',
2930
help='Uncompress all data, could be create a memory error for huge file, but is safer for extern file of py eddy tracker')
@@ -34,7 +35,10 @@ if __name__ == '__main__':
3435
args = id_parser().parse_args()
3536

3637
# Original dataset
37-
dataset = TrackEddiesObservations.load_from_netcdf(args.filename, raw_data=False if args.no_raw_mode else True)
38+
dataset = TrackEddiesObservations.load_from_netcdf(
39+
args.filename,
40+
raw_data=False if args.no_raw_mode else True,
41+
remove_vars=args.remove_var)
3842

3943
# Select with id
4044
if args.ids is not None:

0 commit comments

Comments
 (0)