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

Thanks Ethan,

I'm certainly interested if there is a better way to do this.  What I have
is working for me, but the "correct" way is always preferable!

Thanks again,
Happy Thanksgiving,

Chris


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

> 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
>>   


-- 
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