NetCDF  4.9.3
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 
12 static int applycontrols(NCZ_FILE_INFO_T* zinfo);
13 
14 /***************************************************/
15 /* API */
16 
25 int
26 ncz_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_env(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 = (NCZ_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 done:
83  ncurifree(uri);
84  NCJreclaim(json);
85  nullfree(key);
86  return ZUNTRACE(stat);
87 }
88 
97 int
98 ncz_open_dataset(NC_FILE_INFO_T* file, NClist* controls)
99 {
100  int stat = NC_NOERR;
101  NC* nc = NULL;
102  NC_GRP_INFO_T* root = NULL;
103  NCURI* uri = NULL;
104  void* content = NULL;
105  NCjson* json = NULL;
106  NCZ_FILE_INFO_T* zinfo = NULL;
107  int mode;
108  NClist* modeargs = NULL;
109  char* nczarr_version = NULL;
110  char* zarr_format = NULL;
111 
112  ZTRACE(3,"file=%s controls=%s",file->hdr.name,(controls?nczprint_envv(controls):"null"));
113 
114  /* Extract info reachable via file */
115  nc = (NC*)file->controller;
116  mode = nc->mode;
117 
118  root = file->root_grp;
119  assert(root != NULL && root->hdr.sort == NCGRP);
120 
121  /* Add struct to hold NCZ-specific file metadata. */
122  if (!(file->format_file_info = calloc(1, sizeof(NCZ_FILE_INFO_T))))
123  {stat = NC_ENOMEM; goto done;}
124  zinfo = file->format_file_info;
125 
126  /* Fill in NCZ_FILE_INFO_T */
127  zinfo->creating = 0;
128  zinfo->common.file = file;
129  zinfo->native_endianness = (NCZ_isLittleEndian() ? NC_ENDIAN_LITTLE : NC_ENDIAN_BIG);
130  if((zinfo->controllist=nclistclone(controls,1)) == NULL)
131  {stat = NC_ENOMEM; goto done;}
132  zinfo->default_maxstrlen = NCZ_MAXSTR_DEFAULT;
133 
134  /* Add struct to hold NCZ-specific group info. */
135  if (!(root->format_grp_info = calloc(1, sizeof(NCZ_GRP_INFO_T))))
136  {stat = NC_ENOMEM; goto done;}
137  ((NCZ_GRP_INFO_T*)root->format_grp_info)->common.file = file;
138 
139  /* Apply client controls */
140  if((stat = applycontrols(zinfo))) goto done;
141 
142  /* initialize map handle*/
143  if((stat = nczmap_open(zinfo->controls.mapimpl,nc->path,mode,zinfo->controls.flags,NULL,&zinfo->map)))
144  goto done;
145 
146  /* Ok, try to read superblock */
147  if((stat = ncz_read_superblock(file,&nczarr_version,&zarr_format))) goto done;
148 
149  if(nczarr_version == NULL) /* default */
150  nczarr_version = strdup(NCZARRVERSION);
151  if(zarr_format == NULL) /* default */
152  zarr_format = strdup(ZARRVERSION);
153  /* Extract the information from it */
154  if(sscanf(zarr_format,"%d",&zinfo->zarr.zarr_version)!=1)
155  {stat = NC_ENCZARR; goto done;}
156  if(sscanf(nczarr_version,"%lu.%lu.%lu",
157  &zinfo->zarr.nczarr_version.major,
158  &zinfo->zarr.nczarr_version.minor,
159  &zinfo->zarr.nczarr_version.release) == 0)
160  {stat = NC_ENCZARR; goto done;}
161 
162  /* Load auth info from rc file */
163  if((stat = ncuriparse(nc->path,&uri))) goto done;
164  if(uri) {
165  if((stat = NC_authsetup(&zinfo->auth, uri)))
166  goto done;
167  }
168 
169 done:
170  nullfree(zarr_format);
171  nullfree(nczarr_version);
172  ncurifree(uri);
173  nclistfreeall(modeargs);
174  if(json) NCJreclaim(json);
175  nullfree(content);
176  return ZUNTRACE(stat);
177 }
178 
189 int
190 NCZ_isnetcdf4(struct NC_FILE_INFO* h5)
191 {
192  int isnc4 = 1;
193  NC_UNUSED(h5);
194  return isnc4;
195 }
196 
209 int
210 NCZ_get_libversion(unsigned long* majorp, unsigned long* minorp,unsigned long* releasep)
211 {
212  unsigned long m0,m1,m2;
213  sscanf(NCZARRVERSION,"%lu.%lu.%lu",&m0,&m1,&m2);
214  if(majorp) *majorp = m0;
215  if(minorp) *minorp = m1;
216  if(releasep) *releasep = m2;
217  return NC_NOERR;
218 }
219 
231 int
232 NCZ_get_superblock(NC_FILE_INFO_T* file, int* superblockp)
233 {
234  NCZ_FILE_INFO_T* zinfo = file->format_file_info;
235  if(superblockp) *superblockp = zinfo->zarr.nczarr_version.major;
236  return NC_NOERR;
237 }
238 
239 /**************************************************/
240 /* Utilities */
241 
242 static const char*
243 controllookup(NClist* controls, const char* key)
244 {
245  size_t i;
246  for(i=0;i<nclistlength(controls);i+=2) {
247  const char* p = (char*)nclistget(controls,i);
248  if(strcasecmp(key,p)==0) {
249  return (const char*)nclistget(controls,i+1);
250  }
251  }
252  return NULL;
253 }
254 
255 
256 static int
257 applycontrols(NCZ_FILE_INFO_T* zinfo)
258 {
259  size_t i;
260  int stat = NC_NOERR;
261  const char* value = NULL;
262  NClist* modelist = nclistnew();
263  size64_t noflags = 0; /* track non-default negative flags */
264 
265  if((value = controllookup(zinfo->controllist,"mode")) != NULL) {
266  if((stat = NCZ_comma_parse(value,modelist))) goto done;
267  }
268  /* Process the modelist first */
269  zinfo->controls.mapimpl = NCZM_DEFAULT;
270  zinfo->controls.flags |= FLAG_XARRAYDIMS; /* Always support XArray convention where possible */
271  for(i=0;i<nclistlength(modelist);i++) {
272  const char* p = nclistget(modelist,i);
273  if(strcasecmp(p,PUREZARRCONTROL)==0)
274  zinfo->controls.flags |= (FLAG_PUREZARR);
275  else if(strcasecmp(p,XARRAYCONTROL)==0)
276  zinfo->controls.flags |= FLAG_PUREZARR;
277  else if(strcasecmp(p,NOXARRAYCONTROL)==0)
278  noflags |= FLAG_XARRAYDIMS;
279  else if(strcasecmp(p,"zip")==0) zinfo->controls.mapimpl = NCZM_ZIP;
280  else if(strcasecmp(p,"file")==0) zinfo->controls.mapimpl = NCZM_FILE;
281  else if(strcasecmp(p,"s3")==0) zinfo->controls.mapimpl = NCZM_S3;
282  }
283  /* Apply negative controls by turning off negative flags */
284  /* This is necessary to avoid order dependence of mode flags when both positive and negative flags are defined */
285  zinfo->controls.flags &= (~noflags);
286 
287  /* Process other controls */
288  if((value = controllookup(zinfo->controllist,"log")) != NULL) {
289  zinfo->controls.flags |= FLAG_LOGGING;
290  ncsetloglevel(NCLOGNOTE);
291  }
292  if((value = controllookup(zinfo->controllist,"show")) != NULL) {
293  if(strcasecmp(value,"fetch")==0)
294  zinfo->controls.flags |= FLAG_SHOWFETCH;
295  }
296 done:
297  nclistfreeall(modelist);
298  return stat;
299 }
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:458
#define NC_ENDIAN_LITTLE
In HDF5 files you can set the endianness of variables with nc_def_var_endian().
Definition: netcdf.h:305
#define NC_ENDIAN_BIG
In HDF5 files you can set the endianness of variables with nc_def_var_endian().
Definition: netcdf.h:306
#define NC_NOERR
No Error.
Definition: netcdf.h:378
#define NC_ENCZARR
Error at NCZarr layer.
Definition: netcdf.h:528