NASA Logo
Ocean Color Science Software

ocssw V2022
nc_init_compress.c
Go to the documentation of this file.
1 #include <netcdf.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <math.h>
5 #include <stdio.h>
6 #include <nc4utils.h>
7 
8 #define DEFAULT_CHUNK_SIZE 1000
9 
10 #define NEW_CACHE_SIZE 16000000
11 #define NEW_CACHE_NELEMS 2003
12 #define NEW_CACHE_PREEMPTION .75
13 
15  /* Change chunk cache. */
16  int status = nc_set_chunk_cache(NEW_CACHE_SIZE, NEW_CACHE_NELEMS, NEW_CACHE_PREEMPTION);
17  if (status != NC_NOERR) {
18  fprintf(stderr, "-E- %s line %d: Could not set NetCDF4 cache size.\n", __FILE__, __LINE__);
19  exit(EXIT_FAILURE);
20  }
21 }
22 
37 void nc_init_compress(int32_t nc_id, int32_t var_id, int32_t *dimids, int32_t rank,
38  size_t *chunksize, int deflate_level) {
39  int i, status;
40 
41  // if deflate level below 0 then don't deflate
42  if (deflate_level < 1)
43  return;
44 
45  size_t dimlength;
46  if (rank > 3) {
47  printf("Whoops! Refusing to chunk/compress more than 3 dimensions");
48  exit(EXIT_FAILURE);
49  }
50  size_t suggested_size[3] = {32,256,40};
51  if (rank == 2) {
52  suggested_size[0] = 256;
53  suggested_size[1] = 2048;
54  }
55  // Set chunksize for each dimension, if one has not been provided
56  for (i = 0; i < rank; i++) {
57  if (chunksize==NULL || chunksize[i]==0) {
58  chunksize[i] = suggested_size[i];
59  }
60  status = nc_inq_dimlen(nc_id, dimids[i], &dimlength);
61  if (status != NC_NOERR) {
62  fprintf(stderr, "-E- %s line %d: Could not read size of dimension.\n", __FILE__, __LINE__);
63  exit(EXIT_FAILURE);
64  }
65  if(chunksize[i] > dimlength)
66  chunksize[i] = dimlength;
67  }
68 
69  /* Set compression */
70  /* First set chunking */
71  status = nc_def_var_chunking(nc_id, var_id, NC_CHUNKED, chunksize);
72  if (status != NC_NOERR) {
73  printf("-E- %s %d: %s \n", __FILE__, __LINE__,
74  nc_strerror(status));
75  exit(EXIT_FAILURE);
76  }
77  /* Now we can set compression */
78  status = nc_def_var_deflate(nc_id, var_id, NC_SHUFFLE, 9,
79  deflate_level);
80  if (status != NC_NOERR) {
81  printf("-E- %s %d: %s \n", __FILE__, __LINE__,
82  nc_strerror(status));
83  exit(EXIT_FAILURE);
84  }
85 }
86 
87 /* Check a set of chunksizes to see if they specify a chunk that is too big. */
88 int check_chunksizes(size_t type_len, int32_t ndims, const size_t *chunksizes) {
89  double dprod;
90  int d;
91 
92  dprod = (double) type_len;
93  for (d = 0; d < ndims; d++) {
94  if (chunksizes[d] < 1)
95  return NC_EINVAL;
96  dprod *= (double) chunksizes[d];
97  }
98 
99  if (dprod > (double) NC_MAX_UINT)
100  return NC_EBADCHUNK;
101 
102  return NC_NOERR;
103 }
int status
Definition: l1_czcs_hdf.c:32
#define NULL
Definition: decode_rs.h:63
void nc_init_compress(int32_t nc_id, int32_t var_id, int32_t *dimids, int32_t rank, size_t *chunksize, int deflate_level)
#define NEW_CACHE_SIZE
#define NEW_CACHE_PREEMPTION
int check_chunksizes(size_t type_len, int32_t ndims, const size_t *chunksizes)
integer, parameter double
#define NEW_CACHE_NELEMS
Extra metadata that will be written to the HDF4 file l2prod rank
void nc_init_chunk_cache()
int i
Definition: decode_rs.h:71