OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
createNCDF.c
Go to the documentation of this file.
1 /*
2  * createNCDF.c
3  *
4  * Created on: Sep 2, 2016
5  * Author: rhealy
6  */
7 
8 #include <string.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <nc4utils.h>
12 #include <netcdf.h>
13 
14 #include <stdio.h>
15 #include <math.h>
16 
17 int createNCDF(int ncid, const char *sname, const char *lname,
18  const char *standard_name, const char *units,
19  void *fill_value,
20  const char *flag_values, const char *flag_meanings,
21  double low, double high, int nt,
22  int rank, int *dimids, size_t *chunksize) {
23 
24  int32_t varid;
25  int status;
26 
27  /* Create the NCDF dataset */
28  status = nc_def_var(ncid, sname, nt, rank, dimids, &varid);
29  if (status != NC_NOERR) {
30  printf("-E- %s %d: %s for %s\n",
31  __FILE__, __LINE__, nc_strerror(status), sname);
32  exit(1);
33  }
34 
35  // Set fill value
36  double fill_value_dbl;
37  memcpy(&fill_value_dbl, fill_value, sizeof (double));
38 
39  int8_t i8;
40  uint8_t ui8;
41  int16_t i16;
42  int32_t i32;
43  float f32;
44  // size_t type_size;
45 
46  if ((low < high) && (low != fill_value_dbl)) {
47  if (nt == NC_BYTE) {
48  i8 = fill_value_dbl;
49  //type_size = 4;
50  status = nc_def_var_fill(ncid, varid, 0, (void *) &i8);
51  } else if (nt == NC_UBYTE) {
52  ui8 = fill_value_dbl;
53  //type_size = 8;
54  status = nc_def_var_fill(ncid, varid, 0, (void *) &ui8);
55  } else if (nt == NC_SHORT) {
56  i16 = fill_value_dbl;
57  //type_size = 2;
58  status = nc_def_var_fill(ncid, varid, 0, (void *) &i16);
59  } else if (nt == NC_INT) {
60  i32 = fill_value_dbl;
61  //type_size = 4;
62  status = nc_def_var_fill(ncid, varid, 0, (void *) &i32);
63  } else if (nt == NC_FLOAT) {
64  f32 = fill_value_dbl;
65  //type_size = 4;
66  status = nc_def_var_fill(ncid, varid, 0, (void *) &f32);
67  } else {
68  //type_size = 4;
69  status = nc_def_var_fill(ncid, varid, 0, (void *) &fill_value_dbl);
70  }
71  check_err(status, __LINE__, __FILE__);
72  }
73 
74 #if 0
75  //nc_init_compress(ncid, varid, dimids, rank, chunksize,5);
76  nc_init_compress2(ncid, sname, varid, dimids, rank, chunksize, type_size, 5);
77 
78 #endif
79 
80  /* Add a "long_name" attribute */
81  status = nc_put_att_text(ncid, varid, "long_name", strlen(lname), lname);
82  if (status != NC_NOERR) {
83  printf("-E- %s %d: %s for %s\n",
84  __FILE__, __LINE__, nc_strerror(status), "long_name");
85  exit(1);
86  }
87 
88  /* Add a "flag_values" attribute if specified*/
89  if (strcmp(flag_values, "") != 0) {
90  status = nc_put_att_text(ncid, varid, "flag_values",
91  strlen(flag_values), flag_values);
92  if (status != NC_NOERR) {
93  printf("-E- %s %d: %s for %s\n",
94  __FILE__, __LINE__, nc_strerror(status), "flag_values");
95  exit(1);
96  }
97  }
98 
99  /* Add a "flag_meanings" attribute if specified*/
100  if (strcmp(flag_meanings, "") != 0) {
101  status = nc_put_att_text(ncid, varid, "flag_meanings",
102  strlen(flag_meanings), flag_meanings);
103  if (status != NC_NOERR) {
104  printf("-E- %s %d: %s for %s\n",
105  __FILE__, __LINE__, nc_strerror(status), "flag_meanings");
106  exit(1);
107  }
108  }
109 
110  /* Add a "valid_range" attribute if one is specified */
111  if (low < high) {
112  switch (nt) { /* Use the appropriate number type */
113  case NC_BYTE:
114  {
115  uint8_t vr[2];
116  vr[0] = (uint8_t) low;
117  vr[1] = (uint8_t) high;
118  status = nc_put_att_uchar(ncid, varid, "valid_min", NC_BYTE, 1, &vr[0]);
119  if (status != NC_NOERR) {
120  printf("-E- %s %d: %s for %s\n",
121  __FILE__, __LINE__, nc_strerror(status), "valid_min");
122  exit(1);
123  }
124  status = nc_put_att_uchar(ncid, varid, "valid_max", NC_BYTE, 1, &vr[1]);
125  if (status != NC_NOERR) {
126  printf("-E- %s %d: %s for %s\n",
127  __FILE__, __LINE__, nc_strerror(status), "valid_max");
128  exit(1);
129  }
130  }
131  break;
132  case NC_UBYTE:
133  {
134  uint8_t vr[2];
135  vr[0] = (uint8_t) low;
136  vr[1] = (uint8_t) high;
137  status = nc_put_att_uchar(ncid, varid, "valid_min", NC_UBYTE, 1, &vr[0]);
138  if (status != NC_NOERR) {
139  printf("-E- %s %d: %s for %s\n",
140  __FILE__, __LINE__, nc_strerror(status), "valid_min");
141  exit(1);
142  }
143  status = nc_put_att_uchar(ncid, varid, "valid_max", NC_UBYTE, 1, &vr[1]);
144  if (status != NC_NOERR) {
145  printf("-E- %s %d: %s for %s\n",
146  __FILE__, __LINE__, nc_strerror(status), "valid_max");
147  exit(1);
148  }
149  }
150  break;
151  case NC_SHORT:
152  {
153  int16_t vr[2];
154  vr[0] = (int16_t) low;
155  vr[1] = (int16_t) high;
156  status = nc_put_att_short(ncid, varid, "valid_range", NC_SHORT, 1, &vr[0]);
157  if (status != NC_NOERR) {
158  printf("-E- %s %d: %s for %s\n",
159  __FILE__, __LINE__, nc_strerror(status), "valid_min");
160  exit(1);
161  }
162  status = nc_put_att_short(ncid, varid, "valid_max", NC_SHORT, 1, &vr[1]);
163  if (status != NC_NOERR) {
164  printf("-E- %s %d: %s for %s\n",
165  __FILE__, __LINE__, nc_strerror(status), "valid_max");
166  exit(1);
167  }
168  }
169  break;
170  case NC_INT:
171  {
172  int32_t vr[2];
173  vr[0] = (int32_t) low;
174  vr[1] = (int32_t) high;
175  status = nc_put_att_int(ncid, varid, "valid_min", NC_INT, 1, &vr[0]);
176  if (status != NC_NOERR) {
177  printf("-E- %s %d: %s for %s\n",
178  __FILE__, __LINE__, nc_strerror(status), "valid_min");
179  exit(1);
180  }
181  status = nc_put_att_int(ncid, varid, "valid_max", NC_INT, 1, &vr[1]);
182  if (status != NC_NOERR) {
183  printf("-E- %s %d: %s for %s\n",
184  __FILE__, __LINE__, nc_strerror(status), "valid_max");
185  exit(1);
186  }
187  }
188  break;
189  case NC_FLOAT:
190  {
191  float vr[2];
192  vr[0] = (float) low;
193  vr[1] = (float) high;
194  status = nc_put_att_float(ncid, varid, "valid_min", NC_FLOAT, 1, &vr[0]);
195  if (status != NC_NOERR) {
196  printf("-E- %s %d: %s for %s\n",
197  __FILE__, __LINE__, nc_strerror(status), "valid_min");
198  exit(1);
199  }
200  status = nc_put_att_float(ncid, varid, "valid_max", NC_FLOAT, 1, &vr[1]);
201  if (status != NC_NOERR) {
202  printf("-E- %s %d: %s for %s\n",
203  __FILE__, __LINE__, nc_strerror(status), "valid_max");
204  exit(1);
205  }
206  }
207  break;
208  case NC_DOUBLE:
209  {
210  double vr[2];
211  vr[0] = low;
212  vr[1] = high;
213  status = nc_put_att_double(ncid, varid, "valid_min", NC_DOUBLE, 1, &vr[0]);
214  if (status != NC_NOERR) {
215  printf("-E- %s %d: %s for %s\n",
216  __FILE__, __LINE__, nc_strerror(status), "valid_min");
217  exit(1);
218  }
219  status = nc_put_att_double(ncid, varid, "valid_max", NC_DOUBLE, 1, &vr[1]);
220  if (status != NC_NOERR) {
221  printf("-E- %s %d: %s for %s\n",
222  __FILE__, __LINE__, nc_strerror(status), "valid_max");
223  exit(1);
224  }
225  }
226  break;
227  default:
228  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
229  fprintf(stderr, "Got unsupported number type (%d) ", nt);
230  fprintf(stderr, "while trying to create NCDF variable, \"%s\", ", sname);
231  return (EXIT_FAILURE);
232  }
233  }
234 
235  /* Add a "units" attribute if one is specified */
236  if (units != NULL && *units != 0) {
237  status = nc_put_att_text(ncid, varid, "units", strlen(units), units);
238  if (status != NC_NOERR) {
239  printf("-E- %s %d: %s for %s\n",
240  __FILE__, __LINE__, nc_strerror(status), "units");
241  exit(1);
242  }
243  }
244 
245  /* Add a "standard_name" attribute if one is specified */
246  if (standard_name != NULL && *standard_name != 0) {
247  status = nc_put_att_text(ncid, varid, "standard_name",
248  strlen(standard_name), standard_name);
249  if (status != NC_NOERR) {
250  printf("-E- %s %d: %s for %s\n",
251  __FILE__, __LINE__, nc_strerror(status), "standard_name");
252  exit(1);
253  }
254  }
255 
256  return 0;
257 }
258 
259 
260 
261 
int createNCDF(int ncid, const char *sname, const char *lname, const char *standard_name, const char *units, void *fill_value, const char *flag_values, const char *flag_meanings, double low, double high, int nt, int rank, int *dimids, size_t *chunksize)
Definition: createNCDF.c:17
int status
Definition: l1_czcs_hdf.c:32
void check_err(const int stat, const int line, const char *file)
Definition: nc4utils.c:35
#define NULL
Definition: decode_rs.h:63
Extra metadata that will be written to the HDF4 file l2prod rank
int nc_init_compress2(int32_t nc_id, char *varnam, int32_t var_id, int32_t *dimids, int32_t rank, size_t *chunksize, int type_size, int deflate_level)
These two strings are used for the product XML output If product_id is not set then prefix is used If the last char of the name_prefix is _ then it is removed If algorithm_id is not set then name_suffix is used If the first char is _ then it is removed l2prod standard_name[0]
float32 f32
Definition: l2bin.cpp:104