Skip to content

Commit

Permalink
Merge pull request #78 from Suizer98/master
Browse files Browse the repository at this point in the history
Change functionality of read_array so it returns multiple variables
  • Loading branch information
czender authored Jan 29, 2024
2 parents 5dce3c5 + 55b0d26 commit 9bed5b5
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions nco/nco.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def get(self, input, **kwargs):
print(self.stdout)
print(self.stderr)
raise NCOException(**retvals)

if return_array:
return self.read_array(output, return_array)
elif return_ma_array:
Expand Down Expand Up @@ -495,15 +495,27 @@ def open_cdf(self, infile):

return file_obj

def read_array(self, infile, var_name):
"""Directly return a numpy array for a given variable name"""
def read_array(self, infile, var_names):
"""Directly return single/multiple numpy arrays for given variable names"""
file_handle = self.read_cdf(infile)
try:
# return the data array
return file_handle.variables[var_name][:]
except KeyError:
print("Cannot find variable: {0}".format(var_name))
raise KeyError
result = {}

if isinstance(var_names, list):
for var_name in var_names:
try:
# return the data arrays 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
else:
try:
# return the single data array
return file_handle.variables[var_names][:]
except KeyError:
print("Cannot find variable: {0}".format(var_names))
raise KeyError

def read_ma_array(self, infile, var_name):
"""Create a masked array based on cdf's FillValue"""
Expand Down

0 comments on commit 9bed5b5

Please sign in to comment.