ocssw  1.0
/disk01/web/ocssw/build/src/viirs_sim_sdr/viirs_sim_sdr.c (r8099/r4907)
Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002     Program:   viirs_sim_sdr
00003 
00004     Description:  Create a VIIRS SDR file set using simulated data
00005 
00006     Arguments:
00007         Type      Name         I/O   Description
00008         ----      ----         ---   -----------
00009         int       argc          I    count of command line args - 3
00010         char *[]  argv          I    command line arguments:
00011                                      [1] input geo data file name
00012                                      [2] id to use in the output file name
00013                                      and options
00014 
00015     Modification history:
00016 
00017     W. Robinson, SAIC  08 Oct 2008  Original development
00018     W. Robinson, SAIC  21 Sep 2010  switch input to clo library
00019 
00020 ----------------------------------------------------------------------------*/
00021 #include "viirs_sim_sdr.h"
00022 #include "l12_parms.h"
00023 #include <stdio.h>
00024 
00025 int main( int argc, char *argv[] )
00026   {
00027   int iscn, ipx, ibnd, idet, n_bad, msec;
00028   ctl_struc ctl;
00029   sdr_info_struc sdr_info;
00030   in_rec_struc in_rec;
00031   out_rec_struc out_rec;
00032   FILE *meta_id;
00033   static float f0bar[MAX_BND];
00034   double esdist;
00035   double esdist_( int *, int *, int * );
00036 
00037  /*
00038   *  shorten output buffering
00039   */
00040   setvbuf(stdout, NULL, _IOLBF, 0);
00041   setvbuf(stderr, NULL, _IOLBF, 0);
00042 
00043   if( viirs_sim_input( argc, argv, &ctl ) != 0 ) exit( 1 );
00044  /*
00045   *  set the origin (distributor) and domain 
00046   */
00047   strcpy( sdr_info.origin, ctl.id_origin );
00048   strcpy( sdr_info.domain, ctl.id_domain );
00049  /*
00050   *  read in the VIIRS band center wavelengths, average F0
00051   */
00052   if( bnd_ix_2_sen_info( "Lambda", (void *)in_rec.lam_band ) < 0 )
00053     {
00054     printf( "%s, %d: failure to read sensor information\n",
00055       __FILE__, __LINE__ );
00056     }
00057   out_rec.lam_band = in_rec.lam_band;
00058   if( bnd_ix_2_sen_info( "Fobar", (void *)f0bar ) < 0 )
00059     {
00060     printf( "%s, %d: failure to read sensor information\n",
00061       __FILE__, __LINE__ );
00062     return 1;
00063     }
00064  /*
00065   *  set up the input file (get attributes, ready for dataset reading)
00066   */
00067   if( rd_sim_init( &ctl, &sdr_info, &in_rec ) != 0 ) return 1;
00068  /*
00069   *  get an initial fo for use in init_sdr
00070   */
00071   in_rec.year = sdr_info.year;
00072   in_rec.yday = sdr_info.day;
00073   msec = *sdr_info.scan_time * 1000.;
00074   esdist = esdist_( &( in_rec.year ), &( in_rec.yday ), &msec );
00075   for( ibnd = 0; ibnd < in_rec.nbnd; ibnd++ )
00076       in_rec.f0[ibnd] = RAD_CGS_2_MKS * pow( 1.0 / esdist , 2 ) * f0bar[ibnd];
00077   out_rec.f0 = &( in_rec.f0[0] );
00078   printf( "%s, %d: info: earth sun distance correction is %f\n", 
00079     __FILE__, __LINE__, esdist );
00080  /*
00081   *  proceed to create the SDR file with waiting data arrays
00082   */
00083   if( init_sdr( &ctl, &sdr_info, &in_rec, &out_rec ) != 0 ) return 1;
00084  /*
00085   * loop through the lines and create the data for the SDR
00086   */
00087   for( iscn = 0; iscn < in_rec.nscan; iscn++ )
00088     {
00089    /*
00090     *  the SDR scan read will also get and combine land and cloud info
00091     */
00092     if( rd_sdr_scan( iscn, &ctl, &sdr_info, &in_rec ) != 0 ) return 1;
00093    /*
00094     *  set some times in scan and create the f0 for the scan time
00095     */
00096     msec = *( sdr_info.scan_time + iscn ) * 1000.;
00097     in_rec.msec = msec;
00098     esdist = esdist_( &( in_rec.year ), &( in_rec.yday ), &msec );
00099    /*  note F0 conversion below from loadl1.c  */
00100     for( ibnd = 0; ibnd < in_rec.nbnd; ibnd++ )
00101       in_rec.f0[ibnd] = RAD_CGS_2_MKS * pow( 1.0 / esdist , 2 ) * f0bar[ibnd];
00102     
00103     out_rec.year = in_rec.year;
00104     out_rec.yday = in_rec.yday;
00105     out_rec.msec = in_rec.msec;
00106     out_rec.f0 = &( in_rec.f0[0] );
00107    /*
00108     *  note any bad values in the scan - only for VNIR
00109     */
00110     n_bad = 0;
00111     for( idet = 0; idet < in_rec.ndet_scan; idet++ )
00112       for( ipx = 0; ipx < in_rec.npix; ipx++ )
00113         for( ibnd = 0; ibnd < N_VNIR_BND; ibnd++ )
00114           {
00115           if( *( in_rec.bnd_q[ibnd] + ipx + in_rec.npix * idet ) != 0 )
00116             n_bad++;
00117           }
00118     printf( "%s %d: scan: %d, # bad samples: %d\n", __FILE__, __LINE__, 
00119       iscn, n_bad );
00120    /*
00121     *  This is where any artifact addition is done and possible conversion
00122     *  to aggregated
00123     */
00124     if( ( ctl.any_artifact == 1 ) && ( in_rec.scn_fmt != 0 ) )
00125       if( mod_artifact( &ctl, &in_rec ) != 0 ) return 1;
00126    /*
00127     *  convert scan format
00128     */
00129     if( scan_cvt( &in_rec, &out_rec ) != 0 )
00130       return 1;
00131    /*
00132     *  should we want to remove a vicarious gain, to be added in l2gen,
00133     *  do it here
00134     */
00135     if( ctl.vic_cal_chg == 1 ) 
00136       {
00137       for( ibnd = 0; ibnd < in_rec.nbnd; ibnd++ )
00138         for( idet = 0; idet < out_rec.ndet_scan; idet++ )
00139           for( ipx = 0; ipx < out_rec.npix; ipx++ )
00140             if( *( out_rec.bnd_q[ ibnd ] + ipx + idet * out_rec.ndet_scan )
00141               == 0 )
00142               {
00143               *( out_rec.bnd_lt[ ibnd ] + ipx + idet * out_rec.npix ) -=
00144                 ctl.offset[ibnd];
00145               *( out_rec.bnd_lt[ ibnd ] + ipx + idet * out_rec.npix ) /=
00146                 ctl.gain[ibnd];
00147               }
00148       }
00149    /*
00150     *  if requested, add the bow tie artifact
00151     *  Only do so if scan format indicates aggregated data
00152     */
00153     if( ( ctl.bowtie_opt == 1 ) && ( out_rec.scn_fmt == 0 ) )
00154       {
00155       for( idet = 0; idet < NDET; idet++ )
00156         {
00157         switch( idet )
00158           {
00159           case 0:
00160           case 15:
00161             for( ibnd = 0; ibnd < in_rec.nbnd; ibnd++ )
00162               {
00163               for( ipx = 0; ipx < 1008; ipx++ )
00164                 *( out_rec.bnd_q[ibnd] + idet * 3200 + ipx ) = 1;
00165               for( ipx = 2192; ipx < 3200; ipx++ )
00166                 *( out_rec.bnd_q[ibnd] + idet * 3200 + ipx ) = 1;
00167               }
00168             break;
00169           case 1:
00170           case 14:
00171             for( ibnd = 0; ibnd < in_rec.nbnd; ibnd++ )
00172               {
00173               for( ipx = 0; ipx < 640; ipx++ )
00174                 *( out_rec.bnd_q[ibnd] + idet * 3200 + ipx ) = 1;
00175               for( ipx = 2560; ipx < 3200; ipx++ )
00176                 *( out_rec.bnd_q[ibnd] + idet * 3200 + ipx ) = 1;
00177               }
00178             break;
00179           }
00180         }
00181       }
00182    /*
00183     *  and output the data
00184     */
00185     if( wr_sdr_scan( iscn, &out_rec ) != 0 ) return 1;
00186     }
00187  /*
00188   *  finish, close files, write file info and exit
00189   */
00190   if( fin_sdr( &ctl, &in_rec, &out_rec ) != 0 ) return 1;
00191 
00192   for( iscn = 0; iscn < out_rec.nbnd + 1; iscn++ )
00193     printf( "Created file # %d: %s\n", iscn, sdr_info.sdr_files[iscn] );
00194   printf( "Path: %s\n", ctl.out_loc );
00195   printf( "Start date: %s\n", sdr_info.st_date );
00196   printf( "Start time: %s\n", sdr_info.st_time );
00197   printf( "End date: %s\n", sdr_info.en_date );
00198   printf( "End time: %s\n", sdr_info.en_time );
00199   if( ctl.meta_use != 0 )
00200     {
00201     if( ( meta_id = fopen( ctl.meta_file, "w" ) ) == NULL )
00202       {
00203       printf( "%s, line %d: Failure to create the metadata file\n", 
00204         __FILE__, __LINE__ );
00205       return 1;
00206       }
00207     for( iscn = 0; iscn < out_rec.nbnd + 1; iscn++ )
00208       fprintf( meta_id, "%s\n", sdr_info.sdr_files[iscn] );
00209     fprintf( meta_id, "%s\n", ctl.out_loc );
00210     fprintf( meta_id, "%s\n", sdr_info.st_date );
00211     fprintf( meta_id, "%s\n", sdr_info.st_time );
00212     fprintf( meta_id, "%s\n", sdr_info.en_date );
00213     fprintf( meta_id, "%s\n", sdr_info.en_time );
00214     fclose( meta_id );
00215     }
00216 
00217   printf( "Successfully completed simulated SDR generation\n" );
00218   return 0;
00219   }