larray-project / larray

N-dimensional labelled arrays in Python
https://larray.readthedocs.io/
GNU General Public License v3.0
8 stars 6 forks source link

import from iode #1032

Open gdementen opened 1 year ago

gdementen commented 1 year ago

Should implement both read_av and read_var. See also #503 for export.

For read_av, see demo_python...\demo_code\util\util.py:du_iode_to_larray

Here is the latest version I used for read_var:

def read_var(fpath, variables=None):
    if isinstance(fpath, Path):
        fpath = str(fpath)
    if not os.path.exists(fpath):
        raise FileNotFoundError(fpath)
    if variables is None:
        arr = iode.wsloadvarpy(fpath)
        time_labels = arr.time.labels
        int_time_labels = time_labels.astype(int)
        if (time_labels == int_time_labels).all():
            arr = arr.set_labels('time', int_time_labels)
        return arr.rename('vars', 'variable')
    else:
        if not isinstance(variables, (tuple, list)):
            raise ValueError("variables argument should be either None or a tuple/list")

        iode.wsloadvar(fpath)
        sample = iode.samplelist()
        int_sample = sample.astype(int)
        if (sample == int_sample).all():
            sample = int_sample
        time = Axis(sample, 'time')
        variable = Axis(variables, 'variable')
        return stack({k: Array(iode.getvector(k), time) for k in variables}, variable).T

PS: the iode module should really be improved, especially concerning its documentation.