/*+++++++++++++++++++++++++ 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 +++++++++++++++*/ 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, ¶m ); 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, ¶m ); 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; }
/*+++++++++++++++++++++++++ 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 short nr, nr_ddr; short num_ddr = 0; FILE *infl = NULL; struct param_record param; struct pir_gome pir; struct fsr2_gome fsr; struct sph2_gome sph; struct ddr_gome *ddr = NULL; #ifdef _WITH_SQL PGconn *conn = NULL; #endif /* * initialization of command-line parameters */ GOME_SET_PARAM( argc, argv, GOME_LEVEL_2, ¶m ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_PARAM, "" ); /* * check if we gave to give version and exit */ if ( param.flag_version == PARAM_SET ) { GOME_SHOW_VERSION( stdout, "gome_lv2" ); exit( EXIT_SUCCESS ); } /* * dump command-line parameters */ if ( param.flag_show == PARAM_SET ) { GOME_SHOW_PARAM( GOME_LEVEL_2, param ); exit( EXIT_SUCCESS ); } /* * open input-file */ if ( (infl = fopen( param.infile, "r" )) == NULL ) NADC_GOTO_ERROR( NADC_ERR_FILE, param.infile ); /* *------------------------- * read/write Product Identifier Content */ GOME_RD_PIR( infl, &pir ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "PIR" ); if ( strncmp( pir.product, "LVL20", 5 ) != 0 && strncmp( pir.product, "TCDO3", 5 ) != 0 ) { NADC_GOTO_ERROR( NADC_ERR_FATAL, "input is not a valid GOME level 2 file" ); } if ( param.write_hdf5 == PARAM_SET ) { param.hdf_file_id = GOME_CRE_H5_FILE( GOME_LEVEL_2, ¶m ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_HDF_CRE, "HDF5 base" ); GOME_WR_H5_PIR( param, &pir ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "PIR" ); } /* * ------------------------- * read File Structure Record */ GOME_LV2_RD_FSR( infl, &fsr ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "FSR" ); if ( param.write_ascii == PARAM_SET ) { GOME_LV2_WR_ASCII_FSR( param, &fsr ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "FSR" ); } GOME_LV2_CHK_SIZE( fsr, param.infile ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_FATAL, param.infile ); /* * ------------------------- * read Specific Product Header */ GOME_LV2_RD_SPH( infl, &fsr, &sph ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "SPH" ); /* * ------------------------- * read DOAS Data Records */ num_ddr = GOME_LV2_RD_DDR( infl, &fsr, &sph, &ddr ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "DDR" ); if ( param.flag_pselect == PARAM_SET ) { short nr_indices, *ddr_indices; ddr_indices = (short *) malloc( num_ddr * sizeof(short)); if ( ddr_indices == NULL ) NADC_GOTO_ERROR( NADC_ERR_ALLOC, "ddr_indices" ); nr_indices = NADC_USRINDX( param.pselect, num_ddr, ddr_indices ); for ( nr = 0; nr < nr_indices; nr++ ) { (void) memcpy( &ddr[nr], &ddr[ddr_indices[nr]], sizeof(struct ddr_gome) ); } num_ddr = nr_indices; free( ddr_indices ); } nr_ddr = 0; nr = 0; do { if ( SELECT_DDR( param, &ddr[nr].glr ) == 1 ) { if ( nr != nr_ddr ) (void) memcpy( ddr + nr_ddr, ddr + nr, sizeof(struct ddr_gome) ); nr_ddr++; } } while ( ++nr < num_ddr ); /* * write SPH */ if ( ddr != NULL ) { sph.start_time.days = ddr->glr.utc_date; if ( ddr->glr.utc_time >= 1500u ) sph.start_time.msec = ddr->glr.utc_time - 1500u; else { sph.start_time.days--; sph.start_time.msec = (3600u * 24u * 1000u) - ddr->glr.utc_time - 1500u; } sph.stop_time.days = ddr[num_ddr-1].glr.utc_date; sph.stop_time.msec = ddr[num_ddr-1].glr.utc_time; } else { sph.start_time.days = 0; sph.start_time.msec = 0; sph.stop_time.days = 0; sph.stop_time.msec = 0; } if ( param.write_ascii == PARAM_SET ) { GOME_LV2_WR_ASCII_SPH( param, &sph ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "SPH" ); } if ( param.write_hdf5 == PARAM_SET ) { GOME_LV2_WR_H5_SPH( param, &sph ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "SPH" ); } /* * write DDR records */ if ( param.write_ascii == PARAM_SET ) { GOME_LV2_WR_ASCII_DDR( param, sph, nr_ddr, ddr ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_FILE_WR, "DDR" ); } if ( param.write_hdf5 == PARAM_SET ) { GOME_LV2_WR_H5_DDR( param, nr_ddr, ddr ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "DDR" ); GOME_LV2_WR_H5_IRR( param, nr_ddr, ddr ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_HDF_WR, "IRR" ); } /* * connect to PostgreSQL database and write meta data */ #ifdef _WITH_SQL if ( param.write_sql == PARAM_SET ) { int meta_id = -1; CONNECT_NADC_DB( &conn, "gome" ); 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 ) GOME_LV2_DEL_ENTRY( conn, param.infile, sph.soft_version ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_SQL, "PSQL(remove)" ); if ( param.flag_sql_remove == PARAM_SET ) goto done; meta_id = GOME_LV2_WR_SQL_META( conn, param.flag_verbose, param.infile, &sph, &fsr ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_SQL, "SQL_META" ); GOME_LV2_WR_SQL_TILE( conn, param.flag_verbose, meta_id, sph.soft_version, nr_ddr, ddr ); if ( IS_ERR_STAT_FATAL ) NADC_GOTO_ERROR( NADC_ERR_SQL, "SQL_DDR" ); } #endif /* * when an error has occurred we jump to here: */ done: /* * close connection to PostgresSQL database */ #ifdef _WITH_SQL if ( param.write_sql == PARAM_SET && conn != NULL ) PQfinish( conn ); #endif /* * close input-file */ if ( infl != NULL ) (void) fclose( infl ); /* * 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 ( ddr != NULL ) { for ( nr = 0; nr < num_ddr; nr++ ) { if ( ddr[nr].irr1 != NULL ) free( ddr[nr].irr1 ); if ( ddr[nr].irr2 != NULL ) free( ddr[nr].irr2 ); } free( ddr ); } /* * 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; }
/*+++++++++++++++++++++++++ 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, ¶m ); 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, ¶m ); 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; }