cerfacs-globc/icclim

InvalidIcclimArgumentError refers to non-existent list_indices function

DamienIrving opened this issue · 3 comments

  • icclim version: 5.2.1
  • Python version: 3.10.4

Description

If I try and run icclim with an incorrect index name I get the following error:

import icclim

icclim.index(in_files=pr_files, index_name='hello', var_name='pr)
InvalidIcclimArgumentError                Traceback (most recent call last)
Input In [57], in <cell line: 1>()
----> 1 icclim.index(in_files=pr_files, index_name='hello', var_name='pr')

File /g/data/xv83/dbi599/miniconda3/envs/icclim/lib/python3.10/site-packages/icclim/main.py:237, in index(in_files, index_name, var_name, slice_mode, time_range, out_file, threshold, callback, callback_percentage_start_value, callback_percentage_total, base_period_time_range, window_width, only_leap_years, ignore_Feb29th, interpolation, out_unit, netcdf_version, user_index, save_percentile, logs_verbosity, indice_name, user_indice, transfer_limit_Mbytes)
    235 index: EcadIndex | None
    236 if index_name is not None:
--> 237     index = EcadIndex.lookup(index_name)
    238 else:
    239     index = None

File /g/data/xv83/dbi599/miniconda3/envs/icclim/lib/python3.10/site-packages/icclim/models/ecad_indices.py:462, in EcadIndex.lookup(query)
    460     if e.short_name.upper() == query.upper():
    461         return e
--> 462 raise InvalidIcclimArgumentError(
    463     f"Unknown index {query}."
    464     f" You can list the available indices"
    465     f" with `icclim.list_indices()`."
    466 )

InvalidIcclimArgumentError: 'Unknown index hello. You can list the available indices with `icclim.list_indices()`.'

As far as I can tell icclim.list_indices() was recently removed (37f88c3), so I was wondering how to go about generating a list of the available indices?

bzah commented

Ah oops, I forgot to update the error message, sorry for making it misleading.
icclim.list_indices() was removed from the public API in favour of EcadIndex.list().
In icclim 5.2 you can do:

from icclim.models.ecad_indices import EcadIndex

EcadIndex.list()

The output format is group | short_name | definition .

Alternatively, you can iterate over EcadIndex:

for index in EcadIndex:
   print(index.short_name)

However, we will soon add new indices which are not part of ECAD specification, so the above wont be sufficient.
We should probably re-add list_indices() to list all indices, especially if it's useful to some users.

Thanks - EcadIndex.list() is exactly what I need.

In my instance the use case is that I'm writing a command line program for calculating climate indices using icclim. The user specifies which index they want to calculate as a command line argument so I needed a list of metrics to pass as choices to argparse.

bzah commented

@DamienIrving I'm very happy to see icclim being used but, just so you know, there is CLI included in xclim[1].
I never used it so I'm not exactly sure what you can/can't do with it though.
Nowadays, icclim use xclim as an engine to compute indices, so the results should be identical.

[1] https://xclim.readthedocs.io/en/latest/notebooks/cli.html