Skip to content

Commit

Permalink
Refactored read_array function to handle both single variable and lis…
Browse files Browse the repository at this point in the history
…t inputs
  • Loading branch information
Suizer98 authored and Sui Zer committed Jan 29, 2024
1 parent da96e47 commit 68a0f96
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 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 @@ -496,20 +496,27 @@ def open_cdf(self, infile):
return file_obj

def read_array(self, infile, var_names):
"""Directly return multiple numpy arrays for given variable names"""
"""Directly return single/multiple numpy arrays for given variable names"""
file_handle = self.read_cdf(infile)
result = {}

for var_name in var_names:
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 data array for each variable
result[var_name] = file_handle.variables[var_name][:]
# return the single data array
return file_handle.variables[var_names][:]
except KeyError:
print("Cannot find variable: {0}".format(var_name))
print("Cannot find variable: {0}".format(var_names))
raise KeyError

return result

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

0 comments on commit 68a0f96

Please sign in to comment.