Skip to content

Commit a53f436

Browse files
author
haileyajohnson
committed
f order array support
1 parent 73f29bb commit a53f436

790 files changed

Lines changed: 179 additions & 70 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cdm/core/src/main/java/ucar/nc2/iosp/IospHelper.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public static Object readData(LayoutBB layout, DataType dataType, Object arr) {
301301
if (showLayoutTypes)
302302
System.out.println("***BB LayoutType=" + layout.getClass().getName());
303303

304-
if (dataType.getPrimitiveClassType() == byte.class || (dataType == DataType.CHAR)) {
304+
if (dataType.getPrimitiveClassType() == byte.class || (dataType == DataType.CHAR) || dataType == DataType.BOOLEAN) {
305305
byte[] pa = (byte[]) arr;
306306
while (layout.hasNext()) {
307307
LayoutBB.Chunk chunk = layout.next();
@@ -312,8 +312,12 @@ public static Object readData(LayoutBB layout, DataType dataType, Object arr) {
312312
pa[pos++] = bb.get();
313313
}
314314
// return (dataType == DataType.CHAR) ? convertByteToChar(pa) : pa;
315-
if (dataType == DataType.CHAR)
315+
if (dataType == DataType.CHAR) {
316316
return convertByteToChar(pa);
317+
}
318+
else if (dataType == DataType.BOOLEAN) {
319+
return convertByteToBoolean(pa);
320+
}
317321
else
318322
return pa;
319323

@@ -389,6 +393,23 @@ public static Object readData(LayoutBB layout, DataType dataType, Object arr) {
389393
pa[pos++] = bb.get();
390394
}
391395
return pa;
396+
} else if (dataType == DataType.STRING) {
397+
String[] pa = (String[]) arr;
398+
int recsize = layout.getElemSize();
399+
while (layout.hasNext()) {
400+
LayoutBB.Chunk chunk = layout.next();
401+
ByteBuffer bb = chunk.getByteBuffer();
402+
bb.position(chunk.getSrcElem() * recsize);
403+
int pos = (int) chunk.getDestElem();
404+
for (int i = 0; i < chunk.getNelems(); i++) {
405+
char[] ch = new char[dataType.getSize()];
406+
for ( int j = 0; j < ch.length; j++) {
407+
ch[j] = (char)bb.get();
408+
}
409+
pa[pos++] = new String(ch);
410+
}
411+
}
412+
return pa;
392413
}
393414

394415
throw new IllegalStateException();
@@ -666,9 +687,15 @@ public static Object makePrimitiveArray(int size, DataType dataType) {
666687
*/
667688
public static Object makePrimitiveArray(int size, DataType dataType, Object fillValue) {
668689

669-
if (dataType.getPrimitiveClassType() == byte.class || (dataType == DataType.CHAR)) {
690+
if (dataType.getPrimitiveClassType() == byte.class || (dataType == DataType.CHAR) || dataType == DataType.BOOLEAN) {
670691
byte[] pa = new byte[size];
671-
byte val = ((Number) fillValue).byteValue();
692+
byte val;
693+
if (dataType == DataType.CHAR) {
694+
byte[] bytes = ((String) fillValue).getBytes();
695+
val = bytes.length > 0 ? ((String) fillValue).getBytes()[0] : 0;
696+
} else {
697+
val = ((Number) fillValue).byteValue();
698+
}
672699
if (val != 0)
673700
for (int i = 0; i < size; i++)
674701
pa[i] = val;
@@ -777,6 +804,17 @@ public static byte[] convertCharToByte(char[] from) {
777804
return to;
778805
}
779806

807+
public static boolean[] convertByteToBoolean(byte[] from) {
808+
boolean[] to = null;
809+
if (from != null) {
810+
int size = from.length;
811+
to = new boolean[size];
812+
for (int i = 0; i < size; i++)
813+
to[i] = from[i] != 0;
814+
}
815+
return to;
816+
}
817+
780818
public static long transferData(Array result, WritableByteChannel channel) throws java.io.IOException {
781819

782820
// LOOK should we buffer ??

cdm/zarr/src/main/java/ucar/nc2/iosp/zarr/ZArray.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public enum Order {
6060
// .zarray fields
6161
private final int[] shape;
6262
private final int[] chunks;
63-
private final Number fillValue;
63+
private final Object fillValue;
6464
private final DataType datatype;
6565
private final String dtype;
6666
private final ZarrFilter compressor;
@@ -69,7 +69,7 @@ public enum Order {
6969
private final List<ZarrFilter> filters;
7070
private final String separator;
7171

72-
public ZArray(int[] shape, int[] chunks, Number fill_value, String dtype, ZarrFilter compressor, String order,
72+
public ZArray(int[] shape, int[] chunks, Object fill_value, String dtype, ZarrFilter compressor, String order,
7373
List<ZarrFilter> filters, String separator) throws ZarrFormatException {
7474
this.shape = shape;
7575
this.chunks = chunks;
@@ -99,7 +99,7 @@ public List<ZarrFilter> getFilters() {
9999
return this.filters;
100100
}
101101

102-
public Number getFillValue() {
102+
public Object getFillValue() {
103103
return fillValue;
104104
}
105105

@@ -176,13 +176,15 @@ public ZArray deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
176176
.mapToInt(JsonNode::asInt).toArray();
177177
String dtype = ((JsonNode) root.path(ZarrKeys.DTYPE)).asText();
178178
JsonNode fillValueNode = (JsonNode) root.path(ZarrKeys.FILL_VALUE);
179-
final Number fill;
179+
final Object fill;
180180
if (fillValueNode.isLong()) {
181181
fill = fillValueNode.longValue();
182182
} else if (fillValueNode.isFloat()) {
183183
fill = fillValueNode.floatValue();
184-
} else {
184+
} else if (fillValueNode.isNumber()){
185185
fill = fillValueNode.asDouble();
186+
} else {
187+
fill = fillValueNode.asText("");
186188
}
187189

188190
String order = ((JsonNode) root.path(ZarrKeys.ORDER)).asText();

cdm/zarr/src/main/java/ucar/nc2/iosp/zarr/ZarrHeader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private void makeGroup(RandomAccessDirectoryItem item) throws ZarrFormatExceptio
9999
// make new Group
100100
Group.Builder group = Group.builder();
101101
String location = ZarrPathUtils.trimLocation(item.getLocation());
102-
if (location.equals(this.rootLocation + ZarrKeys.ZGROUP)) {
102+
if (location.equals(this.rootLocation + '/' + ZarrKeys.ZGROUP)) {
103103
group = this.rootGroup;
104104
}
105105
// set Group name
@@ -216,15 +216,15 @@ private Group.Builder findGroup(String location) throws ZarrFormatException {
216216
*/
217217
class VInfo {
218218
private final int[] chunks;
219-
private final Number fillValue;
219+
private final Object fillValue;
220220
private final ZarrFilter compressor;
221221
private final ByteOrder byteOrder;
222222
private final ZArray.Order order;
223223
private final String separator;
224224
private final List<ZarrFilter> filters;
225225
private final long offset;
226226

227-
VInfo(int[] chunks, Number fillValue, ZarrFilter compressor, ByteOrder byteOrder, ZArray.Order order,
227+
VInfo(int[] chunks, Object fillValue, ZarrFilter compressor, ByteOrder byteOrder, ZArray.Order order,
228228
String separator, List<ZarrFilter> filters, long offset) {
229229
this.chunks = chunks;
230230
this.fillValue = fillValue;
@@ -240,7 +240,7 @@ public int[] getChunks() {
240240
return this.chunks;
241241
}
242242

243-
public Number getFillValue() {
243+
public Object getFillValue() {
244244
return this.fillValue;
245245
}
246246

cdm/zarr/src/main/java/ucar/nc2/iosp/zarr/ZarrIosp.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ public Array readData(Variable v2, Section section) throws IOException, InvalidR
7474
long offset = vinfo.getOffset();
7575
DataType dataType = v2.getDataType();
7676

77-
// if data is uninitialized, return array with fill value
78-
if (!checkIsDataFile(offset)) {
79-
Object pa = IospHelper.makePrimitiveArray((int) section.computeSize(), dataType, vinfo.getFillValue());
80-
if (dataType == DataType.CHAR)
81-
pa = IospHelper.convertByteToChar((byte[]) pa);
82-
return Array.factory(dataType, section.getShape(), pa);
83-
}
77+
// // if data is uninitialized, return array with fill value
78+
// if (!checkIsDataFile(offset)) {
79+
// Object pa = IospHelper.makePrimitiveArray((int) section.computeSize(), dataType, vinfo.getFillValue());
80+
// if (dataType == DataType.CHAR)
81+
// pa = IospHelper.convertByteToChar((byte[]) pa);
82+
// return Array.factory(dataType, section.getShape(), pa);
83+
// }
8484

8585
// create layout object
8686
Layout layout = new ZarrLayoutBB(v2, section, this.raf);
@@ -96,19 +96,4 @@ public Array readData(Variable v2, Section section) throws IOException, InvalidR
9696

9797
return array;
9898
}
99-
100-
/**
101-
* Checks whether the files containing @pos is a datafile based on filename.
102-
* Any file that is not a known metadata file is assumed to be a datafile.
103-
*/
104-
private boolean checkIsDataFile(long pos) throws IOException {
105-
try {
106-
this.raf.seek(pos);
107-
} catch (EOFException eof) {
108-
return false;
109-
}
110-
String filename = ZarrPathUtils.trimLocation(((RandomAccessDirectory) this.raf).getCurrentFile().getLocation());
111-
return !(filename.endsWith(ZarrKeys.ZGROUP) || filename.endsWith(ZarrKeys.ZARRAY)
112-
|| filename.endsWith(ZarrKeys.ZATTRS));
113-
}
11499
}

cdm/zarr/src/main/java/ucar/nc2/iosp/zarr/ZarrLayoutBB.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class ZarrLayoutBB implements LayoutBB {
3232
private int nBytes; // number of bytes per chunk
3333
private int totalNChunks; // total number of chunks
3434
private int totalChunkSize; // total number of elements per chunk
35+
private boolean F_order = false;
3536

3637
// bytes representing compressing
3738
private static final int ZARR_COMPRESSOR_OFFSET = 16;
@@ -52,6 +53,7 @@ public ZarrLayoutBB(Variable v2, Section wantSection, RandomAccessFile raf) thro
5253

5354
// transpose Section and chunk if F order
5455
if (vinfo.getOrder() == ZArray.Order.F) {
56+
this.F_order = true;
5557
List<Range> ranges = wantSection.getRanges();
5658
List<Range> transpose = new ArrayList<>();
5759
int[] temp = new int[ndims];
@@ -119,19 +121,39 @@ public boolean hasNext() {
119121

120122
public LayoutBBTiled.DataChunk next() {
121123
// TODO: handle uninitialized chunks
122-
DataChunk chunk = new ZarrLayoutBB.DataChunk(this.currChunk, totalChunkSize, this.chunkNum);
124+
DataChunk chunk = new ZarrLayoutBB.DataChunk(this.currChunk, this.chunkNum);
123125
incrementChunk();
124126
return chunk;
125127
}
126128

127129
private void incrementChunk() {
128-
this.chunkNum++;
129-
int i = this.currChunk.length - 1;
130-
while (this.currChunk[i] + 1 >= nChunks[i] && i > 0) {
131-
this.currChunk[i] = 0;
132-
i--;
130+
//this.chunkNum++;
131+
int i;
132+
if (F_order) {
133+
i = 0;
134+
while (this.currChunk[i] + 1 >= nChunks[i] && i < this.currChunk.length-1) {
135+
this.currChunk[i] = 0;
136+
i++;
137+
}
138+
} else {
139+
i = this.currChunk.length - 1;
140+
while (this.currChunk[i] + 1 >= nChunks[i] && i > 0) {
141+
this.currChunk[i] = 0;
142+
i--;
143+
}
133144
}
134145
this.currChunk[i]++;
146+
this.chunkNum = indicesToChunkNum();
147+
}
148+
149+
private int indicesToChunkNum() {
150+
int num = 0;
151+
int sz = 1;
152+
for (int i = chunkSize.length-1; i >= 0; i--) {
153+
num += sz * this.currChunk[i];
154+
sz *= nChunks[i];
155+
}
156+
return num;
135157
}
136158
}
137159

@@ -140,7 +162,7 @@ private class DataChunk implements LayoutBBTiled.DataChunk {
140162
private int[] offset; // start indices of chunk in elements
141163
private long rafOffset; // start position of chunk in bytes
142164

143-
DataChunk(int[] index, int totalChunkSize, int chunkNum) {
165+
DataChunk(int[] index, int chunkNum) {
144166
this.offset = new int[index.length];
145167
this.rafOffset = varOffset + (chunkNum * (nBytes + data_bytes_offset)) + data_bytes_offset;
146168
for (int i = 0; i < index.length; i++) {

cdm/zarr/src/main/java/ucar/unidata/io/zarr/RandomAccessDirectory.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,20 @@ private static List<MFile> sortIterator(Iterator<MFile> mfiles) {
8181
}
8282

8383
/**
84-
* @return RandomAccessFile containing the current file pointer
84+
* Return the directory item containing the specified position
85+
* @param pos
86+
* @return Directory item containing bytes at `pos`
8587
*/
86-
public RandomAccessFile getCurrentFile() {
87-
return this.currentFile;
88+
public RandomAccessDirectoryItem getFileAtPos(int pos) {
89+
long tempPos = 0;
90+
for (RandomAccessDirectoryItem item : this.children) {
91+
long rafLength = item.length();
92+
if (tempPos + rafLength > pos) {
93+
return item;
94+
}
95+
tempPos += rafLength;
96+
}
97+
return null;
8898
}
8999

90100
/**
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)