OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
GEO_locate_one_granule.c
Go to the documentation of this file.
1 #include <time.h>
2 #include "hdf.h"
3 #include "HdfEosDef.h"
4 #include "mapi.h"
5 #include "GEO_earth.h"
6 #include "GEO_input.h"
7 #include "GEO_main_func.h"
8 #include "GEO_output.h"
9 #include "PGS_MODIS_35251.h"
10 
11 PGSt_SMF_status GEO_locate_one_granule(
12  char l1a_file_name[PGSd_PC_FILE_PATH_MAX],
13  char geo_file_name[PGSd_PC_FILE_PATH_MAX],
14  GEO_param_struct const * geo_params,
15  int const version
16  )
17 /*
18 !C*****************************************************************************
19 !Description:
20  subroutine in input group of the Level-1A geolocation
21  software to geolocate one granule. It opens the Level 1A
22  file and creates the geocation output file using the
23  MAPI routine. It calls routines to read and prepare the
24  Level 1A data for geolocation, geolocate each scan and
25  write to the file, write the metadata and close the files.
26 
27 !Input Parameters:
28  l1a_file_name path/file name string of the l1a input file
29  for the granule
30  geo_file_name path/file name string of the geolocation output
31  file for the granule
32  geo_params geolocation program parameters
33  version version of L1A file accessed through the L1A LUN
34 
35 !Output Parameters:
36  None
37 
38 Return values:
39  MODIS_E_BAD_INPUT_ARG If any pointer argument is NULL
40  MODIS_E_GEO If any subroutine fails
41  PGS_S_SUCCESS Otherwise
42 
43 Externally defined:
44  MAPIOK mapi.h
45  MODIS_E_BAD_INPUT_ARG PGS_MODIS_35251.h
46  MODIS_E_GEO PGS_MODIS_35251.h
47  NUM_TEMPS GEO_parameters.h
48  PGS_S_SUCCESS PGS_SMF.h
49  SUCCEED hdf.h
50  TEMP_FVALUE GEO_geo.h
51 
52 Called by: main
53 
54 Routines called:
55  openMODISfile "mapi.h"
56  GEO_prepare_l1a_data "GEO_input.h"
57  GEO_locate_one_scan "GEO_output.h"
58  GEO_initialize_product "GEO_output.h"
59  GEO_set_T_inst2sc "GEO_earth.h"
60  GEO_write_granule_metadata "GEO_output.h"
61  closeMODISfile "mapi.h"
62  modsmf "smfio.h"
63  SWopen "HdfEosDef.h"
64  SWclose "HdfEosDef.h"
65  createMAPIfilehandle "mapi.h"
66  releaseMAPIfilehandle "mapi.h"
67 
68 !Revision History:
69  * $Log: GEO_locate_one_granule.c,v $
70  * Revision 6.4 2013/06/13 21:21:20 jkuyper
71  * Expanded buffer to avoid buffer overflow.
72  *
73  * Revision 6.3 2011/12/12 23:27:17 kuyper
74  * Changed to not open the Geolocation product file unless and until metadata
75  * is successfully extracted from the L1A file.
76  * Changed to fail if a fatal error occurs in any scan.
77  *
78  * Revision 6.2 2011/02/14 21:07:28 kuyper
79  * Corrected const-qualification of *geo_params.
80  *
81  * Revision 6.1 2010/06/07 17:59:25 kuyper
82  * Helped resolve Bug 17 by initializing fill value for raw_mir_enc.
83  * Helped resolve Bug 2470 by dropping maneuver_list.
84  * Changed to expect status codes from all subroutines.
85  * Changed order of parameters passed to GEO_write_granule_metadata().
86  *
87  * Revision 5.1 2004/09/10 14:41:18 vlin
88  * 1. input parameter maneuver_list added
89  * 2. initialize l1a_data.fill_values with fill values
90  * 3. initialize l1a_data.temperatures with fill values
91  * 4. Update calls to functions GEO_initialize_product,
92  * GEO_locate_one_scan, and GEO_write_granule_metadata.
93  *
94  * Revision 4.2 2004/06/01 16:35:43 kuyper
95  * Changed to treat single-scan failures as non-fatal.
96  *
97  * Revision 4.1 2003/02/21 21:14:17 kuyper
98  * Corrected to use void* pointers with %p format code.
99 
100 Requirements:
101  PR03-F-2.1-1
102  PR03-F-2.1-2
103  PR03-F-4.1-1
104  PR03-F-4.1-2
105  PR03-F-4.2-1
106  PR03-F-4.2-2
107  PR03-F-4.3-1
108  PR03-F-4.3-2
109 
110 !Team-unique Header:
111  This software is developed by the MODIS Science Data Support
112  Team for the National Aeronautics and Space Administration,
113  Goddard Space Flight Center, under contract NAS5-32373.
114 
115 !END*************************************************************************
116 */
117 {
118  GEO_bcoord_struct bounding_coords;
120  MODFILE *l1a_file; /* MAPI structure for the Level 1A file */
121  qa_metadata_struct qa_metadata;
122  int i;
123  int32 swfid; /* HDF-EOS file ID. */
124  int retval = PGS_S_SUCCESS;
125  char msgbuf[1024];
126  const char *function = NULL;
127  const char *argument = "";
128  char filefunc[] = __FILE__ ", GEO_locate_one_granule";
129  time_t tnow;
130 
131  /* Begin program logic */
132  if (l1a_file_name == NULL || geo_file_name==NULL || geo_params==NULL)
133  {
134  sprintf(msgbuf,
135  "l1a_file_name = %p, geo_file_name = %p, geo_params = %p",
136  (void *)l1a_file_name, (void *)geo_file_name, (void *)geo_params);
137  modsmf(MODIS_E_BAD_INPUT_ARG, msgbuf, filefunc);
138 
139  return MODIS_E_BAD_INPUT_ARG;
140  }
141 
142  /* Open the input L1A granule file */
143  if ((l1a_file = openMODISfile(l1a_file_name, "a")) == NULL)
144  {
145  sprintf(msgbuf, "openMODISfile(\"%s\")", l1a_file_name);
146  modsmf(MODIS_E_GEO, msgbuf, filefunc);
147 
148  return MODIS_E_GEO;
149  }
150 
151  /* Reset QA counters. */
152  qa_metadata.no_of_pixels = 0;
153  qa_metadata.missingdata = 0;
154  qa_metadata.outofboundsdata = 0;
155  memset(qa_metadata.cumulated_gflags, 0,
156  sizeof(qa_metadata.cumulated_gflags));
157 
158  /* Initialize boundaries. */
159  bounding_coords.northcoord = -0.5*PGS_PI;
160  bounding_coords.southcoord = 0.5*PGS_PI;
161  bounding_coords.eastcoord = PGS_PI;
162  bounding_coords.westcoord = -PGS_PI;
163  bounding_coords.easthemi_ebc = 0.0;
164  bounding_coords.easthemi_wbc = PGS_PI;
165  bounding_coords.westhemi_ebc = -PGS_PI;
166  bounding_coords.westhemi_wbc = 0.0;
167 
168  l1a_data.fill_values.scan_number = (int16)0;
169  l1a_data.fill_values.EV_start_time = (float64)-2E9;
170  l1a_data.fill_values.SD_start_time = (float64)-2E9;
171  l1a_data.fill_values.SV_start_time = (float64)-2E9;
172  l1a_data.fill_values.SD_sun_zenith = (float32)GEO_DOUBLE_FILLVALUE;
173  l1a_data.fill_values.SD_sun_azimuth = (float32)GEO_DOUBLE_FILLVALUE;
174  l1a_data.fill_values.moon_vector = (float32)GEO_DOUBLE_FILLVALUE;
175  l1a_data.fill_values.sun_ref = (float32)GEO_DOUBLE_FILLVALUE;
176  l1a_data.fill_values.mirr_side = (uint16)-1;
177  l1a_data.fill_values.raw_mir_enc = MAX_UINT16_VAL;
178  l1a_data.fill_values.impulse_enc = (float64)GEO_DOUBLE_FILLVALUE;
179  l1a_data.fill_values.impulse_time = (float64)GEO_DOUBLE_FILLVALUE;
180  l1a_data.fill_values.L1_scan_quality = (int32)-1;
181  l1a_data.fill_values.geo_scan_quality = (int8)-127;
182  l1a_data.fill_values.EV_center_time = (float64)-2E9;
183  l1a_data.fill_values.orb_pos = (float64)GEO_DOUBLE_FILLVALUE;
184  l1a_data.fill_values.orb_vel = (float64)GEO_DOUBLE_FILLVALUE;
185  l1a_data.fill_values.T_inst2ECR = (float64)GEO_DOUBLE_FILLVALUE;
186  l1a_data.fill_values.attitude_angels =(float64)GEO_DOUBLE_FILLVALUE;
187  l1a_data.fill_values.EV_frames = (int32)0;
188  l1a_data.fill_values.SD_frames = (int32)0;
189  l1a_data.fill_values.SV_frames = (int32)0;
190  l1a_data.fill_values.num_impulse = (uint8)0;
191  strcpy((char*)l1a_data.fill_values.Scan_type, "");
192  for (i=0; i<NUM_TEMPS; i++)
193  l1a_data.temperatures[i] = (float32)TEMP_FVALUE;
194 
195  /* Read and validate geolocation data from the L1A file */
196  if(GEO_prepare_l1a_data(l1a_file, geo_params, &l1a_data)
197  != PGS_S_SUCCESS)
198  {
199  function = "GEO_prepare_l1a_data";
200  argument = l1a_file_name;
201  }
202  else if ( FAIL == (swfid = SWopen(geo_file_name, DFACC_CREATE)) )
203  {
204  function = "SWopen";
205  argument = geo_file_name;
206  }
207  else
208  { /* Open the output geolocation product file */
209  MODFILE *geo_file = createMAPIfilehandle(swfid);
210 
211  if(NULL == geo_file)
212  {
213  function = "createMAPIfilehandle";
214  argument = geo_file_name;
215  }
216  else
217  {
218  int scan_number;
219 
220  if(GEO_initialize_product(l1a_data.num_scans,
221  &l1a_data.fill_values, geo_file, swfid, geo_params)
222  != PGS_S_SUCCESS)
223  {
224  function = "GEO_initialize_product";
225  argument = l1a_file_name;
226  }
227  else if(GEO_set_T_inst2sc(&geo_params->coord_trans,
228  &l1a_data.ECS_metadata) != PGS_S_SUCCESS)
229  function = "GEO_set_T_inst2sc";
230  else for (scan_number = 0; scan_number < l1a_data.num_scans;
231  scan_number++)
232  {
233  if (scan_number % 10 == 0) {
234  time(&tnow);
235  printf("scan: %d out of %d %s",
236  scan_number, l1a_data.num_scans, asctime(localtime(&tnow)));
237  }
238 
239  /* Geolocate one scan */
240  PGSt_SMF_status status = GEO_locate_one_scan(geo_params,
241  &l1a_data, scan_number, &qa_metadata, &bounding_coords,
242  geo_file);
243  if(status != PGS_S_SUCCESS)
244  {
245  sprintf(msgbuf, "GEO_locate_one_scan(%d)", scan_number);
246  modsmf(MODIS_E_GEO, msgbuf, filefunc);
247  /* No fatal errors are currently returned, but we plan to
248  * add some in the future. */
249  if(PGS_SMF_TestFatalLevel(status) != PGS_FALSE)
250  retval = MODIS_E_GEO;
251  }
252  }
253 
254  qa_metadata.retval = retval;
255 
256  /* Generate and write output metadata to geolocation and L1A files*/
257  if(GEO_write_granule_metadata(geo_file, l1a_file, geo_params,
258  &bounding_coords, version, &l1a_data, &qa_metadata)
259  != PGS_S_SUCCESS)
260  {
261  sprintf(msgbuf, "GEO_write_granule_metadata(\"%s\",\"%s\")",
262  geo_file_name, l1a_file_name);
263  modsmf(MODIS_E_GEO, msgbuf, filefunc);
264 
265  retval = MODIS_E_GEO;
266  }
267 
268  if(MAPIOK != releaseMAPIfilehandle(&geo_file))
269  {
270  sprintf(msgbuf, "releaseMAPIfilehandle(\"%s\")", geo_file_name);
271  modsmf(MODIS_E_GEO, msgbuf, filefunc);
272 
273  retval = MODIS_E_GEO;
274  }
275  }
276 
277  if(SUCCEED != SWclose(swfid))
278  {
279  sprintf(msgbuf, "SWclose(\"%s\")", geo_file_name);
280  modsmf(MODIS_E_GEO, msgbuf, filefunc);
281 
282  retval = MODIS_E_GEO;
283  }
284  }
285 
286  if(function)
287  {
288  sprintf(msgbuf, "%s(\"%s\")", function, argument);
289  modsmf(MODIS_E_GEO, msgbuf, filefunc);
290 
291  retval = MODIS_E_GEO;
292  }
293 
294  if(closeMODISfile(&l1a_file)!=MAPIOK)
295  {
296  sprintf(msgbuf, "closeMODISfile(\"%s\")", l1a_file_name);
297  modsmf(MODIS_E_GEO, msgbuf, filefunc);
298 
299  retval = MODIS_E_GEO;
300  }
301 
302  return retval;
303 }
integer, parameter int16
Definition: cubeio.f90:3
PGSt_SMF_status GEO_prepare_l1a_data(MODFILE *const l1a_file, GEO_param_struct const *geo_params, l1a_data_struct *const l1a_data)
PGSt_SMF_status GEO_set_T_inst2sc(const internal_coord_trans_struct *const, const ECS_metadata_struct *const)
int status
Definition: l1_czcs_hdf.c:32
double westhemi_ebc
Definition: GEO_geo.h:190
#define FAIL
Definition: ObpgReadGrid.h:18
#define NULL
Definition: decode_rs.h:63
#define MODIS_E_BAD_INPUT_ARG
#define NUM_TEMPS
double westhemi_wbc
Definition: GEO_geo.h:192
#define PGS_PI
Definition: GEO_geo.h:172
internal_coord_trans_struct coord_trans
PGSt_SMF_status GEO_locate_one_scan(GEO_param_struct const *const geo_params, l1a_data_struct *const l1a_data, int const scan_number, qa_metadata_struct *const qa_metadata, GEO_bcoord_struct *const bounding_coords, MODFILE *const geo_file)
int16_t * l1a_data
Definition: l1a_seawifs.c:84
#define TEMP_FVALUE
Definition: GEO_geo.h:154
#define MODIS_E_GEO
#define MAX_UINT16_VAL
Definition: GEO_geo.h:175
#define GEO_DOUBLE_FILLVALUE
Definition: GEO_output.h:106
double westcoord
Definition: GEO_geo.h:185
double easthemi_ebc
Definition: GEO_geo.h:186
double southcoord
Definition: GEO_geo.h:184
PGSt_SMF_status GEO_locate_one_granule(char l1a_file_name[PGSd_PC_FILE_PATH_MAX], char geo_file_name[PGSd_PC_FILE_PATH_MAX], GEO_param_struct const *geo_params, int const version)
double eastcoord
Definition: GEO_geo.h:183
PGSt_SMF_status GEO_write_granule_metadata(MODFILE *const geo_file, MODFILE *const l1a_file, GEO_param_struct const *const geo_parameter, GEO_bcoord_struct const *const bounding_coords, int const version, l1a_data_struct *const l1a_data, qa_metadata_struct *const qa_metadata)
double easthemi_wbc
Definition: GEO_geo.h:188
this program makes no use of any feature of the SDP Toolkit that could generate such a then geolocation is calculated at that and then aggregated up to Resolved feature request Bug by adding three new int8 SDSs for each high resolution offsets between the high resolution geolocation and a bi linear interpolation extrapolation of the positions This can be used to reconstruct the high resolution geolocation Resolved Bug by delaying cumulation of gflags until after validation of derived products Resolved Bug by setting Latitude and Longitude to the correct fill resolving to support Near Real Time because they may be unnecessary if use of entrained ephemeris and attitude data is turned resolving bug report Corrected to filter out Aqua attitude records with missing status helping resolve bug MOD_PR03 will still correctly write scan and pixel data that does not depend upon the start time
Definition: HISTORY.txt:248
PGSt_SMF_status GEO_initialize_product(int const number_of_scans, fill_values_struct const *fill_values, MODFILE *const geo_file, int32 const swfid, GEO_param_struct const *GEO_param)
double northcoord
Definition: GEO_geo.h:182
int i
Definition: decode_rs.h:71
uint32 cumulated_gflags[8]
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
version
Definition: setup.py:15