sasze  0.0-0.dev0.dirty.el6
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
sas_utils.h
Go to the documentation of this file.
1 /*******************************************************************************
2 *
3 * COPYRIGHT (C) 2012 Battelle Memorial Institute. All Rights Reserved.
4 *
5 ********************************************************************************
6 *
7 * Authors:
8 *
9 * name: Brian D. Ermold
10 * phone: (509) 375-2277
11 * email: brian.ermold@pnnl.gov
12 *
13 *******************************************************************************/
14 
15 /** @file sas_utils.h
16  * SAS Ingest Utility Functions Header.
17  */
18 
19 #ifndef _SAS_UTILS
20 #define _SAS_UTILS
21 
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <string.h>
26 #include <time.h>
27 
28 #include "dsproc3.h"
29 
30 /**
31  * @defgroup SAS_UTILS SAS Utilities
32  */
33 /*@{*/
34 
35 /** The constant PI */
36 #define PI 3.1415926
37 
38 /** Missing value used in data values */
39 #define MISSING -9999
40 
41 /** Maximum number of wavelengths */
42 #define SAS_WL_MAX 2048
43 
44 /** Maximum length of a line in an input file */
45 #define SAS_LINE_MAX 32768
46 
47 /** Maximum number of data values per line */
48 #define SAS_VALS_MAX 4096
49 
50 #ifndef DEG_TO_RAD
51 /** Convert degrees to radians */
52 #define DEG_TO_RAD(a) (a * 0.017453292519943)
53 #endif
54 
55 #ifndef RAD_TO_DEG
56 /** Convert radians to degrees */
57 #define RAD_TO_DEG(a) (a * 57.29577951308232)
58 #endif
59 
60 #ifndef INTERPOLATE
61 /** Macro to interpolate between two values */
62 #define INTERPOLATE(x,x0,y0,x1,y1) (y0 + (x - x0) * ((y1 - y0)/(x1 - x0)))
63 #endif
64 
65 /**
66  * Convenience macro for initializing a variable in an output
67  * dataset and getting the index into its data array.
68  */
69 #define GET_REQUIRED_DS_VAR(dataset,varname,var,errval) \
70 if (!(var = dsproc_get_var(dataset, varname))) { \
71  DSPROC_ERROR( \
72  "Required Variable Not Found In Dataset", \
73  "Variable '%s' not found in dataset: %s\n", \
74  varname, dataset->name); \
75  return(errval); \
76 }
77 
78 /**
79  * Convenience macro for initializing a variable in an output
80  * dataset and getting the index into its data array.
81  */
82 #define ALLOC_DS_VAR(dataset,varname,datap,length,var,errval) \
83 GET_REQUIRED_DS_VAR(dataset,varname,var,errval); \
84 if (!(datap = dsproc_alloc_var_data_index(var, 0, length))) { \
85  return(errval); \
86 }
87 
88 /**
89  * Convenience macro for setting variable data in an output dataset.
90  */
91 #define SET_DS_VAR_DATA(dataset,varname,type,length,cvar,var,errval) \
92 GET_REQUIRED_DS_VAR(dataset,varname,var,errval); \
93 if (!dsproc_set_var_data(var,type, 0, length, NULL, cvar)) { \
94  return(errval); \
95 }
96 
97 /**
98  * Convenience macro for setting the value of an attribute
99  * if it exists in the specified CDSGroup or CDSVar.
100  */
101 #define SET_DS_ATT_IF_FOUND(parent,errval,name,format,...) \
102 if (dsproc_get_att(parent, name)) { \
103  if (!dsproc_set_att_text(parent, name, format, __VA_ARGS__)) { \
104  return(errval); \
105  } \
106 }
107 
108 /**
109  * SAS File List.
110  */
111 typedef struct {
112 
113  int nfiles; /**< number of files in the list */
114  char **files; /**< list of file names */
115  int nalloced; /**< allocated length of the file list */
116 
117 } SasFileList;
118 
119 /**
120  * SAS File Types.
121  */
122 typedef enum
123 {
124  SASHEVIS, /**< SASHE VIS Record */
125  SASHENIR, /**< SASHE NIR Record */
126  SASZEVIS, /**< SASZE VIS Record */
127  SASZENIR /**< SASZE NIR Record */
128 
129 } SasFileType;
130 
131 /**
132  * SAS Configuration Table.
133  */
134 typedef struct
135 {
136  char conf_dir[PATH_MAX]; /**< full path to the conf files directory */
137  char conf_file[PATH_MAX]; /**< name of the conf file */
138 
139  char header[2048]; /**< table file header */
140 
141  size_t nrows; /**< number of rows */
142  size_t ncols; /**< number of columns */
143  double **cols; /**< column vectors */
144 
145  double *buffer; /**< buffer used to parse the lines in the file */
146  size_t rows_alloced; /**< number of rows allocated */
147  size_t cols_alloced; /**< number of columns allocated */
148 
150 
151 /**
152  * SAS Data Record.
153  */
154 typedef struct {
155  time_t time;
156  int tag;
157  float earth_sun_dist;
158  float airmass;
159  float pressure_pa;
160  float temp_mio_p;
161  float temp_mio_p_f;
162  float temp_mio_trh;
163  float rh_mio;
164  float temp_collector;
165  float rh_collector;
166  float t_dewpt_collector;
167  float temp_chiller;
168  float rh_chiller;
169  float t_dewpt_chiller;
170  float x_tilt;
171  float y_tilt;
172  float x_tilt_stddev;
173  float y_tilt_stddev;
174  float band_az_raw;
175  float band_az_n;
176  float solar_azimuth;
177  float solar_zenith;
178  float inner_band_angle_raw;
179  float inner_band_angle;
180  float inner_band_scat_ang;
181  float outer_band_angle_raw;
182  float outer_band_angle;
183  float outer_band_scat_ang;
184  short shutter_open_tf;
185  float spectrometer_clock_ticks;
186  float t_avantes_bench;
187  float t_avantes_ad;
188  float t_int;
189  int n_avg;
190  float *spec;
191  int nalloced;
192 } SasRecord;
193 
194 /**
195  * SAS Raw Data Structure.
196  */
197 typedef struct {
198 
199  SasFileType file_type; /**< SAS file type */
200  const char *file_name; /**< name of the file being processed */
201  char full_path[PATH_MAX]; /**< full path to the raw file */
202  FILE *fp; /**< current file pointer */
203  int linenum; /**< line number in the current file */
204  char line[SAS_LINE_MAX]; /**< read line buffer */
205  double vals[SAS_VALS_MAX]; /**< parse line buffer */
206 
207  time_t begin_time; /**< time of the first record */
208  time_t end_time; /**< time of the last record */
209 
210  /* Raw Data Header */
211 
212  char serial_number[64]; /**< serial number of spectrometer */
213  char model_number[64]; /**< model number of system */
214  char collector_sn[64]; /**< serial number of collector */
215  int pixel_range[2]; /**< the pixel range */
216  char op_mode[4096]; /**< operating mode description */
217 
218  int nwavelens; /**< number of wavelengths */
219  double wavelens[SAS_WL_MAX]; /**< array of wavelengths */
220 
221  /* Raw Data Records */
222 
223  int nrecs; /**< number of records read in */
224  SasRecord *records; /**< array of raw data records */
225  int nalloced; /**< number of records allocated */
226  time_t *times; /**< array of all record times */
227  int *tags; /**< array of all record tags */
228 
229  /* Used for SASHE Data Processing */
230 
231  int max_recsets; /**< number of record sets allocated */
232  int nrecsets; /**< number of record sets found */
233  int *recsets; /**< start indexes of the record sets */
234  time_t *rs_times; /**< record set times */
235  double *rs_sza; /**< solar zenith angles */
236  double *rs_cossza; /**< cosines of solar zenith angles */
237  double *rs_coscor; /**< cosine corrections */
238  int rs_drk; /**< index offset of dark counts */
239  int rs_tot; /**< index offset of total irradiance */
240  int rs_sb1; /**< index offset of side blocked 1 */
241  int rs_blk; /**< index offset of sun blocked */
242  int rs_sb2; /**< index offset of side blocked 2 */
243 
244  SasConfigTable *Io_calib_table; /**< Io calibration table */
245  double Io_calib[SAS_WL_MAX]; /**< Io calibration values */
246 
247  /* Used for SASZE Data Processing */
248 
249  SasConfigTable *resp_table; /**< response function config table */
250 
251  int cal_max_ntimes; /**< number of cal_times allocated */
252  int cal_ntimes; /**< number of cal_times */
253  time_t *cal_times; /**< array of shutter open times */
254 
255  double dark_avg[SAS_WL_MAX]; /**< averages of darks counts */
256  double dark_sum2[SAS_WL_MAX]; /**< sums of darks counts squared */
257  double dark_std[SAS_WL_MAX]; /**< standard deviations of darks */
258  double resp_vals[SAS_WL_MAX]; /**< response function values */
259  int ndarks[SAS_WL_MAX]; /**< number of darks in average */
260  double toaIos[SAS_WL_MAX]; /**< interpolated Gueymard Io values */
261 
262 } SasRawData;
263 
264 int sas_add_file_to_list(SasFileList *list, const char *file_name);
265 void sas_clear_file_list(SasFileList *list);
266 
268 
269 time_t sas_conf_file_name_time(const char *file_name);
270 int sas_conf_file_name_time_compare(const void *str1, const void *str2);
271 
272 int sas_find_nearest_double(int n, double *x, double v);
273 int sas_find_nearest_float(int n, float *x, float v);
274 
276 
277 void sas_free_file_list(SasFileList *list);
278 
279 void sas_free_raw_data(SasRawData *raw);
280 
282  const char *conf_dir,
283  const char *pattern);
284 
286  SasConfigTable *table,
287  int x_col,
288  size_t x_nvals,
289  double *x_vals,
290  int y_col,
291  double *y_vals);
292 
294  SasConfigTable *table,
295  int x_col,
296  size_t x_nvals,
297  float *x_vals,
298  int y_col,
299  float *y_vals);
300 
302  SasConfigTable *table,
303  int x_col,
304  size_t x_nvals,
305  double *x_vals,
306  int y_col,
307  double *y_vals);
308 
310  SasConfigTable *table,
311  int x_col,
312  size_t x_nvals,
313  float *x_vals,
314  int y_col,
315  float *y_vals);
316 
318 
320  const char *conf_dir,
321  const char *conf_file,
322  size_t ncols,
323  SasConfigTable *table);
324 
325 int sas_print_config_table(const char *file, SasConfigTable *table);
326 
327 time_t sas_raw_file_name_time(const char *file_name);
328 int sas_raw_file_name_time_compare(const void *str1, const void *str2);
329 
331 
332 int sas_string_to_doubles(char *string, int buflen, double *buffer);
333 
334 /*@}*/
335 
336 /**
337  * @defgroup SAS_READ_DATA SAS Read Data
338  */
339 /*@{*/
340 
341 int sas_read_line(SasRawData *raw, char **line);
342 int sas_parse_header(SasRawData *raw);
343 int sas_parse_record(SasRawData *raw);
344 int sas_read_data(
345  const char *input_dir,
346  const char *file_name,
347  SasRawData *raw,
348  int raw_dsid);
349 
350 /*@}*/
351 
352 /**
353  * @defgroup SAS_STORE_DATA SAS Store Data
354  */
355 /*@{*/
356 
357 int sas_set_a0_metadata(SasRawData *raw, CDSGroup *dataset);
358 int sas_store_a0_data(SasRawData *raw, int dsid);
359 
360 /*@}*/
361 
362 #endif /* _SAS_UTILS */