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  /* Add a "long_name" attribute */
75  status = nc_put_att_text(ncid, varid, "long_name", strlen(lname), lname);
76  if (status != NC_NOERR) {
77  printf("-E- %s %d: %s for %s\n",
78  __FILE__, __LINE__, nc_strerror(status), "long_name");
79  exit(1);
80  }
81 
82  /* Add a "flag_values" attribute if specified*/
83  if (strcmp(flag_values, "") != 0) {
84  status = nc_put_att_text(ncid, varid, "flag_values",
85  strlen(flag_values), flag_values);
86  if (status != NC_NOERR) {
87  printf("-E- %s %d: %s for %s\n",
88  __FILE__, __LINE__, nc_strerror(status), "flag_values");
89  exit(1);
90  }
91  }
92 
93  /* Add a "flag_meanings" attribute if specified*/
94  if (strcmp(flag_meanings, "") != 0) {
95  status = nc_put_att_text(ncid, varid, "flag_meanings",
96  strlen(flag_meanings), flag_meanings);
97  if (status != NC_NOERR) {
98  printf("-E- %s %d: %s for %s\n",
99  __FILE__, __LINE__, nc_strerror(status), "flag_meanings");
100  exit(1);
101  }
102  }
103 
104  /* Add a "valid_range" attribute if one is specified */
105  if (low < high) {
106  switch (nt) { /* Use the appropriate number type */
107  case NC_BYTE:
108  {
109  uint8_t vr[2];
110  vr[0] = (uint8_t) low;
111  vr[1] = (uint8_t) high;
112  status = nc_put_att_uchar(ncid, varid, "valid_min", NC_BYTE, 1, &vr[0]);
113  if (status != NC_NOERR) {
114  printf("-E- %s %d: %s for %s\n",
115  __FILE__, __LINE__, nc_strerror(status), "valid_min");
116  exit(1);
117  }
118  status = nc_put_att_uchar(ncid, varid, "valid_max", NC_BYTE, 1, &vr[1]);
119  if (status != NC_NOERR) {
120  printf("-E- %s %d: %s for %s\n",
121  __FILE__, __LINE__, nc_strerror(status), "valid_max");
122  exit(1);
123  }
124  }
125  break;
126  case NC_UBYTE:
127  {
128  uint8_t vr[2];
129  vr[0] = (uint8_t) low;
130  vr[1] = (uint8_t) high;
131  status = nc_put_att_uchar(ncid, varid, "valid_min", NC_UBYTE, 1, &vr[0]);
132  if (status != NC_NOERR) {
133  printf("-E- %s %d: %s for %s\n",
134  __FILE__, __LINE__, nc_strerror(status), "valid_min");
135  exit(1);
136  }
137  status = nc_put_att_uchar(ncid, varid, "valid_max", NC_UBYTE, 1, &vr[1]);
138  if (status != NC_NOERR) {
139  printf("-E- %s %d: %s for %s\n",
140  __FILE__, __LINE__, nc_strerror(status), "valid_max");
141  exit(1);
142  }
143  }
144  break;
145  case NC_SHORT:
146  {
147  int16_t vr[2];
148  vr[0] = (int16_t) low;
149  vr[1] = (int16_t) high;
150  status = nc_put_att_short(ncid, varid, "valid_range", NC_SHORT, 1, &vr[0]);
151  if (status != NC_NOERR) {
152  printf("-E- %s %d: %s for %s\n",
153  __FILE__, __LINE__, nc_strerror(status), "valid_min");
154  exit(1);
155  }
156  status = nc_put_att_short(ncid, varid, "valid_max", NC_SHORT, 1, &vr[1]);
157  if (status != NC_NOERR) {
158  printf("-E- %s %d: %s for %s\n",
159  __FILE__, __LINE__, nc_strerror(status), "valid_max");
160  exit(1);
161  }
162  }
163  break;
164  case NC_INT:
165  {
166  int32_t vr[2];
167  vr[0] = (int32_t) low;
168  vr[1] = (int32_t) high;
169  status = nc_put_att_int(ncid, varid, "valid_min", NC_INT, 1, &vr[0]);
170  if (status != NC_NOERR) {
171  printf("-E- %s %d: %s for %s\n",
172  __FILE__, __LINE__, nc_strerror(status), "valid_min");
173  exit(1);
174  }
175  status = nc_put_att_int(ncid, varid, "valid_max", NC_INT, 1, &vr[1]);
176  if (status != NC_NOERR) {
177  printf("-E- %s %d: %s for %s\n",
178  __FILE__, __LINE__, nc_strerror(status), "valid_max");
179  exit(1);
180  }
181  }
182  break;
183  case NC_FLOAT:
184  {
185  float vr[2];
186  vr[0] = (float) low;
187  vr[1] = (float) high;
188  status = nc_put_att_float(ncid, varid, "valid_min", NC_FLOAT, 1, &vr[0]);
189  if (status != NC_NOERR) {
190  printf("-E- %s %d: %s for %s\n",
191  __FILE__, __LINE__, nc_strerror(status), "valid_min");
192  exit(1);
193  }
194  status = nc_put_att_float(ncid, varid, "valid_max", NC_FLOAT, 1, &vr[1]);
195  if (status != NC_NOERR) {
196  printf("-E- %s %d: %s for %s\n",
197  __FILE__, __LINE__, nc_strerror(status), "valid_max");
198  exit(1);
199  }
200  }
201  break;
202  case NC_DOUBLE:
203  {
204  double vr[2];
205  vr[0] = low;
206  vr[1] = high;
207  status = nc_put_att_double(ncid, varid, "valid_min", NC_DOUBLE, 1, &vr[0]);
208  if (status != NC_NOERR) {
209  printf("-E- %s %d: %s for %s\n",
210  __FILE__, __LINE__, nc_strerror(status), "valid_min");
211  exit(1);
212  }
213  status = nc_put_att_double(ncid, varid, "valid_max", NC_DOUBLE, 1, &vr[1]);
214  if (status != NC_NOERR) {
215  printf("-E- %s %d: %s for %s\n",
216  __FILE__, __LINE__, nc_strerror(status), "valid_max");
217  exit(1);
218  }
219  }
220  break;
221  default:
222  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
223  fprintf(stderr, "Got unsupported number type (%d) ", nt);
224  fprintf(stderr, "while trying to create NCDF variable, \"%s\", ", sname);
225  return (EXIT_FAILURE);
226  }
227  }
228 
229  /* Add a "units" attribute if one is specified */
230  if (units != NULL && *units != 0) {
231  status = nc_put_att_text(ncid, varid, "units", strlen(units), units);
232  if (status != NC_NOERR) {
233  printf("-E- %s %d: %s for %s\n",
234  __FILE__, __LINE__, nc_strerror(status), "units");
235  exit(1);
236  }
237  }
238 
239  /* Add a "standard_name" attribute if one is specified */
240  if (standard_name != NULL && *standard_name != 0) {
241  status = nc_put_att_text(ncid, varid, "standard_name",
242  strlen(standard_name), standard_name);
243  if (status != NC_NOERR) {
244  printf("-E- %s %d: %s for %s\n",
245  __FILE__, __LINE__, nc_strerror(status), "standard_name");
246  exit(1);
247  }
248  }
249 
250  return 0;
251 }
252 
253 
254 
255 
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
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:98