Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subsetting cdms2 variables using arbitrary indices #55

Open
chaosphere2112 opened this issue Nov 22, 2016 · 0 comments
Open

Subsetting cdms2 variables using arbitrary indices #55

chaosphere2112 opened this issue Nov 22, 2016 · 0 comments
Assignees
Milestone

Comments

@chaosphere2112
Copy link
Contributor

chaosphere2112 commented Nov 22, 2016

It would be great to be able to subset a cdms2 transientVariable using arbitrary indices - similar to the numpy.take function but applicable to multiple dimensions concurrently, so the 14 or so lines below could be condensed into something much more compact:

climInfile = '/work/durack1/Shared/obs_data/WOA13/141027_woa/woa13_decav_t00_01.nc'
cF = cdm.open(climInfile)
climDepths = cF.getAxis('depth').getData()
anomDepths = uF.getAxis('level').getData()
commonLevels = np.intersect1d(climDepths,anomDepths)
clim = cF('t_an') ; #,depth=commonLevels) - it would be great to just load the indices I want!
inds = []
for x,val in enumerate(commonLevels):
    inds.append(np.where(climDepths==commonLevels[x])[0][0])
climTrim = np.take(clim,inds,axis=1)
climTrim = cdm.createVariable(climTrim,id='climTrim',missing_value=1e20)
climTrim.setAxis(0,clim.getAxis(0))
climTrim.setAxis(1,uF.getAxis('level'))
climTrim.setAxis(2,uF.getAxis('latitude'))
climTrim.setAxis(3,uF.getAxis('longitude'))
del(clim,inds,x,val,commonLevels,anomDepths,climDepths,climInfile) ; gc.collect()
cF.close()

In the case above, uF contains a subset of the depths found in the cF file which is loaded above.

Ideally it would be great if this worked not only in index space, but also in value/data space so for e.g. in the above target levels could be specified rather than the indices captured in the variable inds.

So for an example what I want is:

clim.shape
(1, 102, 180, 360)
climTrim = clim[:,[0,5,10,20,50,100],:,:] ; an example using index values,
or using the variable above
climTrim = clim[:,inds,:,:] ; a list (or equivalent type) variable that contains the indexes,
or alternatively the axis values (rather than index values):
depths = [0,5,10,20,50,100,values=True] in whatever native units the coordinate is stored

@doutriaux1 can you add the cdms2 and enhancement labels please - and a milestone linked to existing issue CDAT/cdat#885 and PR CDAT/cdat#915

Migrated from: CDAT/cdat#1288

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants