Exemple #1
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_RD_IMLM( const char *flname, struct imlm_hdr *hdr,
		   struct imlm_rec **imlm_out )
{
     char   *cpntr, ctemp[SHORT_STRING_LENGTH];

     unsigned int numRec = 0u;

     struct imlm_rec *rec = NULL;
/*
 * strip path of file-name & remove extension ".gz"
 */
     if ( (cpntr = strrchr( flname, '/' )) != NULL ) {
          (void) nadc_strlcpy( ctemp, ++cpntr, SHORT_STRING_LENGTH );
     } else {
          (void) nadc_strlcpy( ctemp, flname, SHORT_STRING_LENGTH );
     }
     if ( (cpntr = strstr( ctemp, ".gz" )) != NULL ) *cpntr = '\0';
/*
 * initialize IMLM header structure
 */
     (void) nadc_strlcpy( hdr->product, ctemp, 42 );
     NADC_RECEIVEDATE( flname, hdr->receive_date );
     (void) strcpy( hdr->creation_date, "" );
     (void) strcpy( hdr->software_version, "" );
     hdr->file_size = nadc_file_size( flname );
     hdr->numRec    = 0u;
     imlm_out[0] = NULL;
/*
 * read header of the IMLM product
 */
     Read_IMLM_Header( flname, hdr );
     if ( IS_ERR_STAT_FATAL )
	  NADC_RETURN_ERROR( NADC_ERR_FATAL, "Read_IMLM_Header" );
     if ( hdr->numRec == 0u ) return;
/*
 * allocate enough space to store all records
 */
     rec = (struct imlm_rec *) 
	  malloc( hdr->numRec * sizeof( struct imlm_rec ));
     if ( rec == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "rec" );
/*
 * read records from the IMLM product
 */
     if ( (numRec = Read_IMLM_Record( flname, rec )) != hdr->numRec ) {
	  char msg[SHORT_STRING_LENGTH];

	  free( rec );
	  (void) snprintf( msg, SHORT_STRING_LENGTH,
			   "failed to read all records %u out of %u\n", 
			   numRec, hdr->numRec );
	  NADC_RETURN_ERROR( NADC_ERR_FILE_RD, msg );
     }
     imlm_out[0] = rec;
}
Exemple #2
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
int main( int argc, char *argv[] )
{
     bool flag_remove  = FALSE;
     bool flag_replace = FALSE;

     char flname[MAX_STRING_LENGTH];
     char *cpntr, ctemp[SHORT_STRING_LENGTH];

     int ncid;
     int retval;

     struct imap_hdr hdr;
     struct imap_rec *rec = NULL;

#ifdef _WITH_SQL
     PGconn *conn = NULL;
#endif
/*
 * check command-line parameters
 */
     if ( argc > 2 ) {
	  register int nr = 1;
	  do {
	       if ( strncmp( argv[nr], "-remove", 7 ) == 0 )
		    flag_remove = TRUE;
	       else if ( strncmp( argv[nr], "-replace", 8 ) == 0 )
		    flag_replace = TRUE;
	       else
		    NADC_GOTO_ERROR( NADC_ERR_PARAM, NADC_PARAMS );
	  } while( ++nr < (argc-1) );
	  (void) nadc_strlcpy( flname, argv[argc-1], MAX_STRING_LENGTH );
     } else if ( argc == 2 ) {
	  (void) nadc_strlcpy( flname, argv[1], MAX_STRING_LENGTH );
     } else
	  NADC_GOTO_ERROR( NADC_ERR_PARAM, NADC_PARAMS );
/*
 * strip path of file-name
 */
     if ( (cpntr = strrchr( flname, '/' )) != NULL ) {
          (void) nadc_strlcpy( ctemp, ++cpntr, SHORT_STRING_LENGTH );
     } else {
          (void) nadc_strlcpy( ctemp, flname, SHORT_STRING_LENGTH );
     }
/*
 * read the IMAP CH4 product
 */
     if ( (retval = nc_open( flname, NC_NOWRITE, &ncid )) != NC_NOERR )
	  NADC_GOTO_ERROR( NADC_ERR_FATAL, nc_strerror(retval) );
     SCIA_RD_NC_CH4_META( ncid, &hdr );
     if ( hdr.numProd > 1 ) 
	  NADC_GOTO_ERROR( NADC_ERR_FATAL, 
			   "no more than one orbit per ingest" );
     (void) nadc_strlcpy( hdr.product, ctemp, SHORT_STRING_LENGTH );
     NADC_RECEIVEDATE( flname, hdr.receive_date );
     hdr.file_size = nadc_file_size( flname );
     hdr.numRec = SCIA_RD_NC_CH4_REC( ncid, &rec );
     if ( (retval = nc_close( ncid )) != NC_NOERR )
	  NADC_GOTO_ERROR( NADC_ERR_FATAL, nc_strerror(retval) );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_FILE_RD, flname );
/*
 * connect to PostgreSQL database
 */
#ifdef _WITH_SQL
     CONNECT_NADC_DB( &conn, "scia" );
     if ( IS_ERR_STAT_FATAL ) {
	  NADC_ERROR( NADC_ERR_SQL, "IMAP (connect)" );
	  NADC_Err_Trace( stderr );
	  return NADC_ERR_FATAL;
     }
     if ( flag_remove || flag_replace ) {
	  SCIA_DEL_ENTRY_IMAP_CH4( conn, hdr.product );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_SQL, "IMAP (remove)" );
     }
/*
 * write meta-information to database
 */
     if ( ! flag_remove ) {
	  SCIA_WR_SQL_CH4_META( conn, &hdr );
	  if ( IS_ERR_STAT_FATAL ) {
	       NADC_ERROR( NADC_ERR_SQL, "IMAP (header)" );
	  } else {
	       SCIA_WR_SQL_CH4_TILE( conn, hdr.product, hdr.numRec, rec );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_ERROR( NADC_ERR_SQL, "IMAP (records)" );
	  }
     }
#endif
 done:
/*
 * close connection to PostgreSQL database
 */
#ifdef _WITH_SQL
     if ( conn != NULL ) PQfinish( conn );
#endif
/*
 * free allocated memory
 */
     if ( rec != NULL ) free( rec );
/*
 * display error messages
 */
     NADC_Err_Trace( stderr );
     if ( IS_ERR_STAT_FATAL )
          return NADC_ERR_FATAL;
     else
          return NADC_ERR_NONE;
}
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_RD_IMAP_CH4( bool qflag, const char *flname, struct imap_hdr *hdr,
		       struct imap_rec **imap_out )
{
     register unsigned int ni, nr;

     char   *cpntr, ctemp[SHORT_STRING_LENGTH];
     float  *rbuff;
     double *dbuff;

     hid_t   fid = -1;
     hsize_t dims[2];

     struct imap_rec *rec = NULL;
/*
 * strip path of file-name
 */
     if ( (cpntr = strrchr( flname, '/' )) != NULL ) {
          (void) nadc_strlcpy( ctemp, ++cpntr, SHORT_STRING_LENGTH );
     } else {
          (void) nadc_strlcpy( ctemp, flname, SHORT_STRING_LENGTH );
     }
/*
 * initialize IMAP header structure
 */
     (void) nadc_strlcpy( hdr->product, ctemp, ENVI_FILENAME_SIZE );
     NADC_RECEIVEDATE( flname, hdr->receive_date );
     (void) strcpy( hdr->creation_date, hdr->receive_date );
     (void) strcpy( hdr->l1b_product, "" );
     (void) strcpy( hdr->validity_start, "" );
     (void) strcpy( hdr->validity_stop, "" );
     (void) nadc_strlcpy( hdr->software_version, ctemp+9, 4 );
     if ( (cpntr = strrchr( ctemp, '_' )) != NULL ) {
	  char str_magic[5], str_orbit[6];

	  (void) nadc_strlcpy( str_magic, cpntr+1, 5 );
	  while ( --cpntr > ctemp ) if ( *cpntr == '_' ) break;
	  (void) nadc_strlcpy( str_orbit, cpntr+1, 6 );
	  hdr->counter[0] = 
	       (unsigned short) strtoul( str_magic, (char **) NULL, 10 );
	  hdr->orbit[0]   = 
	       (unsigned int) strtoul( str_orbit, (char **) NULL, 10 );
     } else {
	  hdr->counter[0] = 0u;
	  hdr->orbit[0]   = 0u;
     }
     hdr->numProd   = 1u;
     hdr->numRec    = 0u;
     hdr->file_size = nadc_file_size( flname );
     imap_out[0] = NULL;
/*
 * open IMAP product in original HDF5 format
 */
     H5E_BEGIN_TRY {
	  fid = H5Fopen( flname, H5F_ACC_RDONLY, H5P_DEFAULT );
     } H5E_END_TRY;
     if ( fid < 0 ) NADC_RETURN_ERROR( NADC_ERR_HDF_FILE, flname );

     (void) H5LTget_dataset_info( fid, "/Data/Geolocation/time", 
				  dims, NULL, NULL );
     if ( dims[0] == 0 ) return;
     hdr->numRec = (unsigned int) dims[0];
/*
 * allocate enough space to store all records
 */
     rec = (struct imap_rec *) 
	  malloc( hdr->numRec * sizeof( struct imap_rec ));
     if ( rec == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "rec" );

     if ( (dbuff = (double *) malloc( hdr->numRec * sizeof(double) )) == NULL )
	  NADC_RETURN_ERROR( NADC_ERR_ALLOC, "rbuff" );
     (void) H5LTread_dataset_double( fid, "/Data/Geolocation/time", dbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].jday = dbuff[ni];
     free( dbuff );

     if ( (rbuff = (float *) malloc( hdr->numRec * sizeof(float) )) == NULL )
	  NADC_RETURN_ERROR( NADC_ERR_ALLOC, "rbuff" );
     (void) H5LTread_dataset_float( fid, "/Data/Geolocation/Longitude", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ )
	  rec[ni].lon_center = LON_IN_RANGE( rbuff[ni] );
     (void) H5LTread_dataset_float( fid, "/Data/Geolocation/Latitude", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].lat_center = rbuff[ni];

     (void) H5LTread_dataset_float( fid, "/Data/FitResults/VCD_CH4", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].ch4_vcd = rbuff[ni];
     (void) H5LTread_dataset_float( fid, 
				    "/Data/FitResults/VCD_CH4_ERROR", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].ch4_error = rbuff[ni];
     (void) H5LTread_dataset_float( fid, 
				    "/Data/FitResults/VCD_CH4_MODEL", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].ch4_model = rbuff[ni];
     (void) H5LTread_dataset_float( fid, "/Data/FitResults/xVMR_CH4", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].ch4_vmr = rbuff[ni];
     (void) H5LTread_dataset_float( fid, "/Data/FitResults/VCD_CO2", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].co2_vcd = rbuff[ni];
     (void) H5LTread_dataset_float( fid, 
				    "/Data/FitResults/VCD_CO2_ERROR", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].co2_error = rbuff[ni];
     (void) H5LTread_dataset_float( fid, 
				    "/Data/FitResults/VCD_CO2_MODEL", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].co2_model = rbuff[ni];
/*
 * read auxiliary/geolocation data
 */
     (void) H5LTread_dataset_float( fid, "/Data/Geolocation/state_id", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) 
	  rec[ni].meta.stateID = (unsigned char) rbuff[ni];
     (void) H5LTread_dataset_float( fid, "/Data/Geolocation/IntegrationTime", 
				    rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) {
	  if ( rbuff[ni] == 0.12f ) 
	       rec[ni].meta.intg_time = 0.125f;
	  else
	  rec[ni].meta.intg_time = rbuff[ni];
     }
     (void) H5LTread_dataset_float( fid, "/Data/Auxiliary/pixels_ch4", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) 
	  rec[ni].meta.pixels_ch4 = (unsigned short) rbuff[ni];
     (void) H5LTread_dataset_float( fid, "/Data/Auxiliary/pixels_co2", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) 
	  rec[ni].meta.pixels_co2 = (unsigned short) rbuff[ni];
     (void) H5LTread_dataset_float( fid, "/Data/Geolocation/AirMassFactor", 
				    rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].meta.amf = rbuff[ni];
     (void) H5LTread_dataset_float( fid, 
				    "/Data/Geolocation/LineOfSightZenithAngle", 
				    rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].meta.lza = rbuff[ni];
     (void) H5LTread_dataset_float( fid, "/Data/Geolocation/SolarZenithAngle", 
				    rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].meta.sza = rbuff[ni];
     (void) H5LTread_dataset_float( fid, "/Data/Geolocation/ScanRange", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].meta.scanRange = rbuff[ni];
     SET_IMAP_BS_FLAG( rbuff, hdr->numRec, rec);
     (void) H5LTread_dataset_float( fid, "/Data/Geolocation/SurfaceElevation", 
				    rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].meta.elev = rbuff[ni];

     (void) H5LTread_dataset_float( fid, 
				    "/Data/Auxiliary/BU_ch4_window", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].meta.bu_ch4 = rbuff[ni];
     (void) H5LTread_dataset_float( fid, 
				    "/Data/Auxiliary/residual_ch4", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].meta.resi_ch4 = rbuff[ni];

     (void) H5LTread_dataset_float( fid, 
				    "/Data/Auxiliary/BU_co2_window", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].meta.bu_co2 = rbuff[ni];
     (void) H5LTread_dataset_float( fid, 
				    "/Data/Auxiliary/residual_co2", rbuff );
     for ( ni = 0; ni < hdr->numRec; ni++ ) rec[ni].meta.resi_co2 = rbuff[ni];
/*
 * read corners of the tiles
 */
     rbuff = (float *) realloc( rbuff, 
				NUM_CORNERS * hdr->numRec * sizeof(float) );
     if ( rbuff == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "rbuff" );
     (void) H5LTread_dataset_float( fid, "/Data/Geolocation/CornerLongitudes", 
				    rbuff );
     for ( ni = nr = 0; ni < hdr->numRec; ni++ ) {
	  register unsigned short nc = 0;

	  rec[ni].lon_corner[1] = LON_IN_RANGE( rbuff[nr] ); nr++;
	  rec[ni].lon_corner[0] = LON_IN_RANGE( rbuff[nr] ); nr++;
	  rec[ni].lon_corner[2] = LON_IN_RANGE( rbuff[nr] ); nr++;
	  rec[ni].lon_corner[3] = LON_IN_RANGE( rbuff[nr] ); nr++;
	  do {
	       if ( fabsf(rec[ni].lon_center-rec[ni].lon_corner[nc]) > 180.f ) {
		    if ( rec[ni].lon_center > 0.f )
			 rec[ni].lon_corner[nc] += 360.f;
		    else
			 rec[ni].lon_corner[nc] -= 360.f;
	       }
	  } while ( ++nc < NUM_CORNERS );
     }
     (void) H5LTread_dataset_float( fid, "/Data/Geolocation/CornerLatitudes", 
				    rbuff );
     for ( ni = nr = 0; ni < hdr->numRec; ni++ ) {
	  rec[ni].lat_corner[1] = rbuff[nr++];
	  rec[ni].lat_corner[0] = rbuff[nr++];
	  rec[ni].lat_corner[2] = rbuff[nr++];
	  rec[ni].lat_corner[3] = rbuff[nr++];
     }
     free( rbuff );
/*
 * select IMAP records
 */
     if ( qflag ) hdr->numRec = SELECT_IMAP_RECORDS( hdr->numRec, rec );

     /* obtain validity period from data records */
     if ( hdr->numRec > 0 ) {
	  SciaJDAY2adaguc( rec->jday, hdr->validity_start );
	  SciaJDAY2adaguc( rec[hdr->numRec-1].jday, hdr->validity_stop );
	  imap_out[0] = rec;
     } else
	  free( rec );
}
Exemple #4
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
int main( int argc, char *argv[] )
     /*@globals  errno, stderr, nadc_stat, nadc_err_stack;@*/
     /*@modifies errno, stderr, nadc_stat, nadc_err_stack@*/
{
     unsigned int num_dsd;

     FILE  *fp = NULL;

     struct param_record param;
     struct mph_envi     mph;
     struct sph_meris    sph;
     struct dsd_envi     *dsd = NULL;
/*
 * initialization of command-line parameters
 */
     MERIS_SET_PARAM( argc, argv, MERIS_LEVEL_1, &param );
     if ( IS_ERR_STAT_FATAL ) 
          NADC_GOTO_ERROR( NADC_ERR_PARAM, "" );
/*
 * check if we have to display version and exit
 */
     if ( param.flag_version == PARAM_SET ) {
	  MERIS_SHOW_VERSION( stdout, "meris_fr1" );
	  exit( EXIT_SUCCESS );
     }
/*
 * dump command-line parameters
 */
     if ( param.flag_show == PARAM_SET ) {
          MERIS_SHOW_PARAM( MERIS_LEVEL_1, param );
          exit( EXIT_SUCCESS );
     }
/*
 * open input-file
 */
     if ( (fp = fopen( param.infile, "r" )) == NULL )
	  NADC_GOTO_ERROR( NADC_ERR_FILE, param.infile );
/*
 * create output file
 */
     if ( param.write_hdf5 == PARAM_SET ) {
          param.hdf_file_id = MERIS_CRE_H5_FILE( MERIS_LEVEL_1, &param );
          if ( IS_ERR_STAT_FATAL )
               NADC_GOTO_ERROR( NADC_ERR_HDF_CRE, "HDF5 base" );
          MERIS_WR_H5_VERSION( param.hdf_file_id );
     }
/*
 * -------------------------
 * read Main Product Header
 */
     ENVI_RD_MPH( fp, &mph );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "MPH" );
     if ( mph.tot_size != nadc_file_size( param.infile ) )
          NADC_GOTO_ERROR( NADC_ERR_FATAL, "file size check failed" );
     if ( param.write_ascii == PARAM_SET ) {
	  ENVI_WR_ASCII_MPH( param, &mph );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "MPH" );
     }
/*      if ( param.write_hdf5 == PARAM_SET ) { */
/* 	  SCIA_WR_H5_MPH( param, &mph ); */
/* 	  if ( IS_ERR_STAT_FATAL ) */
/* 	       NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "MPH" ); */
/*      } */
/*
 * -------------------------
 * read Specific Product Header
 */
     MERIS_RD_SPH( fp, mph, &sph );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "SPH" );
     if ( param.write_ascii == PARAM_SET ) {
	  MERIS_WR_ASCII_SPH( param, &sph );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "SPH" );
     }
/*      if ( param.write_hdf5 == PARAM_SET ) { */
/* 	  SCIA_OL2_WR_H5_SPH( param, &sph ); */
/* 	  if ( IS_ERR_STAT_FATAL ) */
/* 	       NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "SPH" ); */
/*      } */
/*
 * -------------------------
 * read Data Set Descriptor records
 */
     dsd = (struct dsd_envi *)
	  malloc( (mph.num_dsd-1) * sizeof( struct dsd_envi ) );
     if ( dsd == NULL ) NADC_GOTO_ERROR( NADC_ERR_ALLOC, "dsd" );
     num_dsd = ENVI_RD_DSD( fp, mph, dsd );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "DSD" );
     if ( param.write_ascii == PARAM_SET ) {
	  ENVI_WR_ASCII_DSD( param, num_dsd, dsd );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "DSD" );
     }
/*
 * when an error has occurred we jump to here:
 */
 done:
/*
 * close input file
 */
     if ( fp != NULL ) (void) fclose( fp );
/*
 * close HDF5 output file
 */
     if ( param.write_hdf5 == PARAM_SET ) {
	  if ( param.hdf_file_id >= 0 && H5Fclose( param.hdf_file_id ) < 0 )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_FILE, param.hdf5_name );
     }
/*
 * free allocated memory
 */
     if ( dsd != NULL ) free( dsd );
/*
 * display error messages?
 */
     if ( param.flag_silent == PARAM_UNSET ) NADC_Err_Trace( stderr );
     if ( IS_ERR_STAT_FATAL ) 
	  return NADC_ERR_FATAL;
     else
	  return NADC_ERR_NONE;
}
Exemple #5
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
int main( int argc, char *argv[] )
     /*@globals  errno, stderr, nadc_stat, nadc_err_stack;@*/
     /*@modifies errno, stderr, nadc_stat, nadc_err_stack@*/
{
     register unsigned short n_nfit, n_lfit, n_ofit;

     unsigned int nr_state, num_dsd, num_dsr, num_geo;

     FILE  *fp = NULL;
#ifdef _WITH_SQL
     PGconn *conn = NULL;
#endif
     struct param_record param;
     struct mph_envi     mph;
     struct sph_sci_ol   sph;
     struct dsd_envi     *dsd = NULL;
     struct sqads_sci_ol *sqads;
     struct lads_scia    *lads;
     struct state2_scia  *state;
     struct ngeo_scia    *ngeo = NULL;
     struct lgeo_scia    *lgeo;
     struct cld_sci_ol   *cld;
     struct nfit_scia    *nfit;
     struct lfit_scia    *lfit, *ofit;
     struct lcld_scia   *lcld;
/*
 * initialization of command-line parameters
 */
     SCIA_SET_PARAM( argc, argv, SCIA_LEVEL_2, &param );
     if ( IS_ERR_STAT_FATAL ) 
          NADC_GOTO_ERROR( NADC_ERR_PARAM, "" );
/*
 * check if we have to display version and exit
 */
     if ( param.flag_version == PARAM_SET ) {
	  SCIA_SHOW_VERSION( stdout, "scia_ol2" );
	  exit( EXIT_SUCCESS );
     }
/*
 * dump command-line parameters
 */
     if ( param.flag_show == PARAM_SET ) {
          SCIA_SHOW_PARAM( SCIA_LEVEL_2, param );
          exit( EXIT_SUCCESS );
     }
/*
 * connect to PostgreSQL database
 */
#ifdef _WITH_SQL
     if ( param.write_sql == PARAM_SET ) {
	  CONNECT_NADC_DB( &conn, "scia" );
	  if ( IS_ERR_STAT_FATAL ) 
	       NADC_GOTO_ERROR( NADC_ERR_SQL, "PSQL" );
	  if ( param.flag_sql_remove == PARAM_SET 
	       || param.flag_sql_replace == PARAM_SET )
	       SCIA_OL2_DEL_ENTRY( conn, param.flag_verbose, param.infile );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_SQL, "PSQL(remove)" );
	  if ( param.flag_sql_remove == PARAM_SET ) goto done;
     }
#endif
/*
 * open input-file
 */
     if ( (fp = fopen( param.infile, "r" )) == NULL )
	  NADC_GOTO_ERROR( NADC_ERR_FILE, param.infile );
/*
 * create output HDF5 file
 */
     if ( param.write_hdf5 == PARAM_SET ) {
	  param.hdf_file_id = SCIA_CRE_H5_FILE( SCIA_LEVEL_2, &param );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_CRE, "HDF5 base" );
	  SCIA_WR_H5_VERSION( param.hdf_file_id );
/*
 * create for data structures for SCIAMACHY level 1b data
 */
	  CRE_SCIA_OL2_H5_STRUCTS( param );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_CRE, "STRUCTS" );
     }
/*
 * -------------------------
 * read Main Product Header
 */
     ENVI_RD_MPH( fp, &mph );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "MPH" );
     if ( mph.tot_size != nadc_file_size( param.infile ) )
	  NADC_GOTO_ERROR( NADC_ERR_FATAL, "file size check failed" );
     if ( param.write_ascii == PARAM_SET ) {
	  ENVI_WR_ASCII_MPH( param, &mph );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "MPH" );
     }
     if ( param.write_hdf5 == PARAM_SET ) {
	  SCIA_WR_H5_MPH( param, &mph );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "MPH" );
     }
/*
 * -------------------------
 * read Specific Product Header
 */
     SCIA_OL2_RD_SPH( fp, mph, &sph );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "SPH" );
     if ( param.write_ascii == PARAM_SET ) {
	  SCIA_OL2_WR_ASCII_SPH( param, &sph );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "SPH" );
     }
     if ( param.write_hdf5 == PARAM_SET ) {
	  SCIA_OL2_WR_H5_SPH( param, &sph );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "SPH" );
     }
/*
 * -------------------------
 * read Data Set Descriptor records
 */
     dsd = (struct dsd_envi *)
	  malloc( (mph.num_dsd-1) * sizeof( struct dsd_envi ) );
     if ( dsd == NULL ) NADC_GOTO_ERROR( NADC_ERR_ALLOC, "dsd" );
     num_dsd = ENVI_RD_DSD( fp, mph, dsd );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "DSD" );
     if ( param.write_ascii == PARAM_SET ) {
	  ENVI_WR_ASCII_DSD( param, num_dsd, dsd );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "DSD" );
     }
     if ( param.write_meta == PARAM_SET ) goto done;
#ifdef _WITH_SQL
     if ( param.write_sql == PARAM_SET ) {
	  const char dsd_name[] = "LEVEL_1B_PRODUCT";

	  unsigned int indx_dsd = ENVI_GET_DSD_INDEX( num_dsd, dsd, dsd_name );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_PDS_RD, dsd_name );
	  SCIA_OL2_WR_SQL_META( conn, param.flag_verbose, param.infile, 
				dsd[indx_dsd].flname, &mph, &sph );
	  if ( IS_ERR_STAT_WARN ) goto done;
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_SQL, "SQL_META" );
     }
#endif
/*
 * -------------------------
 * read/write Summary of Quality Flags per State records
 */
     num_dsr = SCIA_OL2_RD_SQADS( fp, num_dsd, dsd, &sqads );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "SQADS" );
     if ( num_dsr > 0 ) {
	  if ( param.write_ascii == PARAM_SET ) {
	       SCIA_OL2_WR_ASCII_SQADS( param, num_dsr, sqads );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "SQADS" );
	  }
	  if ( param.write_hdf5 == PARAM_SET ) {
	       SCIA_OL2_WR_H5_SQADS( param, num_dsr, sqads );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "SQADS" );
	  }
	  free( sqads );
     }
/*
 * -------------------------
 * read/write Geolocation of the States
 */
     num_dsr = SCIA_RD_LADS( fp, num_dsd, dsd, &lads );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "LADS" );
     if ( num_dsr > 0 ) {
	  if ( param.write_ascii == PARAM_SET ) {
	       SCIA_WR_ASCII_LADS( param, num_dsr, lads );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "LADS" );
	  }
	  if ( param.write_hdf5 == PARAM_SET ) {
	       SCIA_WR_H5_LADS( param, num_dsr, lads );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "LADS" );
	  }
	  free( lads );
     }
/*
 * -------------------------
 * read/write Static Parameters
 */
/*
 * -------------------------
 * read/write States of the Product
 */
     nr_state = SCIA_LV2_RD_STATE( fp, num_dsd, dsd, &state );
     if ( IS_ERR_STAT_FATAL || nr_state  ==  0 )
          NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "STATE" );

     if ( param.write_ascii == PARAM_SET ) {
	  SCIA_LV2_WR_ASCII_STATE( param, nr_state, state );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "STATE" );
     }
     if ( param.write_hdf5 == PARAM_SET ) {
	  SCIA_LV2_WR_H5_STATE( param, nr_state, state );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "STATE" );
     }
#ifdef _WITH_SQL
     if ( param.write_sql == PARAM_SET ) {
	  SCIA_OL2_MATCH_STATE( conn, param.flag_verbose, 
				&mph, nr_state, state );
	  if ( IS_ERR_STAT_FATAL )
               NADC_GOTO_ERROR( NADC_ERR_SQL, "SQL_STATE" );
     }
#endif
     free( state );
/*
 * -------------------------
 * read/write Nadir Geolocation
 */
     num_geo = SCIA_OL2_RD_NGEO( fp, num_dsd, dsd, &ngeo );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "NGEO" );
     if ( num_geo > 0 ) {
	  if ( param.write_ascii == PARAM_SET ) {
	       SCIA_OL2_WR_ASCII_NGEO( param, num_geo, ngeo );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "NGEO" );
	  }
	  if ( param.write_hdf5 == PARAM_SET ) {
	       SCIA_OL2_WR_H5_NGEO( param, num_geo, ngeo );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "NGEO" );
	  }
     }
/*
 * -------------------------
 * read/write Limb Geolocation
 */
     num_dsr = SCIA_OL2_RD_LGEO( fp, num_dsd, dsd, &lgeo );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "LGEO" );
     if ( num_dsr > 0 ) {
	  if ( param.write_ascii == PARAM_SET ) {
	       SCIA_OL2_WR_ASCII_LGEO( param, num_dsr, lgeo );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "LGEO" );
	  }
	  if ( param.write_hdf5 == PARAM_SET ) {
	       SCIA_OL2_WR_H5_LGEO( param, num_dsr, lgeo );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "LGEO" );
	  }
	  free( lgeo );
     }
/*
 * -------------------------
 * read/write Clouds and Aerosol data
 */
     if ( param.write_cld == PARAM_SET ) {
	  num_dsr = SCIA_OL2_RD_CLD( fp, num_dsd, dsd, &cld );
	  if ( IS_ERR_STAT_FATAL ) 
	       NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "CLD" );
	  if ( num_dsr > 0 ) {
	       if ( param.write_ascii == PARAM_SET ) {
                    SCIA_OL2_WR_ASCII_CLD( param, num_dsr, cld );
                    if ( IS_ERR_STAT_FATAL )
                         NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "CLD" );
               }
               if ( param.write_hdf5 == PARAM_SET ) {
                    SCIA_OL2_WR_H5_CLD( param, num_dsr, cld );
                    if ( IS_ERR_STAT_FATAL )
                         NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "CLD" );
               }
#ifdef _WITH_SQL
	       if ( param.write_sql == PARAM_SET ) {
#ifdef _KNMI_SQL
		    SCIA_OL2_WR_SQL_CLD( conn, param.infile, num_dsr, cld );
#else
		    SCIA_OL2_WR_SQL_CLD( conn, param.flag_verbose, 
					 param.infile, 
					 num_dsr, ngeo, cld );
#endif
		    if ( IS_ERR_STAT_FATAL )
			 NADC_GOTO_ERROR( NADC_ERR_SQL, "SQL_CLD" );
	       }
#endif
               free( cld );
	  }
     }
/*
 * -------------------------
 * read/write Nadir fitting window application data
 */
     if ( param.write_nadir == PARAM_SET ) {
	  for ( n_nfit = 0; n_nfit < MAX_NFIT_SPECIES; n_nfit++ ) {
	       num_dsr = SCIA_OL2_RD_NFIT( fp, nfit_name[n_nfit], 
					   num_dsd, dsd, &nfit );
	       if ( param.flag_silent == PARAM_UNSET 
		    && param.write_sql == PARAM_UNSET ) 
		    (void) fprintf( stdout, "%s:\t%6u\n", 
				    nfit_name[n_nfit] , num_dsr );
	       if ( IS_ERR_STAT_FATAL ) 
		    NADC_GOTO_ERROR( NADC_ERR_PDS_RD, 
				     nfit_name[n_nfit] );
	       if ( num_dsr > 0 ) {
		    if ( param.write_ascii == PARAM_SET ) {
			 SCIA_OL2_WR_ASCII_NFIT( nfit_name[n_nfit], param, 
						 num_dsr, nfit );
			 if ( IS_ERR_STAT_FATAL )
			      NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "NFIT" );
		    }
		    if ( param.write_hdf5 == PARAM_SET ) {
			 SCIA_OL2_WR_H5_NFIT( nfit_name[n_nfit], param, 
					      num_dsr, nfit );
			 if ( IS_ERR_STAT_FATAL )
			      NADC_GOTO_ERROR( NADC_ERR_HDF_WR, 
					       nfit_name[n_nfit] );
		    }
#ifdef _WITH_SQL
		    if ( param.write_sql == PARAM_SET ) {
#ifdef _KNMI_SQL
			 SCIA_OL2_WR_SQL_NFIT( conn, param.infile,
					       nfit_name[n_nfit], 
					       num_dsr, nfit );

#else
			 SCIA_OL2_WR_SQL_NFIT( conn, param.flag_verbose, 
					       param.infile, nfit_name[n_nfit], 
					       num_geo, ngeo, num_dsr, nfit );
#endif
			 if ( IS_ERR_STAT_FATAL )
			      NADC_GOTO_ERROR( NADC_ERR_SQL, "SQL_NFIT" );
		    }
#endif
		    free( nfit );
	       }
	  }
     }
/*
 * -------------------------
 * read/write Limb fitting window application data
 */
     if ( param.write_limb == PARAM_SET ) {
	  for ( n_lfit = 0; n_lfit < MAX_LFIT_SPECIES; n_lfit++ ) {
	       num_dsr = SCIA_OL2_RD_LFIT( fp, lfit_name[n_lfit],
					   num_dsd, dsd, &lfit );
	       if ( param.flag_silent == PARAM_UNSET 
		    && param.write_sql == PARAM_UNSET ) 
		    (void) fprintf( stdout, "%s:\t%6u\n", 
				    lfit_name[n_lfit], num_dsr );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_PDS_RD, 
				     lfit_name[n_lfit] );
	       if ( num_dsr > 0 ) {
		    if ( param.write_ascii == PARAM_SET ) {
			 SCIA_OL2_WR_ASCII_LFIT( lfit_name[n_lfit], param, 
						 num_dsr, lfit );
			 if ( IS_ERR_STAT_FATAL )
			      NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "LFIT" );
		    }
		    if ( param.write_hdf5 == PARAM_SET ) {
			 SCIA_OL2_WR_H5_LFIT( lfit_name[n_lfit], param, 
					      num_dsr, lfit );
			 if ( IS_ERR_STAT_FATAL )
			      NADC_GOTO_ERROR( NADC_ERR_HDF_WR, 
					       lfit_name[n_lfit] );
		    }
		    free( lfit );
	       }
	  }
     }
/*
 * -------------------------
 * read/write Occultation fitting window application data
 */
     if ( param.write_occ != PARAM_SET ) {
	  for ( n_ofit = 0; n_ofit < MAX_OFIT_SPECIES; n_ofit++ ) {
	       num_dsr = SCIA_OL2_RD_LFIT( fp, ofit_name[n_ofit],
					   num_dsd, dsd, &ofit );
	       if ( param.flag_silent == PARAM_UNSET
		    && param.write_sql == PARAM_UNSET ) 
		    (void) fprintf( stdout, "%s:\t%6u\n", 
				    ofit_name[n_ofit], num_dsr );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_PDS_RD, 
				     ofit_name[n_ofit] );
	       if ( num_dsr > 0 ) {
		    if ( param.write_ascii == PARAM_SET ) {
			 SCIA_OL2_WR_ASCII_LFIT( ofit_name[n_ofit], param, 
						 num_dsr, ofit );
			 if ( IS_ERR_STAT_FATAL )
			      NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "OFIT" );
		    }
		    if ( param.write_hdf5 == PARAM_SET ) {
			 SCIA_OL2_WR_H5_LFIT( ofit_name[n_ofit], param, 
					      num_dsr, ofit );
			 if ( IS_ERR_STAT_FATAL )
			      NADC_GOTO_ERROR( NADC_ERR_HDF_WR, 
					       ofit_name[n_ofit] );
		    }
		    free( ofit );
	       }
	  }
     }
/*
 * -------------------------
 * read/write Limb Clouds data sets
 */
     num_dsr = SCIA_OL2_RD_LCLD( fp, num_dsd, dsd, &lcld );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "LCLD" );
     if ( num_dsr > 0 ) {
	  if ( param.write_ascii == PARAM_SET ) {
	       SCIA_OL2_WR_ASCII_LCLD( param, num_dsr, lcld );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "LCLD" );
	  }
	  if ( param.write_hdf5 == PARAM_SET ) {
	       SCIA_OL2_WR_H5_LCLD( param, num_dsr, lcld );
	       if ( IS_ERR_STAT_FATAL )
		    NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "LCLD" );
	  }
	  free( lcld );
     }
/*
 * -------------------------
 * read/write Ozone profile from Nadir measurements (TBD)
 */
/*
 * when an error has occurred we jump to here:
 */
 done:
/*
 * close input file
 */
     if ( fp != NULL ) (void) fclose( fp );
/*
 * close connection to PostgreSQL database
 */
#ifdef _WITH_SQL
     if ( param.write_sql == PARAM_SET && conn != NULL ) PQfinish( conn );
#endif
/*
 * close HDF5 output file
 */
     if ( param.write_hdf5 == PARAM_SET ) {
	  if ( param.hdf_file_id >= 0 && H5Fclose( param.hdf_file_id ) < 0 )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_FILE, param.hdf5_name );
     }
/*
 * free allocated memory
 */
     if ( dsd != NULL ) free( dsd );
     if ( ngeo != NULL ) free( ngeo );
/*
 * display error messages?
 */
     if ( param.flag_silent == PARAM_UNSET ) NADC_Err_Trace( stderr );
     if ( IS_ERR_STAT_FATAL ) 
	  return NADC_ERR_FATAL;
     else
	  return NADC_ERR_NONE;
}
Exemple #6
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
int main( int argc, char *argv[] )
     /*@globals  errno, stderr, stdout, nadc_stat, nadc_err_stack, 
       Use_Extern_Alloc;@*/
     /*@modifies errno, stderr, stdout, nadc_stat, nadc_err_stack@*/
{
     register size_t ns;

     size_t num_state_all = 0;
     size_t num_state = 0;

     unsigned short num;
     unsigned int   num_dsd;

     FILE  *fd = NULL;
#ifdef _WITH_SQL
     PGconn *conn = NULL;
#endif
     struct param_record param;
     struct mph_envi  mph;
     struct sph0_scia sph;
     struct dsd_envi  *dsd = NULL;
     struct mds0_states *states_all = NULL;
     struct mds0_states *states = NULL;
     struct mds0_aux  *aux;
     struct mds0_det  *det;
     struct mds0_pmd  *pmd;
/*
 * initialization of command-line parameters
 */
     SCIA_SET_PARAM( argc, argv, SCIA_LEVEL_0, &param );
     if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_PARAM, "" );
/*
 * check if we have to display version and exit
 */
     if ( param.flag_version == PARAM_SET ) {
	  SCIA_SHOW_VERSION( stdout, "scia_nl0" );
	  exit( EXIT_SUCCESS );
     }
/*
 * dump command-line parameters
 */
     if ( param.flag_show == PARAM_SET ) {
          SCIA_SHOW_PARAM( SCIA_LEVEL_0, param );
          exit( EXIT_SUCCESS );
     }
/*
 * connect to PostgreSQL database
 */
#ifdef _WITH_SQL
     if ( param.write_sql == PARAM_SET ) {
	  CONNECT_NADC_DB( &conn, "scia" );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_SQL, "PSQL" );
	  if ( param.flag_sql_remove == PARAM_SET 
	       || param.flag_sql_replace == PARAM_SET )
	       SCIA_LV0_DEL_ENTRY( conn, param.flag_verbose, param.infile );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_SQL, "PSQL(remove)" );
	  if ( param.flag_sql_remove == PARAM_SET ) goto done;
     }
#endif
/*
 * open input-file
 */
     if ( (fd = fopen( param.infile, "r" )) == NULL )
	  NADC_GOTO_ERROR( NADC_ERR_FILE, param.infile );
/*
 * create output file
 */
     if ( param.write_hdf5 == PARAM_SET ) {
	  param.hdf_file_id = SCIA_CRE_H5_FILE( SCIA_LEVEL_0, &param );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_CRE, "HDF5 base" );
	  SCIA_WR_H5_VERSION( param.hdf_file_id );
     }
/*
 * -------------------------
 * read Main Product Header
 */
     ENVI_RD_MPH( fd, &mph );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "MPH" );
     if ( mph.tot_size != nadc_file_size( param.infile ) )
	  NADC_GOTO_ERROR( NADC_ERR_FATAL, "file size check failed" );
     if ( param.write_ascii == PARAM_SET ) {
	  ENVI_WR_ASCII_MPH( param, &mph );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "MPH" );
     }
     if ( param.write_hdf5 == PARAM_SET ) {
	  SCIA_WR_H5_MPH( param, &mph );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "MPH" );
     }
#ifdef _WITH_SQL
     if ( param.write_sql == PARAM_SET ) {
	  SCIA_LV0_WR_SQL_META( conn, param.flag_verbose, param.infile, &mph );
	  if ( IS_ERR_STAT_WARN ) goto done;
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_SQL, "SQL_META" );
     }
#endif
/*
 * -------------------------
 * read Specific Product Header
 */
     SCIA_LV0_RD_SPH( fd, mph, &sph );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "SPH" );
     if ( param.write_ascii == PARAM_SET ) {
	  SCIA_LV0_WR_ASCII_SPH( param, &sph );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "SPH" );
     }
     if ( param.write_hdf5 == PARAM_SET ) {
	  SCIA_LV0_WR_H5_SPH( param, &sph );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "SPH" );
     }
/*
 * -------------------------
 * read Data Set Descriptor records
 */
     dsd = (struct dsd_envi *)
	  malloc( (mph.num_dsd-1) * sizeof( struct dsd_envi ) );
     if ( dsd == NULL ) NADC_GOTO_ERROR( NADC_ERR_ALLOC, "dsd" );
     num_dsd = ENVI_RD_DSD( fd, mph, dsd );
     if ( IS_ERR_STAT_FATAL ) 
	  NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "DSD" );
     if ( param.write_ascii == PARAM_SET ) {
	  ENVI_WR_ASCII_DSD( param, num_dsd, dsd );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "DSD" );
     }
     if ( param.write_meta == PARAM_SET ) goto done;
/*
 * -------------------------
 * read SCIAMACHY source packets
 *
 * first read MDS info data
 */
     num_state_all = SCIA_LV0_RD_MDS_INFO( fd, num_dsd, dsd, &states_all );
     if ( IS_ERR_STAT_FATAL )
	  NADC_GOTO_ERROR( NADC_ERR_FILE_RD, "RD_MDS_INFO" );
     if ( param.write_ascii == PARAM_SET ) {
	  SCIA_LV0_WR_ASCII_INFO( param, num_state_all, states_all );
	  if ( IS_ERR_STAT_FATAL ) 
	       NADC_GOTO_ERROR( NADC_ERR_FILE_RD, "WR_MDS_INFO" );
     }
     num_state = SCIA_LV0_SELECT_MDS(param, num_state_all, states_all, &states);
     SCIA_LV0_FREE_MDS_INFO( num_state_all, states_all );

     if ( num_state == 0 ) goto done;

#ifdef _WITH_SQL
     if ( param.write_sql == PARAM_SET ) {
	  struct mds0_sql sqlState[256];

	  for ( ns = 0; ns < num_state && ns < 256; ns++ ) {
	       sqlState[ns].nrAux = 
		    SCIA_LV0_RD_AUX( fd, states[ns].info_aux,
				     states[ns].num_aux, &aux );
	       if ( IS_ERR_STAT_FATAL ) goto failed;

	       sqlState[ns].nrDet = 
		    SCIA_LV0_RD_DET( fd, states[ns].info_det,
				     states[ns].num_det, BAND_ALL, &det );
	       if ( IS_ERR_STAT_FATAL ) goto failed;

	       sqlState[ns].nrPMD = 
		    SCIA_LV0_RD_PMD( fd, states[ns].info_pmd,
				     states[ns].num_pmd, &pmd );
	       if ( IS_ERR_STAT_FATAL ) goto failed;

	       /* set DateTime and StateID of current state */
	       (void) memcpy( &sqlState[ns].mjd, &states[ns].mjd,
			      sizeof(struct mjd_envi) );
	       sqlState[ns].stateID = states[ns].state_id;

	       /* obtain OBM temperature of current state */
	       GET_SCIA_LV0_STATE_OBMtemp( TRUE, sqlState[ns].nrAux, aux,
					   &sqlState[ns].obmTemp );
	       if ( IS_ERR_STAT_FATAL ) goto failed;

	       /* obtain Detectory array temperature of current state */
	       GET_SCIA_LV0_STATE_DETtemp( sqlState[ns].nrDet, det, 
					   sqlState[ns].chanTemp );
	       if ( IS_ERR_STAT_FATAL ) goto failed;

	       /* obtain Detectory array temperature of current state */
	       GET_SCIA_LV0_STATE_PMDtemp( sqlState[ns].nrPMD, pmd, 
					   &sqlState[ns].pmdTemp );
	       if ( IS_ERR_STAT_FATAL ) goto failed;

	       /* release allocated memory */
	       if ( sqlState[ns].nrAux > 0 ) free( aux );
	       SCIA_LV0_FREE_MDS_DET( sqlState[ns].nrDet, det );
	       if ( sqlState[ns].nrPMD > 0 ) free( pmd );
	  }
 failed:
	  SCIA_LV0_MATCH_STATE( conn, param.flag_verbose,
				&mph, num_state, sqlState );
	  if ( IS_ERR_STAT_FATAL )
	       NADC_GOTO_ERROR( NADC_ERR_SQL, "SQL_STATE" );
	  goto done;
     }
#endif
/* 
 * process Auxiliary source packets
 */
     if ( param.flag_silent == PARAM_UNSET )
	  NADC_Info_Proc( stdout, "Auxiliary MDS", num_state );

     for ( ns = 0; ns < num_state; ns++ ) {
	  if ( states[ns].num_aux == 0 ) continue;
	  
	  if ( param.flag_silent == PARAM_UNSET )
	       NADC_Info_Update( stdout, 3, ns );

	  num = SCIA_LV0_RD_AUX( fd, states[ns].info_aux, states[ns].num_aux,
				 &aux );
	  if ( IS_ERR_STAT_FATAL ) {
	       NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "MDS_AUX" );
	  }
	  
	  if ( param.write_hdf5 == PARAM_SET ) {
	       SCIA_LV0_WR_H5_AUX( param, ns, num, aux );
	  } else if ( param.write_ascii == PARAM_SET ) {
 	       SCIA_LV0_WR_ASCII_AUX( param, ns, num, aux );
	  }
	  free( aux );
	  if ( IS_ERR_STAT_FATAL ) {
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "MDS_AUX" );
	  }
     }
     if ( param.flag_silent == PARAM_UNSET )
	  NADC_Info_Finish( stdout, 3, ns );
/* 
 * process Detector source packets
 */
     if ( param.flag_silent == PARAM_UNSET )
	  NADC_Info_Proc( stdout, "Detector MDS", num_state );
     
     for ( ns = 0; ns < num_state; ns++ ) {
	  if ( states[ns].num_det == 0 ) continue;

	  if ( param.flag_silent == PARAM_UNSET )
	       NADC_Info_Update( stdout, 3, ns );

	  num = SCIA_LV0_RD_DET( fd, states[ns].info_det, states[ns].num_det,
				 param.chan_mask, &det );
	  if ( IS_ERR_STAT_FATAL ) {
	       NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "MDS_DET" );
	  }
	  if ( param.write_hdf5 == PARAM_SET ) {
	       SCIA_LV0_WR_H5_DET( param, ns, num, det );
	  } else if ( param.write_ascii == PARAM_SET ) {
	       SCIA_LV0_WR_ASCII_DET( param, ns, num, det );
	  }
	  SCIA_LV0_FREE_MDS_DET( num, det );
	  if ( IS_ERR_STAT_FATAL ) {
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "MDS_DET" );
	  }
     }
     if ( param.flag_silent == PARAM_UNSET )
	  NADC_Info_Finish( stdout, 3, ns );
/* 
 * process PMD source packets
 */
     if ( param.flag_silent == PARAM_UNSET )
	  NADC_Info_Proc( stdout, "PMD MDS", num_state );

     for ( ns = 0; ns < num_state; ns++ ) {
	  if ( states[ns].num_pmd == 0 ) continue;

	  if ( param.flag_silent == PARAM_UNSET )
	       NADC_Info_Update( stdout, 3, ns );

	  num = SCIA_LV0_RD_PMD( fd, states[ns].info_pmd, states[ns].num_pmd,
				 &pmd );
	  if ( IS_ERR_STAT_FATAL ) {
	       NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "MDS_PMD" );
	  }
	  if ( param.write_hdf5 == PARAM_SET ) {
	       SCIA_LV0_WR_H5_PMD( param, ns, num, pmd );
	  } else if ( param.write_ascii == PARAM_SET ) {
	       SCIA_LV0_WR_ASCII_PMD( param, ns, num, pmd );
	  }
	  free( pmd );
	  if ( IS_ERR_STAT_FATAL ) {
	       NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "MDS_PMD" );
	  }
     }
     if ( param.flag_silent == PARAM_UNSET )
	  NADC_Info_Finish( stdout, 3, ns );
/*
 * when an error has occurred we jump to here:
 */
 done:
/*
 * close input file
 */
     if ( fd != NULL ) (void) fclose( fd );
/*
 * close connection to PostgreSQL database
 */
#ifdef _WITH_SQL
     if ( param.write_sql == PARAM_SET && conn != NULL ) PQfinish( conn );
#endif
/*
 * close HDF5 output file
 */
     if ( param.write_hdf5 == PARAM_SET ) {
	  if ( param.hdf_file_id >= 0 && H5Fclose( param.hdf_file_id ) < 0 )
	       NADC_GOTO_ERROR( NADC_ERR_HDF_FILE, param.hdf5_name );
     }
/*
 * free allocated memory
 */
     if ( dsd != NULL ) free( dsd );
     SCIA_LV0_FREE_MDS_INFO( num_state, states );
/*
 * display error messages?
 */
     if ( param.flag_silent == PARAM_UNSET ) NADC_Err_Trace( stderr );
     if ( IS_ERR_STAT_FATAL ) 
	  return NADC_ERR_FATAL;
     else
	  return NADC_ERR_NONE;
}