OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
GEO_write_input_metadata.c
Go to the documentation of this file.
1 #include "PGS_EPH.h"
2 #include "GEO_earth.h"
3 #include "GEO_main.h"
4 #include "GEO_output.h"
5 #include "GEO_product.h"
6 #include "PGS_MODIS_35251.h"
7 
8 PGSt_SMF_status GEO_write_input_metadata(
9  MODFILE * const geo_file,
10  int const l1a_version,
11  PGSt_integer const param_version,
12  pointer_metadata_struct * const pointer_metadata,
13  PGSt_tag sc_tag
14 )
15 
16 /*******************************************************************************
17 !C
18 !Description:
19  Routine in Output group of the Level-1A geolocation software to write
20  the input pointer metadata.
21 
22 !Input Parameters:
23  geo_file MAPI structure for geolocation file
24  l1a_version Version number of input/output files.
25  param_version Version number of the parameter file.
26  sc_taq Spacecraft tag
27 
28 !Output Parameters:
29  None.
30 
31 !Input/Output Parameters:
32  pointer_metadata the universal references of geolocation inputs
33 
34 Return Parameters:
35  MODIS_E_BAD_INPUT_ARG If pointer_metadata is null.
36  MODIS_E_GEO If any subroutine failed
37  PGS_S_SUCCESS Otherwise
38 
39 Externally Defined:
40  ATTIT_INPUT "GEO_product.h"
41  EPHEM_INPUT "GEO_product.h"
42  l1aID "L1a_data.h"
43  MAPIOK "mapi.h"
44  MAX_EA_FILES "GEO_product.h"
45  MAX_EA_INPUTS "GEO_product.h"
46  MAX_NC_NAME "netcdf.h"
47  MODIS_E_BAD_INPUT_ARG "PGS_MODIS_35251.h"
48  MODIS_E_GEO "PGS_MODIS_35251.h"
49  MODIS_N_GEO_EXCESS_FILES "PGS_MOIDS_35251.h"
50  MODIS_W_GEO_MISSING_MET "PGS_MOIDS_35251.h"
51  PARAM "GEO_main.h"
52  PGSd_EOS_AM "PGS_TD.h"
53  PGSd_SC_ATTIT_DATA "PGS_EPH.h"
54  PGSd_SC_EPHEM_DATA "PGS_EPH.h"
55  PGSd_UR_FIELD_SIZE "PGS_EPH.h"
56  PGS_S_SUCCESS "PGS_SMF.h"
57 
58 Called by:
59  GEO_write_granule_metadata()
60 
61 Routines Called:
62  GEO_get_ephatt_inputs "GEO_earth.h"
63  modsmf "smfio.h"
64  PGS_PC_GetNumberOfFiles "PGS_PC.h"
65  PGS_PC_GetUniversalRef "PGS_PC.h"
66  putMODIStable "mapi.h"
67 
68 !Revision History:
69 $Log: GEO_write_input_metadata.c,v $
70 Revision 6.2 2012/06/25 15:45:02 kuyper
71 Corrected to insert null characters only where needed.
72 
73 Revision 6.1 2010/05/28 17:27:59 kuyper
74 Helped resolve Bug 2471 by changing to get epheris and atttitude input files
75  from a PCF entry rather than from the corresponding *.met files.
76 Moved MAX_EA_FILES, MAX_EA_INPUTS to GEO_product.h.
77 Changed to no longer assume that input_strings are nul-terminated, and to
78  ensure that output strings are nul-delimited.
79 
80 James Kuyper Jr. James.R.Kuyper@NASA.gov
81 
82 Revision 5.1 2008/09/21 19:00:19 kuyper
83 Added sc_tag argument, in order to enable supression of the error message
84  for missing attitude files for Terra runs.
85 
86 Revision 4.8 2004/04/09 21:09:36 kuyper
87 Corrected typo.
88 
89 Revision 4.7 2003/12/30 22:03:02 kuyper
90 Changed to initialize input_strings.
91 
92 Revision 4.6 2003/11/12 20:35:39 vlin
93 Function returns PGSt_SMF_status, check for null inputs.
94 
95 Revision 4.5 2003/10/23 21:40:05 kuyper
96 Increased value of MAX_EA_INPUTS. (sufficiently?)
97 
98 Revision 4.4 2003/08/28 16:02:50 kuyper
99 Corrected prolog.
100 
101 Revision 4.3 2003/08/26 21:18:10 kuyper
102 Minor re-write, to silence warnings from GNU compiler.
103 
104 Revision 4.2 2003/08/20 17:54:12 kuyper
105 Corrected direction of copies when merging input pointer strings.
106 
107 Revision 4.1 2003/08/12 22:11:22 kuyper
108 Initial revision.
109 
110 James Kuyper Jr. (kuyper@saicmodis.com)
111 
112 Requirements:
113  None
114 
115 !Team-unique Header:
116  This software is developed by the MODIS Science Data Support
117  Team for the National Aeronautics and Space Administration,
118  Goddard Space Flight Center, under contract NAS5-32373.
119 
120 References and Credits
121  None
122 
123 Design Notes
124 !END**************************************************************************
125 */
126 {
127  typedef char ephem_inputs_t[MAX_EA_INPUTS*PGSd_UR_FIELD_SIZE];
128  static ephem_inputs_t *ephem_inputs;
129  typedef char attit_inputs_t[MAX_EA_INPUTS*PGSd_UR_FIELD_SIZE];
130  static attit_inputs_t *attit_inputs;
131  if(!ephem_inputs) {
132  ephem_inputs = (ephem_inputs_t*) calloc(MAX_EA_FILES, sizeof(ephem_inputs_t));
133  attit_inputs = (attit_inputs_t*) calloc(MAX_EA_FILES, sizeof(attit_inputs_t));
134  }
135  static PGSt_integer num_ephem, num_attit, num_ptrs;
136  static ptrdiff_t ephem_length[MAX_EA_FILES], attit_length[MAX_EA_FILES];
137 
138  char input_strings[MAX_EA_INPUTS][PGSd_UR_FIELD_SIZE];
139 
140  PGSt_SMF_status retval = PGS_S_SUCCESS;
141  PGSt_integer ver;
142  /* temp_ver is needed, because PGS_PC_GetReference() updates the number
143  * pointed at by the pointer passed to it.
144  */
145  PGSt_integer temp_ver;
146  char attribname[MAX_NC_NAME];
147  int str;
148  char *p, *q;
149  char msgbuf[PGSd_UR_FIELD_SIZE*2];
150  char filefunc[] = __FILE__ ", GEO_write_input_metadata";
151 
152  if (geo_file == NULL || pointer_metadata == NULL)
153  {
154  sprintf(msgbuf,"geo_file: %p, pointer_metadata: %p", (void*)geo_file,
155  (void*)pointer_metadata);
156  modsmf(MODIS_E_BAD_INPUT_ARG, msgbuf, filefunc);
157  return MODIS_E_BAD_INPUT_ARG;
158  }
159 
160  temp_ver = (PGSt_integer)l1a_version;
161  if(PGS_PC_GetUniversalRef(l1aID, &temp_ver,
162  pointer_metadata->inputpointer[0]) != PGS_S_SUCCESS)
163  {
164  sprintf(msgbuf, "PGS_PC_GetUniversalRef(%ld,%d)",
165  (long)l1aID, l1a_version);
166  modsmf(MODIS_E_GEO, msgbuf, filefunc);
167 
168  retval = MODIS_E_GEO;
169  }
170 
171  if(num_ptrs == 0)
172  { /* Haven't yet initialized the constant inputs. */
173 
174  temp_ver = param_version;
175  if(PGS_PC_GetUniversalRef(PARAM, &temp_ver,
176  pointer_metadata->inputpointer[1]) != PGS_S_SUCCESS)
177  {
178  sprintf(msgbuf, "PGS_PC_GetUniversalRef(%ld,%d)",
179  (long)PARAM, param_version);
180  modsmf(MODIS_E_GEO, msgbuf, filefunc);
181 
182  retval = MODIS_E_GEO;
183  }
184 
185  num_ptrs = 2;
186 
187 
188  /* Check ephemeris files. */
189  if(PGS_PC_GetNumberOfFiles(PGSd_SC_EPHEM_DATA, &num_ephem)
190  != PGS_S_SUCCESS)
191  {
192  sprintf(msgbuf, "PGS_PC_GetNumberOfFiles(%ld)",
193  (long)PGSd_SC_EPHEM_DATA);
194  modsmf(MODIS_E_GEO, msgbuf, filefunc);
195 
196  retval = MODIS_E_GEO;
197  }
198 
199  if(num_ephem > MAX_EA_FILES)
200  {
201  /* It is perfectly legal to stage more files than needed. This
202  * shouldn't even be a warning.
203  */
204  sprintf(msgbuf, "%d ephemeris", MAX_EA_FILES);
205  modsmf(MODIS_N_GEO_EXCESS_FILES, msgbuf, filefunc);
206 
207  num_ephem = MAX_EA_FILES;
208  }
209 
210  for(ver=0; ver<num_ephem; ver++)
211  {
212  temp_ver = ver+1;
213  if(PGS_PC_GetUniversalRef(PGSd_SC_EPHEM_DATA, &temp_ver,
214  pointer_metadata->inputpointer[num_ptrs]) != PGS_S_SUCCESS)
215  {
216  sprintf(msgbuf, "PGS_PC_GetUniversalRef(%ld,%d)",
217  (long)PGSd_SC_EPHEM_DATA, ver+1);
218  modsmf(MODIS_E_GEO, msgbuf, filefunc);
219 
220  retval = MODIS_E_GEO;
221  }
222  else
223  ++num_ptrs;
224 
225 
226  if(GEO_get_ephatt_inputs(PGSd_SC_EPHEM_DATA, (PGSt_integer) (ver+1),
227  input_strings) != PGS_S_SUCCESS)
228  {
229  sprintf(msgbuf, "GEO_get_ephatt_inputs(%ld, %d)",
230  (long)PGSd_SC_EPHEM_DATA, ver+1);
231  modsmf(MODIS_E_GEO, msgbuf, filefunc);
232 
233  return MODIS_E_GEO;
234  }
235 
236  /* Catenate input strings into a single null-delimited string */
237  p = ephem_inputs[ver];
238  for(str=0; str<MAX_EA_INPUTS && input_strings[str][0]; str++)
239  {
240  /* Append input_strings[str] to ephem_inputs[ver] */
241  q = input_strings[str];
242 
243  while(q < (char*)(input_strings + str + 1) &&
244  (*p++ = *q++));
245  if(p[-1])
246  *p++ = '\0'; /* nul terminate */
247  }
248 
249  ephem_length[ver] = p - ephem_inputs[ver];
250  }
251 
252  /* Check Attitude Files */
253  if(PGS_PC_GetNumberOfFiles(PGSd_SC_ATTIT_DATA, &num_attit)
254  != PGS_S_SUCCESS && sc_tag!=PGSd_EOS_AM)
255  {
256  /* Attitude files are not always necessary for Terra runs; LUN
257  * 600280 might be set to "MODIS Packet".
258  */
259  sprintf(msgbuf, "PGS_PC_GetNumberOfFiles(%ld)",
260  (long)PGSd_SC_ATTIT_DATA);
261  modsmf(MODIS_E_GEO, msgbuf, filefunc);
262 
263  retval = MODIS_E_GEO;
264  }
265 
266  if(num_attit > MAX_EA_FILES)
267  {
268  /* It is perfectly legal to stage more files than needed. This
269  * shouldn't even be a warning.
270  */
271  sprintf(msgbuf, "%d attitude", MAX_EA_FILES);
272  modsmf(MODIS_N_GEO_EXCESS_FILES, msgbuf, filefunc);
273 
274  num_attit = MAX_EA_FILES;
275  }
276 
277  for(ver=0; ver<num_attit; ver++)
278  {
279  temp_ver = ver+1;
280  if(PGS_PC_GetUniversalRef(PGSd_SC_ATTIT_DATA, &temp_ver,
281  pointer_metadata->inputpointer[num_ptrs]) != PGS_S_SUCCESS)
282  {
283  sprintf(msgbuf, "PGS_PC_GetUniversalRef(%ld,%d)",
284  (long)PGSd_SC_ATTIT_DATA, ver+1);
285  modsmf(MODIS_E_GEO, msgbuf, filefunc);
286 
287  retval = MODIS_E_GEO;
288  }
289  else
290  ++num_ptrs;
291 
292  if(GEO_get_ephatt_inputs(PGSd_SC_ATTIT_DATA, (PGSt_integer)(ver+1),
293  input_strings) != PGS_S_SUCCESS)
294  {
295  sprintf(msgbuf, "GEO_get_ephatt_inputs(%ld, %d)",
296  (long)PGSd_SC_EPHEM_DATA, ver+1);
297  modsmf(MODIS_E_GEO, msgbuf, filefunc);
298 
299  return MODIS_E_GEO;
300  }
301 
302  /* Catenate input strings into a single null-delimited string */
303  p = attit_inputs[ver];
304  for(str=0; str<MAX_EA_INPUTS; str++)
305  {
306  /* Append input_strings[str] to attit_inputs[ver] */
307  q = input_strings[str];
308 
309  while(q < (char*)(input_strings + str + 1) &&
310  (*p++ = *q++));
311  if(p[-1])
312  *p++ = '\0'; /* nul terminate */
313  }
314 
315  attit_length[ver] = p - attit_inputs[ver];
316  }
317  }
318 
319  /* Write out the ephemeris/attitude input strings */
320  for(ver=0; ver<num_ephem; ver++)
321  {
322  sprintf(attribname, "%s.%d", EPHEM_INPUT, ver+1);
323 
324  if(putMODISfileinfo(geo_file, attribname, TXT, ephem_length[ver],
325  ephem_inputs[ver]) != MAPIOK)
326  {
327  sprintf(msgbuf, "putMODISfileinfo(\"%s\", \"%s\")",
328  geo_file->filename, attribname);
329  modsmf(MODIS_E_GEO, msgbuf, filefunc);
330 
331  retval = MODIS_E_GEO;
332  }
333  }
334 
335  for(ver=0; ver<num_attit; ver++)
336  {
337  sprintf(attribname, "%s.%d", ATTIT_INPUT, ver+1);
338 
339  if(putMODISfileinfo(geo_file, attribname, TXT, attit_length[ver],
340  attit_inputs[ver]) != MAPIOK)
341  {
342  sprintf(msgbuf, "putMODISfileinfo(\"%s\", \"%s\")",
343  geo_file->filename, attribname);
344  modsmf(MODIS_E_GEO, msgbuf, filefunc);
345 
346  retval = MODIS_E_GEO;
347  }
348  }
349 
350  return retval;
351 }
352 
PGSt_SMF_status GEO_write_input_metadata(MODFILE *const geo_file, int const l1a_version, PGSt_integer const param_version, pointer_metadata_struct *const pointer_metadata, PGSt_tag sc_tag)
data_t q
Definition: decode_rs.h:74
#define MAX_EA_FILES
Definition: GEO_product.h:253
#define MODIS_N_GEO_EXCESS_FILES
PGSt_SMF_status GEO_get_ephatt_inputs(PGSt_PC_Logical file_logical, PGSt_integer file_version, char universal_references[][PGSd_UR_FIELD_SIZE])
#define l1aID
Definition: GEO_main.h:67
#define MAX_NC_NAME
Definition: Granule.h:9
#define NULL
Definition: decode_rs.h:63
#define MODIS_E_BAD_INPUT_ARG
#define PARAM
Definition: GEO_main.h:69
#define MODIS_E_GEO
#define EPHEM_INPUT
Definition: GEO_product.h:251
#define ATTIT_INPUT
Definition: GEO_product.h:247
const char * str
Definition: l1c_msi.cpp:35
char inputpointer[INPUTPOINTERS][PGSd_PC_UREF_LENGTH_MAX]
#define MAX_EA_INPUTS
Definition: GEO_product.h:254
float p[MODELMAX]
Definition: atrem_corl1.h:131