Re: [netcdf-java] Removing a dimension from a variable

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


On 11/21/07 1:09 PM, "Ethan Davis" <edavis@xxxxxxxxxxxxxxxx> wrote:

> Hi Christopher,
> 
> Christopher Mueller wrote: I am subsetting an opendap nc file to a local nc
> file and I¹m running into a bit of a problem. After subsetting, the variable
> of interest has 4 dimensions (x, y, level, record) however there is only 1
> level. I would like to remove the level dimension as it is unnecessary and
> causes some problems later in the application. Is there a way to do this? I¹ve
> tried removing the dimension from the ³root group² of the NetcdfWritable file
> with no luck. I¹ve also tried iterating through the dimensions
> (var.getDimensions().remove(i)) and removing the one with length = 1, also no
> luck.
> 
>> Has anyone done this before? Is it possible, or do I need to extract the
>> data, reduce the resulting array, and then recreate the variable by hand?
>> 
> I haven't tried this but you can create a new Variable without the level
> dimension by calling Variable.slice() on the existing Variable and specifying
> the level dimension. Then, before you call create() on your
> NetcdfFileWritable, you can remove the old Variable and add the new one.
> 
> Hope that helps,
> 
> Ethan
> 

-- 
Christopher Mueller | Scientist
Applied Science Associates
70 Dean Knauss Drive | Narragansett, RI 02882 USA
p: +1 401-789-6224 | f: +1 401-789-1932
e: cmueller@xxxxxxxxxxxxxx | www.asascience.com

ASA | Science. Services. Solutions

Imagination is everything.  It is the preview of life's coming attractions.
- Albert Einstein

Whether you think you can or think you can't, either way you are right. -
Henry Ford



  • 2007 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: