Beispiel #1
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;
}
Beispiel #2
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;
}