|
| 1 | +--- |
| 2 | +title: Reading the Zarr data model |
| 3 | +last_updated: 2021-12-22 |
| 4 | +sidebar: netcdfJavaTutorial_sidebar |
| 5 | +toc: false |
| 6 | +permalink: reading_zarr.html |
| 7 | +--- |
| 8 | +## Zarr |
| 9 | + |
| 10 | +As of version 5.5.1, the netCDF-Java library provides read-only support for the [Zarr v2 data model](https://zarr.readthedocs.io/en/stable/spec/v2.html#){:target="_blank"}. |
| 11 | +Any dataset that adheres to the v2 spec can be read into a `NetcdfFile` object, as long as the following is true: |
| 12 | + |
| 13 | +* all filters and compressors used by the dataset must be known to the netCDF-Java library (see [Filters](reading_zarr.html#filters)) |
| 14 | +* the underlying storage of the dataset must be a directory store, zip store, or object store |
| 15 | + |
| 16 | +### Enabling Zarr support |
| 17 | + |
| 18 | +To use Zarr in the netCDF-Java library, you must include the `cdm-zarr` module in your netCDF-Java build. |
| 19 | +See [here](using_netcdf_java_artifacts.html) for more information on including optional modules. |
| 20 | + |
| 21 | +### How to read a Zarr dataset |
| 22 | + |
| 23 | +Reading a Zarr dataset is syntactically the same as reading a netCDF file: |
| 24 | + |
| 25 | +{% capture rmd %} |
| 26 | +{% includecodeblock netcdf-java&docs/src/test/java/examples/ZarrExamples.java&readZarrStores %} |
| 27 | +{% endcapture %} |
| 28 | +{{ rmd | markdownify }} |
| 29 | + |
| 30 | +If the file is a legal Zarr dataset, the library will map it to a `NetcdfFile` object for reading. |
| 31 | +See [reading CDM files](reading_cdm.html) for more examples on accessing data once the `NetcdfFile` object is returned. |
| 32 | + |
| 33 | +## Filters |
| 34 | + |
| 35 | +As of netCDF-Java version 5.5.1, a `ucar.nc2.filter` package is included, that provides a suite of implemented filters and compressors, |
| 36 | +as well as a mechanism for user-supplied filters. This package is used by both the Zarr and HDF5 IOSPs, and is available for public use. |
| 37 | + |
| 38 | +The current list of filters included natively in the netCDF-Java library is: |
| 39 | + |
| 40 | +* Deflate (zlib) |
| 41 | +* Shuffle |
| 42 | +* 32-bit Checksum (CRC, Fletcher, and Adler) |
| 43 | +* ScaleOffset |
| 44 | + |
| 45 | +This list is still expanding, but if the filter you are looking for is not provided at this time, you are able to provide it yourself |
| 46 | +(See [Implementing a Filter](#implementing-a-filter)) for details.) |
| 47 | + |
| 48 | +### Implementing a Filter |
| 49 | + |
| 50 | +To add a user-supplied `Filter` to the netDF-Java library, you will have to provide two classes: |
| 51 | + |
| 52 | +* A class that extends the `ucar.nc2.filter.Filter` abstract class |
| 53 | +* A class that implements the `ucar.nc2.filter.FilterProvider` interface |
| 54 | + |
| 55 | +Once implemented, you will need to include these classes as JAR files in your classpath. See [here](runtime_loading.html) for more information. |
| 56 | + |
| 57 | +#### `Filter` implementation |
| 58 | + |
| 59 | +To implement a user-supplied filter, you will need to extend the abstract `ucar.nc2.filter.Filter` class, and provide implementations for the following methods: |
| 60 | +* `encode` takes a `byte[]` of unfiltered data and returns a `byte[]` of filtered data |
| 61 | +* `decode` takes a `byte[]` of filtered data and returns a `byte[]` of unfiltered data |
| 62 | + |
| 63 | +Your `Filter` class should look something like this: |
| 64 | + |
| 65 | +{% capture rmd %} |
| 66 | +{% includecodeblock netcdf-java&docs/src/test/java/examples/ZarrExamples.java&implementFilter %} |
| 67 | +{% endcapture %} |
| 68 | +{{ rmd | markdownify }} |
| 69 | + |
| 70 | +#### `FilterProvider` implementation |
| 71 | + |
| 72 | +For the netCDF-Java library to find your `Filter` implementation, you will need to provide a `FilterProvider` as well. |
| 73 | + |
| 74 | +{% capture rmd %} |
| 75 | +{% includecodeblock netcdf-java&docs/src/test/java/examples/ZarrExamples.java&implementFilterProvider %} |
| 76 | +{% endcapture %} |
| 77 | +{{ rmd | markdownify }} |
| 78 | + |
| 79 | +{%include note.html content=" |
| 80 | +`getName` and `getId` |
| 81 | +When reading data, [IOSPs](writing_iosp.html) can look up filters by either a `String` or `int` (name or id). Currently, the `ucar.nc2.filter` |
| 82 | +package is shared by the Zarr and HDF5 IOSPs. The Zarr IOSP looks up filters by name; if you plan to use your third party filter with Zarr data, |
| 83 | +the string returned by `getName` should match that specified by the [NumCodecs](https://numcodecs.readthedocs.io/en/stable/) library. |
| 84 | +The HDF5 IOSP looks up filters by id; if you plan to use your third party filter with HDF5 data, the int returned by `getId` should adhere to the |
| 85 | +[guidelines](https://portal.hdfgroup.org/display/support/Registered+Filter+Plugins) set by the HDF group. |
| 86 | +If your filter is registered with the HDF group, your `getId` method should return the HDF id. If your filter is not registered with the HDF group, |
| 87 | +" %} |
| 88 | + |
| 89 | +There are two more methods in the `FilterProvider` interface: the `canProvide` methods. By default, these methods work as follows: |
| 90 | +* `boolean canProvide(String name)` returns true if the string returned by `getName()` matches the string passed to the method |
| 91 | +* `boolean canProvide(int id)` returns true if the int returned by `getId()` matches the int passed to the method |
| 92 | + |
| 93 | +It is unlikely that you would want to override these methods, as the netCDF-Java [IOSPs](writing_iosp.html) look for the correct filter implementations |
| 94 | +for a dataset by either name or numeric id. However, it is possible to write your own implementation of `canProvide`; for example, the |
| 95 | +following `FilterProvider` returns an instance of a `DefaultFilter` regardless of the name or id provided. |
| 96 | + |
| 97 | +{% capture rmd %} |
| 98 | +{% includecodeblock netcdf-java&docs/src/test/java/examples/ZarrExamples.java&implementFilterProvider2 %} |
| 99 | +{% endcapture %} |
| 100 | +{{ rmd | markdownify }} |
0 commit comments