OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
ncio.c
Go to the documentation of this file.
1 /*******************************************************************
2 
3  ncio.c - a set of convenience netcdf functions
4 
5  (not all netcdf operations require a convenience function, but in
6  some cases, these are nice)
7 
8  ncio_dim_siz - for a dimension name, return a dimension size
9  ncio_grab_stdscl_ds - get an entire dataset and scale it using netcdf
10  standards
11  *******************************************************************/
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <netcdf.h>
16 #include <stdlib.h>
17 
18 int ncio_dim_siz(int ncid, char *dim_nm)
19 /*******************************************************************
20 
21  ncio_dim_siz
22 
23  purpose: find the size of a named dimension
24 
25  Returns type: int - size of dimension or -1 if problem
26 
27  Parameters: (in calling order)
28  Type Name I/O Description
29  ---- ---- --- -----------
30  int ncid I netcdf id of file
31  char * dim_nm I name of dimension
32 
33  Modification history:
34  Programmer Date Description of change
35  ---------- ---- ---------------------
36  W. Robinson 1 Aug 2013 original development
37 
38  *******************************************************************/ {
39  int dim_id, status, ret;
40  size_t dim_len;
41 
42  status = nc_inq_dimid(ncid, dim_nm, &dim_id);
43  if (status != NC_NOERR) {
44  printf("%s, %d: nc_inq_dimid failure\n", __FILE__, __LINE__);
45  ret = -1;
46  } else {
47  status = nc_inq_dimlen(ncid, dim_id, &dim_len);
48  if (status != NC_NOERR) {
49  printf("%s, %d: nc_inq_dimid failure\n", __FILE__, __LINE__);
50  ret = -1;
51  } else
52  ret = (int) dim_len;
53  }
54  return ret;
55 }
56 
57 int ncio_grab_f_ds(int ncid, char *ds_name, float *data)
58 /*******************************************************************
59 
60  ncio_grab_f_ds
61 
62  purpose: grab dataset and return it in float format
63 
64  Returns type: int - 0 if OK, -1 if problem
65 
66  Parameters: (in calling order)
67  Type Name I/O Description
68  ---- ---- --- -----------
69  int ncid I netcdf id of file
70  char * ds_name I name of dataset to read
71  float * data O pointer to a pre-allocated
72  array to receive the data
73 
74  Modification history:
75  Programmer Date Description of change
76  ---------- ---- ---------------------
77  W. Robinson 1 Aug 2013 original development
78 
79  *******************************************************************/ {
80  int status;
81  int var_id;
82  /*
83  * get ID of the dataset
84  */
85  if ((status = nc_inq_varid(ncid, ds_name, &var_id)) != NC_NOERR) {
86  printf("%s, %d: nc_inq_varid returned error %d\n", __FILE__, __LINE__,
87  status);
88  return -1;
89  }
90  /*
91  * read the dataset as a float
92  */
93  if ((status = nc_get_var_float(ncid, var_id, data)) != NC_NOERR) {
94  printf("%s, %d: nc_get_var_float returned error %d\n", __FILE__, __LINE__,
95  status);
96  return -1;
97  }
98  return 0;
99 }
100 
101 int ncio_grab_stdsclf_ds(int ncid, char *ds_name, float fill, float *data)
102 /*******************************************************************
103 
104  ncio_grab_stdsclf_ds
105 
106  purpose: grab dataset with standard scaling, floating point values
107  read an entire dataset that is scaled and return as unscaled
108  float
109 
110  This uses the netcdf standard dataset attributes scale_factor and add_offset
111  to convert the scaled values in the dataset into real values and uses
112  attributes _FillValue and missing_value to identify non-values and replace
113  them with a value you choose
114 
115  Returns type: int - 0 if OK, -1 if problem
116 
117  Parameters: (in calling order)
118  Type Name I/O Description
119  ---- ---- --- -----------
120  int ncid I netcdf id of file
121  char * ds_name I name of dataset to read
122  float fill I a value to put in the output
123  dataset for any fill or
124  missing values - preferably
125  something not in normal data
126  range
127  float * data O pointer to a pre-allocated
128  array to receive the data
129 
130  Modification history:
131  Programmer Date Description of change
132  ---------- ---- ---------------------
133  W. Robinson 1 Aug 2013 original development
134 
135  *******************************************************************/ {
136  int ndim, dim_ids[100], status, ntot, i;
137  int var_id;
138  size_t dim_len;
139  float scale, offset, fillv, missv;
140  char dim_nam[NC_MAX_NAME + 1];
141  /*
142  * get ID of the dataset
143  */
144  if ((status = nc_inq_varid(ncid, ds_name, &var_id)) != NC_NOERR) {
145  printf("%s, %d: nc_inq_varid returned error %d\n", __FILE__, __LINE__,
146  status);
147  return -1;
148  }
149  /*
150  * get all the needed attribute for the dataset
151  */
152  if ((status = nc_get_att_float(ncid, var_id, "scale_factor", &scale))
153  != NC_NOERR) {
154  printf("%s, %d: nc_get_att_float returned error %d\n", __FILE__, __LINE__,
155  status);
156  return -1;
157  }
158  if ((status = nc_get_att_float(ncid, var_id, "add_offset", &offset))
159  != NC_NOERR) {
160  printf("%s, %d: nc_get_att_float returned error %d\n", __FILE__, __LINE__,
161  status);
162  return -1;
163  }
164  if ((status = nc_get_att_float(ncid, var_id, "_FillValue", &fillv))
165  != NC_NOERR) {
166  printf("%s, %d: nc_get_att_float returned error %d\n", __FILE__, __LINE__,
167  status);
168  return -1;
169  }
170  status = nc_get_att_float(ncid, var_id, "missing_value", &missv);
171  if ((status = nc_get_att_float(ncid, var_id, "missing_value", &missv))
172  != NC_NOERR) {
173  printf("%s, %d: nc_get_att_float returned error %d\n", __FILE__, __LINE__,
174  status);
175  return -1;
176  }
177  /*
178  * get the size for de-scaling use
179  */
180  if ((status = nc_inq_var(ncid, var_id, NULL, NULL, &ndim, dim_ids, NULL))
181  != NC_NOERR) {
182  printf("%s, %d: nc_inq_var returned error %d\n", __FILE__, __LINE__,
183  status);
184  return -1;
185  }
186  ntot = 1;
187  for (i = 0; i < ndim; i++) {
188  if ((status = nc_inq_dim(ncid, *(dim_ids + i), dim_nam, &dim_len))
189  != NC_NOERR) {
190  printf("%s, %d: nc_inq_dim returned error %d\n", __FILE__, __LINE__,
191  status);
192  return -1;
193  }
194  ntot *= dim_len;
195  }
196  /*
197  * read the dataset as a float and convert the data values to unscaled values
198  * and fill, missing values to the desired fill
199  */
200  if ((status = nc_get_var_float(ncid, var_id, data)) != NC_NOERR) {
201  printf("%s, %d: nc_get_var_float returned error %d\n", __FILE__, __LINE__,
202  status);
203  return -1;
204  }
205  for (i = 0; i < ntot; i++)
206  *(data + i) = (*(data + i) == fillv) || (*(data + i) == missv) ?
207  fill : *(data + i) * scale + offset;
208  return 0;
209 }
int status
Definition: l1_czcs_hdf.c:32
#define NULL
Definition: decode_rs.h:63
int ncio_grab_stdsclf_ds(int ncid, char *ds_name, float fill, float *data)
Definition: ncio.c:101
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
int ncio_dim_siz(int ncid, char *dim_nm)
Definition: ncio.c:18
int ncio_grab_f_ds(int ncid, char *ds_name, float *data)
Definition: ncio.c:57
l2prod offset
int i
Definition: decode_rs.h:71