Skip to content

Enable read_array function to return multiple arrays #77

@Suizer98

Description

@Suizer98

I am modifying the read_array function inside nco.py as below:

   def read_array(self, infile, var_names):
        file_handle = self.read_cdf(infile)

        for var_name in var_names:
            try:
                # return the data array for each variable
                result[var_name] = file_handle.variables[var_name][:]
            except KeyError:
                print("Cannot find variable: {0}".format(var_name))
                raise KeyError

        return result

The usage for my case would be:

variables = ["longitude", "latitude", "time"]
result = nco.ncks(
    input="sample.nc", options=["-v " + ",".join(variables)], returnArray=variables
)

lon = result["longitude"][:]
lat = result["latitude"][:]
...

so we don't have to call returnArray=[] one by one.
I remember the native nco commands extract multiple variables such as:
ncks -v latitudes,longitudes
can support extracting multiple variables from netcdf file.
If this kind of actual feature is already existed, feel free to address how should I use/call it, in case I missed it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions