CDAT / cdutil

Climate Utilities for CDAT
3 stars 2 forks source link

cdutil.ANNUALCYCLE.climatology() failed in cdutil 3.1.0 #44

Open chengzhuzhang opened 5 years ago

chengzhuzhang commented 5 years ago

As discussed in an email with @doutriaux1 and @dnadeau4 the same function works in cdutil 2.10, while failed in cdutil 3.1.0

Below is the code:

import cdms2
import cdutil

filename = 'test_data_amip_r1i1p1_mo_regrid_3x3_sgp.nc'
fin = cdms2.open(filename)
var = fin('tas')
cdutil.setTimeBoundsMonthly(var)
var_season = cdutil.ANNUALCYCLE.climatology(var).squeeze()

Below is the error:

Traceback (most recent call last):
  File "cdutil_test.py", line 8, in <module>
    var_season = cdutil.ANNUALCYCLE.climatology(var).squeeze()
  File "/Users/zhang40/anaconda2/envs/arm_diags_env_0207_1/lib/python2.7/site-packages/cdms2/avariable.py", line 1706, in squeeze
    return(MV.squeeze(self))
  File "/Users/zhang40/anaconda2/envs/arm_diags_env_0207_1/lib/python2.7/site-packages/cdms2/MV2.py", line 352, in squeeze
    maresult, axes=axes, attributes=attributes, grid=grid, id=id)
  File "/Users/zhang40/anaconda2/envs/arm_diags_env_0207_1/lib/python2.7/site-packages/cdms2/tvariable.py", line 203, in __init__
    self.initDomain(axes, copyaxes=copyaxes)
  File "/Users/zhang40/anaconda2/envs/arm_diags_env_0207_1/lib/python2.7/site-packages/cdms2/tvariable.py", line 357, in initDomain
    raise CDMSError("Wrong number of axes to initialize domain.")
cdms2.error.CDMSError: Wrong number of axes to initialize domain.
chengzhuzhang commented 5 years ago

test_data_amip_r1i1p1_mo_regrid_3x3_sgp.nc.zip

Here is the data used for testing

lee1043 commented 5 years ago

@chengzhuzhang it looks like the error is coming from using squeeze in-correctly. Please try replacing last line of your code as below: var_season = cdutil.ANNUALCYCLE.climatology(var)(squeeze=1)

lee1043 commented 5 years ago

var.shape --> (324, 1, 1): monthly time series on one grid (or area-averaged) ann = cdutil.ANNUALCYCLE.climatology(var) ann.shape --> (12, 1, 1): annual cycle of var var_season = cdutil.ANNUALCYCLE.climatology(var)(squeeze=1) var_season.shape --> (12,): annual cycle of var, lat/lon dimensions removed

chengzhuzhang commented 5 years ago

Thank you @lee1043. I talked to @dnadeau4 , it seems like using asma() also works. My question is, since the same line worked for an older version cdat2.10. Is it because cdat3.10 dropped support for squeeze() ?

doutriaux1 commented 5 years ago

@chengzhuzhang you're right it might needs a cleanup at the cdms level. @dnadeau4 what do you think?

lee1043 commented 5 years ago

@chengzhuzhang in my understanding asma returns numpy array instead of transient variable; so you lose coordinate information. I think it might be still useful to keep var type as transient variable.

>>> type(var)
<class 'cdms2.tvariable.TransientVariable'>
>>> var_season = cdutil.ANNUALCYCLE.climatology(var).asma()
>>> var_season.shape
(12, 1, 1)
>>> type(var_season)
<class 'numpy.ma.core.MaskedArray'>
lee1043 commented 5 years ago

I agree usage of squeeze seems not very clear in the current documents, for example: https://cdms.readthedocs.io/en/latest/search.html?q=squeeze&check_keywords=yes&area=default https://cdms.readthedocs.io/en/latest/generated/MV2.squeeze.html?highlight=squeeze

durack1 commented 5 years ago

@doutriaux1 this could be a candidate for migration into the https://github.com/CDAT/cdutil issue list

github-actions[bot] commented 4 years ago

Marking issue as stale, since there has been no activity in 30 days.

Unless the issue is updated or the 'stale' tag is removed, this issue will be closed in 7 days.

durack1 commented 4 years ago

@jasonb5 I think this is still a valid bug? no