void NADC_FRESCO_WR_SQL_SCIA_TILE( PGconn *conn, const char *prodName,
				   unsigned int num_rec, 
				   const struct fresco_rec *rec )
{
     register unsigned int nr;
     register unsigned int insertedRows = 0u;
     register unsigned int failedRows = num_rec;

     char   sql_query[SQL_STR_SIZE], cbuff[SQL_STR_SIZE];

     char       *pntr;
     int        nrow, numChar, meta_id;

     long long  tile_id;

     PGresult   *res;
/*
 * 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, prodName );
     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, prodName );
     }
     pntr = PQgetvalue( res, 0, 0 );
     meta_id = (int) strtol( pntr, (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 tiles in products
 */
     for ( nr = 0; nr < num_rec; nr++ ) {
	  /* obtain next value for serial pk_tile */
	  res = PQexec( conn,
			"SELECT nextval(\'tile_fresco_pk_tile_seq\')" );
	  if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
	       NADC_GOTO_ERROR( NADC_ERR_SQL, 
				PQresultErrorMessage(res) );
	  pntr = PQgetvalue( res, 0, 0 );
	  tile_id = strtoll( pntr, (char **) NULL, 10 );
	  PQclear( res );

	  numChar = snprintf( sql_query, SQL_STR_SIZE, SQL_INSERT_SCIA_TILE, 
			      TILE_TBL_NAME, tile_id, meta_id, rec[nr].jday,
			      rec[nr].meta.intg_time, 
			      rec[nr].meta.errorFlag, rec[nr].cloudFraction, 
			      rec[nr].cloudTopHeight, rec[nr].cloudTopPress,
			      rec[nr].cloudAlbedo, rec[nr].surfaceHeight, 
			      rec[nr].groundPress, rec[nr].surfaceAlbedo,
			      rec[nr].lon_corner[0],rec[nr].lat_corner[0],
			      rec[nr].lon_corner[1],rec[nr].lat_corner[1],
			      rec[nr].lon_corner[2],rec[nr].lat_corner[2],
			      rec[nr].lon_corner[3],rec[nr].lat_corner[3],
			      rec[nr].lon_corner[0],rec[nr].lat_corner[0] );

 	  /* (void) fprintf( stderr, "%s [%-d]\n", 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 );
	       res = PQexec( conn, "ROLLBACK" );
	       if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
		    NADC_ERROR( NADC_ERR_SQL,
				PQresultErrorMessage(res) );
	       goto done;
	  }
	  PQclear( res );

	  insertedRows++;
	  failedRows--;
     }
/*
 * 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, "insertedRows=%-u", insertedRows );
     NADC_ERROR( NADC_ERR_NONE, cbuff );
     if ( failedRows > 0 ) {
	  (void) snprintf( cbuff, SQL_STR_SIZE, "failedRows=%-u", failedRows );
	  NADC_ERROR( NADC_ERR_NONE, cbuff );
     }
}
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void MERIS_WR_ASCII_SPH( struct param_record param, 
			 const struct sph_meris *sph )
{
     register unsigned int  nr = 0;

     FILE *outfl = CRE_ASCII_File( param.outfile, "sph" );

     if ( outfl == NULL || IS_ERR_STAT_FATAL )
	  NADC_RETURN_ERROR( NADC_ERR_FILE_CRE, param.outfile );
/*
 * write ASCII dump of SPH record
 */
     nadc_write_header( outfl, nr, param.infile, 
			 "Specific Product Header of Level 1b Product" );
     nadc_write_text( outfl, ++nr, "SPH_DESCRIPTOR", sph->descriptor );
     nadc_write_short( outfl, ++nr, "STRIPLINE_CONTINUITY_INDICATOR", 
		       sph->stripline );
     nadc_write_short( outfl, ++nr, "SLICE_POSITION", sph->slice_pos );
     nadc_write_ushort( outfl, ++nr, "NUM_SLICES", sph->num_slices );
     nadc_write_text( outfl, ++nr, "FIRST_LINE_TIME", sph->start_time );
     nadc_write_text( outfl, ++nr, "LAST_LINE_TIME", sph->stop_time );
     nadc_write_double( outfl, ++nr, "FIRST_FIRST_LAT", 
			6, sph->first_first_lat );
     nadc_write_double( outfl, ++nr, "FIRST_FIRST_LONG", 
			6, sph->first_first_lon );
     nadc_write_double( outfl, ++nr, "FIRST_MID_LAT", 6, sph->first_mid_lat );
     nadc_write_double( outfl, ++nr, "FIRST_MID_LONG", 6, sph->first_mid_lon );
     nadc_write_double( outfl, ++nr, "FIRST_LAST_LAT", 
			6, sph->first_last_lat );
     nadc_write_double( outfl, ++nr, "FIRST_LAST_LONG", 
			6, sph->first_last_lon );
     nadc_write_double( outfl, ++nr, "LAST_FIRST_LAT", 
			6, sph->last_first_lat );
     nadc_write_double( outfl, ++nr, "LAST_FIRST_LONG", 
			6, sph->last_first_lon );
     nadc_write_double( outfl, ++nr, "LAST_MID_LAT", 6, sph->last_mid_lat );
     nadc_write_double( outfl, ++nr, "LAST_MID_LONG", 6, sph->last_mid_lon );
     nadc_write_double( outfl, ++nr, "LAST_LAST_LAT", 6, sph->last_last_lat );
     nadc_write_double( outfl, ++nr, "LAST_LAST_LONG", 6, sph->last_last_lon );
     nr++;    /* spare_1 */
     nadc_write_bool( outfl, ++nr, "TRANS_ERR_FLAG", sph->trans_err );
     nadc_write_bool( outfl, ++nr, "FORMAT_ERR_FLAG", sph->format_err );
     nadc_write_bool( outfl, ++nr, "DATABASE_FLAG", sph->database );
     nadc_write_bool( outfl, ++nr, "COARSE_ERR_FLAG", sph->coarse_err );
     nadc_write_bool( outfl, ++nr, "ECMWF_TYPE", sph->ecmwf_type );
     nadc_write_ushort( outfl, ++nr, "NUM_TRANS_ERR", sph->num_trans_err );
     nadc_write_ushort( outfl, ++nr, "NUM_FORMAT_ERR", sph->num_format_err );
     nadc_write_float( outfl, ++nr, "TRANS_ERR_THRESH", 
		       6, sph->thres_trans_err );
     nadc_write_float( outfl, ++nr, "FORMAT_ERR_THRESH", 
		       6, sph->thres_format_err );
     nr++;    /* spare_2 */
     nadc_write_ushort( outfl, ++nr, "NUM_BANDS", sph->num_bands );
     nadc_write_text( outfl, ++nr, "BAND_WAVELEN", sph->band_wavelen );
     nadc_write_text( outfl, ++nr, "BANDWIDTH", sph->bandwidth );
     nadc_write_double( outfl, ++nr, "INST_FOV", 6, sph->inst_fov );
     nadc_write_bool( outfl, ++nr, "PROC_MODE", sph->proc_mode );
     nadc_write_bool( outfl, ++nr, "OFFSET_COMP", sph->offset_comp );
     nadc_write_double( outfl, ++nr, "LINE_TIME_INTERVAL", 
			6, sph->line_time_interval );
     nadc_write_ushort( outfl, ++nr, "LINE_LENGTH", sph->line_length );
     nadc_write_ushort( outfl, ++nr, "LINES_PER_TIE_PT", sph->lines_per_tie );
     nadc_write_ushort( outfl, ++nr, "SAMPLES_PER_TIE_PT", 
			sph->samples_per_tie );
     nadc_write_float( outfl, ++nr, "COLUMN_SPACING", 6, sph->column_spacing );
     nr++;    /* spare_3 */
     (void) fclose( outfl );
}
Beispiel #3
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 );
}
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_WR_SQL_CH4_TILE( PGconn *conn, const char *prodName,
			   unsigned int num_rec, 
			   const struct imap_rec *rec )
{
     register unsigned int nr;
     register unsigned int affectedRows = 0u;

     char   sql_query[SQL_STR_SIZE], cbuff[SQL_STR_SIZE];

     char   *pntr;
     int    nrow, numChar, meta_id;

     long long tile_id;

     PGresult *res;
/*
 * 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, prodName );
     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, prodName );
     }
     pntr = PQgetvalue( res, 0, 0 );
     meta_id = (int) strtol( pntr, (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 tiles in products
 */
     for ( nr = 0; nr < num_rec; nr++ ) {
	  /* obtain next value for serial pk_tile */
	  res = PQexec( conn,
			"SELECT nextval(\'tile_imap_ch4_pk_tile_seq\')" );
	  if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
	       NADC_GOTO_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
	  pntr = PQgetvalue( res, 0, 0 );
	  tile_id = strtoll( pntr, (char **) NULL, 10 );
	  PQclear( res );

	  numChar = snprintf( sql_query, SQL_STR_SIZE, SQL_INSERT_TILE, 
			      TILE_TBL_NAME, tile_id, meta_id, rec[nr].jday,
			      NINT(16 * rec[nr].meta.intg_time),
			      rec[nr].meta.elev, 
			      rec[nr].ch4_vcd, rec[nr].ch4_error,
			      rec[nr].co2_vcd, rec[nr].co2_error,
			      rec[nr].ch4_vmr,
			      rec[nr].lon_corner[0],rec[nr].lat_corner[0],
			      rec[nr].lon_corner[1],rec[nr].lat_corner[1],
			      rec[nr].lon_corner[2],rec[nr].lat_corner[2],
			      rec[nr].lon_corner[3],rec[nr].lat_corner[3],
			      rec[nr].lon_corner[0],rec[nr].lat_corner[0] );

  	  (void) fprintf( stderr, "%s [%-d]\n", 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 );
	       res = PQexec( conn, "ROLLBACK" );
	       if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
		    NADC_ERROR( NADC_ERR_SQL, PQresultErrorMessage(res) );
	       goto done;
	  }
	  PQclear( res );

	  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 );
}
Beispiel #5
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_LV2_WR_ASCII_SPH( struct param_record param, 
			    const struct sph2_scia *sph )
{
     register unsigned int  ni, nr = 0;

     char           cbuff[MX_SZ_CBUFF];
     unsigned short ubuff[8];
     unsigned int   ndim;
     unsigned int   dims[2];

     FILE *outfl = CRE_ASCII_File( param.outfile, "sph" );

     if ( outfl == NULL || IS_ERR_STAT_FATAL )
	  NADC_RETURN_ERROR( NADC_ERR_FILE_CRE, param.outfile );
/*
 * write ASCII dump of SPH record
 */
     nadc_write_header( outfl, nr, param.infile, 
			 "Specific Product Header of Level 2 Product" );
     nadc_write_text( outfl, ++nr, "SPH_DESCRIPTOR", sph->descriptor );
     nadc_write_short( outfl, ++nr, "STRIPLINE_CONTINUITY_INDICATOR", 
		       sph->stripline );
     nadc_write_short( outfl, ++nr, "SLICE_POSITION", sph->slice_pos );
     nadc_write_ushort( outfl, ++nr, "NUM_SLICES", sph->no_slice );
     nadc_write_text( outfl, ++nr, "START_TIME", sph->start_time );
     nadc_write_text( outfl, ++nr, "STOP_TIME", sph->stop_time );
     nadc_write_double( outfl, ++nr, "START_LAT", 6, sph->start_lat );
     nadc_write_double( outfl, ++nr, "START_LON", 6, sph->start_lon );
     nadc_write_double( outfl, ++nr, "STOP_LAT", 6, sph->stop_lat );
     nadc_write_double( outfl, ++nr, "STOP_LON", 6, sph->stop_lon );
     ++nr;    /* spare_1 */
     nadc_write_text( outfl, ++nr, "FITTING_ERROR_SUM", sph->fit_error );
     nadc_write_ushort( outfl, ++nr, "NO_OF_DOAS_FITTING_WIN", 
			 sph->no_doas_win );
     ndim = 2;
     for ( ni = 0; ni < sph->no_doas_win; ni++ ) {
	  (void) snprintf(cbuff, MX_SZ_CBUFF, "DOAS_FITTING_WINDOW_%02u", ni);
	  ubuff[0] = sph->doas_win[ni].wv_min;
	  ubuff[1] = sph->doas_win[ni].wv_max;
	  nadc_write_arr_ushort( outfl, ++nr, cbuff, 1, &ndim, ubuff );
     }
     nr += (MAX_DOAS_FITTING_WIN - sph->no_doas_win);
     nadc_write_ushort( outfl, ++nr, "NO_OF_BIAS_FITTING_WIN", 
			 sph->no_bias_win );
     for ( ni = 0; ni < sph->no_bias_win; ni++ ) {
	  (void) snprintf(cbuff, MX_SZ_CBUFF, "BIAS_FITTING_WINDOW_%02u", ni);
	  ubuff[0] = sph->bias_win[ni].wv_min;
	  ubuff[1] = sph->bias_win[ni].wv_max;
	  if ( sph->bias_win[ni].nr_micro == 0 )
	       nadc_write_arr_ushort( outfl, ++nr, cbuff, 1, &ndim, ubuff );
	  else {
	       register unsigned int nm, nb;

	       dims[0] = 2;
	       dims[1] = sph->bias_win[ni].nr_micro+1;
	       for ( nb = 2, nm = 0; nm < sph->bias_win[ni].nr_micro; nm++ ) {
		    ubuff[nb++] = sph->bias_win[ni].micro_min[nm];
		    ubuff[nb++] = sph->bias_win[ni].micro_max[nm];
	       }
	       nadc_write_arr_ushort( outfl, ++nr, cbuff, 2, dims, ubuff );
	  }
     }
     nadc_write_ushort( outfl, ++nr, "NO_OF_DOAS_MOL",  sph->no_doas_mol );
     for ( ni = 0; ni < MAX_DOAS_SPECIES; ni++ ) {
	  (void) snprintf( cbuff, MX_SZ_CBUFF, "DOAS_MOLECULE_%02u", ni );
          nadc_write_text( outfl, ++nr, cbuff, sph->doas_mol[ni] );
     }
     nadc_write_ushort( outfl, ++nr, "NO_OF_BIAS_MOL", sph->no_bias_mol );
     for ( ni = 0; ni < sph->no_bias_mol; ni++ ) {
	  (void) snprintf( cbuff, MX_SZ_CBUFF, "BIAS_MOLECULE_%02u", ni );
          nadc_write_text( outfl, ++nr, cbuff, sph->bias_mol[ni] );
     }
     (void) fclose( outfl );
}
Beispiel #6
0
/*+++++++++++++++++++++++++
.IDENTifer   nadc_write_arr_float 
.PURPOSE     write keyword and its values in uniform fashion
.INPUT/OUTPUT
  call as    nadc_write_arr_float ( fp, key_num, key_wrd, val_ndim, 
                                  val_count, key_val );

     input:
            FILE *fp                 :  file pointer
            unsigned int  key_num    :  (unique) number for this keyword
            char key_wrd[]           :  keyword description 
            int  digits              :  number of digits (floating point only)
	    int  val_ndim            :  number  of dimensions
	                                Ps. a negative dimension flips the axis
	    unsigned int val_count[] :  size of the array dimensions
            float key_val[]          :  values for this keyword

.RETURNS     nothing (check global error status)
.COMMENTS    none
-------------------------*/
void nadc_write_arr_float( FILE *fp, unsigned int key_num, 
			   const char key_wrd[], int val_ndim, 
			   const unsigned int val_count[], 
			   int digits, const float key_val[] )
{
     register unsigned int nx, ny;

     char str_fmt[12];

     (void) snprintf( str_fmt, 12, " %%.%-dg", digits );
     if ( val_ndim == 1 ) {
	  (void) fprintf( fp, "%3u %s %-33s %s array[%-u]\n", key_num,
			  FIELD_SEPARATOR, key_wrd, FIELD_SEPARATOR, 
			  val_count[0] );
	  (void) fprintf( fp, "#" );
	  for ( nx = 0; nx < val_count[0]; nx++ )
	       (void) fprintf( fp, str_fmt, *key_val++ );
	  (void) fprintf( fp, "\n" );
     } else if ( val_ndim == 2 ) {
	  (void) fprintf( fp, "%3u %s %-33s %s array[%-u,%-u]\n", key_num,
			  FIELD_SEPARATOR, key_wrd, FIELD_SEPARATOR, 
			  val_count[0], val_count[1] );

	  for ( ny = 0; ny < val_count[1]; ny++ ) {
	       (void) fprintf( fp, "#" );
	       for ( nx = 0; nx < val_count[0]; nx++ ) {
		    if ( isnormal( *key_val ) )
			 (void) fprintf( fp, str_fmt, *key_val++ );
		    else
			 (void) fprintf( fp, " NaN" );
	       }
	       (void) fprintf( fp, "\n" );
	  }
     } else if ( val_ndim == -1 ) {
	  (void) fprintf( fp, "%3u %s %-33s %s array[1,%-u]\n", key_num,
			  FIELD_SEPARATOR, key_wrd, FIELD_SEPARATOR, 
			  val_count[0] );
	  for ( nx = 0; nx < val_count[0]; nx++ ) {
	       (void) fprintf( fp, "#" );
	       (void) fprintf( fp, str_fmt, *key_val++ );
	       (void) fprintf( fp, "\n" );
	  }
     } else if ( val_ndim == -2 ) {
	  (void) fprintf( fp, "%3u %s %-33s %s array[%-u,%-u]\n", key_num,
			  FIELD_SEPARATOR, key_wrd, FIELD_SEPARATOR, 
			  val_count[1], val_count[0] );

	  for ( nx = 0; nx < val_count[0]; nx++ ) {
	       (void) fprintf( fp, "#" );
	       for ( ny = 0; ny < val_count[1]; ny++ ) {
		    if ( isnormal(key_val[nx + ny * val_count[0]] ) )
			 (void) fprintf( fp, str_fmt, 
					 key_val[nx + ny * val_count[0]] );
		    else
			 (void) fprintf( fp, " NaN" ); 
	       }
	       (void) fprintf( fp, "\n" );
	  }
     } else {
	  NADC_RETURN_ERROR( NADC_ERR_FILE_RD, key_wrd );
     }
}
Beispiel #7
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] );
}
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_LV1C_WR_ASCII_CALOPT( const struct param_record param,
				const struct cal_options *calopt )
{
     register unsigned int nr = 0;

     char date_str[UTC_STRING_LENGTH];

     unsigned int   count[1];

     FILE *outfl = CRE_ASCII_File( param.outfile, "calopt" );

     if ( outfl == NULL || IS_ERR_STAT_FATAL )
	  NADC_RETURN_ERROR( NADC_ERR_FILE_CRE, param.outfile );
/*
 * write ASCII dump of CAL_OPTIONS record
 */
     nadc_write_header( outfl, nr, param.infile, 
			 "Calibration Options GADS to SciaL1C" );
     nadc_write_text( outfl,++nr, "l1b_product_name", calopt->l1b_prod_name );
     if ( calopt->geo_filter != SCHAR_ZERO ) {
	  nadc_write_double( outfl, ++nr, "start_lat", 6, 
			     calopt->start_lat/1e6 );
	  nadc_write_double( outfl, ++nr, "start_lon", 6, 
			     calopt->start_lon/1e6 );
	  nadc_write_double( outfl, ++nr, "end_lat", 6, calopt->end_lat/1e6 );
	  nadc_write_double( outfl, ++nr, "end_lon", 6, calopt->end_lon/1e6 );
     } else
	  nr += 4;
     if ( calopt->time_filter != SCHAR_ZERO ) {
	  (void) MJD_2_ASCII( calopt->start_time.days, 
			      calopt->start_time.secnd,
                              calopt->start_time.musec, date_str );
          nadc_write_text( outfl, ++nr, "start_time", date_str );
	  (void) MJD_2_ASCII( calopt->stop_time.days, 
			      calopt->stop_time.secnd,
                              calopt->stop_time.musec, date_str );
          nadc_write_text( outfl, ++nr, "stop_time", date_str );

     } else
	  nr += 2;
     if ( calopt->category_filter != SCHAR_ZERO ) {
	  count[0] = 5;
          nadc_write_arr_ushort( outfl, ++nr, "category", 
                                  1, count, calopt->category );
     } else
	  nr += 1;
     if ( calopt->nadir_mds != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "nadir_mds_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "nadir_mds_flag", "FALSE" );
     if ( calopt->limb_mds != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "limb_mds_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "limb_mds_flag", "FALSE" );
     if ( calopt->occ_mds != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "occ_mds_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "occ_mds_flag", "FALSE" );
     if ( calopt->moni_mds != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "moni_mds_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "moni_mds_flag", "FALSE" );

     if ( calopt->pmd_mds != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "pmd_mds_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "pmd_mds_flag", "FALSE" );
     if ( calopt->frac_pol_mds != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "frac_pol_mds_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "frac_pol_mds_flag", "FALSE" );
     if ( calopt->slit_function != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "slit_function_gads_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "slit_function_gads_flag", "FALSE" );
     if ( calopt->sun_mean_ref != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "sun_mean_ref_gads_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "sun_mean_ref_gads_flag", "FALSE" );
     if ( calopt->leakage_current != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "leakage_current_gads_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "leakage_current_gads_flag", "FALSE");
     if ( calopt->spectral_cal != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "spectral_cal_gads_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "spectral_cal_gads_flag", "FALSE" );
     if ( calopt->pol_sens != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "pol_sens_gads_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "pol_sens_gads_flag", "FALSE" );
     if ( calopt->rad_sens != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "rad_sens_gads_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "rad_sens_gads_flag", "FALSE" );
     if ( calopt->ppg_etalon != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "ppg_etalon_gads_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "ppg_etalon_gads_flag", "FALSE" );
     nadc_write_ushort( outfl,++nr, "num_nadir_clusters", calopt->num_nadir );
     nadc_write_ushort( outfl,++nr, "num_limb_clusters", calopt->num_limb );
     nadc_write_ushort( outfl,++nr, "num_occ_clusters", calopt->num_occ );
     nadc_write_ushort( outfl,++nr, "num_moni_clusters", calopt->num_moni );
     count[0] = MAX_CLUSTER;
     nadc_write_arr_schar( outfl, ++nr, "nadir_cluster_flag", 
			    1, count, calopt->nadir_cluster );
     nadc_write_arr_schar( outfl, ++nr, "limb_cluster_flag", 
			    1, count, calopt->limb_cluster );
     nadc_write_arr_schar( outfl, ++nr, "occ_cluster_flag", 
			    1, count, calopt->occ_cluster );
     nadc_write_arr_schar( outfl, ++nr, "moni_cluster_flag", 
			    1, count, calopt->moni_cluster );
     if ( calopt->mem_effect_cal != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "mem_effect_cal_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "mem_effect_cal_flag", "FALSE" );
     if ( calopt->leakage_cal != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "leakage_cal_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "leakage_cal_flag", "FALSE" );
     if ( calopt->straylight_cal != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "straylight_cal_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "straylight_cal_flag", "FALSE" );
     if ( calopt->ppg_cal != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "ppg_cal_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "ppg_cal_flag", "FALSE" );
     if ( calopt->etalon_cal != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "etalon_cal_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "etalon_cal_flag", "FALSE" );
     if ( calopt->wave_cal != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "spectal_cal_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "spectral_cal_flag", "FALSE" );
     if ( calopt->polarisation_cal != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "polarisation_cal_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "polarisation_cal_flag", "FALSE" );
     if ( calopt->radiance_cal != SCHAR_ZERO )
	  nadc_write_text( outfl, ++nr, "radiance_cal_flag", "TRUE" );
     else
	  nadc_write_text( outfl, ++nr, "radiance_cal_flag", "FALSE" );

     (void) fclose( outfl );
}