[netcdf-java] Removing a dimension from a variable
Ethan Davis
edavis at unidata.ucar.edu
Wed Nov 21 13:32:21 MST 2007
Hi Chris,
I think it fails because the grid.makeSubset() creates a Variable that
is a "section" view of the underlying grid variable. In other words, it
isn't a fully contained new Variable, any data access on the new
Variable reads from the underlying Variable. Our code doesn't allow a
slice to be made on an already sectioned Variable. Instead it throws the
exception you are seeing.
Your Array stuff works because you are actually reading the data into
memory and creating a new Variable.
There may be a better way to deal with this. But I'm going to let John
Caron, the author of most of this code, answer that question. I think he
is back in the office next week.
Sorry I don't have a better answer for you,
Ethan
Christopher Mueller wrote:
> Hi Ethan,
>
> Thanks for the response. I actually tried Variable.slice() but it throws an
> invalid range exception (below):
>
> ucar.ma2.InvalidRangeException: Variable.slice: cannot slice a section
> at ucar.nc2.Variable.makeSlice(Variable.java:475)
> at ucar.nc2.dataset.VariableDS.slice(VariableDS.java:98)
>
> I should probably mention that I'm obtaining the variable from a
> GridDatatype ("grid" below) using GridDatatype.getVariable(). I know that
> the index I'm trying to use in the slice is correct (levelRange is 1 value -
> so there's only 1 value for the level dimension).
>
> I have come up with a way of doing what I needed to do, but I'm not sure if
> it's really a "good" way of doing it. It runs fine (and dumps the 1-length
> dimension) perfectly using the code below. The "Variable sliceV..." line is
> where it crashes (I comment it out when it works properly).
>
>
> if((llbb != null) || (timeRange != null) || (stride_h >= 1))
> grid = grid.makeSubset(timeRange, levelRange, llbb, 1, stride_h, stride_h);
>
> Variable gridV = (Variable) grid.getVariable();
> Variable sliceV = gridV.slice(gridV.findDimensionIndex(levelName), 0);
>
> Array arrV = gridV.read().reduce(gridV.findDimensionIndex(levelName));
>
> Variable newGridV = new Variable(gds.getNetcdfFile(), null, null,
> gridV.getShortName());
>
> newGridV.setDataType(gridV.getDataType());
>
> for(Attribute a : (List<Attribute>)gridV.getAttributes()){
> newGridV.addAttribute(a); }
> Dimension levelD = gridV.getDimension(gridV.findDimensionIndex(levelName));
>
> List<Dimension> dims = gridV.getDimensions();
>
> if(levelD != null){ dims.remove(levelD); }
>
> newGridV.setDimensions(dims);
> newGridV.setCachedData(arrV, false);
>
>
> Does this look like a reasonable way of doing it? Any idea why I'm getting
> the error when I try to slice? That would be the preferred way in my mind -
> it seems more flexible (I can do it to any of the dimensions easily)...
>
> Thanks,
>
> Best,
>
> Chris
>
--
Ethan R. Davis Telephone: (303) 497-8155
Software Engineer Fax: (303) 497-8690
UCAR Unidata Program Center E-mail: edavis at ucar.edu
P.O. Box 3000
Boulder, CO 80307-3000 http://www.unidata.ucar.edu/
---------------------------------------------------------------------------
More information about the netcdf-java
mailing list