Skip to content

Commit

Permalink
Change functionality of read_array so it returns multiple variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Suizer98 committed Jan 27, 2024
1 parent 5dce3c5 commit da96e47
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions nco/nco.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,20 @@ 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 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 = {}

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

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

0 comments on commit da96e47

Please sign in to comment.