Skip to content

Subsetting cdms2 variables using arbitrary indices #55

@chaosphere2112

Description

@chaosphere2112

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions