Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

ucar.ma2.Array.factory(DataType type, int[] shape) creates an array of unexpected size when shape is empty #1211

Open
elzbietaa opened this issue Feb 4, 2019 · 6 comments

Comments

@elzbietaa
Copy link

Summary

When creating an Array given an empty shape, the value returned by Array.getSize() is equal to 1 e.g.:

Array array = Array.factory(DataType.INT, new int[0]);

whereas we'd expect it to be zero.

Note 1: whether the DataType version of the method is used, or an overloaded one taking a Class does not matter. Neither does the type - the size is always == 1 for empty shapes.

Note 2: We're using edu.ucar:cmd-4.6.7.

If this behaviour is expected, can you please point out to documentation highliting the reasoning for that (if available).

@lesserwhirls
Copy link
Collaborator

Greetings!

The getSize() method returns the total number of elements in the array, so even though the dimensionality is zero (scalar), there is still one value to be found. However, if you use the getRank() method, you will get 0, as that is the rank of the array.

As an example:

Array scalar = Array.factory(DataType.INT, new int[0]);
Array multidim = Array.factory(DataType.INT, new int[] {3, 4});

System.out.printf("scalar Rank: %d\n", scalar.getRank());
System.out.printf("scalar Number of elements: %d\n", scalar.getSize());
System.out.println();
System.out.printf("multidim Rank: %d\n", multidim.getRank());
System.out.printf("multidim Number of elements: %d\n", multidim.getSize());

would give:

scalar Rank: 0
scalar Number of elements: 1

multidim Rank: 2
multidim Number of elements: 12

@elzbietaa
Copy link
Author

Greetings, thank you for the prompt reply!

So just to clarify: the only way to create an Array of size == 0 is to use a non-empty shape array where the elements' product is equal to 0, e.g.:

Array.factory(DataType.INT, new int[]{0}); ,
Array.factory(DataType.INT, new int[]{0, 0, 0}); , or
Array.factory(DataType.INT, new int[]{2, 0, 0}); ...
?

Side question: do I understand correctly then, that the reported behaviour is not considered a bug but a feature?

Best regards

@lesserwhirls
Copy link
Collaborator

Hmmm, that's a good question. I'm not sure if it even makes sense for netCDF-Java to be able to create a zero sized Array object (an Array with no values), or if that in and of itself is a bug. I know we treat unlimited Dimensions as having a length of 0, and a vlen Dimensions as having a length = -1. Here we are are not talking about Dimension objects, but rather the number of elements in an Array object. @ethanrd - do you have any insight to offer?

@DennisHeimbigner
Copy link
Contributor

It is possible to create a variable with a zero-size dimension using UNLIMITED.
When initially created, the size of the UNLIMITED dimension should be zero. So
the size of the variable should be zero.
e.g.

netcdf foo {
dimensions:
   D = UNLIMITED;
variables:
	double x(D) ;
}

@lesserwhirls
Copy link
Collaborator

A Variable, yes, but an Array?

@DennisHeimbigner
Copy link
Contributor

May be relevant: #1212

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants