NetCDF 4.10.0
Loading...
Searching...
No Matches
zarr.c
1/*********************************************************************
2 * Copyright 2018, UCAR/Unidata
3 * See netcdf/COPYRIGHT file for copying and redistribution conditions.
4 *********************************************************************/
5
6#include "zincludes.h"
7#include <stddef.h>
8
9/**************************************************/
10/* Forwards */
11
12static int applycontrols(NCZ_FILE_INFO_T* zinfo);
13
14/***************************************************/
15/* API */
16
25int
26ncz_create_dataset(NC_FILE_INFO_T* file, NC_GRP_INFO_T* root, NClist* controls)
27{
28 int stat = NC_NOERR;
29 NCZ_FILE_INFO_T* zinfo = NULL;
30 NCZ_GRP_INFO_T* zgrp = NULL;
31 NCURI* uri = NULL;
32 NC* nc = NULL;
33 NCjson* json = NULL;
34 char* key = NULL;
35
36 ZTRACE(3,"file=%s root=%s controls=%s",file->hdr.name,root->hdr.name,(controls?nczprint_envv(controls):"null"));
37
38 nc = (NC*)file->controller;
39
40 /* Add struct to hold NCZ-specific file metadata. */
41 if (!(zinfo = calloc(1, sizeof(NCZ_FILE_INFO_T))))
42 {stat = NC_ENOMEM; goto done;}
43 file->format_file_info = zinfo;
44 zinfo->common.file = file;
45
46 /* Add struct to hold NCZ-specific group info. */
47 if (!(zgrp = calloc(1, sizeof(NCZ_GRP_INFO_T))))
48 {stat = NC_ENOMEM; goto done;}
49 root->format_grp_info = zgrp;
50 zgrp->common.file = file;
51
52 /* Fill in NCZ_FILE_INFO_T */
53 zinfo->creating = 1;
54 zinfo->common.file = file;
55 zinfo->native_endianness = (NC_isLittleEndian() ? NC_ENDIAN_LITTLE : NC_ENDIAN_BIG);
56 if((zinfo->controllist=nclistclone(controls,1)) == NULL)
57 {stat = NC_ENOMEM; goto done;}
58
59 /* fill in some of the zinfo and zroot fields */
60 zinfo->zarr.zarr_version = atoi(ZARRVERSION);
61 sscanf(NCZARRVERSION,"%lu.%lu.%lu",
62 &zinfo->zarr.nczarr_version.major,
63 &zinfo->zarr.nczarr_version.minor,
64 &zinfo->zarr.nczarr_version.release);
65
66 zinfo->default_maxstrlen = NCZ_MAXSTR_DEFAULT;
67
68 /* Apply client controls */
69 if((stat = applycontrols(zinfo))) goto done;
70
71 /* Load auth info from rc file */
72 if((stat = ncuriparse(nc->path,&uri))) goto done;
73 if(uri) {
74 if((stat = NC_authsetup(&zinfo->auth, uri)))
75 goto done;
76 }
77
78 /* initialize map handle*/
79 if((stat = nczmap_create(zinfo->controls.mapimpl,nc->path,nc->mode,zinfo->controls.flags,NULL,&zinfo->map)))
80 goto done;
81
82 if((stat = NCZMD_set_metadata_handler(zinfo))){
83 goto done;
84 }
85
86done:
87 ncurifree(uri);
88 NCJreclaim(json);
89 nullfree(key);
90 return ZUNTRACE(stat);
91}
92
101int
102ncz_open_dataset(NC_FILE_INFO_T* file, NClist* controls)
103{
104 int stat = NC_NOERR;
105 NC* nc = NULL;
106 NC_GRP_INFO_T* root = NULL;
107 NCURI* uri = NULL;
108 void* content = NULL;
109 NCjson* json = NULL;
110 NCZ_FILE_INFO_T* zinfo = NULL;
111 int mode;
112 NClist* modeargs = NULL;
113 char* nczarr_version = NULL;
114 char* zarr_format = NULL;
115
116 ZTRACE(3,"file=%s controls=%s",file->hdr.name,(controls?nczprint_envv(controls):"null"));
117
118 /* Extract info reachable via file */
119 nc = (NC*)file->controller;
120 mode = nc->mode;
121
122 root = file->root_grp;
123 assert(root != NULL && root->hdr.sort == NCGRP);
124
125 /* Add struct to hold NCZ-specific file metadata. */
126 if (!(file->format_file_info = calloc(1, sizeof(NCZ_FILE_INFO_T))))
127 {stat = NC_ENOMEM; goto done;}
128 zinfo = file->format_file_info;
129
130 /* Fill in NCZ_FILE_INFO_T */
131 zinfo->creating = 0;
132 zinfo->common.file = file;
133 zinfo->native_endianness = (NC_isLittleEndian() ? NC_ENDIAN_LITTLE : NC_ENDIAN_BIG);
134 if((zinfo->controllist=nclistclone(controls,1)) == NULL)
135 {stat = NC_ENOMEM; goto done;}
136 zinfo->default_maxstrlen = NCZ_MAXSTR_DEFAULT;
137
138 /* Add struct to hold NCZ-specific group info. */
139 if (!(root->format_grp_info = calloc(1, sizeof(NCZ_GRP_INFO_T))))
140 {stat = NC_ENOMEM; goto done;}
141 ((NCZ_GRP_INFO_T*)root->format_grp_info)->common.file = file;
142
143 /* Apply client controls */
144 if((stat = applycontrols(zinfo))) goto done;
145
146 /* initialize map handle*/
147 if((stat = nczmap_open(zinfo->controls.mapimpl,nc->path,mode,zinfo->controls.flags,NULL,&zinfo->map)))
148 goto done;
149
150 if((stat = NCZMD_set_metadata_handler(zinfo))) {
151 goto done;
152 }
153
154 /* Ok, try to read superblock */
155 if((stat = ncz_read_superblock(file,&nczarr_version,&zarr_format))) goto done;
156
157 if(nczarr_version == NULL) /* default */
158 nczarr_version = strdup(NCZARRVERSION);
159 if(zarr_format == NULL) /* default */
160 zarr_format = strdup(ZARRVERSION);
161 /* Extract the information from it */
162 if(sscanf(zarr_format,"%d",&zinfo->zarr.zarr_version)!=1)
163 {stat = NC_ENCZARR; goto done;}
164 if(sscanf(nczarr_version,"%lu.%lu.%lu",
165 &zinfo->zarr.nczarr_version.major,
166 &zinfo->zarr.nczarr_version.minor,
167 &zinfo->zarr.nczarr_version.release) == 0)
168 {stat = NC_ENCZARR; goto done;}
169
170 /* Load auth info from rc file */
171 if((stat = ncuriparse(nc->path,&uri))) goto done;
172 if(uri) {
173 if((stat = NC_authsetup(&zinfo->auth, uri)))
174 goto done;
175 }
176
177done:
178 nullfree(zarr_format);
179 nullfree(nczarr_version);
180 ncurifree(uri);
181 nclistfreeall(modeargs);
182 if(json) NCJreclaim(json);
183 nullfree(content);
184 return ZUNTRACE(stat);
185}
186
197int
198NCZ_isnetcdf4(struct NC_FILE_INFO* h5)
199{
200 int isnc4 = 1;
201 NC_UNUSED(h5);
202 return isnc4;
203}
204
217int
218NCZ_get_libversion(unsigned long* majorp, unsigned long* minorp,unsigned long* releasep)
219{
220 unsigned long m0,m1,m2;
221 sscanf(NCZARRVERSION,"%lu.%lu.%lu",&m0,&m1,&m2);
222 if(majorp) *majorp = m0;
223 if(minorp) *minorp = m1;
224 if(releasep) *releasep = m2;
225 return NC_NOERR;
226}
227
239int
240NCZ_get_superblock(NC_FILE_INFO_T* file, int* superblockp)
241{
242 NCZ_FILE_INFO_T* zinfo = file->format_file_info;
243 if(superblockp) *superblockp = zinfo->zarr.nczarr_version.major;
244 return NC_NOERR;
245}
246
247/**************************************************/
248/* Utilities */
249
250static const char*
251controllookup(NClist* controls, const char* key)
252{
253 size_t i;
254 for(i=0;i<nclistlength(controls);i+=2) {
255 const char* p = (char*)nclistget(controls,i);
256 if(strcasecmp(key,p)==0) {
257 return (const char*)nclistget(controls,i+1);
258 }
259 }
260 return NULL;
261}
262
263
264static int
265applycontrols(NCZ_FILE_INFO_T* zinfo)
266{
267 size_t i;
268 int stat = NC_NOERR;
269 const char* value = NULL;
270 NClist* modelist = nclistnew();
271 size64_t noflags = 0; /* track non-default negative flags */
272
273 if((value = controllookup(zinfo->controllist,"mode")) != NULL) {
274 if((stat = NCZ_comma_parse(value,modelist))) goto done;
275 }
276 /* Process the modelist first */
277 zinfo->controls.mapimpl = NCZM_DEFAULT;
278 zinfo->controls.flags |= FLAG_XARRAYDIMS; /* Always support XArray convention where possible */
279 zinfo->controls.flags |= (NCZARR_CONSOLIDATED_DEFAULT ? FLAG_CONSOLIDATED : 0);
280 for(i=0;i<nclistlength(modelist);i++) {
281 const char* p = nclistget(modelist,i);
282 if(strcasecmp(p,PUREZARRCONTROL)==0)
283 zinfo->controls.flags |= (FLAG_PUREZARR);
284 else if(strcasecmp(p,XARRAYCONTROL)==0)
285 zinfo->controls.flags |= FLAG_PUREZARR;
286 else if(strcasecmp(p,NOXARRAYCONTROL)==0)
287 noflags |= FLAG_XARRAYDIMS;
288 else if(strcasecmp(p,"zip")==0) zinfo->controls.mapimpl = NCZM_ZIP;
289 else if(strcasecmp(p,"file")==0) zinfo->controls.mapimpl = NCZM_FILE;
290 else if(strcasecmp(p,"s3")==0) zinfo->controls.mapimpl = NCZM_S3;
291 else if(strcasecmp(p,"consolidated") == 0)
292 zinfo->controls.flags |= FLAG_CONSOLIDATED;
293 }
294 /* Apply negative controls by turning off negative flags */
295 /* This is necessary to avoid order dependence of mode flags when both positive and negative flags are defined */
296 zinfo->controls.flags &= (~noflags);
297
298 /* Process other controls */
299 if((value = controllookup(zinfo->controllist,"log")) != NULL) {
300 zinfo->controls.flags |= FLAG_LOGGING;
301 ncsetloglevel(NCLOGNOTE);
302 }
303 if((value = controllookup(zinfo->controllist,"show")) != NULL) {
304 if(strcasecmp(value,"fetch")==0)
305 zinfo->controls.flags |= FLAG_SHOWFETCH;
306 }
307done:
308 nclistfreeall(modelist);
309 return stat;
310}
#define NC_ENDIAN_BIG
In HDF5 files you can set the endianness of variables with nc_def_var_endian().
Definition netcdf.h:345
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition netcdf.h:497
#define NC_ENCZARR
Error at NCZarr layer.
Definition netcdf.h:567
#define NC_ENDIAN_LITTLE
In HDF5 files you can set the endianness of variables with nc_def_var_endian().
Definition netcdf.h:344
#define NC_NOERR
No Error.
Definition netcdf.h:417