Example #1
0
/*+++++++++++++++++++++++++
.IDENTifer   NADC_Err_Push
.PURPOSE     push error message on stack
.INPUT/OUTPUT
  call as    NADC_Err_Push( mesg_num, file_name, func_name, line, desc );
            
     input:  
           NADC_err_t mesg_num   :  id of error message
	   char       *file_name :  name of the module,
                                    where the error was issued
	   char       *func_name :  name of the function,
                                    where the error was issued
	   int        line       :  line where the error was issued
	   char       *desc      :  string with additional info

.RETURNS     nothing
.COMMENTS    none
-------------------------*/
void NADC_Err_Push( NADC_err_t mesg_num, const char *file_name, 
		    const char *func_name, int line, const char *desc )
{
     register unsigned short ns;

     const unsigned short nused = nadc_err_stack.nused;
/*
 * check number of messages already stored
 */
     if ( nused >= NADC_E_NSLOTS ) return;
/*
 * do not repeat messages, instead count them
 */
     for ( ns = 0; ns < nused; ns++ ) {
	  if ( nadc_err_stack.slots[ns].mesg_num == mesg_num
	       && nadc_err_stack.slots[ns].line == line
	       && strncmp( nadc_err_stack.slots[ns].file_name, 
			   file_name, SHORT_STRING_LENGTH ) == 0
	       && strncmp( nadc_err_stack.slots[ns].desc, 
			   desc, MAX_STRING_LENGTH ) == 0 ) {
	       nadc_err_stack.slots[ns].count++;
	       return;
	  }
     }
/*
 * store new message
 */
     nadc_err_stack.slots[nused].mesg_num = mesg_num;
     (void) nadc_strlcpy( nadc_err_stack.slots[nused].file_name,
			  file_name, SHORT_STRING_LENGTH );
     (void) nadc_strlcpy( nadc_err_stack.slots[nused].func_name,
			  func_name, SHORT_STRING_LENGTH );
     nadc_err_stack.slots[nused].line = line;
     (void) nadc_strlcpy( nadc_err_stack.slots[nused].desc,
			  desc, MAX_STRING_LENGTH );
     nadc_err_stack.nused++;

     switch ( mesg_num ) {
     case NADC_ERR_NONE:
	  nadc_stat |= NADC_STAT_INFO;
	  break;
     case NADC_PDS_DSD_ABSENT:
     case NADC_SDMF_ABSENT:
	  nadc_stat |= NADC_STAT_ABSENT;
	  break;
     case NADC_ERR_WARN:
     case NADC_WARN_PDS_RD:
     case NADC_ERR_SQL_TWICE:
	  nadc_stat |= NADC_STAT_WARN;
	  break;
     default: 
	  nadc_stat |= NADC_STAT_FATAL;
	  break;
     }
}
Example #2
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;
}
Example #3
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
short NADC_USRINDX( const char str_range[], int max_indx, short *indices )
{
     register int nr;

     char    *cpntr, cbuff[MAX_STRING_LENGTH];
     int     num;
     size_t  nchar;

     if ( (cpntr = strchr( str_range, '*' )) == NULL ) {
	  (void) NADC_USRINP( INT16_T, str_range, max_indx, indices, &num );
     } else {
	  nchar = cpntr - str_range;
	  (void) nadc_strlcpy( cbuff, str_range, nchar );
	  (void) sprintf( cbuff, "%s%-d%s", cbuff, max_indx-1, cpntr+1 );
	  (void) NADC_USRINP( INT16_T, cbuff, max_indx, indices, &num );

     }
     for ( nr = 0; nr < num; nr++ ) {
	  if ( indices[nr] < 0 ) indices[nr] += (short) max_indx;
	  if ( indices[nr] < 0 || indices[nr] >= (short) max_indx ) {
	       num = nr;
	       NADC_GOTO_ERROR( NADC_ERR_PARAM, "indices" );
	  }
     }
 done:
     return ((short) num);
}
Example #4
0
/*+++++++++++++++++++++++++
.IDENTifer   Conv_Date
.PURPOSE     convert Date string containing 01, 02,... to Jan, Feb,...
.INPUT/OUTPUT
  call as   Conv_Date( in_date, out_date );

     input:  
            char in_date[]   :  one of DD-MMM-YYYY, YYYY-MM-DD, 
                                       DD-MM-YYYY, YYYY-MM-DD
    output:  
            char out_date[]  :  DD-MMM-YYYY

.RETURNS     error status (0=ok)
.COMMENTS    static function
-------------------------*/
static inline
int Conv_Date( const char in_date[], /*@out@*/ char out_date[] )
{
     char *pntr, day[3], mon[4], year[5];

     (void) sprintf( out_date, "UNKOWN" );
     if ( strlen( in_date ) > DATE_ONLY_STRING_LENGTH )
	  return NADC_ERR_FATAL;

     if ( (pntr = strchr( in_date, '-' )) != NULL ) {
	  if ( (pntr - in_date) == 4 ) 
	       (void) nadc_strlcpy( year, in_date, 5 );
	  else if ( (pntr - in_date) == 2 )
	       (void) nadc_strlcpy( day, in_date, 3 );
	  else
	       return NADC_ERR_FATAL;
	  in_date = pntr + 1;
     }
     if ( (pntr = strchr( in_date, '-' )) != NULL ) {
	  if ( (pntr - in_date) == 3 ) {
	       (void) nadc_strlcpy( mon, in_date, 4 );
	  } else if ( (pntr - in_date) == 2 ) {
	       int mon_num;
	       const char *mon_str[] =
		    { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", 
		      "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };

	       mon_num = atoi( strncpy( mon, in_date, 2 ));
	       if ( mon_num > 0 && mon_num <= 12 )
		    (void) strcpy( mon, mon_str[mon_num-1] );
	       else
		    return NADC_ERR_FATAL;
	  } else
	       return NADC_ERR_FATAL;
	  in_date = pntr + 1;
     }
     if ( strlen( in_date ) == 2 )
	  (void) nadc_strlcpy( day, in_date, 3 );
     else if ( strlen( in_date ) == 4 )
	  (void) nadc_strlcpy( year, in_date, 5 );
     else
	  return NADC_ERR_FATAL;

     (void) snprintf( out_date, 12, "%s-%s-%s", day, mon, year );
     return NADC_ERR_NONE;
}
Example #5
0
/*+++++++++++++++++++++++++
.IDENTifer   nadc_write_header
.PURPOSE     write keyword and value in uniform fashion
.INPUT/OUTPUT
  call as    nadc_write_header( fp, key_num, infl_name, component );

     input:
            FILE *fp               :   file pointer
            unsigned int key_num   :   (unique) number for this keyword
	    char infl_name[]       :   name of input file
	    char component         :   name of component

.RETURNS     nothing (check global error status)
.COMMENTS    none
-------------------------*/
void nadc_write_header( FILE *fp, unsigned int key_num, 
			const char infl_name[], const char component[] )
{
     char   string[26];
     time_t tp[1];

     nadc_write_text( fp, key_num, "Input filename", infl_name );
     nadc_write_text( fp, key_num, "Name of component", component );
     (void) time( tp );
     /* do not copy newline */
     (void) nadc_strlcpy( string, ctime( tp ), 25 );
     nadc_write_text( fp, key_num, "Creation date", string );
}
Example #6
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void NADC_GET_XML_METADB( FILE *fp, char *host, char *port, 
			  char *user, char *passwd )
{
     char   line[MAX_LINE_LENGTH], line_strip[MAX_LINE_LENGTH];
     char   keyword[MAX_LINE_LENGTH];
     char   *sep;

     unsigned int nchar;
/*
 * initialize
 */
     *host = *user = *passwd = '\0'; 
/*
 * read until tag "metatables"
 */
     do {
          if ( fgets( line, MAX_LINE_LENGTH-1, fp ) == NULL )
               NADC_RETURN_ERROR( NADC_ERR_FILE_RD,
				  "error reading nadc.config.xml" );
     } while (strstr( line, "metatables" ) == NULL );
/*
 * read until tag "server"
 */
     do {
          if ( fgets( line, MAX_LINE_LENGTH-1, fp ) == NULL )
               NADC_RETURN_ERROR( NADC_ERR_FILE_RD,
				  "error reading nadc.config.xml" );
     } while (strstr( line, "<server" ) == NULL );
/*
 * read value for "host", "user", "passwd"
 */
     do {
          if ( fgets( line, MAX_LINE_LENGTH-1, fp ) == NULL )
               NADC_RETURN_ERROR( NADC_ERR_FILE_RD,
				  "error reading nadc.config.xml" );
	  NADC_STRIP_ALL( line, line_strip );

	  if ( (sep = strchr( line_strip, '=' )) != NULL ) {
	       nchar = min_t(size_t, MAX_LINE_LENGTH, (sep - line_strip + 1));
	       (void) nadc_strlcpy( keyword, line_strip, nchar );
	       if ( strncmp( keyword, "host", 4 ) == 0 )
		    (void) strcpy( host, ++sep );
	       else if ( strncmp( keyword, "port", 4 ) == 0 )
		    (void) strcpy( port, ++sep );
	       else if ( strncmp( keyword, "user", 4 ) == 0 )
		    (void) strcpy( user, ++sep );
	       else if ( strncmp( keyword, "passwd", 6 ) == 0 )
		    (void) strcpy( passwd, ++sep );
	  }
     } while (strstr( line, "/>" ) == NULL );
}
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_OL2_WR_SQL_META( PGconn *conn, bool be_verbose, const char *sciafl,
			   const char *l1b_product,
			   const struct mph_envi *mph,
			   const struct sph_sci_ol *sph )
{
     PGresult *res;

     char *pntr;
     char str_quality[6];
     char ctemp[SHORT_STRING_LENGTH];
     char sql_query[SQL_STR_SIZE], cbuff[SQL_STR_SIZE];

     bool          first;
     int           nrow, numChar, meta_id;
/*
 * check if product is already in database
 */
     (void) snprintf( sql_query, MAX_STRING_LENGTH, "%s\'%s\'",
		      "SELECT * FROM meta__2P WHERE name=", mph->product );
     res = PQexec( conn, sql_query );
     if ( PQresultStatus( res ) != PGRES_TUPLES_OK ) {
          PQclear( res );
          NADC_RETURN_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     }
     if ( (nrow = PQntuples( res )) != 0 ) {
          PQclear( res );
          NADC_RETURN_ERROR( NADC_ERR_SQL_TWICE, mph->product );
     }
     PQclear( res );
/*
 * obtain next value for serial pk_meta
 */
     res = PQexec( conn,
                   "SELECT nextval(\'meta__2p_pk_meta_seq\')" );
     if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
          NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     pntr = PQgetvalue( res, 0, 0 );
     meta_id = (int) strtol( pntr, (char **) NULL, 10 );
     PQclear( res );
/*
 * no error and not yet stored, then proceed
 */     
/* pk_meta */
     (void) snprintf( sql_query, SQL_STR_SIZE, 
		      "INSERT INTO meta__2P VALUES (%d,", meta_id );
/* name */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s\'%s\',",
		      strcpy(cbuff,sql_query), mph->product );
/* l1b_product */
     (void) nadc_strlcpy( ctemp, l1b_product, ENVI_FILENAME_SIZE );
     SCIA_CHECK_SQL_LV1B_NAME( conn, ctemp );
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s\'%s\',",
		      strcpy(cbuff,sql_query), ctemp );
/* fileSize */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s%u,",
		      strcpy(cbuff,sql_query), mph->tot_size );
/* receiveDate */
     NADC_RECEIVEDATE( sciafl, ctemp );
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s\'%s\',",
		      strcpy(cbuff,sql_query), ctemp );
/* procStage  */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s\'%s\',",
		      strcpy(cbuff,sql_query), mph->proc_stage );
/* procCenter */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s\'%s\',",
		      strcpy(cbuff,sql_query), mph->proc_center );
/* softVersion */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s\'%s\',",
		      strcpy(cbuff,sql_query), mph->soft_version );
/* fittingErrSum */
     nadc_rstrip( str_quality, sph->errorsum );
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s\'%s\',",
		      strcpy(cbuff,sql_query), str_quality );
/* dateTimeStart */
     (void) snprintf( sql_query, SQL_STR_SIZE, 
		      "%sdate_trunc('second', TIMESTAMP \'%s\'),",
		      strcpy(cbuff,sql_query), sph->start_time );
/* muSecStart */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s,",
		      strcpy(cbuff,sql_query), sph->start_time+21 );
/* dateTimeStop */
     (void) snprintf( sql_query, SQL_STR_SIZE, 
		      "%sdate_trunc('second', TIMESTAMP \'%s\'),",
		      strcpy(cbuff,sql_query), sph->stop_time );
/* muSecStop */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s,",
		      strcpy(cbuff,sql_query), sph->stop_time+21 );
/* absOrbit */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s%d,",
		      strcpy(cbuff,sql_query), mph->abs_orbit );
/* relOrbit */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s%d,",
		      strcpy(cbuff,sql_query), mph->rel_orbit );
/* numDataSets */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s%u,",
		      strcpy(cbuff,sql_query), mph->num_data_sets );
/* naditFittingWindows */
     first = TRUE;
     (void) strcat( sql_query, "\'" );
     if ( strncmp( sph->nadir_win_uv0, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_uv0, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_uv1, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_uv1, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_uv2, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_uv2, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_uv3, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_uv3, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_uv4, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_uv4, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_uv5, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_uv5, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_uv6, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_uv6, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_uv7, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_uv7, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_ir0, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_ir0, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_ir1, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_ir1, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_ir2, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_ir2, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_ir3, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_ir3, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_ir4, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_ir4, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->nadir_win_ir5, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->nadir_win_ir5, ctemp );
	  if ( ! first ) strcat( sql_query, "," );
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     (void) strcat( sql_query, "\'," );
/* limbFittingWindows */
     first = TRUE;
     (void) strcat( sql_query, "\'" );
     if ( strncmp( sph->limb_win_pth, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_pth, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_uv0, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_uv0, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_uv1, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_uv1, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_uv2, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_uv2, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_uv3, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_uv3, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_uv4, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_uv4, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_uv5, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_uv5, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_uv6, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_uv6, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_uv7, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_uv7, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_ir0, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_ir0, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_ir1, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_ir1, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_ir2, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_ir2, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_ir3, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_ir3, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->limb_win_ir4, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->limb_win_ir4, ctemp );
	  if ( ! first ) strcat( sql_query, "," );
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     (void) strcat( sql_query, "\'," );
/* occultFittingWindows */
     first = TRUE;
     (void) strcat( sql_query, "\'" );
     if ( strncmp( sph->occl_win_pth, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_pth, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_uv0, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_uv0, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_uv1, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_uv1, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_uv2, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_uv2, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_uv3, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_uv3, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_uv4, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_uv4, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_uv5, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_uv5, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_uv6, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_uv6, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_uv7, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_uv7, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_ir0, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_ir0, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_ir1, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_ir1, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_ir2, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_ir2, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_ir3, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_ir3, ctemp );
	  if ( ! first ) strcat( sql_query, "," ); else first = FALSE;
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     if ( strncmp( sph->occl_win_ir4, "EMPTY", 5 ) != 0 ) {
	  NADC_STRIP( sph->occl_win_ir4, ctemp );
	  if ( ! first ) strcat( sql_query, "," );
	  (void) snprintf( sql_query, SQL_STR_SIZE, "%s%s",
			   strcpy(cbuff,sql_query), ctemp );
     }
     (void) strcat( sql_query, "\'," );
/* numNadirFW */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s%hu,",
		      strcpy(cbuff,sql_query), sph->no_nadir_win );
/* numLimbFW */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s%hu,",
		      strcpy(cbuff,sql_query), sph->no_limb_win );
/* numOccultFW */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s%hu,",
		      strcpy(cbuff,sql_query), sph->no_occl_win );
/* noEntryDMOP */
     (void) snprintf( sql_query, SQL_STR_SIZE, "%s%d,",
		      strcpy(cbuff,sql_query), 0 );
/* delayedBy */
     numChar = snprintf( sql_query, SQL_STR_SIZE, "%s%s)",
			 strcpy( cbuff, sql_query ), "NULL" );
     if ( be_verbose )
	  (void) printf( "%s(): %s [%-d]\n", __func__, sql_query, numChar );
     if ( numChar >= SQL_STR_SIZE )
	  NADC_RETURN_ERROR( NADC_ERR_STRLEN, "sql_query" );
/*
 * do the actual insert
 */
     res = PQexec( conn, sql_query );
     if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
          NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
 done:
     PQclear( res );
}
Example #8
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
short GOME_LV1_RD_SMCD( unsigned char source, FILE *infl, 
			const struct fsr1_gome *fsr, 
			const struct sph1_gome *sph,
			struct smcd_gome **smcd_out )
{
     register int nr;

     char  *smcd_char = NULL;
     char  *smcd_pntr;
     char  mph_buff[MPH_SIZE], sph_buff[SPH_SIZE];

     unsigned short ihr_buff[IHR_SIZE];

     short  nr_smcd = 0;                   /* initialize return value */
     short  num_smcd;
     int    sz_smcd;
     long   byte_offs;

     struct smcd_gome *smcd;
/*
 * initialize some constants
 */
     if ( source == FLAG_SUN ) {
	  num_smcd = fsr->nr_scd;
	  sz_smcd  = fsr->sz_scd;
	  byte_offs = (long) (LVL1_PIR_LENGTH + LVL1_FSR_LENGTH 
			      + fsr->sz_sph + fsr->sz_fcd 
			      + fsr->nr_pcd * fsr->sz_pcd);
     } else {
	  num_smcd = fsr->nr_mcd;
	  sz_smcd  = fsr->sz_mcd;
	  byte_offs = (long) (LVL1_PIR_LENGTH + LVL1_FSR_LENGTH 
			      + fsr->sz_sph + fsr->sz_fcd 
			      + fsr->nr_pcd * fsr->sz_pcd
			      + fsr->nr_scd * fsr->sz_scd);
     }
/*
 * check number of SMCD records
 */
     if ( num_smcd == 0 ) return 0;
/*
 * allocate memory for the SMCD records
 */
     if ( ! Use_Extern_Alloc ) {
	  smcd_out[0] = (struct smcd_gome *)
	       malloc( num_smcd * sizeof( struct smcd_gome ) );
     }
     if ( (smcd = smcd_out[0]) == NULL )
	  NADC_GOTO_ERROR( NADC_ERR_ALLOC, "smcd" );
/*
 * allocate memory to temporary store data for output structure
 */
     if ( (smcd_char = (char *) malloc( (size_t) sz_smcd )) == NULL ) 
	  NADC_GOTO_ERROR( NADC_ERR_ALLOC, "smcd_char" );
/*
 * rewind/read input data file
 */
     (void) fseek( infl, byte_offs, SEEK_SET );
/*
 * read data buffer to SMCD structure
 */
     do {
	  if ( fread( smcd_char, sz_smcd, 1, infl ) != 1 )
	       NADC_GOTO_ERROR( NADC_ERR_PDS_RD, "" );
	  (void) memcpy( &smcd->utc_date, smcd_char, GOME_UINT );
	  smcd_pntr = smcd_char + GOME_UINT;
	  (void) memcpy( &smcd->utc_time, smcd_pntr, GOME_UINT );
	  smcd_pntr += GOME_UINT;
	  (void) memcpy( &smcd->north_sun_zen, smcd_pntr, GOME_FLOAT );
	  smcd_pntr += GOME_FLOAT;
	  (void) memcpy( &smcd->north_sun_azim, smcd_pntr, GOME_FLOAT);
	  smcd_pntr += GOME_FLOAT;
	  (void) memcpy( &smcd->north_sm_zen, smcd_pntr, GOME_FLOAT );
	  smcd_pntr += GOME_FLOAT;
	  (void) memcpy( &smcd->north_sm_azim, smcd_pntr, GOME_FLOAT );
	  smcd_pntr += GOME_FLOAT;
	  (void) memcpy( &smcd->sun_or_moon, smcd_pntr, GOME_FLOAT );
	  smcd_pntr += GOME_FLOAT;
	  (void) memcpy( &smcd->dark_current, smcd_pntr, GOME_FLOAT );
	  smcd_pntr += GOME_FLOAT;
	  (void) memcpy( &smcd->noise_factor, smcd_pntr, GOME_FLOAT );
	  smcd_pntr += GOME_FLOAT;
	  (void) memcpy( &smcd->indx_spec, smcd_pntr, GOME_SHORT );
	  smcd_pntr += GOME_SHORT;
	  (void) memcpy( &smcd->indx_leak, smcd_pntr, GOME_SHORT );
	  smcd_pntr += GOME_SHORT;
	  (void) memcpy( mph_buff, smcd_pntr, MPH_SIZE );
	  smcd_pntr += MPH_SIZE;
	  (void) memcpy( sph_buff, smcd_pntr, SPH_SIZE );
	  smcd_pntr += SPH_SIZE;
	  (void) memcpy( ihr_buff, smcd_pntr, IHR_SIZE * GOME_USHRT );
	  smcd_pntr += IHR_SIZE * GOME_USHRT;
	  (void) memcpy( smcd->indx_bands, smcd_pntr, 
			   NUM_SPEC_BANDS * GOME_SHORT );
	  smcd_pntr += NUM_SPEC_BANDS * GOME_SHORT;
/*
 * read Level 0 data MPH
 */
	  (void) nadc_strlcpy( smcd->mph0.ProductConfidenceData, mph_buff, 3 );
	  (void) nadc_strlcpy( smcd->mph0.UTC_MPH_Generation, mph_buff+2, 25 );
	  (void) nadc_strlcpy( smcd->mph0.ProcessorSoftwareVersion, 
			       mph_buff+26, 9);
/*
 * read Level 0 data SPH
 */
	  (void) nadc_strlcpy( smcd->sph0.sph_5, sph_buff, 2 );
	  (void) nadc_strlcpy( smcd->sph0.sph_6, sph_buff+1, 21 );
/*
 * Get subset counter, PMD and Peltier values
 */
	  smcd->ihr.subsetcounter = ihr_buff[sph->subset_entry];
	  smcd->ihr.prism_temp = ihr_buff[PRE_DISP_TEMP_ENTRY];
	  smcd->ihr.intg.two_byte = ihr_buff[sph->intgstat_entry];
	  for ( nr = 0; nr < PMD_IN_GRID; nr++ ) {
	       smcd->ihr.pmd[0][nr] = ihr_buff[sph->pmd_entry + nr * 4];
	       smcd->ihr.pmd[1][nr] = ihr_buff[sph->pmd_entry + nr * 4 + 1];
	       smcd->ihr.pmd[2][nr] = ihr_buff[sph->pmd_entry + nr * 4 + 2];
	  }
	  for ( nr = 0; nr < SCIENCE_CHANNELS; nr++ ) {
	       smcd->ihr.peltier[nr] = 
		    (short) ihr_buff[sph->peltier_entry + nr];
	  }
	  if ((AVERAGE_MODE_MASK & ihr_buff[sph->status2_entry]) == 0 )
	       smcd->ihr.averagemode = FALSE;
	  else
	       smcd->ihr.averagemode = TRUE;
/*
 * check if we read the whole DSR
 */
	  if ( smcd_pntr - smcd_char != sz_smcd )
	       NADC_GOTO_ERROR( NADC_ERR_PDS_SIZE, "SMCD size" );

#ifdef _SWAP_TO_LITTLE_ENDIAN
	  Sun2Intel_SMCD( smcd );
#endif
	  smcd++;
     } while ( ++nr_smcd < num_smcd );
 done:
     if ( smcd_char != NULL ) free( smcd_char );
/*
 * set return value
 */
     return nr_smcd;
}
Example #9
0
/*+++++++++++++++++++++++++ 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 );
}
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_WR_SQL_CO_META( PGconn *conn, const struct imlm_hdr *hdr )
{
     PGresult *res;

     char  l1b_product[ENVI_FILENAME_SIZE];

     char  *pntr, sql_query[SQL_STR_SIZE];

     int   nrow, numChar, meta_id;
/*
 * check if product is already in database
 */
     (void) snprintf( sql_query, SQL_STR_SIZE, 
		      "SELECT * FROM %s WHERE name=\'%s\'", 
		      META_TBL_NAME, hdr->product );
     res = PQexec( conn, sql_query );
     if ( PQresultStatus( res ) != PGRES_TUPLES_OK ) {
          NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     }
     if ( (nrow = PQntuples( res )) != 0 ) {
          NADC_GOTO_ERROR( NADC_ERR_SQL_TWICE, hdr->product );
     }
     PQclear( res );
/*
 * check presence of SCIA L1b in database
 */
     (void) nadc_strlcpy( l1b_product, hdr->l1b_product, ENVI_FILENAME_SIZE );
     SCIA_CHECK_SQL_LV1B_NAME( conn, l1b_product );
/* 
 * obtain next value for serial pk_meta
 */
     res = PQexec( conn,
                   "SELECT nextval(\'meta_imlm_co_pk_meta_seq\')" );
     if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
          NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     pntr = PQgetvalue( res, 0, 0 );
     meta_id = (int) strtol( pntr, (char **) NULL, 10 );
     PQclear( res );
/*
 * no error and not yet stored, then proceed
 */
     numChar = snprintf( sql_query, SQL_STR_SIZE, SQL_INSERT_META, 
			 META_TBL_NAME, meta_id,
			 hdr->product, l1b_product, hdr->file_size, 
			 hdr->receive_date, hdr->creation_date,
			 hdr->validity_start, hdr->validity_stop,
			 hdr->software_version, hdr->pixelmask_version, 
			 hdr->cloudmask_version, hdr->orbit[0], 
			 hdr->window_pixel[0], hdr->window_pixel[1], 
			 hdr->window_wave[0], hdr->window_wave[1],
			 hdr->numRec );
/*      (void) fprintf( stderr, "%s [%-d]\n", sql_query, numChar ); */
     if ( numChar >= SQL_STR_SIZE )
	  NADC_RETURN_ERROR( NADC_ERR_STRLEN, "sql_query" );
/*
 * do the actual insert
 */
     res = PQexec( conn, sql_query );
     if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
          NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
 done:
     PQclear( res );
}
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_OL2_WR_SQL_CLD( PGconn *conn, bool be_verbose, const char *flname,
			  unsigned int num_rec, 
			  const struct ngeo_scia *geo,
			  const struct cld_sci_ol *cld )
{
     register unsigned int nc;
     register unsigned int affectedRows = 0u;

     char sql_query[SQL_STR_SIZE], cbuff[SQL_STR_SIZE];

     bool       do_update;
     char       *cpntr, sciafl[SHORT_STRING_LENGTH];
     int        nrow, numChar, meta_id;
     long long  tile_id;

     PGresult   *res;

     const double SecPerDay = 24. * 60 * 60;
/*
 * strip path of file-name
 */
     if ( (cpntr = strrchr( flname, '/' )) != NULL ) {
          (void) nadc_strlcpy( sciafl, ++cpntr, SHORT_STRING_LENGTH );
     } else {
          (void) nadc_strlcpy( sciafl, flname, SHORT_STRING_LENGTH );
     }
/*
 * check if product is already in database
 */
     (void) snprintf( sql_query, SQL_STR_SIZE, 
		      "SELECT pk_meta FROM %s WHERE name=\'%s\'", 
		      META_TBL_NAME, sciafl );
     res = PQexec( conn, sql_query );
     if ( PQresultStatus( res ) != PGRES_TUPLES_OK ) {
          NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     }
     if ( (nrow = PQntuples( res )) == 0 ) {
          NADC_GOTO_ERROR( NADC_ERR_FATAL, sciafl );
     }
     cpntr = PQgetvalue( res, 0, 0 );
     meta_id = (int) strtol( cpntr, (char **) NULL, 10 );     
     PQclear( res );
/*
 * Start a transaction block
 */
     res = PQexec( conn, "BEGIN" );
     if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
          NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     PQclear( res );
/*
 * insert all cloud/aerosol records
 */
     for ( nc = 0; nc < num_rec; nc++ ) {
	  register double jday = cld[nc].mjd.days
	       + (cld[nc].mjd.secnd + cld[nc].mjd.musec / 1e6) / SecPerDay;

/* check if entry is already present */
	  do_update = FALSE;
          (void) snprintf( sql_query, SQL_STR_SIZE, 
			   CHECK_IF_PRESENT, TILE_TBL_NAME, jday );
          res = PQexec( conn, sql_query );
          if ( PQresultStatus( res ) != PGRES_TUPLES_OK ) {
               NADC_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
	       PQclear( res );
	       res = PQexec( conn, "ROLLBACK" );
	       if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
		    NADC_ERROR( NADC_ERR_SQL,
				PQresultErrorMessage(res) );
	       goto done;
          } 
	  if ( PQntuples( res ) != 0 ) { /* should check product version */
	       do_update = TRUE;
               cpntr = PQgetvalue( res, 0, 0 );
               tile_id = strtoll( cpntr, (char **) NULL, 10 );
               PQclear( res );
               numChar = GET_UPDATE_QUERY( sql_query, 
					   meta_id, tile_id, geo+nc, cld+nc );
          } else {
               PQclear( res );
               /* obtain next value for serial pk_tile */
	       res = PQexec( conn,
			     "SELECT nextval(\'tile_cld_ol_pk_tile_seq\')" );
	       if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
		    NADC_GOTO_ERROR( NADC_ERR_SQL, 
				     PQresultErrorMessage(res) );
	       cpntr = PQgetvalue( res, 0, 0 );
	       tile_id = strtoll( cpntr, (char **) NULL, 10 );
	       PQclear( res );
               numChar = GET_INSERT_QUERY( sql_query, meta_id, tile_id, 
					   jday, geo+nc, cld+nc );
          }
	  if ( be_verbose )
	       (void) printf( "%s(): %s [%-d]\n", __func__, sql_query, numChar );
	  if ( numChar >= SQL_STR_SIZE ) {
               NADC_ERROR( NADC_ERR_STRLEN, "sql_query" );
	       res = PQexec( conn, "ROLLBACK" );
	       if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
		    NADC_ERROR( NADC_ERR_SQL,
				PQresultErrorMessage(res) );
	       goto done;
	  }
/* do actual insert */
	  res = PQexec( conn, sql_query );
	  if ( PQresultStatus( res ) != PGRES_COMMAND_OK ) {
	       NADC_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
	       PQclear( res );
	       res = PQexec( conn, "ROLLBACK" );
	       if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
		    NADC_ERROR( NADC_ERR_SQL,
				PQresultErrorMessage(res) );
	       goto done;
	  }
	  PQclear( res );
	  if ( do_update ) continue;

	  affectedRows += 1;
     }
/*
 * end the transaction
 */
     res = PQexec( conn, "COMMIT" );
     if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
          NADC_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
 done:
     PQclear( res );
     (void) snprintf( cbuff, SQL_STR_SIZE, "affectedRows=%-u", 
		      affectedRows );
     NADC_ERROR( NADC_ERR_NONE, cbuff );
}
Example #12
0
/*+++++++++++++++++++++++++
.IDENTifer  SCIA_LV1_INIT_DSD
.PURPOSE    determine number of DSD's in output file, 
            and reset all (static) global variables
.INPUT/OUTPUT
  call as   SCIA_LV1_INIT_DSD( write_lv1c, num_dsd, dsd );
     input:
            unsigned int num_dsd      :  number of DSD's (input)
            
.RETURNS     number of DSD's in outputfile (unsigned int)
.COMMENTS    static function
-------------------------*/
static
void SCIA_LV1_INIT_DSD( unsigned char write_lv1c, unsigned int num_dsd_in, 
			const struct dsd_envi *dsd_in )
     /*@globals  num_nadir_states, num_limb_states, num_occult_states,
       num_monitor_states, num_dsd_wr, num_dsd_out, dsd_out,
       nadc_stat, nadc_err_stack;@*/
     /*@modifies num_nadir_states, num_limb_states, num_occult_states,
       num_monitor_states, num_dsd_wr, num_dsd_out, dsd_out,
       nadc_stat, nadc_err_stack@*/
{
     register unsigned int nr_in, nr_out;
/*
 * initialize number of DSD in output-file equal to input-file
 */
     num_nadir_states = 0;
     num_limb_states = 0;
     num_occult_states = 0;
     num_monitor_states = 0;

     num_dsd_wr = 0u;
     num_dsd_out = num_dsd_in;
/*
 * determine number of DSD's on selections given in param record
 *
 * do not write DSD records for ADS, except STATES
 */
     if ( write_lv1c == PARAM_SET ) {
	  num_dsd_out--;  /* no PMD */
	  num_dsd_out--;  /* no AUX */
	  num_dsd_out--;  /* no LCPN */
	  num_dsd_out--;  /* no DARK */
	  num_dsd_out--;  /* no PPGN */
	  num_dsd_out--;  /* no SCPN */
	  num_dsd_out--;  /* no SRSN */

	  if ( ! IS_SCIA_LV1C( num_dsd_in, dsd_in ) ) {
	       num_dsd_out++;  /* add CalOpt */
	       num_dsd_out++;  /* add PMD nadir */
	       num_dsd_out++;  /* add PolV nadir */
	       num_dsd_out++;  /* add PMD Limb */
	       num_dsd_out++;  /* add PolV Limb */
	       num_dsd_out++;  /* add PMD Occultation */
	       num_dsd_out++;  /* add PolV Occultation */
	  }
     }
     if ( num_dsd_out == 0u || num_dsd_out >= MAX_ARRAY_DSD ) {
	  char msg[SHORT_STRING_LENGTH];

	  (void) snprintf( msg, SHORT_STRING_LENGTH, "%s: %-u",
			   "invalid number of DSD records", num_dsd_out );
	  NADC_RETURN_ERROR( NADC_ERR_FATAL, msg );
     }
     if ( write_lv1c == PARAM_UNSET ) {
	  (void) memcpy(dsd_out, dsd_in, num_dsd_in * sizeof(struct dsd_envi));
     } else {
	  nr_in = nr_out= 0;
	  do {
	       (void) nadc_strlcpy( dsd_out[nr_out].name, 
				    dsd_in[nr_in].name, 29 );
	       (void) nadc_strlcpy( dsd_out[nr_out].type, 
				    dsd_in[nr_in].type, 2 );
	       (void) nadc_strlcpy( dsd_out[nr_out].flname, 
				    dsd_in[nr_in].flname, ENVI_FILENAME_SIZE );
	       nr_out++;
	  } while ( strncmp( dsd_in[nr_in++].name, "STATES", 6 ) );
/*
 * skip all ADS records (except STATES)
 */
	  while ( dsd_in[nr_in].type[0] != 'M' ) nr_in++;

	  (void) nadc_strlcpy( dsd_out[nr_out].name, "CAL_OPTIONS", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].type, "G", 2 );

	  (void) nadc_strlcpy( dsd_out[nr_out].name, "NADIR", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out].type, "M", 2 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].flname, 
			       "NOT USED", ENVI_FILENAME_SIZE );

	  (void) nadc_strlcpy( dsd_out[nr_out].name, "NADIR_PMD", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out].type, "M", 2 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].flname, 
			       "NOT USED", ENVI_FILENAME_SIZE );

	  (void) nadc_strlcpy( dsd_out[nr_out].name, "NADIR_FRAC_POL", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out].type, "M", 2 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].flname, 
			       "NOT USED", ENVI_FILENAME_SIZE );

	  (void) nadc_strlcpy( dsd_out[nr_out].name, "LIMB", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out].type, "M", 2 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].flname, 
			       "NOT USED", ENVI_FILENAME_SIZE );

	  (void) nadc_strlcpy( dsd_out[nr_out].name, "LIMB_PMD", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out].type, "M", 2 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].flname, 
			       "NOT USED", ENVI_FILENAME_SIZE );

	  (void) nadc_strlcpy( dsd_out[nr_out].name, "LIMB_FRAC_POL", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out].type, "M", 2 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].flname, 
			       "NOT USED", ENVI_FILENAME_SIZE );

	  (void) nadc_strlcpy( dsd_out[nr_out].name, "OCCULTATION", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out].type, "M", 2 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].flname, 
			       "NOT USED", ENVI_FILENAME_SIZE );

	  (void) nadc_strlcpy( dsd_out[nr_out].name, "OCCULTATION_PMD", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out].type, "M", 2 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].flname, 
			       "NOT USED", ENVI_FILENAME_SIZE );

	  (void) nadc_strlcpy( dsd_out[nr_out].name, 
			       "OCCULTATION_FRAC_POL", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out].type, "M", 2 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].flname, 
			       "NOT USED", ENVI_FILENAME_SIZE );

	  (void) nadc_strlcpy( dsd_out[nr_out].name, "MONITORING", 29 );
	  (void) nadc_strlcpy( dsd_out[nr_out].type, "M", 2 );
	  (void) nadc_strlcpy( dsd_out[nr_out++].flname, 
			       "NOT USED", ENVI_FILENAME_SIZE );
	  do {
	       if ( dsd_in[nr_in].type[0] == 'R' ) {
		    (void) memcpy( &dsd_out[nr_out++], &dsd_in[nr_in], 
				   sizeof(struct dsd_envi) );
	       }
	  } while( ++nr_in < num_dsd_in );
     }
/*
 * make sure that all integer DSD parameters are set to zero
 */
     nr_out = 0;
     do {
	  if ( dsd_out[nr_out].type[0] != 'R' ) {
	       if ( dsd_out[nr_out].type[0] == 'M' ) {
		    (void) nadc_strlcpy( dsd_out[nr_out].flname, 
					 "NOT USED", ENVI_FILENAME_SIZE );
	       }
	       dsd_out[nr_out].offset = 0u;
	       dsd_out[nr_out].size = 0u;
	       dsd_out[nr_out].num_dsr = 0u;
	       dsd_out[nr_out].dsr_size = 0;
	  }
     } while( ++nr_out < num_dsd_out );
}
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_LV1_MATCH_STATE( PGconn *conn, bool be_verbose,
			   const struct mph_envi *mph, unsigned short numState,
			   const struct lads_scia *lads,
			   const struct sqads1_scia *sqads,
			   const struct state1_scia *state )
{
     register unsigned short ni;
     register int nr;

     char date_str1[UTC_STRING_LENGTH], date_str2[UTC_STRING_LENGTH];

     char sql_query[SQL_STR_SIZE], cbuff[SQL_STR_SIZE];
     char *sql_long_query;

     char           *pntr;
     unsigned short numMatch;
     int            i_indx, i_state, i_date, i_musec, i_softv;
     int            meta_id, nrow, numRows;
     size_t         nc, numChar, sql_long_sz;
     unsigned int   musec;
     double         delayedBy;

     PGresult *res;

     const double SecPerDay = 24. * 60 * 60;

     struct coord_envi    corners[NUM_CORNERS];
     struct stateinfo_rec *stateRow = NULL;
/*
 * get all potential matching states from table "stateinfo"
 */
     if ( numState == 0 ) return;
     MJD_2_DATETIME( state->mjd.days, state->mjd.secnd, 0, date_str1 );
     MJD_2_DATETIME( state[numState-1].mjd.days, state[numState-1].mjd.secnd, 
		     0, date_str2 );
     numChar = snprintf( sql_query, SQL_STR_SIZE, SELECT_FROM_STATEINFO,
			 date_str1, date_str2 );
     if ( be_verbose )
	  (void) printf( "%s(): %s [%-zd]\n", __func__, sql_query, numChar );
     if ( numChar >= SQL_STR_SIZE )
          NADC_RETURN_ERROR( NADC_ERR_STRLEN, "sql_query" );
     res = PQexec( conn, sql_query );
     if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
	  NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     if ( (numRows = PQntuples( res )) == 0 ) 
	  NADC_GOTO_ERROR( NADC_ERR_WARN, NO_DMOP_FOUND );
     stateRow = (struct stateinfo_rec *) 
	  malloc( numRows * sizeof(struct stateinfo_rec) );
     if ( stateRow == NULL )
	  NADC_GOTO_ERROR( NADC_ERR_ALLOC, "stateRow" );

     i_indx  = PQfnumber( res, "pk_stateinfo" );
     i_state = PQfnumber( res, "stateID" );
     i_date  = PQfnumber( res, "dateTimeStart" );
     i_musec = PQfnumber( res, "muSecStart" );
     i_softv = PQfnumber( res, "softVersion" );
     for ( nr = 0; nr < numRows; nr++ ) {
	  pntr = PQgetvalue( res, nr, i_indx );
	  stateRow[nr].indxDMOP = 
	       (unsigned int) strtoul( pntr, (char **) NULL, 10 );
	  pntr = PQgetvalue( res, nr, i_state );
	  stateRow[nr].stateID  = (unsigned char) atoi( pntr );
	  pntr = PQgetvalue( res, nr, i_date );
	  musec = (unsigned int) strtoul( PQgetvalue( res, nr, i_musec ),
					   (char **) NULL, 10 );
	  stateRow[nr].jday = DATETIME_2_JULIAN( pntr, musec );
	  pntr = PQgetvalue( res, nr, i_softv );
	  (void) nadc_strlcpy( stateRow[nr].softVersion, pntr, 4 );
	  stateRow[nr].indxState = USHRT_MAX;
	  stateRow[nr].dtMatch = 30 / SecPerDay;
     }
     PQclear( res );
/*
 * loop over states from files to identify matching states from DMOP list
 */
     for ( ni = 0; ni < numState; ni++ ) {
	  register double jday  = state[ni].mjd.days 
	       + (state[ni].mjd.secnd + state[ni].mjd.musec / 1e6) / SecPerDay;
	  
	  for ( nr = 0; nr < numRows; nr++ ) {
	       register double dtime = fabs(jday - stateRow[nr].jday);

	       if ( (unsigned char) state[ni].state_id == stateRow[nr].stateID
		    && stateRow[nr].dtMatch > dtime ) {
		    stateRow[nr].indxState  = ni;
		    stateRow[nr].dtMatch    = dtime;
	       }
	  }
     }
/*
 * remove false/double matches
 */
     for ( nr = 1; nr < numRows; nr++ ) {
	  if ( stateRow[nr-1].indxState == stateRow[nr].indxState ) {
	       if ( stateRow[nr-1].dtMatch > stateRow[nr].dtMatch )
		    stateRow[nr-1].indxState = USHRT_MAX;
	       else
		    stateRow[nr].indxState = USHRT_MAX;
	  }
     }
/*
 * obtain pk_meta from table meta__1P
 */
     numChar = snprintf( sql_query, SQL_STR_SIZE,
			 "SELECT pk_meta FROM meta__1P WHERE name=\'%s\'",
			 mph->product );
     if ( be_verbose )
          (void) printf( "%s(): %s [%-zd]\n", __func__, sql_query, numChar );
     if ( numChar >= SQL_STR_SIZE )
          NADC_RETURN_ERROR( NADC_ERR_STRLEN, "sql_query" );
      res = PQexec( conn, sql_query );
     if ( PQresultStatus( res ) != PGRES_TUPLES_OK ) {
          NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     }
     if ( (nrow = PQntuples( res )) == 0 ) {
          NADC_GOTO_ERROR( NADC_ERR_FATAL, mph->product );
     }
     pntr = PQgetvalue( res, 0, 0 );
     meta_id = (int) strtol( pntr, (char **) NULL, 10 );
     PQclear( res );
/*
 * allocate memory for SQL string (maximum pk_stateinfo is 2 million ~ 7 digits)
 */
     sql_long_sz = (size_t) (42 + (numRows+1) * (7+1) + 4);
     if ( (sql_long_query = (char *) malloc( sql_long_sz )) == NULL ) {
	  free( stateRow );
	  NADC_RETURN_ERROR( NADC_ERR_ALLOC, "sql_long_query" );
     }
/*
 * insert all matches in table "stateinfo_meta__1P"
 */
     numChar = snprintf( sql_long_query, sql_long_sz, 
			 "INSERT INTO stateinfo_meta__1P VALUES(%-d,\'{",
			 meta_id );
     numMatch = 0;
     delayedBy = 0.;
     for ( nr = 0; nr < numRows; nr++ ) {
	  if ( (ni = stateRow[nr].indxState) == USHRT_MAX ) continue;

/* do not add entry for states without MDS attached */
	  if ( state[ni].flag_mds == (unsigned char) 1 ) continue;

	  nc = numChar;
	  if ( numMatch > 0 ) {
	       numChar += snprintf( sql_long_query+nc, sql_long_sz-nc, 
				    ",%-u", stateRow[nr].indxDMOP );
	  } else {
	       numChar += snprintf( sql_long_query+nc, sql_long_sz-nc, 
				    "%-u", stateRow[nr].indxDMOP );
	  }
	  if ( numChar >= sql_long_sz ) {
	       free( sql_long_query ); free( stateRow );
	       NADC_RETURN_ERROR( NADC_ERR_STRLEN, "sql_long_query" );
	  }
	  numMatch++;
	  delayedBy += (stateRow[nr].dtMatch *= SecPerDay);
     }
     nc = numChar;
     numChar += snprintf( sql_long_query+nc, sql_long_sz-nc, 
			  "}\',\'%1s\')", mph->proc_stage );
     if ( numChar >= sql_long_sz ) {
	  free( sql_long_query ); free( stateRow );
	  NADC_RETURN_ERROR( NADC_ERR_STRLEN, "sql_long_query" );
     }
     if ( be_verbose )
	  (void) printf( "%s(): %s [%-zd]\n", __func__, sql_long_query, numChar );
     res = PQexec( conn, sql_long_query );
     free( sql_long_query );
     if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
          NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     PQclear( res );
/*
 * check number of matches found
 */
     if ( numMatch == 0 ) {
	  free( stateRow );
	  NADC_RETURN_ERROR( NADC_ERR_WARN, NO_MATCHES_FOUND );
     }
     delayedBy /= numMatch;
/*
 * update meta__1P (noEntryDMOP, delayedBy)
 */
     (void) snprintf( sql_query, SQL_STR_SIZE, 
		      "UPDATE meta__1P SET noEntryDMOP=%d,",
		      (int)(numState - numMatch) );
     (void) snprintf( sql_query, SQL_STR_SIZE, 
		      "%s delayedBy=%.3f", strcpy(cbuff,sql_query), 
		      delayedBy );
     numChar = snprintf( sql_query, SQL_STR_SIZE, "%s WHERE pk_meta=%d",
			 strcpy(cbuff,sql_query), meta_id );
     if ( be_verbose )
	  (void) printf( "%s(): %s [%-zd]\n", __func__, sql_query, numChar );
     if ( numChar >= SQL_STR_SIZE )
          NADC_RETURN_ERROR( NADC_ERR_STRLEN, "sql_query" );
     res = PQexec( conn, sql_query );
     if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
	  NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     PQclear( res );
     (void) snprintf( cbuff, SQL_STR_SIZE,
		      "noEntryDMOP=%-d (States:%-hu DMOP:%-d), delayedBy=%-.3f",
		      numState - numMatch, numState, numRows, delayedBy );
     NADC_ERROR( NADC_ERR_NONE, cbuff );
/*
 * Start a transaction block
 */
     res = PQexec( conn, "BEGIN" );
     if ( PQresultStatus( res ) != PGRES_COMMAND_OK ) {
          NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
     }
     PQclear( res );
/*
 * update stateinfo (softVersion, orbitPhase, tile)
 */
     for ( nr = 0; nr < numRows; nr++ ) {
	  if ( (ni = stateRow[nr].indxState) == USHRT_MAX ) continue;

          /* do not add entry for states without MDS attached */
	  if ( state[ni].flag_mds == (unsigned char) 1 ) continue;

	  /* only update when procStage of product is higher */
	  if ( mph->proc_stage[0] < stateRow[nr].softVersion[1] ) continue;
	  stateRow[nr].softVersion[1] = mph->proc_stage[0];

	  (void) snprintf( sql_query, SQL_STR_SIZE, 
			   "UPDATE stateinfo SET softVersion=\'%s\',", 
			   stateRow[nr].softVersion );
	  (void) snprintf( sql_query, SQL_STR_SIZE, 
			   "%s orbitPhase=%f,", strcpy(cbuff,sql_query),
			   state[ni].orbit_phase );
	  if ( sqads[ni].flag_saa_region == (unsigned char) 1 )
	       (void) snprintf( sql_query, SQL_STR_SIZE, 
				"%s saaFlag=TRUE,", strcpy(cbuff,sql_query) );
	  else
	       (void) snprintf( sql_query, SQL_STR_SIZE, 
				"%s saaFlag=FALSE,", strcpy(cbuff,sql_query) );
	  CorrectLongitudes( lads[ni].corner, corners ); 
	  (void) snprintf( sql_query, SQL_STR_SIZE, 
			   GEO_POLY_FORMAT, strcpy(cbuff,sql_query), "tile=",
			   corners[0].lon / 1e6, corners[0].lat / 1e6,
			   corners[3].lon / 1e6, corners[3].lat / 1e6,
			   corners[2].lon / 1e6, corners[2].lat / 1e6,
			   corners[1].lon / 1e6, corners[1].lat / 1e6,
			   corners[0].lon / 1e6, corners[0].lat / 1e6,
			   4326 );
	  numChar = snprintf( sql_query, SQL_STR_SIZE, 
			      "%s WHERE pk_stateinfo=%u",
			      strcpy(cbuff,sql_query), stateRow[nr].indxDMOP );
	  if ( be_verbose )
	       (void) printf( "%s(): %s [%-zd]\n", __func__, sql_query, numChar );
	  if ( numChar >= SQL_STR_SIZE ) {
	       NADC_ERROR( NADC_ERR_STRLEN, "sql_query" );
	       PQclear( res );
	       res = PQexec( conn, "ROLLBACK" );
	       if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
		    NADC_ERROR( NADC_ERR_SQL,
				PQresultErrorMessage(res) );
	       goto done;
	  }
	  res = PQexec( conn, sql_query );
	  if ( PQresultStatus( res ) != PGRES_COMMAND_OK ) {
	       NADC_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
	       PQclear( res );
	       res = PQexec( conn, "ROLLBACK" );
	       if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
		    NADC_ERROR( NADC_ERR_SQL,
				PQresultErrorMessage(res) );
	       goto done;
	  }
	  PQclear( res );
     }
/*
 * end the transaction
 */
     res = PQexec( conn, "COMMIT" );
     if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
          NADC_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
 done:
     PQclear( res );
     if ( stateRow != NULL ) free( stateRow );
}
Example #14
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_LV1_DEL_ENTRY( PGconn *conn, bool be_verbose, const char *flname )
{
    register int nr;

    unsigned int indx;

    char     sql_query[SQL_STR_SIZE];
    char     procStage[2];
    char     *cpntr, sciafl[SHORT_STRING_LENGTH];
    int      nrow, numChar;

    PGresult *res, *res_update;
    /*
     * strip path of file-name
     */
    if ( (cpntr = strrchr( flname, '/' )) != NULL ) {
        (void) nadc_strlcpy( sciafl, ++cpntr, SHORT_STRING_LENGTH );
    } else {
        (void) nadc_strlcpy( sciafl, flname, SHORT_STRING_LENGTH );
    }
    /*
     * check if we have to reverse field "softVersion[1]" in table "stateinfo"
     */
    numChar = snprintf( sql_query, SQL_STR_SIZE, SELECT_FROM_STATEINFO,
                        sciafl );
    if ( be_verbose )
        (void) printf( "%s(): %s [%-d]\n", __func__, sql_query, numChar );
    if ( numChar >= SQL_STR_SIZE )
        NADC_RETURN_ERROR( NADC_ERR_STRLEN, "sql_query" );
    res = PQexec( conn, sql_query );
    if ( PQresultStatus( res ) != PGRES_TUPLES_OK ) {
        NADC_RETURN_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
    }
    /* return when no entries are found */
    if ( (nrow = PQntuples( res )) == 0 ) goto done;
    /*
     * update field "softVersion[1]" in table "stateinfo"
     */
    (void) nadc_strlcpy( procStage, sciafl+10, 2 );
    for ( nr = 0; nr < nrow; nr++ ) {
        cpntr = PQgetvalue( res, nr, 0 );
        indx = (unsigned int) strtoul( cpntr, (char **)NULL, 10 );
        numChar = snprintf( sql_query, SQL_STR_SIZE,
                            UPDATE_STATEINFO, indx, procStage );
        if ( be_verbose )
            (void) printf( "%s(): %s [%-d]\n", __func__, sql_query, numChar );
        if ( numChar >= SQL_STR_SIZE )
            NADC_RETURN_ERROR( NADC_ERR_STRLEN, "sql_query" );
        res_update = PQexec( conn, sql_query );
        if ( PQresultStatus( res_update ) != PGRES_COMMAND_OK ) {
            NADC_ERROR( NADC_ERR_SQL,
                        PQresultErrorMessage(res_update) );
            PQclear( res_update );
            goto done;
        }
        PQclear( res_update );
    }
done:
    PQclear( res );
    /*
     * remove entry from table "meta__1P"
     * referenced entries in table "stateinfo_meta__0P" are removed by the engine
     */
    numChar = snprintf( sql_query, SQL_STR_SIZE, DELETE_FROM_META, sciafl );
    if ( be_verbose )
        (void) printf( "%s(): %s [%-d]\n", __func__, sql_query, numChar );
    if ( numChar >= SQL_STR_SIZE )
        NADC_RETURN_ERROR( NADC_ERR_STRLEN, "sql_query" );
    res = PQexec( conn, sql_query );
    if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
        NADC_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
    PQclear( res );
}
Example #15
0
/*+++++++++++++++++++++++++
.IDENTifer   Read_IMLM_Header
.PURPOSE     read header of (ascii) SRON IMLM product
.INPUT/OUTPUT
  call as   Read_IMLM_Header( flname, hdr );
     input:
            char flname[]        :  name of the IMLM product
    output:
            struct imlm_hdr *hdr :  header info from IMLM product

.RETURNS     nothing, error status is passed by global nadc_stat
.COMMENTS    static function
-------------------------*/
static
void Read_IMLM_Header( const char flname[], /*@out@*/ struct imlm_hdr *hdr )
   /*@globals  errno, nadc_stat, nadc_err_stack;@*/
   /*@modifies errno, nadc_stat, nadc_err_stack, hdr@*/
{
     char   line[MAX_LINE_LENGTH];
     char   sciaDate[12], sciaTime[16];

     unsigned int nline = 0u;

     gzFile fp;
/*
 * open the IMLM product
 */
     if ( (fp = gzopen( flname, "r" )) == NULL )
	  NADC_RETURN_ERROR( NADC_ERR_FILE, flname );

     do {
          if ( gzeof ( fp ) == 1 ) break;
          if ( gzgets( fp, line, MAX_LINE_LENGTH ) == NULL ) break;
	  if ( strncmp( line, "ENDFILE", 7 ) == 0 ) break;

	  if ( *line == '#' ) {
	       if ( strncmp( line+2, "Generated", 9 ) == 0 ) {
		    char *cpntr = strrchr( line, '>' );
		    if ( cpntr == NULL ) continue;
		    (void) sscanf(cpntr+4, "%*s %s %s", sciaDate, sciaTime);
		    (void) snprintf( hdr->creation_date, 28,
                                     "%s %s", sciaDate, sciaTime );
		    cpntr[1] = '\0';
		    (void) nadc_strlcpy( hdr->contact, line+15, 64 );
	       } else if ( strncmp( line+2, "Level-2", 7 ) == 0 ) {
		    (void) sscanf( line, "# Level-2 format version: %s",
				   hdr->product_format );
	       } else if ( strncmp( line+2, "Level-1b", 8 ) == 0 ) {
		    (void) sscanf( line, "# Level-1b product: %s", 
				   hdr->l1b_product );
	       } else if ( strncmp( line+2, "Sensing start", 13 ) == 0 ) {
		    (void) sscanf( line, "# Sensing start: %s %s",
				   sciaDate, sciaTime );
		    DateTime2ADAGUC( sciaDate, sciaTime, hdr->validity_start );
	       } else if ( strncmp( line+2, "Sensing stop", 12 ) == 0 ) {
		    (void) sscanf( line, "# Sensing stop: %s %s",
				   sciaDate, sciaTime );
		    DateTime2ADAGUC( sciaDate, sciaTime, hdr->validity_stop );
	       } else if ( strncmp( line+2, "Orbit", 5 ) == 0 ) {
		    (void) sscanf( line, "# Orbit: %u", hdr->orbit );
	       } else if ( strncmp( line+2, "IMLM", 4 ) == 0 ) {
		    (void) sscanf( line, "# IMLM version: %s %*c", 
				   hdr->software_version );
	       } else if ( strncmp( line+2, "SCIAMACHY", 9 ) == 0 ) {
		    (void) sscanf( line, "# SCIAMACHY channel: %hhu", 
				   &hdr->scia_channel );
	       } else if ( strncmp( line+2, "Window (pixels)", 15 ) == 0 ) {
		    (void) sscanf( line, "# Window (pixels): %hu %*s %hu", 
				   hdr->window_pixel, hdr->window_pixel+1 );
	       } else if ( strncmp( line+2, "Window (nm)", 11 ) == 0 ) {
		    (void) sscanf( line, "# Window (nm): %f %*s %f", 
				   hdr->window_wave, hdr->window_wave+1 );
	       } else if ( strncmp( line+2, "Pixel mask", 10 ) == 0 ) {
		    (void) sscanf( line, "# Pixel mask version: %s (%*c", 
				   hdr->pixelmask_version );
	       } else if ( strncmp( line+2, "Cloud mask", 10 ) == 0 ) {
		    (void) sscanf( line, "# Cloud mask version: %s (%*c", 
				   hdr->cloudmask_version );
	       }
	  } else
	       ++nline;
     } while( TRUE );

     hdr->numProd = 1;
     hdr->numRec  = nline;
     (void) gzclose( fp );
}
Example #16
0
/*+++++++++++++++++++++++++
.IDENTifer   GOME_SET_PARAM
.PURPOSE     initializes struct with command-line parameters
.INPUT/OUTPUT
  call as   GOME_SET_PARAM( argc, argv, instrument, param );
     input:  
             int  argc       :   number of parameters
	     char *argv[]    :   parameter values
	     int instrument  :   code for instrument en data product level
    output:  
	     struct param_record 
	            *param   :     struct holding user-defined settings

.RETURNS     Nothing (check global error status)
.COMMENTS    none
-------------------------*/
void GOME_SET_PARAM( int argc, char *argv[], int instrument,
		     struct param_record *param )
{
     char   *cpntr;
     char   outfile[MAX_STRING_LENGTH];
     char   prog_master[SHORT_STRING_LENGTH];
     int    narg, num;
     float  rbuff[4];

     bool select_mds = FALSE;
/*
 * initialise param-structure
 */
     NADC_INIT_PARAM( param );
     (void) snprintf( param->program, sizeof(param->program), "%s", argv[0] );
/*
 * check number of options
 */
     if ( argc == 0 || argv[0] == NULL )
	  NADC_RETURN_ERROR( NADC_ERR_PARAM, "none found!?!" );
/*
 * strip path to program
 */
     if ( (cpntr = strrchr( argv[0], '/' )) != NULL ) {
	  (void) nadc_strlcpy( prog_master, ++cpntr, SHORT_STRING_LENGTH );
     } else {
	  (void) nadc_strlcpy( prog_master, argv[0], SHORT_STRING_LENGTH );
     }
/*
 * get command-line parameters
 */
     narg = 0;
     while ( ++narg < argc ) {
	  Check_User_Option( stderr, instrument, prog_master, argv[narg] );
/*
 * obtain name of input file
 */
	  if ( argv[narg][0] != '-' ) {
	       if ( param->flag_infile == PARAM_UNSET ) {
		    if ( strlen(argv[narg]) < MAX_STRING_LENGTH ) {
			 (void) strcpy( param->infile, argv[narg] );
		    } else {
			 char cbuff[MAX_STRING_LENGTH];

			 (void) snprintf( cbuff, MAX_STRING_LENGTH,
					 "Filename too long (max: %zd)\n",
					 MAX_STRING_LENGTH );
			 
			 NADC_RETURN_ERROR( NADC_ERR_FATAL, cbuff );
		    }
		    param->flag_infile = PARAM_SET;
	       } else {
		    NADC_RETURN_ERROR( NADC_ERR_PARAM, argv[narg] );
	       }
/*
 * process command-line options starting with one "-" (= standalone options)
 */
	  } else if ( argv[narg][0] == '-' && argv[narg][1] != '-' ) {
	       if ( (argv[narg][1] == 'h' && argv[narg][2] == '\0')
		    || strncmp( argv[narg]+1, "help", 4 ) == 0 )
		    Show_All_Options( stdout, instrument, prog_master );
	       if ( argv[narg][1] == 'V' 
		    || strncmp( argv[narg]+1, "version", 7 ) == 0 ) {
		    param->flag_version = PARAM_SET;
		    return;                             /* nothing else todo */
	       }
	       if ( strncmp( argv[narg]+1, "show", 4 ) == 0 ) {
		    param->flag_show = PARAM_SET;
		    param->write_ascii = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "silent", 6 ) == 0 ) {
		    param->flag_silent = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "verbose", 7 ) == 0 ) {
		    param->flag_verbose = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "check", 5 ) == 0 ) {
		    param->flag_check = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "meta", 4 ) == 0 ) {
		    param->write_meta = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "ascii", 5 ) == 0 ) {
		    param->write_ascii = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "sql", 3 ) == 0 ) {
#if defined(_WITH_SQL)
		    param->write_sql = PARAM_SET;
		    if ( instrument == GOME_LEVEL_1 ) {
			 param->calib_pmd = GOME_CAL_PMD;
			 param->write_pmd_geo = PARAM_UNSET;
		    }
#else
		    NADC_RETURN_ERROR( NADC_ERR_FATAL, 
				"no PostgreSQL support, recompile" );
#endif
	       } else if ( strncmp( argv[narg]+1, "remove", 6 ) == 0 ) {
		    param->flag_sql_remove = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "replace", 7 ) == 0 ) {
		    param->flag_sql_replace = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "hdf5", 4 ) == 0 ) {
		    param->write_hdf5 = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "compress", 8 ) == 0 ) {
		    param->flag_deflate = PARAM_SET;
/*
 * selection on all kind of data sets
 */
	       } else if ( strncmp( argv[narg]+1, "no_mds", 6 ) == 0 ) {
		    if ( ! select_mds ) {
			 select_mds = TRUE;
			 Do_Not_Extract_MDS( instrument, param );
		    }
	       } else if ( strncmp( argv[narg]+1, "moon", 4 ) == 0 ) {
		    if ( ! select_mds ) {
			 select_mds = TRUE;
			 Do_Not_Extract_MDS( instrument, param );
		    }
		    param->write_moon = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "sun", 3 ) == 0 ) {
		    if ( ! select_mds ) {
			 select_mds = TRUE;
			 Do_Not_Extract_MDS( instrument, param );
		    }
		    param->write_sun = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "no_pmd_geo", 10 ) == 0 ) {
		    param->write_pmd_geo = PARAM_UNSET;
	       } else if ( strncmp( argv[narg]+1, "no_pmd", 6 ) == 0 ) {
		    param->write_pmd = PARAM_UNSET;
		    param->write_pmd_geo = PARAM_UNSET;
	       } else if ( strncmp( argv[narg]+1, "no_moon", 7 ) == 0 ) {
		    param->write_moon = PARAM_UNSET;
	       } else if ( strncmp( argv[narg]+1, "no_sun", 6 ) == 0 ) {
		    param->write_sun = PARAM_UNSET;
	       } else if ( strncmp( argv[narg]+1, "blind", 5 ) == 0 ) {
		    param->write_blind = PARAM_SET;
	       } else if ( strncmp( argv[narg]+1, "stray", 5 ) == 0 ) {
		    param->write_stray = PARAM_SET;
	       } 
	  } else if ( argv[narg][0] == '-' && argv[narg][1] == '-' ) {
	       if ( strncmp( argv[narg]+2, "band", 4 ) == 0 ) {
		    if ( (cpntr = strchr( argv[narg], '=' )) == NULL ) { 
			 param->chan_mask = BAND_NONE;
		    } else {
			 param->chan_mask = Set_Band_Val( ++cpntr );
			 if ( param->chan_mask == BAND_NONE ) {
			      NADC_RETURN_ERROR( 
						 NADC_ERR_PARAM, argv[narg] );
			 }
		    }
	       } else if ( strncmp( argv[narg]+2, "ipixel", 6 ) == 0 ) {
		    if ( param->flag_pselect == PARAM_UNSET 
			 && (cpntr = strchr( argv[narg], '=' )) != NULL ) {
                         if ( strlen( ++cpntr ) < SHORT_STRING_LENGTH ) {
                              (void) strcpy( param->pselect, cpntr );
                         } else {
                              char cbuff[SHORT_STRING_LENGTH];

                              (void) snprintf( cbuff, SHORT_STRING_LENGTH,
                                       "Pixel selection too long (max: %d)\n",
                                        (int) SHORT_STRING_LENGTH );
                              NADC_RETURN_ERROR( NADC_ERR_FATAL,cbuff );
                         }
                         param->flag_pselect = PARAM_SET;
                    }
	       } else if ( strncmp( argv[narg]+2, "xpixel", 6 ) == 0 ) {
		    if ( param->flag_subset == PARAM_UNSET 
			 && (cpntr = strchr( argv[narg], '=' )) != NULL ) {
			 param->flag_subset = PARAM_SET;
                         param->write_subset = SUBSET_NONE;

			 if ( strchr( cpntr, 'E' ) != NULL )
			      param->write_subset += SUBSET_EAST;

			 if ( strchr( cpntr, 'C' ) != NULL )
			      param->write_subset += SUBSET_CENTER;

			 if ( strchr( cpntr, 'W' ) != NULL )
			      param->write_subset += SUBSET_WEST;

			 if ( strchr( cpntr, 'B' ) != NULL )
			      param->write_subset += SUBSET_BACK;
		    }
		    if ( param->write_subset == SUBSET_NONE )
			 NADC_RETURN_ERROR(NADC_ERR_PARAM, argv[narg]);
	       } else if ( strncmp( argv[narg]+2, "wave", 4 ) == 0 ) {
		    if ( param->flag_wave == PARAM_UNSET 
			 && (cpntr = strchr( argv[narg], '=' )) != NULL ) {
                         (void) NADC_USRINP( FLT32_T, cpntr+1, 
                                             2, param->wave, &num );
                         param->flag_wave = PARAM_SET;
                    }
	       } else if ( strncmp( argv[narg]+2, "time", 4 ) == 0 ) {
		    if ( param->flag_period == PARAM_UNSET ) {
			 param->flag_period = PARAM_SET;
			 Set_Time_Window( argc, argv, &narg, 
					  param->bgn_date, param->end_date );
			 if ( IS_ERR_STAT_FATAL )
			      NADC_RETURN_ERROR(NADC_ERR_PARAM, "");
		    }
	       } else if ( strncmp( argv[narg]+2, "region", 6 ) == 0 ) {
		    if ( param->flag_geoloc == PARAM_UNSET
			 && (cpntr = strchr( argv[narg], '=' )) != NULL ) {

			 (void) NADC_USRINP( FLT32_T, cpntr+1, 4, rbuff, &num);
			 if ( num == 4 ) {
			      param->geo_lat[0] = 
				   min_t( float, rbuff[0], rbuff[1] );
			      param->geo_lat[1] = 
				   max_t( float, rbuff[0], rbuff[1] );

			      if ( rbuff[2] < rbuff[3] )
				   param->flag_geomnmx = PARAM_SET;
			      else
				   param->flag_geomnmx = PARAM_UNSET;
			      param->geo_lon[0] = 
				   min_t( float, rbuff[2], rbuff[3] );
			      param->geo_lon[1] = 
				   max_t( float, rbuff[2], rbuff[3] );
			 } else {
			      NADC_RETURN_ERROR( 
						 NADC_ERR_PARAM, argv[narg] );
			 }
			 param->flag_geoloc = PARAM_SET;
		    }
               } else if ( strncmp( argv[narg]+2, "cloud", 5 ) == 0 ) {
Example #17
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;
}
Example #18
0
/*+++++++++++++++++++++++++ FUNCTIONS +++++++++++++++++++++++++*/
void NADC_USRINP( int type, const char *string, int maxval, 
		  void *pntr, int *nrval )
{
     register int  ii;

     int    nr, nrstep;
     char   *str_bgn, *str_ptr, *pntcm, *pnt2d, cval[21], *cmval[NR_CMVAL];
     double dmval[NR_CMVAL];

     union u_buff {
	  unsigned char ucval;
	  short  sval;
	  unsigned short usval;
	  int    ival;
	  unsigned int uval;
	  float  rval;
	  double dval; 
     } *uu;

     uu = (union u_buff *) malloc( (size_t) maxval * sizeof( union u_buff )); 
     if ( uu == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "uu" );
/*
 * Initialisation
 */
     *nrval = 0;
     nr = 0;
     do {
	  cmval[nr] = (char *) malloc( strlen( string ) );
	  if ( cmval[nr] == NULL ) {
	       free( uu );
	       NADC_RETURN_ERROR( NADC_ERR_ALLOC, "cmval" );
	  }
     } while ( ++nr < NR_CMVAL );
/*
 * Copy only non-blanks from input string
 */
     str_bgn = (char *) malloc( strlen( string ) + 1 );
     if ( (str_ptr = str_bgn) == NULL ) {
	  free( uu );
	  for ( nr = 0; nr < NR_CMVAL; nr++ ) free( cmval[nr] );
	  NADC_RETURN_ERROR( NADC_ERR_ALLOC, "str_bgn" );
     }
     (void) strcpy( str_ptr, string );
     (void) strtok( str_ptr, " " );
     while( strlen( str_ptr ) > (size_t) 0 && *nrval < maxval ) {
	  nr = 0;
	  pntcm = strchr( str_ptr, ',' );
	  pnt2d = strchr( str_ptr, ':' );
/*
 * Does the string contain zero or more characters and no "," or ":" ?
 *   or does it contain a "," before a ":" ?
 */
	  if ( pnt2d == NULL || (pntcm != NULL && pntcm < pnt2d) ) {
	       if ( pntcm == NULL ) {
		    (void) strcpy( cval, str_ptr );
		    *str_ptr = '\0';
	       } else { 
		    (void) nadc_strlcpy( cval, str_ptr, 
					 (size_t)(pntcm - str_ptr)+1 );
		    str_ptr = pntcm + 1;
	       }
	       switch( type ) {
	       case UINT8_T:
		    uu[*nrval].ucval = (unsigned char) atoi( cval );
		    break;
	       case INT16_T:
		    uu[*nrval].sval = (short) atoi( cval );
		    break;
	       case UINT16_T:
		    uu[*nrval].usval = (unsigned short) atoi( cval );
		    break;
               case INT32_T:
		    uu[*nrval].ival = atoi( cval );
		    break;
               case UINT32_T: 
		    uu[*nrval].uval = (unsigned int) atol( cval );
		    break;
               case FLT32_T: 
		    uu[*nrval].rval = (float) atof( cval );
		    break;
               case FLT64_T:
		    uu[*nrval].dval = atof( cval );
		    break;
	       }
	       (*nrval)++;
	  } else {                                            /*store CSTART*/
/*
 * It contains a ":" before a "," !
 */
	       (void) nadc_strlcpy( cmval[nr], str_ptr, 
				    (size_t)(pnt2d - str_ptr)+1 );
	       nr++;
	       str_ptr = pnt2d + 1;
	       pnt2d = strchr( str_ptr, ':' );
/*
 * There are two cases after a:b:c there is no futher input: pntcm == NULL
 * or there is more something like a:b:c,d...
 */
	       if ( pntcm == NULL ) {
		    if ( pnt2d != NULL ) {                        /*get CEND*/
			 (void) nadc_strlcpy( cmval[nr], str_ptr, 
					      (size_t)(pnt2d - str_ptr)+1 );
			 nr++;
			 str_ptr = pnt2d + 1;
			 pnt2d = strchr( str_ptr, ':' );
			 if ( pnt2d == NULL ) {                /*store CINCR*/
			      (void) strcpy( cmval[nr], str_ptr );
			      *str_ptr = '\0';
			 } else {                  /*wrong syntax a:b:c:d...*/
			      NADC_RETURN_ERROR( NADC_ERR_PARAM,
					       "a:b:c:d..." );
			 }
		    } else {                                 /*set CINCR = 1*/
			 (void) strcpy( cmval[nr++], str_ptr );
			 (void) strcpy( cmval[nr], "1" );
			 *str_ptr = '\0';
		    }
	       } else {
/*
 * The string still contains a "," 
 */
		    if ( pnt2d != NULL && pnt2d < pntcm ) {       /*get CEND*/
			 (void) nadc_strlcpy( cmval[nr], str_ptr, 
					      (size_t)(pnt2d - str_ptr)+1 );
			 nr++;
			 str_ptr = pnt2d + 1;
			 pnt2d = strchr( str_ptr, ':' );
		    } else {                                 /*set CINCR = 1*/
			 (void) nadc_strlcpy( cmval[nr], str_ptr,
					      (size_t)(pntcm - str_ptr)+1 );
			 nr++;
			 (void) strcpy( cmval[nr], "1" );
		    }
		    if ( pnt2d != NULL && pnt2d < pntcm ) {
			      NADC_RETURN_ERROR( NADC_ERR_PARAM,
					       "a:b:c:d,.." );
		    } else {                                     /*get CINCR*/
			 (void) nadc_strlcpy( cmval[nr], str_ptr, 
					      (size_t) (pntcm - str_ptr)+1 );
			 nr++;
			 str_ptr = pntcm + 1;
		    }
	       }
/*
 * We've found CSTART, CEND and CINCR
 */
	       dmval[0] = atof( cmval[0] );
	       dmval[1] = atof( cmval[1] );
	       dmval[2] = atof( cmval[2] );
	       if ( dmval[2] >= -DBL_EPSILON && dmval[2] <= DBL_EPSILON ) 
		    dmval[2] = 1.0;
	       else if ( dmval[2] > DBL_EPSILON &&
			 (dmval[0] - dmval[1]) >= DBL_EPSILON )
		    dmval[2] *= -1.;
	       else if ( dmval[2] < DBL_EPSILON &&
			 (dmval[1] - dmval[0]) >= DBL_EPSILON )
		    dmval[2] *= -1.;
	       nrstep = abs( (int)((dmval[0] - dmval[1])/dmval[2]) ) + 1;

	       if ( nrstep + *nrval > maxval ) {
		    NADC_ERROR( NADC_ERR_PARAM, "exceed array (truncated)" );
		    nrstep = maxval - *nrval;
	       }

	       switch( type ) {
	       case UINT8_T:
		    ii = 0;
		    do { 
			 uu[*nrval].ucval = 
			      (unsigned char) (dmval[0] + ii * dmval[2]);
			 (*nrval)++;
		    } while ( ++ii < nrstep );
		    break;
	       case INT16_T:
		    ii = 0;
		    do { 
			 uu[*nrval].sval = 
			      (short) (dmval[0] + ii * dmval[2]);
			 (*nrval)++;
		    } while ( ++ii < nrstep );
		    break;
	       case UINT16_T:
		    ii = 0;
		    do { 
			 uu[*nrval].usval = 
			      (unsigned short) (dmval[0] + ii * dmval[2]);
			 (*nrval)++;
		    } while ( ++ii < nrstep );
		    break;
	       case INT32_T:
		    ii = 0;
		    do { 
			 uu[*nrval].ival = 
			      (int) (dmval[0] + ii * dmval[2]);
			 (*nrval)++;
		    } while ( ++ii < nrstep );
		    break;
	       case UINT32_T: 
		    ii = 0;
		    do { 
			 uu[*nrval].uval = 
			      (unsigned int) (dmval[0] + ii * dmval[2]);
			 (*nrval)++;
		    } while ( ++ii < nrstep );
		    break;
	       case FLT32_T: 
		    ii = 0;
		    do { 
			 uu[*nrval].rval = 
			      (float) (dmval[0] + ii * dmval[2]);
			 (*nrval)++;
		    } while ( ++ii < nrstep );
		    break;
	       default:
		    ii = 0;
		    do { 
			 uu[*nrval].dval = dmval[0] + ii * dmval[2];
			 (*nrval)++;
		    } while ( ++ii < nrstep );
		    break;
	       }
	  }
     }
/*
 * Fill the output array
 */
     switch( type ) {
     case UINT8_T: {
	  unsigned char *buff = (unsigned char *) pntr;
	  ii = 0;
	  do { buff[ii] = uu[ii].ucval; } while ( ++ii < *nrval );
	  break;
     }
     case INT16_T: {
	  short *buff = (short *) pntr;
	  ii = 0;
	  do { buff[ii] = uu[ii].sval; } while ( ++ii < *nrval );
	  break;
     }
     case UINT16_T: {
	  unsigned short *buff = (unsigned short *) pntr;
	  ii = 0;
	  do { buff[ii] = uu[ii].usval; } while ( ++ii < *nrval );
	  break;
     }
     case INT32_T: {
	  int *buff = (int *) pntr;
	  ii = 0;
	  do { buff[ii] = uu[ii].ival; } while ( ++ii < *nrval );
	  break;
     }
     case UINT32_T: {
	  unsigned int *buff = (unsigned int *) pntr;
	  ii = 0;
	  do { buff[ii] = uu[ii].uval; } while ( ++ii < *nrval );
	  break;
     }
     case FLT32_T: {
	  float *buff = (float *) pntr;
	  ii = 0;
	  do { buff[ii] = uu[ii].rval; } while ( ++ii < *nrval );
	  break;
     }
     default: {
	  double *buff = (double *) pntr;
	  ii = 0;
	  do { buff[ii] = uu[ii].dval; } while ( ++ii < *nrval );
	  break;
     }}
/*
 * free allocated memory
 */
     if ( str_bgn != NULL ) free( str_bgn );
     free( uu );
     for ( nr = 0; nr < NR_CMVAL; nr++ ) free( cmval[nr] );
}