OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
viirs_sim_sdr.c
Go to the documentation of this file.
1 /*-----------------------------------------------------------------------------
2  Program: viirs_sim_sdr
3 
4  Description: Create a VIIRS SDR file set using simulated data
5 
6  Arguments:
7  Type Name I/O Description
8  ---- ---- --- -----------
9  int argc I count of command line args - 3
10  char *[] argv I command line arguments:
11  [1] input geo data file name
12  [2] id to use in the output file name
13  and options
14 
15  Modification history:
16 
17  W. Robinson, SAIC 08 Oct 2008 Original development
18  W. Robinson, SAIC 21 Sep 2010 switch input to clo library
19 
20 ----------------------------------------------------------------------------*/
21 #include "viirs_sim_sdr.h"
22 #include "l12_parms.h"
23 #include <stdio.h>
24 
25 int main(int argc, char *argv[]) {
26  int iscn, ipx, ibnd, idet, n_bad, msec;
27  ctl_struc ctl;
28  sdr_info_struc sdr_info;
29  in_rec_struc in_rec;
30  out_rec_struc out_rec;
31  FILE *meta_id;
32  static float f0bar[MAX_BND];
33  double esdist;
34  double esdist_(int *, int *, int *);
35 
36  /*
37  * shorten output buffering
38  */
39  setvbuf(stdout, NULL, _IOLBF, 0);
40  setvbuf(stderr, NULL, _IOLBF, 0);
41 
42  if (viirs_sim_input(argc, argv, &ctl) != 0) exit(1);
43  /*
44  * set the origin (distributor) and domain
45  */
46  strcpy(sdr_info.origin, ctl.id_origin);
47  strcpy(sdr_info.domain, ctl.id_domain);
48  /*
49  * read in the VIIRS band center wavelengths, average F0
50  */
51  if (bnd_ix_2_sen_info("Lambda", (void *) in_rec.lam_band) < 0) {
52  printf("%s, %d: failure to read sensor information\n",
53  __FILE__, __LINE__);
54  }
55  out_rec.lam_band = in_rec.lam_band;
56  if (bnd_ix_2_sen_info("Fobar", (void *) f0bar) < 0) {
57  printf("%s, %d: failure to read sensor information\n",
58  __FILE__, __LINE__);
59  return 1;
60  }
61  /*
62  * set up the input file (get attributes, ready for dataset reading)
63  */
64  if (rd_sim_init(&ctl, &sdr_info, &in_rec) != 0) return 1;
65  /*
66  * get an initial fo for use in init_sdr
67  */
68  in_rec.year = sdr_info.year;
69  in_rec.yday = sdr_info.day;
70  msec = *sdr_info.scan_time * 1000.;
71  esdist = esdist_(&(in_rec.year), &(in_rec.yday), &msec);
72  for (ibnd = 0; ibnd < in_rec.nbnd; ibnd++)
73  in_rec.f0[ibnd] = RAD_CGS_2_MKS * pow(1.0 / esdist, 2) * f0bar[ibnd];
74  out_rec.f0 = &(in_rec.f0[0]);
75  printf("%s, %d: info: earth sun distance correction is %f\n",
76  __FILE__, __LINE__, esdist);
77  /*
78  * proceed to create the SDR file with waiting data arrays
79  */
80  if (init_sdr(&ctl, &sdr_info, &in_rec, &out_rec) != 0) return 1;
81  /*
82  * loop through the lines and create the data for the SDR
83  */
84  for (iscn = 0; iscn < in_rec.nscan; iscn++) {
85  /*
86  * the SDR scan read will also get and combine land and cloud info
87  */
88  if (rd_sdr_scan(iscn, &ctl, &sdr_info, &in_rec) != 0) return 1;
89  /*
90  * set some times in scan and create the f0 for the scan time
91  */
92  msec = *(sdr_info.scan_time + iscn) * 1000.;
93  in_rec.msec = msec;
94  esdist = esdist_(&(in_rec.year), &(in_rec.yday), &msec);
95  /* note F0 conversion below from loadl1.c */
96  for (ibnd = 0; ibnd < in_rec.nbnd; ibnd++)
97  in_rec.f0[ibnd] = RAD_CGS_2_MKS * pow(1.0 / esdist, 2) * f0bar[ibnd];
98 
99  out_rec.year = in_rec.year;
100  out_rec.yday = in_rec.yday;
101  out_rec.msec = in_rec.msec;
102  out_rec.f0 = &(in_rec.f0[0]);
103  /*
104  * note any bad values in the scan - only for VNIR
105  */
106  n_bad = 0;
107  for (idet = 0; idet < in_rec.ndet_scan; idet++)
108  for (ipx = 0; ipx < in_rec.npix; ipx++)
109  for (ibnd = 0; ibnd < N_VNIR_BND; ibnd++) {
110  if (*(in_rec.bnd_q[ibnd] + ipx + in_rec.npix * idet) != 0)
111  n_bad++;
112  }
113  printf("%s %d: scan: %d, # bad samples: %d\n", __FILE__, __LINE__,
114  iscn, n_bad);
115  /*
116  * This is where any artifact addition is done and possible conversion
117  * to aggregated
118  */
119  if ((ctl.any_artifact == 1) && (in_rec.scn_fmt != 0))
120  if (mod_artifact(&ctl, &in_rec) != 0) return 1;
121  /*
122  * convert scan format
123  */
124  if (scan_cvt(&in_rec, &out_rec) != 0)
125  return 1;
126  /*
127  * should we want to remove a vicarious gain, to be added in l2gen,
128  * do it here
129  */
130  if (ctl.vic_cal_chg == 1) {
131  for (ibnd = 0; ibnd < in_rec.nbnd; ibnd++)
132  for (idet = 0; idet < out_rec.ndet_scan; idet++)
133  for (ipx = 0; ipx < out_rec.npix; ipx++)
134  if (*(out_rec.bnd_q[ ibnd ] + ipx + idet * out_rec.ndet_scan)
135  == 0) {
136  *(out_rec.bnd_lt[ ibnd ] + ipx + idet * out_rec.npix) -=
137  ctl.offset[ibnd];
138  *(out_rec.bnd_lt[ ibnd ] + ipx + idet * out_rec.npix) /=
139  ctl.gain[ibnd];
140  }
141  }
142  /*
143  * if requested, add the bow tie artifact
144  * Only do so if scan format indicates aggregated data
145  */
146  if ((ctl.bowtie_opt == 1) && (out_rec.scn_fmt == 0)) {
147  for (idet = 0; idet < NDET; idet++) {
148  switch (idet) {
149  case 0:
150  case 15:
151  for (ibnd = 0; ibnd < in_rec.nbnd; ibnd++) {
152  for (ipx = 0; ipx < 1008; ipx++)
153  *(out_rec.bnd_q[ibnd] + idet * 3200 + ipx) = 1;
154  for (ipx = 2192; ipx < 3200; ipx++)
155  *(out_rec.bnd_q[ibnd] + idet * 3200 + ipx) = 1;
156  }
157  break;
158  case 1:
159  case 14:
160  for (ibnd = 0; ibnd < in_rec.nbnd; ibnd++) {
161  for (ipx = 0; ipx < 640; ipx++)
162  *(out_rec.bnd_q[ibnd] + idet * 3200 + ipx) = 1;
163  for (ipx = 2560; ipx < 3200; ipx++)
164  *(out_rec.bnd_q[ibnd] + idet * 3200 + ipx) = 1;
165  }
166  break;
167  }
168  }
169  }
170  /*
171  * and output the data
172  */
173  if (wr_sdr_scan(iscn, &out_rec) != 0) return 1;
174  }
175  /*
176  * finish, close files, write file info and exit
177  */
178  if (fin_sdr(&ctl, &in_rec, &out_rec) != 0) return 1;
179 
180  for (iscn = 0; iscn < out_rec.nbnd + 1; iscn++)
181  printf("Created file # %d: %s\n", iscn, sdr_info.sdr_files[iscn]);
182  printf("Path: %s\n", ctl.out_loc);
183  printf("Start date: %s\n", sdr_info.st_date);
184  printf("Start time: %s\n", sdr_info.st_time);
185  printf("End date: %s\n", sdr_info.en_date);
186  printf("End time: %s\n", sdr_info.en_time);
187  if (ctl.meta_use != 0) {
188  if ((meta_id = fopen(ctl.meta_file, "w")) == NULL) {
189  printf("%s, line %d: Failure to create the metadata file\n",
190  __FILE__, __LINE__);
191  return 1;
192  }
193  for (iscn = 0; iscn < out_rec.nbnd + 1; iscn++)
194  fprintf(meta_id, "%s\n", sdr_info.sdr_files[iscn]);
195  fprintf(meta_id, "%s\n", ctl.out_loc);
196  fprintf(meta_id, "%s\n", sdr_info.st_date);
197  fprintf(meta_id, "%s\n", sdr_info.st_time);
198  fprintf(meta_id, "%s\n", sdr_info.en_date);
199  fprintf(meta_id, "%s\n", sdr_info.en_time);
200  fclose(meta_id);
201  }
202 
203  printf("Successfully completed simulated SDR generation\n");
204  return 0;
205 }
#define NULL
Definition: decode_rs.h:63
int fin_sdr(ctl_struc *ctl, in_rec_struc *in_rec, out_rec_struc *out_rec)
Definition: fin_sdr.c:3
int32 * msec
Definition: l1_czcs_hdf.c:31
double esdist_(int32_t *year, int32_t *day, int32_t *msec)
#define MAX_BND
Definition: viirs_sim_sdr.h:34
int init_sdr(ctl_struc *ctl, sdr_info_struc *sdr_info, in_rec_struc *in_rec, out_rec_struc *out_rec)
Definition: init_sdr.c:14
int main(int argc, char *argv[])
Definition: viirs_sim_sdr.c:25
int bnd_ix_2_sen_info(char *pname, void *pval)
int rd_sim_init(ctl_struc *ctl, sdr_info_struc *sdr_info, in_rec_struc *in_rec)
Definition: rd_sim_init.c:4
#define RAD_CGS_2_MKS
Definition: viirs_sim_sdr.h:36
int viirs_sim_input(int argc, char *argv[], ctl_struc *ctl)
int scan_cvt(in_rec_struc *in_rec, out_rec_struc *out_rec)
Definition: scan_cvt.c:5
int mod_artifact(ctl_struc *ctl, in_rec_struc *in_rec)
Definition: mod_artifact.c:4
real *8 function esdist(iyr, iday, msec)
Definition: esdist.f:3
int wr_sdr_scan(int, out_rec_struc *)
Definition: wr_sdr_scan.c:3
#define NDET
Definition: polcor.c:13
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
#define N_VNIR_BND
Definition: viirs_sim_sdr.h:31
int rd_sdr_scan(int iscn, ctl_struc *ctl, sdr_info_struc *sdr_info, in_rec_struc *in_rec)
Definition: rd_sdr_scan.c:4