OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l3despeckle_input.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "l3despeckle_input.h"
6 
7 int input_init(instr *input_str) {
8  input_str->infile[0] = '\0';
9  input_str->ofile[0] = '\0';
10  input_str->pfile[0] = '\0';
11 
12  strcpy(input_str->out_parm, "DEFAULT");
13  strcpy(input_str->repimg, "ORIGINAL");
14 
15  input_str->tflag = 'O';
16 
17  input_str->syear = 9999;
18  input_str->sday = 999;
19 
20  input_str->eyear = 9999;
21  input_str->eday = 999;
22 
23  input_str->sorbit = -1;
24  input_str->eorbit = -1;
25 
26  input_str->reduce_fac = 1;
27 
28  input_str->noext = 0;
29 
30  input_str->merged[0] = '\0';
31 
32  input_str->loneast = +180;
33  input_str->lonwest = -180;
34  input_str->latnorth = +90;
35  input_str->latsouth = -90;
36 
37  input_str->verbose = 0;
38 
39  return 0;
40 }
41 
42 /*-----------------------------------------------------------------------------
43  Function: l3despeckle_input
44 
45  Returns: int (status)
46  The return code is a negative value if any error occurs, otherwise,
47  returns 0.
48 
49  Description:
50  Convert the arguments from the command line into a structure input
51  variable.
52 
53  Parameters: (in calling order)
54  Type Name I/O Description
55  ---- ---- --- -----------
56  int argc I number of arguments
57  char **argv I list of arguments
58  instr input O structure variable for inputs
59 
60 ----------------------------------------------------------------------------*/
61 
62 int l3despeckle_input(int argc, char **argv, instr *input) {
63  int i;
64  char str_buf[4096];
65 
66  if (argc == 1) return (-1);
67 
68  /* */
69  /* Set input values to defaults */
70  /* */
71  if (input_init(input) != 0) {
72  printf("-E- %s: Error initializing input structure.\n", __FILE__);
73  return (-1);
74  }
75 
76  /* */
77  /* Loop through command arguments and update input struct */
78  /* */
79  for (i = 1; i < argc; i++)
80  if (get_item(argv[i], input) != 0)
81  return -1;
82 
83 
84  /* */
85  /* Build string of parameters for metadata */
86  /* */
87  sprintf(str_buf, "INFILE = %s|", input->infile);
88  strcat(input->parms, str_buf);
89  sprintf(str_buf, "OFILE = %s|", input->ofile);
90  strcat(input->parms, str_buf);
91  sprintf(str_buf, "PFILE = %s|", input->ofile);
92  strcat(input->parms, str_buf);
93 
94  sprintf(str_buf, "SYEAR = %ld|", input->syear);
95  strcat(input->parms, str_buf);
96  sprintf(str_buf, "EYEAR = %ld|", input->eyear);
97  strcat(input->parms, str_buf);
98 
99  sprintf(str_buf, "SDAY = %ld|", input->sday);
100  strcat(input->parms, str_buf);
101  sprintf(str_buf, "EDAY = %ld|", input->eday);
102  strcat(input->parms, str_buf);
103 
104  sprintf(str_buf, "SORBIT = %ld|", input->sorbit);
105  strcat(input->parms, str_buf);
106  sprintf(str_buf, "EORBIT = %ld|", input->eorbit);
107  strcat(input->parms, str_buf);
108 
109  sprintf(str_buf, "OUT_PARM = %s|", input->out_parm);
110  strcat(input->parms, str_buf);
111 
112  sprintf(str_buf, "TFLAG = %c|", input->tflag);
113  strcat(input->parms, str_buf);
114  sprintf(str_buf, "REPIMG = %s|", input->repimg);
115  strcat(input->parms, str_buf);
116 
117  sprintf(str_buf, "REDUCE_FAC = %ld|", input->reduce_fac);
118  strcat(input->parms, str_buf);
119 
120  sprintf(str_buf, "MERGED = %s|", input->merged);
121  strcat(input->parms, str_buf);
122 
123  sprintf(str_buf, "LONEAST = %f|", input->loneast);
124  strcat(input->parms, str_buf);
125 
126  sprintf(str_buf, "LONWEST = %f|", input->lonwest);
127  strcat(input->parms, str_buf);
128 
129  sprintf(str_buf, "LATNORTH = %f|", input->latnorth);
130  strcat(input->parms, str_buf);
131 
132  sprintf(str_buf, "LATSOUTH = %f|", input->latsouth);
133  strcat(input->parms, str_buf);
134 
135  sprintf(str_buf, "VERBOSE = %ld|", input->verbose);
136  strcat(input->parms, str_buf);
137 
138  return 0;
139 }
140 
141 int get_item(char *arg, instr *input) {
142  char *tmp_str;
143  char keyword [20];
144  char parm_str[4096];
145  char tmp_file[FILENAME_MAX];
146  int ilen1, iret;
147 
148  if ((tmp_str = strchr(arg, '=')) == NULL) {
149  printf("Invalid argument \"%s\"\n", arg);
150  return -1;
151  }
152 
153  ilen1 = tmp_str - arg;
154  strncpy(keyword, arg, ilen1);
155  keyword[ilen1] = '\0';
156  strcpy(parm_str, tmp_str + 1);
157 
158  /* change keyword to lower case */
159  tmp_str = keyword;
160  while (*tmp_str != '\0') {
161  if (isupper(*tmp_str)) *tmp_str = tolower(*tmp_str);
162  tmp_str++;
163  }
164 
165  if (strncmp(keyword, "parfile", 3) == 0) {
166  iret = par_file(parm_str, input);
167  return iret;
168 
169  } else if (strcmp(keyword, "in") == 0) {
170  parse_file_name(parm_str, tmp_file);
171  strcpy(input->infile, tmp_file);
172 
173  } else if (strcmp(keyword, "out") == 0) {
174  parse_file_name(parm_str, tmp_file);
175  strcpy(input->ofile, tmp_file);
176 
177  } else if (strcmp(keyword, "infile") == 0) {
178  parse_file_name(parm_str, tmp_file);
179  strcpy(input->infile, tmp_file);
180 
181  } else if (strcmp(keyword, "ofile") == 0) {
182  parse_file_name(parm_str, tmp_file);
183  strcpy(input->ofile, tmp_file);
184 
185  } else if (strcmp(keyword, "pfile") == 0) {
186  parse_file_name(parm_str, tmp_file);
187  strcpy(input->pfile, tmp_file);
188 
189  } else if (strcmp(keyword, "rep") == 0) {
190  strcpy(input->repimg, parm_str);
191 
192  } else if (strcmp(keyword, "tflag") == 0) {
193  memcpy(&input->tflag, parm_str, 1);
194 
195  } else if (strcmp(keyword, "syear") == 0) {
196  input->syear = atoi(parm_str);
197 
198  } else if (strcmp(keyword, "eyear") == 0) {
199  input->eyear = atoi(parm_str);
200 
201  } else if (strcmp(keyword, "sday") == 0) {
202  input->sday = atoi(parm_str);
203 
204  } else if (strcmp(keyword, "eday") == 0) {
205  input->eday = atoi(parm_str);
206 
207  } else if (strcmp(keyword, "orbit1") == 0) {
208  input->sorbit = atoi(parm_str);
209 
210  } else if (strcmp(keyword, "orbit2") == 0) {
211  input->eorbit = atoi(parm_str);
212 
213  } else if (strcmp(keyword, "gsfcqual") == 0) {
214 
215  } else if (strcmp(keyword, "out_parm") == 0) {
216  strcpy(input->out_parm, ":");
217  strcat(input->out_parm, parm_str);
218  strcat(input->out_parm, ":");
219 
220  } else if (strcmp(keyword, "reduce_fac") == 0) {
221  input->reduce_fac = atoi(parm_str);
222 
223  } else if (strcmp(keyword, "noext") == 0) {
224  input->noext = atoi(parm_str);
225 
226  } else if (strcmp(keyword, "merged") == 0) {
227  parse_file_name(parm_str, tmp_file);
228  strcpy(input->merged, tmp_file);
229 
230  } else if (strcmp(keyword, "loneast") == 0) {
231  input->loneast = atof(parm_str);
232 
233  } else if (strcmp(keyword, "lonwest") == 0) {
234  input->lonwest = atof(parm_str);
235 
236  } else if (strcmp(keyword, "latnorth") == 0) {
237  input->latnorth = atof(parm_str);
238 
239  } else if (strcmp(keyword, "latsouth") == 0) {
240  input->latsouth = atof(parm_str);
241 
242  } else if (strcmp(keyword, "verbose") == 0) {
243  input->verbose = atoi(parm_str);
244 
245  } else {
246  goto Invalid_return;
247 
248  }
249 
250  return 0;
251 
252 
253 Invalid_return:
254  printf("Invalid argument \"%s\"\n", arg);
255  return -1;
256 }
257 
258 int par_file(char *pfile, instr *input) {
259  FILE *fp;
260  char arg[2048];
261  int iret;
262 
263  if ((fp = fopen(pfile, "r")) == NULL) {
264  printf("Error on opening the parameter file - %s\n", pfile);
265  return -1;
266  }
267 
268  while ((fgets(arg, 2047, fp)) != NULL) {
269  /* skip the comment or blank line */
270  if (arg[0] == '#' || arg[0] == ';' || arg[0] == ' ' || arg[0] == '\0' ||
271  arg[0] == '\n')
272  continue;
273 
274  arg[strlen(arg) - 1] = '\0'; /* replace the last char new_line to NULL */
275  iret = get_item(arg, input);
276  if (iret != 0) {
277  fclose(fp);
278  return -1;
279  }
280  }
281 
282  fclose(fp);
283  return 0;
284 }
#define NULL
Definition: decode_rs.h:63
instr * input
int par_file(char *pfile, instr *input)
void parse_file_name(const char *inpath, char *outpath)
int input_init(instr *input_str)
PARAM_TYPE_NONE Default value No parameter is buried in the product name name_prefix is case insensitive string compared to the product name PARAM_TYPE_VIS_WAVE The visible wavelength bands from the sensor are buried in the product name The product name is compared by appending and name_suffix ie aph_412_giop where prod_ix will be set to PARAM_TYPE_IR_WAVE same search method as PARAM_TYPE_VIS_WAVE except only wavelength above are looped through but prod_ix is still based ie aph_2_giop for the second and prod_ix set to PARAM_TYPE_INT name_prefix is compared with the beginning of the product name If name_suffix is not empty the it must match the end of the product name The characters right after the prefix are read as an integer and prod_ix is set to that number strncpy(l2prod->name_prefix, "myprod", UNITLEN)
int l3despeckle_input(int argc, char **argv, instr *input)
int get_item(char *arg, instr *input)
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")