ocssw  1.0
/disk01/web/ocssw/build/src/geogen_modis/GEO_locate_one_granule.c (r8102/r7924)
Go to the documentation of this file.
00001 #include <time.h>
00002 #include "hdf.h"
00003 #include "HdfEosDef.h"
00004 #include "mapi.h"
00005 #include "GEO_earth.h"
00006 #include "GEO_input.h"
00007 #include "GEO_main_func.h"
00008 #include "GEO_output.h"
00009 #include "PGS_MODIS_35251.h"
00010 
00011 PGSt_SMF_status GEO_locate_one_granule(
00012     char            l1a_file_name[PGSd_PC_FILE_PATH_MAX],
00013     char            geo_file_name[PGSd_PC_FILE_PATH_MAX],
00014     GEO_param_struct const  * geo_params,
00015     int         const version
00016     )
00017 /*
00018 !C*****************************************************************************
00019 !Description:   
00020         subroutine in input group of the Level-1A geolocation
00021                 software to geolocate one granule.  It opens the Level 1A 
00022         file and creates the geocation output file using the
00023         MAPI routine.  It calls routines to read and prepare the 
00024         Level 1A data for geolocation, geolocate each scan and
00025         write to the file, write the metadata and close the files.
00026 
00027 !Input Parameters:
00028                 l1a_file_name   path/file name string of the l1a input file
00029                 for the granule
00030                 geo_file_name   path/file name string of the geolocation output
00031                             file for the granule
00032                 geo_params  geolocation program parameters
00033                 version     version of L1A file accessed through the L1A LUN
00034 
00035 !Output Parameters:
00036                 None
00037 
00038 Return values:
00039         MODIS_E_BAD_INPUT_ARG   If any pointer argument is NULL
00040         MODIS_E_GEO     If any subroutine fails
00041         PGS_S_SUCCESS       Otherwise
00042 
00043 Externally defined:
00044         MAPIOK          mapi.h
00045         MODIS_E_BAD_INPUT_ARG   PGS_MODIS_35251.h
00046         MODIS_E_GEO     PGS_MODIS_35251.h
00047         NUM_TEMPS               GEO_parameters.h
00048         PGS_S_SUCCESS       PGS_SMF.h
00049         SUCCEED         hdf.h
00050         TEMP_FVALUE             GEO_geo.h
00051 
00052 Called by:      main
00053 
00054 Routines called:
00055      openMODISfile                  "mapi.h"
00056      GEO_prepare_l1a_data           "GEO_input.h"
00057      GEO_locate_one_scan            "GEO_output.h"
00058      GEO_initialize_product         "GEO_output.h"
00059      GEO_set_T_inst2sc              "GEO_earth.h"
00060      GEO_write_granule_metadata     "GEO_output.h"
00061      closeMODISfile                 "mapi.h"
00062          modsmf                         "smfio.h"
00063      SWopen                         "HdfEosDef.h"
00064      SWclose                        "HdfEosDef.h"
00065      createMAPIfilehandle           "mapi.h"
00066      releaseMAPIfilehandle          "mapi.h"
00067 
00068 !Revision History:
00069  * $Log: GEO_locate_one_granule.c,v $
00070  * Revision 6.3  2011/12/12 23:27:17  kuyper
00071  * Changed to not open the Geolocation product file unless and until metadata
00072  * is successfully extracted from the L1A file.
00073  * Changed to fail if a fatal error occurs in any scan.
00074  *
00075  * Revision 6.2  2011/02/14 21:07:28  kuyper
00076  * Corrected const-qualification of *geo_params.
00077  *
00078  * Revision 6.1  2010/06/07 17:59:25  kuyper
00079  * Helped resolve Bug 17 by initializing fill value for raw_mir_enc.
00080  * Helped resolve Bug 2470 by dropping maneuver_list.
00081  * Changed to expect status codes from all subroutines.
00082  * Changed order of parameters passed to GEO_write_granule_metadata().
00083  *
00084  * Revision 5.1  2004/09/10 14:41:18  vlin
00085  * 1. input parameter maneuver_list added
00086  * 2. initialize l1a_data.fill_values with fill values
00087  * 3. initialize l1a_data.temperatures with fill values
00088  * 4. Update calls to functions GEO_initialize_product,
00089  *    GEO_locate_one_scan, and GEO_write_granule_metadata.
00090  *
00091  * Revision 4.2  2004/06/01 16:35:43  kuyper
00092  * Changed to treat single-scan failures as non-fatal.
00093  *
00094  * Revision 4.1  2003/02/21 21:14:17  kuyper
00095  * Corrected to use void* pointers with %p format code.
00096 
00097 Requirements:
00098                 PR03-F-2.1-1
00099                 PR03-F-2.1-2
00100                 PR03-F-4.1-1
00101                 PR03-F-4.1-2
00102                 PR03-F-4.2-1
00103                 PR03-F-4.2-2
00104                 PR03-F-4.3-1
00105                 PR03-F-4.3-2
00106 
00107 !Team-unique Header:
00108                 This software is developed by the MODIS Science Data Support
00109                 Team for the National Aeronautics and Space Administration,
00110                 Goddard Space Flight Center, under contract NAS5-32373.
00111 
00112 !END*************************************************************************
00113 */
00114 {
00115     GEO_bcoord_struct bounding_coords;
00116     l1a_data_struct l1a_data;
00117     MODFILE *l1a_file; /* MAPI structure for the Level 1A file */
00118     qa_metadata_struct qa_metadata;
00119     int         i;
00120     int32   swfid;      /* HDF-EOS file ID. */
00121     int retval = PGS_S_SUCCESS;
00122     char msgbuf[1024];
00123     const char *function = NULL;
00124     const char *argument = "";
00125     char filefunc[] = __FILE__ ", GEO_locate_one_granule";
00126 
00127     time_t tnow;
00128     struct tm *tmnow;
00129 
00130     /* Begin program logic */
00131     if (l1a_file_name == NULL || geo_file_name==NULL || geo_params==NULL)
00132     {
00133       sprintf(msgbuf,
00134     "l1a_file_name = %p, geo_file_name = %p, geo_params = %p",
00135     (void *)l1a_file_name, (void *)geo_file_name, (void *)geo_params);
00136       modsmf(MODIS_E_BAD_INPUT_ARG, msgbuf, filefunc);
00137 
00138       return MODIS_E_BAD_INPUT_ARG;
00139     }
00140 
00141     /* Open the input L1A granule file */
00142     if ((l1a_file = openMODISfile(l1a_file_name, "a")) == NULL)
00143     {
00144       sprintf(msgbuf, "openMODISfile(\"%s\")", l1a_file_name);
00145       modsmf(MODIS_E_GEO, msgbuf, filefunc);
00146 
00147       return MODIS_E_GEO;
00148     }
00149     
00150     /* Reset QA counters.   */
00151     qa_metadata.no_of_pixels = 0;
00152     qa_metadata.missingdata = 0;
00153     qa_metadata.outofboundsdata = 0;
00154     memset(qa_metadata.cumulated_gflags, 0,
00155     sizeof(qa_metadata.cumulated_gflags));
00156 
00157     /* Initialize boundaries.   */
00158     bounding_coords.northcoord = -0.5*PGS_PI;
00159     bounding_coords.southcoord = 0.5*PGS_PI;
00160     bounding_coords.eastcoord = PGS_PI;
00161     bounding_coords.westcoord = -PGS_PI;
00162     bounding_coords.easthemi_ebc = 0.0;
00163     bounding_coords.easthemi_wbc = PGS_PI;
00164     bounding_coords.westhemi_ebc = -PGS_PI;
00165     bounding_coords.westhemi_wbc = 0.0;
00166 
00167     l1a_data.fill_values.scan_number = (int16)0;
00168     l1a_data.fill_values.EV_start_time = (float64)-2E9;
00169     l1a_data.fill_values.SD_start_time = (float64)-2E9;
00170     l1a_data.fill_values.SV_start_time = (float64)-2E9;
00171     l1a_data.fill_values.SD_sun_zenith = (float32)GEO_DOUBLE_FILLVALUE;
00172     l1a_data.fill_values.SD_sun_azimuth = (float32)GEO_DOUBLE_FILLVALUE;
00173     l1a_data.fill_values.moon_vector = (float32)GEO_DOUBLE_FILLVALUE;
00174     l1a_data.fill_values.sun_ref = (float32)GEO_DOUBLE_FILLVALUE;
00175     l1a_data.fill_values.mirr_side = (uint16)-1;
00176     l1a_data.fill_values.raw_mir_enc = MAX_UINT16_VAL;
00177     l1a_data.fill_values.impulse_enc = (float64)GEO_DOUBLE_FILLVALUE;
00178     l1a_data.fill_values.impulse_time = (float64)GEO_DOUBLE_FILLVALUE;
00179     l1a_data.fill_values.L1_scan_quality = (int32)-1;
00180     l1a_data.fill_values.geo_scan_quality = (int8)-127;
00181     l1a_data.fill_values.EV_center_time = (float64)-2E9;
00182     l1a_data.fill_values.orb_pos = (float64)GEO_DOUBLE_FILLVALUE;
00183     l1a_data.fill_values.orb_vel = (float64)GEO_DOUBLE_FILLVALUE;
00184     l1a_data.fill_values.T_inst2ECR = (float64)GEO_DOUBLE_FILLVALUE;
00185     l1a_data.fill_values.attitude_angels =(float64)GEO_DOUBLE_FILLVALUE;
00186     l1a_data.fill_values.EV_frames = (int32)0;
00187     l1a_data.fill_values.SD_frames = (int32)0;
00188     l1a_data.fill_values.SV_frames = (int32)0;
00189     l1a_data.fill_values.num_impulse = (uint8)0;
00190     strcpy((char*)l1a_data.fill_values.Scan_type, "");
00191     for (i=0; i<NUM_TEMPS; i++)
00192     l1a_data.temperatures[i] = (float32)TEMP_FVALUE;
00193 
00194     /* Read and validate geolocation data from the L1A file */
00195     if(GEO_prepare_l1a_data(l1a_file, geo_params, &l1a_data)
00196     != PGS_S_SUCCESS)
00197     {
00198     function = "GEO_prepare_l1a_data";
00199     argument = l1a_file_name;
00200     }
00201     else if ( FAIL == (swfid = SWopen(geo_file_name, DFACC_CREATE)) )
00202     {
00203     function = "SWopen";
00204     argument = geo_file_name;
00205     }
00206     else
00207     {   /* Open the output geolocation product file */
00208     MODFILE *geo_file = createMAPIfilehandle(swfid);
00209 
00210     if(NULL == geo_file)
00211     {
00212         function = "createMAPIfilehandle";
00213         argument = geo_file_name;
00214     }
00215     else
00216     {
00217         int scan_number;
00218 
00219         if(GEO_initialize_product(l1a_data.num_scans,
00220         &l1a_data.fill_values, geo_file, swfid, geo_params)
00221         != PGS_S_SUCCESS)
00222         {
00223         function = "GEO_initialize_product";
00224         argument = l1a_file_name;
00225         }
00226         else if(GEO_set_T_inst2sc(&geo_params->coord_trans,
00227         &l1a_data.ECS_metadata) != PGS_S_SUCCESS)
00228         function = "GEO_set_T_inst2sc";
00229         else for (scan_number = 0; scan_number < l1a_data.num_scans;
00230             scan_number++)
00231         {
00232           if ((scan_number % 10) == 0)
00233         {
00234           time(&tnow);
00235           tmnow = localtime(&tnow);
00236           printf("scan: %ld out of %ld %s", scan_number, 
00237              l1a_data.num_scans, asctime(tmnow));
00238         }
00239 
00240         /* Geolocate one scan */
00241         PGSt_SMF_status status = GEO_locate_one_scan(geo_params,
00242             &l1a_data, scan_number, &qa_metadata, &bounding_coords,
00243             geo_file);
00244         if(status != PGS_S_SUCCESS)
00245         {
00246             sprintf(msgbuf, "GEO_locate_one_scan(%d)", scan_number);
00247             modsmf(MODIS_E_GEO, msgbuf, filefunc);
00248             /* No fatal errors are currently returned, but we plan to
00249              * add some in the future. */
00250             if(PGS_SMF_TestFatalLevel(status) != PGS_FALSE)
00251             retval = MODIS_E_GEO;
00252         } 
00253         }
00254 
00255         qa_metadata.retval = retval;
00256 
00257         /* Generate and write output metadata to geolocation and L1A files*/
00258         if(GEO_write_granule_metadata(geo_file, l1a_file, geo_params, 
00259         &bounding_coords, version, &l1a_data, &qa_metadata)
00260         != PGS_S_SUCCESS)
00261         {
00262         sprintf(msgbuf, "GEO_write_granule_metadata(\"%s\",\"%s\")",
00263             geo_file_name, l1a_file_name);
00264         modsmf(MODIS_E_GEO, msgbuf, filefunc);
00265 
00266         retval = MODIS_E_GEO;
00267         }
00268 
00269         if(MAPIOK != releaseMAPIfilehandle(&geo_file))
00270         {
00271         sprintf(msgbuf, "releaseMAPIfilehandle(\"%s\")", geo_file_name);
00272         modsmf(MODIS_E_GEO, msgbuf, filefunc);
00273         
00274         retval = MODIS_E_GEO;
00275         }
00276     }
00277 
00278     if(SUCCEED != SWclose(swfid))
00279     {
00280         sprintf(msgbuf, "SWclose(\"%s\")", geo_file_name);
00281         modsmf(MODIS_E_GEO, msgbuf, filefunc);
00282       
00283         retval = MODIS_E_GEO;
00284     }
00285     }
00286 
00287     if(function)
00288     {
00289     sprintf(msgbuf, "%s(\"%s\")", function, argument);
00290     modsmf(MODIS_E_GEO, msgbuf, filefunc);
00291       
00292     retval = MODIS_E_GEO;
00293     }
00294 
00295     if(closeMODISfile(&l1a_file)!=MAPIOK)
00296     {
00297     sprintf(msgbuf, "closeMODISfile(\"%s\")", l1a_file_name);
00298     modsmf(MODIS_E_GEO, msgbuf, filefunc);
00299       
00300     retval = MODIS_E_GEO;
00301     }
00302 
00303     return retval;
00304 }