-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathzvar.c
2376 lines (2132 loc) · 74.7 KB
/
zvar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2003-2019, University Corporation for Atmospheric
* Research. See COPYRIGHT file for copying and redistribution
* conditions.*/
/**
* @file
* @internal This file handles the ZARR variable functions.
*
* @author Dennis Heimbigner, Ed Hartnett
*/
#include "zincludes.h"
#include <math.h> /* For pow() used below. */
/* Mnemonics */
#define CREATE 0
#define NOCREATE 1
#ifdef LOGGING
static void
reportchunking(const char* title, NC_VAR_INFO_T* var)
{
int i;
char buf[8192];
buf[0] = '\0'; /* for strlcat */
strlcat(buf,title,sizeof(buf));
strlcat(buf,"chunksizes for var ",sizeof(buf));
strlcat(buf,var->hdr.name,sizeof(buf));
strlcat(buf,"sizes=",sizeof(buf));
for(i=0;i<var->ndims;i++) {
char digits[64];
if(i > 0) strlcat(buf,",",sizeof(buf));
snprintf(digits,sizeof(digits),"%ld",(unsigned long)var->chunksizes[i]);
strlcat(buf,digits,sizeof(buf));
}
LOG((3,"%s",buf));
}
#endif
/* Mnemonic */
#define READING 1
#define WRITING 0
/** @internal Default size for unlimited dim chunksize. */
#define DEFAULT_1D_UNLIM_SIZE (4096)
/** Number of bytes in 64 KB. */
#define SIXTY_FOUR_KB (65536)
/** @internal Temp name used when renaming vars to preserve varid
* order. */
#define NC_TEMP_NAME "_netcdf4_temporary_variable_name_for_rename"
/**
* @internal Check a set of chunksizes to see if they specify a chunk
* that is too big.
*
* @param grp Pointer to the group info.
* @param var Pointer to the var info.
* @param chunksizes Array of chunksizes to check.
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Invalid variable ID.
* @returns ::NC_EBADCHUNK Bad chunksize.
*/
static int
check_chunksizes(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var, const size_t *chunksizes)
{
double dprod;
size_t type_len;
int d;
int retval = NC_NOERR;
if ((retval = nc4_get_typelen_mem(grp->nc4_info, var->type_info->hdr.id, &type_len)))
goto done;
if (var->type_info->nc_type_class == NC_VLEN)
dprod = (double)sizeof(nc_hvl_t);
else
dprod = (double)type_len;
for (d = 0; d < var->ndims; d++)
dprod *= (double)chunksizes[d];
if (dprod > (double) NC_MAX_UINT)
{retval = NC_EBADCHUNK; goto done;}
done:
return retval;
}
/**
* @internal Determine some default chunksizes for a variable.
*
* @param grp Pointer to the group info.
* @param var Pointer to the var info.
*
* @returns ::NC_NOERR for success
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Invalid variable ID.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
ncz_find_default_chunksizes2(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var)
{
int d;
size_t type_size;
float num_values = 1, num_unlim = 0;
int retval;
size_t suggested_size;
#ifdef LOGGING
double total_chunk_size;
#endif
type_size = var->type_info->size;
#ifdef LOGGING
/* Later this will become the total number of bytes in the default
* chunk. */
total_chunk_size = (double) type_size;
#endif
if(var->chunksizes == NULL) {
if((var->chunksizes = calloc(1,sizeof(size_t)*var->ndims)) == NULL)
return NC_ENOMEM;
}
/* How many values in the variable (or one record, if there are
* unlimited dimensions). */
for (d = 0; d < var->ndims; d++)
{
assert(var->dim[d]);
if (! var->dim[d]->unlimited)
num_values *= (float)var->dim[d]->len;
else {
num_unlim++;
var->chunksizes[d] = 1; /* overwritten below, if all dims are unlimited */
}
}
/* Special case to avoid 1D vars with unlim dim taking huge amount
of space (DEFAULT_CHUNK_SIZE bytes). Instead we limit to about
4KB */
if (var->ndims == 1 && num_unlim == 1) {
if (DEFAULT_CHUNK_SIZE / type_size <= 0)
suggested_size = 1;
else if (DEFAULT_CHUNK_SIZE / type_size > DEFAULT_1D_UNLIM_SIZE)
suggested_size = DEFAULT_1D_UNLIM_SIZE;
else
suggested_size = DEFAULT_CHUNK_SIZE / type_size;
var->chunksizes[0] = suggested_size / type_size;
LOG((4, "%s: name %s dim %d DEFAULT_CHUNK_SIZE %d num_values %f type_size %d "
"chunksize %ld", __func__, var->hdr.name, d, DEFAULT_CHUNK_SIZE, num_values, type_size, var->chunksizes[0]));
}
if (var->ndims > 1 && var->ndims == num_unlim) { /* all dims unlimited */
suggested_size = pow((double)DEFAULT_CHUNK_SIZE/type_size, 1.0/(double)(var->ndims));
for (d = 0; d < var->ndims; d++)
{
var->chunksizes[d] = suggested_size ? suggested_size : 1;
LOG((4, "%s: name %s dim %d DEFAULT_CHUNK_SIZE %d num_values %f type_size %d "
"chunksize %ld", __func__, var->hdr.name, d, DEFAULT_CHUNK_SIZE, num_values, type_size, var->chunksizes[d]));
}
}
/* Pick a chunk length for each dimension, if one has not already
* been picked above. */
for (d = 0; d < var->ndims; d++)
if (!var->chunksizes[d])
{
suggested_size = (pow((double)DEFAULT_CHUNK_SIZE/(num_values * type_size),
1.0/(double)(var->ndims - num_unlim)) * var->dim[d]->len - .5);
if (suggested_size > var->dim[d]->len)
suggested_size = var->dim[d]->len;
var->chunksizes[d] = suggested_size ? suggested_size : 1;
LOG((4, "%s: name %s dim %d DEFAULT_CHUNK_SIZE %d num_values %f type_size %d "
"chunksize %ld", __func__, var->hdr.name, d, DEFAULT_CHUNK_SIZE, num_values, type_size, var->chunksizes[d]));
}
#ifdef LOGGING
/* Find total chunk size. */
for (d = 0; d < var->ndims; d++)
total_chunk_size *= (double) var->chunksizes[d];
LOG((4, "total_chunk_size %f", total_chunk_size));
#endif
/* But did this result in a chunk that is too big? */
retval = check_chunksizes(grp, var, var->chunksizes);
if (retval)
{
/* Other error? */
if (retval != NC_EBADCHUNK)
return THROW(retval);
/* Chunk is too big! Reduce each dimension by half and try again. */
for ( ; retval == NC_EBADCHUNK; retval = check_chunksizes(grp, var, var->chunksizes))
for (d = 0; d < var->ndims; d++)
var->chunksizes[d] = var->chunksizes[d]/2 ? var->chunksizes[d]/2 : 1;
}
/* Do we have any big data overhangs? They can be dangerous to
* babies, the elderly, or confused campers who have had too much
* beer. */
for (d = 0; d < var->ndims; d++)
{
size_t num_chunks;
size_t overhang;
assert(var->chunksizes[d] > 0);
num_chunks = (var->dim[d]->len + var->chunksizes[d] - 1) / var->chunksizes[d];
if(num_chunks > 0) {
overhang = (num_chunks * var->chunksizes[d]) - var->dim[d]->len;
var->chunksizes[d] -= overhang / num_chunks;
}
}
#ifdef LOGGING
reportchunking("find_default: ",var);
#endif
return NC_NOERR;
}
#if 0
/**
* @internal Give a var a secret ZARR name. This is needed when a var
* is defined with the same name as a dim, but it is not a coord var
* of that dim. In that case, the var uses a secret name inside the
* ZARR file.
*
* @param var Pointer to var info.
* @param name Name to use for base of secret name.
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EMAXNAME Name too long to fit secret prefix.
* @returns ::NC_ENOMEM Out of memory.
* @author Dennis Heimbigner, Ed Hartnett
*/
static int
give_var_secret_name(NC_VAR_INFO_T *var, const char *name)
{
/* Set a different ncz name for this variable to avoid name
* clash. */
if (strlen(name) + strlen(NON_COORD_PREPEND) > NC_MAX_NAME)
return NC_EMAXNAME;
size_t ncz_name_size = (strlen(NON_COORD_PREPEND) + strlen(name) + 1) *
sizeof(char);
if (!(var->ncz_name = malloc(ncz_name_size)))
return NC_ENOMEM;
snprintf(var->ncz_name, ncz_name_size, "%s%s", NON_COORD_PREPEND, name);
return NC_NOERR;
}
#endif /*0*/
/**
* @internal This is called when a new netCDF-4 variable is defined
* with nc_def_var().
*
* @param ncid File ID.
* @param name Name.
* @param xtype Type.
* @param ndims Number of dims. ZARR has maximum of 32.
* @param dimidsp Array of dim IDs.
* @param varidp Gets the var ID.
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Invalid variable ID.
* @returns ::NC_ENOTNC4 Attempting netcdf-4 operation on file that is
* not netCDF-4/NCZ.
* @returns ::NC_ESTRICTNC3 Attempting netcdf-4 operation on strict nc3
* netcdf-4 file.
* @returns ::NC_ELATEDEF Too late to change settings for this variable.
* @returns ::NC_ENOTINDEFINE Not in define mode.
* @returns ::NC_EPERM File is read only.
* @returns ::NC_EMAXDIMS Classic model file exceeds ::NC_MAX_VAR_DIMS.
* @returns ::NC_ESTRICTNC3 Attempting to create netCDF-4 type var in
* classic model file
* @returns ::NC_EBADNAME Bad name.
* @returns ::NC_EBADTYPE Bad type.
* @returns ::NC_ENOMEM Out of memory.
* @returns ::NC_EHDFERR Error returned by ZARR layer.
* @returns ::NC_EINVAL Invalid input
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ_def_var(int ncid, const char *name, nc_type xtype, int ndims,
const int *dimidsp, int *varidp)
{
NC_GRP_INFO_T *grp;
NC_VAR_INFO_T *var;
NC_DIM_INFO_T *dim;
NC_FILE_INFO_T *h5;
NC_TYPE_INFO_T *type = NULL;
NCZ_VAR_INFO_T* zvar = NULL;
char norm_name[NC_MAX_NAME + 1];
int d;
int retval;
NCglobalstate* gstate = NC_getglobalstate();
ZTRACE(1,"ncid=%d name=%s xtype=%d ndims=%d dimids=%s",ncid,name,xtype,ndims,nczprint_idvector(ndims,dimidsp));
/* Find info for this file and group, and set pointer to each. */
if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
BAIL(retval);
assert(grp && grp->format_grp_info && h5);
#ifdef LOOK
/* HDF5 allows maximum of 32 dimensions. */
if (ndims > H5S_MAX_RANK)
BAIL(NC_EMAXDIMS);
#endif
/* If it's not in define mode, strict nc3 files error out,
* otherwise switch to define mode. This will also check that the
* file is writable. */
if (!(h5->flags & NC_INDEF))
{
if (h5->cmode & NC_CLASSIC_MODEL)
BAIL(NC_ENOTINDEFINE);
if ((retval = NCZ_redef(ncid)))
BAIL(retval);
}
assert(!h5->no_write);
/* Check and normalize the name. */
if ((retval = nc4_check_name(name, norm_name)))
BAIL(retval);
/* Not a Type is, well, not a type.*/
if (xtype == NC_NAT)
BAIL(NC_EBADTYPE);
/* For classic files, only classic types are allowed. */
if (h5->cmode & NC_CLASSIC_MODEL && xtype > NC_DOUBLE)
BAIL(NC_ESTRICTNC3);
/* For classic files limit number of dims. */
if (h5->cmode & NC_CLASSIC_MODEL && ndims > NC_MAX_VAR_DIMS)
BAIL(NC_EMAXDIMS);
/* cast needed for braindead systems with signed size_t */
if ((unsigned long) ndims > X_INT_MAX) /* Backward compat */
BAIL(NC_EINVAL);
/* Check that this name is not in use as a var, grp, or type. */
if ((retval = nc4_check_dup_name(grp, norm_name)))
BAIL(retval);
/* For non-scalar vars, dim IDs must be provided. */
if (ndims && !dimidsp)
BAIL(NC_EINVAL);
/* Check all the dimids to make sure they exist. */
for (d = 0; d < ndims; d++)
if ((retval = nc4_find_dim(grp, dimidsp[d], &dim, NULL)))
BAIL(retval);
/* These degrubbing messages sure are handy! */
LOG((2, "%s: name %s type %d ndims %d", __func__, norm_name, xtype, ndims));
#ifdef LOGGING
{
int dd;
for (dd = 0; dd < ndims; dd++)
LOG((4, "dimid[%d] %d", dd, dimidsp[dd]));
}
#endif
/* If this is a user-defined type, there is a type struct with
* all the type information. For atomic types, fake up a type
* struct. */
if((retval = ncz_gettype(h5,grp,xtype,&type)))
BAIL(retval);
/* Create a new var and fill in some cache setting values. */
if ((retval = nc4_var_list_add(grp, norm_name, ndims, &var)))
BAIL(retval);
/* Add storage for NCZ-specific var info. */
if (!(var->format_var_info = calloc(1, sizeof(NCZ_VAR_INFO_T))))
BAIL(NC_ENOMEM);
zvar = var->format_var_info;
zvar->common.file = h5;
zvar->scalar = (ndims == 0 ? 1 : 0);
zvar->dimension_separator = gstate->zarr.dimension_separator;
assert(zvar->dimension_separator != 0);
/* Set these state flags for the var. */
var->is_new_var = NC_TRUE;
var->meta_read = NC_TRUE;
var->atts_read = NC_TRUE;
#ifdef NETCDF_ENABLE_NCZARR_FILTERS
/* Set the filter list */
assert(var->filters == NULL);
var->filters = (void*)nclistnew();
#endif
/* Point to the type, and increment its ref. count */
var->type_info = type;
#ifdef LOOK
var->type_info->rc++;
#endif
type = NULL;
/* Propagate the endianness */
var->endianness = var->type_info->endianness;
/* Set variables no_fill to match the database default unless the
* variable type is variable length (NC_STRING or NC_VLEN) or is
* user-defined type. */
if (var->type_info->nc_type_class <= NC_STRING)
var->no_fill = (h5->fill_mode == NC_NOFILL);
/* Assign dimensions to the variable. At the same time, check to
* see if this is a coordinate variable. If so, it will have the
* same name as one of its dimensions. If it is a coordinate var,
* is it a coordinate var in the same group as the dim? Also, check
* whether we should use contiguous or chunked storage. */
var->storage = NC_CHUNKED;
for (d = 0; d < ndims; d++)
{
NC_GRP_INFO_T *dim_grp;
/* Look up each dimension */
if ((retval = nc4_find_dim(grp, dimidsp[d], &dim, &dim_grp)))
BAIL(retval);
assert(dim && dim->format_dim_info);
/* Check for unlimited dimension and turn off contiguous storage. */
if (dim->unlimited)
var->storage = NC_CHUNKED;
/* Track dimensions for variable */
var->dimids[d] = dimidsp[d];
var->dim[d] = dim;
}
/* Determine default chunksizes for this variable. (Even for
* variables which may be contiguous.) */
LOG((4, "allocating array of %d size_t to hold chunksizes for var %s",
var->ndims, var->hdr.name));
if(!var->chunksizes) {
if(var->ndims) {
if (!(var->chunksizes = calloc(var->ndims, sizeof(size_t))))
BAIL(NC_ENOMEM);
if ((retval = ncz_find_default_chunksizes2(grp, var)))
BAIL(retval);
} else {
/* Pretend that scalars are like var[1] */
if (!(var->chunksizes = calloc(1, sizeof(size_t))))
BAIL(NC_ENOMEM);
var->chunksizes[0] = 1;
}
}
/* Compute the chunksize cross product */
zvar->chunkproduct = 1;
if(!zvar->scalar)
{for(d=0;d<var->ndims;d++) {zvar->chunkproduct *= var->chunksizes[d];}}
zvar->chunksize = zvar->chunkproduct * var->type_info->size;
/* Set cache defaults */
var->chunkcache = gstate->chunkcache;
/* Create the cache */
if((retval=NCZ_create_chunk_cache(var,zvar->chunkproduct*var->type_info->size,zvar->dimension_separator,&zvar->cache)))
BAIL(retval);
/* Set the per-variable chunkcache defaults */
zvar->cache->params = var->chunkcache;
/* Return the varid. */
if (varidp)
*varidp = var->hdr.id;
LOG((4, "new varid %d", var->hdr.id));
exit:
if (type)
if ((retval = nc4_type_free(type)))
BAILLOG(retval);
return ZUNTRACE(retval);
}
/**
* @internal This functions sets extra stuff about a netCDF-4 variable which
* must be set before the enddef but after the def_var.
*
* @note All pointer parameters may be NULL, in which case they are ignored.
* @param ncid File ID.
* @param varid Variable ID.
* @param shuffle Pointer to shuffle setting.
* @param deflate Pointer to deflate setting.
* @param deflate_level Pointer to deflate level.
* @param fletcher32 Pointer to fletcher32 setting.
* @param contiguous Pointer to contiguous setting.
* @param chunksizes Array of chunksizes.
* @param no_fill Pointer to no_fill setting.
* @param fill_value Pointer to fill value.
* @param endianness Pointer to endianness setting.
*
* @returns ::NC_NOERR for success
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Invalid variable ID.
* @returns ::NC_ENOTNC4 Attempting netcdf-4 operation on file that is
* not netCDF-4/NCZ.
* @returns ::NC_ESTRICTNC3 Attempting netcdf-4 operation on strict nc3
* netcdf-4 file.
* @returns ::NC_ELATEDEF Too late to change settings for this variable.
* @returns ::NC_ENOTINDEFINE Not in define mode.
* @returns ::NC_EPERM File is read only.
* @returns ::NC_EINVAL Invalid input
* @returns ::NC_EBADCHUNK Bad chunksize.
* @author Dennis Heimbigner, Ed Hartnett
*/
static int
ncz_def_var_extra(int ncid, int varid, int *shuffle, int *unused1,
int *unused2, int *fletcher32, int *storagep,
const size_t *chunksizes, int *no_fill,
const void *fill_value, int *endianness,
int *quantize_mode, int *nsd)
{
NC_GRP_INFO_T *grp;
NC_FILE_INFO_T *h5;
NC_VAR_INFO_T *var;
NCZ_VAR_INFO_T *zvar;
int d;
int retval = NC_NOERR;
int storage = NC_CHUNKED;
size_t contigchunksizes[NC_MAX_VAR_DIMS]; /* Fake chunksizes if storage is contiguous or compact */
LOG((2, "%s: ncid 0x%x varid %d", __func__, ncid, varid));
ZTRACE(2,"ncid=%d varid=%d shuffle=%d fletcher32=%d no_fill=%d, fill_value=%p endianness=%d quantize_mode=%d nsd=%d",
ncid,varid,
(shuffle?*shuffle:-1),
(fletcher32?*fletcher32:-1),
(no_fill?*no_fill:-1),
fill_value,
(endianness?*endianness:-1),
(quantize_mode?*quantize_mode:-1),
(nsd?*nsd:-1)
);
/* Find info for this file and group, and set pointer to each. */
if ((retval = nc4_find_nc_grp_h5(ncid, NULL, &grp, &h5)))
goto done;
assert(grp && h5);
/* Trying to write to a read-only file? No way, Jose! */
if (h5->no_write)
{retval = NC_EPERM; goto done;}
/* Find the var. */
if (!(var = (NC_VAR_INFO_T *)ncindexith(grp->vars, varid)))
{retval = NC_ENOTVAR; goto done;}
assert(var && var->hdr.id == varid);
zvar = var->format_var_info;
ZTRACEMORE(1,"\tstoragep=%d chunksizes=%s",(storagep?*storagep:-1),(chunksizes?nczprint_sizevector(var->ndims,chunksizes):"null"));
/* Can't turn on parallel and deflate/fletcher32/szip/shuffle
* before HDF5 1.10.3. */
#ifdef NETCDF_ENABLE_NCZARR_FILTERS
#ifndef HDF5_SUPPORTS_PAR_FILTERS
if (h5->parallel == NC_TRUE)
if (nclistlength(((NClist*)var->filters)) > 0 || fletcher32 || shuffle)
{retval = NC_EINVAL; goto done;}
#endif
#endif
/* If the HDF5 dataset has already been created, then it is too
* late to set all the extra stuff. */
if (var->created)
{retval = NC_ELATEDEF; goto done;}
#if 0
/* Check compression options. */
if (deflate && !deflate_level)
{retval = NC_EINVAL; goto done;}
/* Valid deflate level? */
if (deflate)
{
if (*deflate)
if (*deflate_level < NC_MIN_DEFLATE_LEVEL ||
*deflate_level > NC_MAX_DEFLATE_LEVEL)
{retval = NC_EINVAL; goto done;}
/* For scalars, just ignore attempt to deflate. */
if (!var->ndims)
goto done;
/* If szip is in use, return an error. */
if ((retval = nc_inq_var_szip(ncid, varid, &option_mask, NULL)))
goto done;
if (option_mask)
{retval = NC_EINVAL; goto done;}
/* Set the deflate settings. */
var->storage = NC_CONTIGUOUS;
var->deflate = *deflate;
if (*deflate)
var->deflate_level = *deflate_level;
LOG((3, "%s: *deflate_level %d", __func__, *deflate_level));
}
#endif
/* Shuffle filter? */
if (shuffle && *shuffle) {
retval = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_SHUFFLE,NULL,NULL);
if(!retval || retval == NC_ENOFILTER) {
if((retval = NCZ_def_var_filter(ncid,varid,H5Z_FILTER_SHUFFLE,0,NULL))) return retval;
var->storage = NC_CHUNKED;
}
}
/* Fletcher32 checksum error protection? */
if (fletcher32 && fletcher32) {
retval = nc_inq_var_filter_info(ncid,varid,H5Z_FILTER_FLETCHER32,NULL,NULL);
if(!retval || retval == NC_ENOFILTER) {
if((retval = NCZ_def_var_filter(ncid,varid,H5Z_FILTER_FLETCHER32,0,NULL))) return retval;
var->storage = NC_CHUNKED;
}
}
/* Handle storage settings. */
if (storagep)
{
storage = *storagep;
/* Does the user want a contiguous or compact dataset? Not so
* fast! Make sure that there are no unlimited dimensions, and
* no filters in use for this data. */
if (storage != NC_CHUNKED)
{
#ifdef NCZARR_FILTERS
if (nclistlength(((NClist*)var->filters)) > 0)
{retval = NC_EINVAL; goto done;}
#endif
for (d = 0; d < var->ndims; d++) {
if (var->dim[d]->unlimited)
{retval = NC_EINVAL; goto done;}
contigchunksizes[d] = var->dim[d]->len; /* Fake a single big chunk */
}
chunksizes = (const size_t*)contigchunksizes;
storage = NC_CHUNKED; /*only chunked supported */
}
if (storage == NC_CHUNKED && var->ndims == 0) {
{retval = NC_EINVAL; goto done;}
} else if (storage == NC_CHUNKED && var->ndims > 0) {
var->storage = NC_CHUNKED;
/* If the user provided chunksizes, check that they are valid
* and that their total size of chunk is less than 4 GB. */
if (chunksizes)
{
/* Check the chunksizes for validity. */
if ((retval = check_chunksizes(grp, var, chunksizes)))
goto done;
/* Ensure chunksize is smaller than dimension size */
for (d = 0; d < var->ndims; d++)
if (!var->dim[d]->unlimited && var->dim[d]->len > 0 &&
chunksizes[d] > var->dim[d]->len)
{retval = NC_EBADCHUNK; goto done;}
}
}
/* Is this a variable with a chunksize greater than the current
* cache size? */
if (var->storage == NC_CHUNKED)
{
int anyzero = 0; /* check for any zero length chunksizes */
zvar = var->format_var_info;
assert(zvar->cache != NULL);
zvar->cache->valid = 0;
if(chunksizes) {
for (d = 0; d < var->ndims; d++) {
var->chunksizes[d] = chunksizes[d];
if(chunksizes[d] == 0) anyzero = 1;
}
}
/* If chunksizes == NULL or anyzero then use defaults */
if(chunksizes == NULL || anyzero) { /* Use default chunking */
if ((retval = ncz_find_default_chunksizes2(grp, var)))
goto done;
}
assert(var->chunksizes != NULL);
/* Set the chunksize product for this variable. */
zvar->chunkproduct = 1;
for (d = 0; d < var->ndims; d++)
zvar->chunkproduct *= var->chunksizes[d];
zvar->chunksize = zvar->chunkproduct * var->type_info->size;
}
/* Adjust cache */
if((retval = NCZ_adjust_var_cache(var))) goto done;
#ifdef LOGGING
{
int dfalt = (chunksizes == NULL);
reportchunking(dfalt ? "extra: default: " : "extra: user: ", var);
}
#endif
}
/* Are we setting a fill modes? */
if (no_fill)
{
if (*no_fill)
{
/* NC_STRING types may not turn off fill mode. It's disallowed
* by HDF5 and will cause a HDF5 error later. */
if (*no_fill)
if (var->type_info->hdr.id == NC_STRING)
{retval = NC_EINVAL; goto done;}
/* Set the no-fill mode. */
var->no_fill = NC_TRUE;
}
else
var->no_fill = NC_FALSE;
}
/* Are we setting a fill value? */
if (fill_value && no_fill && !(*no_fill))
{
/* Copy the fill_value. */
LOG((4, "Copying fill value into metadata for variable %s",
var->hdr.name));
/* If there's a _FillValue attribute, delete it. */
retval = NCZ_del_att(ncid, varid, NC_FillValue);
if (retval && retval != NC_ENOTATT)
goto done;
/* Create a _FillValue attribute; will also fill in var->fill_value */
if ((retval = nc_put_att(ncid, varid, NC_FillValue, var->type_info->hdr.id,
1, fill_value)))
goto done;
/* Reclaim any existing fill_chunk */
if((retval = NCZ_reclaim_fill_chunk(zvar->cache))) goto done;
} else if (var->fill_value && no_fill && (*no_fill)) { /* Turning off fill value? */
/* If there's a _FillValue attribute, delete it. */
retval = NCZ_del_att(ncid, varid, NC_FillValue);
if (retval && retval != NC_ENOTATT) return retval;
if((retval = NCZ_reclaim_fill_value(var))) return retval;
}
/* Is the user setting the endianness? */
if (endianness)
{
/* Setting endianness is only premitted on atomic integer and
* atomic float types. */
switch (var->type_info->hdr.id)
{
case NC_BYTE:
case NC_SHORT:
case NC_INT:
case NC_FLOAT:
case NC_DOUBLE:
case NC_UBYTE:
case NC_USHORT:
case NC_UINT:
case NC_INT64:
case NC_UINT64:
break;
default:
{retval = NC_EINVAL; goto done;}
}
var->type_info->endianness = *endianness;
/* Propagate */
var->endianness = *endianness;
}
/* Remember quantization settings. They will be used when data are
* written.
* Code block is identical to one in hdf5var.c---consider functionalizing */
if (quantize_mode)
{
/* Only four valid mode settings. */
if (*quantize_mode != NC_NOQUANTIZE &&
*quantize_mode != NC_QUANTIZE_BITGROOM &&
*quantize_mode != NC_QUANTIZE_GRANULARBR &&
*quantize_mode != NC_QUANTIZE_BITROUND)
return NC_EINVAL;
if (*quantize_mode == NC_QUANTIZE_BITGROOM ||
*quantize_mode == NC_QUANTIZE_GRANULARBR ||
*quantize_mode == NC_QUANTIZE_BITROUND)
{
/* Only float and double types can have quantization. */
if (var->type_info->hdr.id != NC_FLOAT &&
var->type_info->hdr.id != NC_DOUBLE)
return NC_EINVAL;
/* All quantization codecs require number of significant digits */
if (!nsd)
return NC_EINVAL;
/* NSD must be in range. */
if (*nsd <= 0)
return NC_EINVAL;
if (*quantize_mode == NC_QUANTIZE_BITGROOM ||
*quantize_mode == NC_QUANTIZE_GRANULARBR)
{
if (var->type_info->hdr.id == NC_FLOAT &&
*nsd > NC_QUANTIZE_MAX_FLOAT_NSD)
return NC_EINVAL;
if (var->type_info->hdr.id == NC_DOUBLE &&
*nsd > NC_QUANTIZE_MAX_DOUBLE_NSD)
return NC_EINVAL;
}
else if (*quantize_mode == NC_QUANTIZE_BITROUND)
{
if (var->type_info->hdr.id == NC_FLOAT &&
*nsd > NC_QUANTIZE_MAX_FLOAT_NSB)
return NC_EINVAL;
if (var->type_info->hdr.id == NC_DOUBLE &&
*nsd > NC_QUANTIZE_MAX_DOUBLE_NSB)
return NC_EINVAL;
}
var->nsd = *nsd;
}
var->quantize_mode = *quantize_mode;
/* If quantization is turned off, then set nsd to 0. */
if (*quantize_mode == NC_NOQUANTIZE)
var->nsd = 0;
}
done:
return ZUNTRACE(retval);
}
/**
* @internal Set compression settings on a variable. This is called by
* nc_def_var_deflate().
*
* @param ncid File ID.
* @param varid Variable ID.
* @param shuffle True to turn on the shuffle filter.
* @param deflate True to turn on deflation.
* @param deflate_level A number between 0 (no compression) and 9
* (maximum compression).
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Invalid variable ID.
* @returns ::NC_ENOTNC4 Attempting netcdf-4 operation on file that is
* not netCDF-4/NCZ.
* @returns ::NC_ELATEDEF Too late to change settings for this variable.
* @returns ::NC_ENOTINDEFINE Not in define mode.
* @returns ::NC_EINVAL Invalid input
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ_def_var_deflate(int ncid, int varid, int shuffle, int deflate,
int deflate_level)
{
int stat = NC_NOERR;
unsigned int level = (unsigned int)deflate_level;
/* Set shuffle first */
if((stat = ncz_def_var_extra(ncid, varid, &shuffle, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL))) goto done;
if(deflate)
stat = nc_def_var_filter(ncid, varid, H5Z_FILTER_DEFLATE,1,&level);
if(stat) goto done;
done:
return stat;
}
/**
* @internal Set checksum on a variable. This is called by
* nc_def_var_fletcher32().
*
* @param ncid File ID.
* @param varid Variable ID.
* @param fletcher32 Pointer to fletcher32 setting.
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Invalid variable ID.
* @returns ::NC_ENOTNC4 Attempting netcdf-4 operation on file that is
* not netCDF-4/NCZ.
* @returns ::NC_ELATEDEF Too late to change settings for this variable.
* @returns ::NC_ENOTINDEFINE Not in define mode.
* @returns ::NC_EINVAL Invalid input
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ_def_var_fletcher32(int ncid, int varid, int fletcher32)
{
return ncz_def_var_extra(ncid, varid, NULL, NULL, NULL, &fletcher32,
NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}
/**
* @internal Define chunking stuff for a var. This is called by
* nc_def_var_chunking(). Chunking is required in any dataset with one
* or more unlimited dimensions in NCZ, or any dataset using a
* filter.
*
* @param ncid File ID.
* @param varid Variable ID.
* @param contiguous Pointer to contiguous setting.
* @param chunksizesp Array of chunksizes.
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Invalid variable ID.
* @returns ::NC_ENOTNC4 Attempting netcdf-4 operation on file that is
* not netCDF-4/NCZ.
* @returns ::NC_ELATEDEF Too late to change settings for this variable.
* @returns ::NC_ENOTINDEFINE Not in define mode.
* @returns ::NC_EINVAL Invalid input
* @returns ::NC_EBADCHUNK Bad chunksize.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ_def_var_chunking(int ncid, int varid, int contiguous, const size_t *chunksizesp)
{
return ncz_def_var_extra(ncid, varid, NULL, NULL, NULL, NULL,
&contiguous, chunksizesp, NULL, NULL, NULL, NULL, NULL);
}
/**
* @internal Define chunking stuff for a var. This is called by
* the fortran API.
*
* @param ncid File ID.
* @param varid Variable ID.
* @param contiguous Pointer to contiguous setting.
* @param chunksizesp Array of chunksizes.
*
* @returns ::NC_NOERR No error.
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Invalid variable ID.
* @returns ::NC_ENOTNC4 Attempting netcdf-4 operation on file that is
* not netCDF-4/NCZ.
* @returns ::NC_ELATEDEF Too late to change settings for this variable.
* @returns ::NC_ENOTINDEFINE Not in define mode.
* @returns ::NC_EINVAL Invalid input
* @returns ::NC_EBADCHUNK Bad chunksize.
* @author Dennis Heimbigner, Ed Hartnett
*/
int
ncz_def_var_chunking_ints(int ncid, int varid, int contiguous, int *chunksizesp)
{
NC_VAR_INFO_T *var;
size_t *cs;
int i, retval;
/* Get pointer to the var. */
if ((retval = nc4_find_grp_h5_var(ncid, varid, NULL, NULL, &var)))
return THROW(retval);
assert(var);
/* Allocate space for the size_t copy of the chunksizes array. */
if (var->ndims)
if (!(cs = malloc(var->ndims * sizeof(size_t))))
return NC_ENOMEM;
/* Copy to size_t array. */
for (i = 0; i < var->ndims; i++)
cs[i] = chunksizesp[i];
retval = ncz_def_var_extra(ncid, varid, NULL, NULL, NULL, NULL,
&contiguous, cs, NULL, NULL, NULL, NULL, NULL);
if (var->ndims)
free(cs);
return THROW(retval);
}
/**
* @internal This functions sets fill value and no_fill mode for a
* netCDF-4 variable. It is called by nc_def_var_fill().
*
* @note All pointer parameters may be NULL, in which case they are ignored.
* @param ncid File ID.
* @param varid Variable ID.
* @param no_fill No_fill setting.
* @param fill_value Pointer to fill value.
*
* @returns ::NC_NOERR for success
* @returns ::NC_EBADID Bad ncid.
* @returns ::NC_ENOTVAR Invalid variable ID.
* @returns ::NC_ENOTNC4 Attempting netcdf-4 operation on file that is
* not netCDF-4/NCZ.
* @returns ::NC_ESTRICTNC3 Attempting netcdf-4 operation on strict nc3
* netcdf-4 file.
* @returns ::NC_ELATEDEF Too late to change settings for this variable.
* @returns ::NC_ENOTINDEFINE Not in define mode.
* @returns ::NC_EPERM File is read only.
* @returns ::NC_EINVAL Invalid input
* @author Dennis Heimbigner, Ed Hartnett
*/
int
NCZ_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)