And I have one more problem:
So here's the deal, I can't have all the data that I'm going to write in
memory, So I'm writing each value 1 by 1 in this way:
I call addValue with the index (which is the n-dimensional position), the
value and the variable.
this works great for numbers:
private void addDoubleValue4d(int[] index, Double value, Variable var)
throws IOException, InvalidRangeException {
Preconditions.checkArgument(index.length==4);
//Shape for 1 value in a 4 dimensional space
ArrayDouble arrAmp = new ArrayDouble(new int[]{1, 1, 1, 1});
arrAmp.setDouble(0, value);
nc.write(var.getName(), index, arrAmp);
}
Now, when it comes to String values I would expect it to work the same way:
private void addStringValue2d(int[] index, String value, Variable var)
throws IOException, InvalidRangeException {
Preconditions.checkArgument(index.length==2);
//Shape for 1 value in a 2 dimensional space
ArrayObject arrAmp = new ArrayObject(String.class,new int[]{1, 1});
arrAmp.setObject(0, value);
nc.writeStringData(var.getName(), index, arrAmp);
}
But writeStringData gets he shape of the var (4x2) and then tries to access
an illegal position (2).
It seems that the problem is that strlen in writeStringData is not computed
with Math.min(array.getSize(), v2.shape[rank -1])
int strlen = v2.shape[rank-1];
// turn it into an ArrayChar
ArrayChar cvalues = ArrayChar.makeFromStringArray((ArrayObject) values, strlen);
I would have made the change on my own but there is no test suit (at least I
couldn't find it) so I'm not sure of the impact of such a change.
On Tue, Mar 3, 2009 at 12:13 PM, Fernando O. <fotero@xxxxxxxxx> wrote:
> Hi all
> I'm having this problem: I'm trying to write a string data, so I tried
> what appears in this example:
> http://www.unidata.ucar.edu/support/help/MailArchives/netcdf/msg02393.html
>
>
> Basically: I create an ArrayChar and then write it with nc.writeStringData,
> but I get an exception: Must be of type String
>
> if (values.getElementType() != String.class)
> throw new IllegalArgumentException("Must be ArrayObject of String ");
>
>
> I took a look into the code and ArrayChar returns char:
> The code for getElementType() is:
>
> /** Return the element class type */
> public Class getElementType() { return char.class; }
>
> So, I tried to use ArrayObject ...
>
> java.lang.IllegalArgumentException: variable INSI is not type CHAR
> this is because of this:
>
> if (v2.getDataType() != DataType.CHAR)
> throw new IllegalArgumentException("variable " + varName + " is not
> type CHAR");
>
>
> Ok, I had the var created with dataType.String, So I changed it to Char
>
> My question is: is this ok? should I create a var of type char then create
> an ArrayObject of Strings and then write that using writeStringData?
>
>