Skip to content

Commit daba3f0

Browse files
committed
allow (and test) xarray data structures as input and output
1 parent 5e93d2f commit daba3f0

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

python/cdo/cdo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def setupLogging(logFile):
9494
return logger
9595
# }}}
9696

97-
# extra execptions for CDO {{{
97+
# extra exceptions for CDO {{{
9898

9999
class CDOException(Exception):
100100

@@ -452,7 +452,7 @@ def __call__(self, *args, **kwargs):
452452
cmd.append(' '.join(kwargs["input"]))
453453
elif self.hasXarray:
454454
import xarray # <<-- python2 workaround
455-
if type(kwargs["input"]) == xarray.core.dataset.Dataset:
455+
if type(kwargs["input"]) in [xarray.core.dataset.Dataset,xarray.core.dataarray.DataArray]:
456456
# create a temp nc file from input data
457457
tmpfile = self.tempStore.newFile()
458458
kwargs["input"].to_netcdf(tmpfile)

python/test/test_cdo.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,15 @@ def test_xarray_input(self):
460460

461461
xarrayFile.close()
462462

463+
def test_xarray_input_and_output(self):
464+
cdo = Cdo()
465+
ifile = cdo.setcalendar('360_day', input=' -settaxis,1800-01-01,12:00:00,1months -seq,1,10000',options='-f nc -r')
466+
(var,) = cdo.showname(input=ifile)
467+
self.assertEqual('seq',var)
468+
x = cdo.selyear('1985/2100', input=ifile, returnXArray=var)
469+
y = cdo.yearsum(input=x, returnXArray=var)
470+
self.assertEqual(4059768.0,float(y[:,0,0].sum()))
471+
463472
def test_xarray_output(self):
464473
cdo = Cdo()
465474
try:

0 commit comments

Comments
 (0)