ocssw  1.0
/disk01/web/ocssw/build/src/viirs_sim_sdr/viirs_sim_sdr.h (r8096/r5433)
Go to the documentation of this file.
00001 /*******************************************************************
00002 
00003    viirs_sim_sdr.h
00004 
00005    purpose: include file for the viirs sdr simulation program
00006 
00007    Parameters: 
00008       Type              Name            I/O     Description
00009       ----              ----            ---     -----------
00010 
00011    Modification history:
00012       Programmer        Date            Description of change
00013       ----------        ----            ---------------------
00014       W. Robinson, SAIC 23-Sep-2008     Original development
00015       W. Robinson, SAIC 20 Nov 2008     modify for line-by-line I/O,
00016                                         structure seperation for in, out, 
00017                                         and info
00018 
00019 *******************************************************************/
00020 
00021 /*
00022  *  Note that hdf5.h is needed for this
00023  */
00024 #include "h5io.h"
00025 #include "readL2scan.h"
00026 #include <stdio.h>
00027 #include <string.h>
00028 #include <math.h>
00029 
00030 #define NDET 16
00031 #define N_VNIR_BND 7
00032 #define N_IR_BND 9
00033 /* assume 1 geo file */
00034 #define MAX_BND N_VNIR_BND + N_IR_BND
00035 #define MAX_FILES MAX_BND + 1
00036 #define RAD_CGS_2_MKS 10.
00037 #define SEC_PER_SCAN 1.7864
00038 #define PI 3.141592654
00039 /*
00040  *  some needed missing data fill value indicators
00041  */
00042 #define ONBOARD_PT_FLOAT32_FILL  -999.7
00043 #define ONBOARD_PT_UINT16_FILL  65533
00044 #define SOUB_UINT16_FILL 65528
00045 #define ERR_UINT16_FILL 65531
00046 #define MISS_UINT16_FILL 65534
00047 #define MISS_FLOAT32_FILL -999.8
00048 /*
00049  *  VIIRS scan angle types
00050  */
00051 #define VIR_SCAN_AGSMP 0   /* aggregated sample # */
00052 #define VIR_SCAN_UASMP 1   /* unaggregated sample # */
00053 #define VIR_SCAN_ANG 2     /* scan angle, degrees */
00054 #define VIR_SCAN_AOI 3     /* AOI on HAM mirror, degrees */
00055 /*
00056  *  input controls to program
00057  */
00058 struct ctl_struc_d {
00059   char in_geo_file[500];  /*  1st arg, the geo data file with basic 
00060                               data in it */
00061   char out_loc[500];  /*  output SDR location.  Files will be named
00062                           VYYYYDDDHHMMSS_GEO.h5 for geo and
00063                           VYYYYDDDHHMMSS_SDRMXX.h5 for the band files,
00064                           XX is the band number  */
00065   int l2_use;  /* flag for existance (1) or absence (0) of an L2 file with
00066                   TOA radiances needed for the SDR */
00067   char l2_file[500];  /* name of the input L2 file */
00068   int meta_use; /* 1 if metadata output is desired, otherwise 0 */
00069   char meta_file[500];  /* name of file to put metadata in */
00070   int rhos_use;  /* flag for existance (1) or absence (0) of a surface 
00071                     reflectance file for filling gaps in TOA data*/
00072   char rhos_file[500];  /* name of surface reflectance file */
00073   int rhos_opt;   /* reflectance data use option: 0 - replace where Lt 
00074                      from ocean color is missing, 1 - replace everywhere */
00075   int bowtie_opt;  /* include the bow tie deletion if 1, not if 0 */
00076   int any_artifact;  /* are there any artifacts? 0 = no, 1 = yes */
00077   int out_scn_fmt;  /* switch to modify scan format: 2 = default - do not 
00078       change input data format, 1 - if unaggregated with margin, remove the 
00079       margin, 0 - make output aggregated = standard SDR format */
00080   int oxt_mode;  /* optical crosstalk (OXT) mode: 0 (default) no Xtalk applied,
00081                     1 apply it */
00082   char oxt_coef[500];  /* OXT influence coefficients, default is 
00083                     $VIIRS_SIM_DATA/oxt_default.h5 */
00084   char inter_band[500];  /* Inter-band data table, default is 
00085                        $VIIRS_SIM_DATA/inter_band_default.dat */
00086   int make_m;  /* control to make M bands: 0 - do the NIV NIR 1-7, 
00087                 1 - do all 16 bands */
00088   int sdr_overwrite;  /* control to allow overwrite of an existing SDR file:
00089                 0 - (default) do not allow, 1 - allow overwrite */
00090  /*  for removing vicarious gain and offset from the TOA from l2gen_inv 
00091      if ever needed */
00092   int vic_cal_chg;  /* 0 if no change, 1 if a change in gain or offset */
00093                     /* from default of gain 1, offset 0 */
00094   float gain[MAX_BND];  /* gains  */
00095   float offset[MAX_BND];  /* offset, eq: Lt = ( Lt0 * gain ) + offset  */
00096  /* create time control */
00097   char cre_time[23];   /* create time in form YYYYMMDD/HHMMSS.FFFFFF */
00098   int fname_opt;  /* file name option: 0 to make std name, 
00099                      1 to omit create time */
00100  /*  for the count conversion and electronic crosstalk  */
00101   int count_cal_opt;  /* cal option: 0 - no count calibration, 1 - do cal
00102                          and de-cal (set if electronic Xtalk is set),
00103                          2 - as (1), but also integerize the dn value */
00104   char count_cal_gain_file[500];  /* file containing the gain and dark
00105                                      for count to radiance conversion */
00106   char count_cal_rvs_file[500];  /* file containing the RVS for count
00107                                     to radiance conversion */
00108   char count_decal_gain_file[500];  /* as above, but for de-calibration
00109                                        if different from the cal */
00110   char count_decal_rvs_file[500];  /* as count_cal_rvs_file, but for de-cal */
00111   int count_dark_opt;  /* set to 0 for no dark subtraction, 1 to do subtract */
00112   int ext_opt;  /* electronic crosstalk mode: 0(default) - no crosstalk, */
00113                /* 1 - perform the crosstalk  */
00114   char ext_coeff_file[500];  /* name of ext file or 'Unspecified' for */
00115                /* use of trivial coefficients (zero values) */
00116   char id_domain[500];  /* domain designator in dataset and attributes */
00117   char id_origin[500];  /* origin designator in dataset and attributes */
00118  /*  noise controls - an option and a file of coeffs */
00119   int noise_mode; /* 0 if not adding noise, 1 if adding it */
00120   char noise_coef[500];  /* noise coeff file */
00121  /*  stray light controls - an option and a file of coeffs */
00122   int stray_opt;  /* stray light use option 0 - no, 1 - yes */
00123   char stray_tbl[500];  /* stray light PSF coeff file */
00124   };
00125 
00126 typedef struct ctl_struc_d ctl_struc;
00127 
00128 typedef long long int64;
00129 typedef unsigned long long uint64;
00130 
00131 /*
00132  *  general data information from the simulated geo file and synthesized L2
00133  */
00134 struct sdr_info_struc_d {
00135   char origin[50];  /* Originator of the data, for file name create and attr */
00136   char domain[50];  /* Domain - for file name create */
00137   int year;  /*  information about the geo data - 4 digit year*/
00138   int day;   /*  day of year */
00139   double start_sec;   /*  data start second */
00140   char ofile_base[200];  /* storage for the output file base with time */
00141   char sdr_files[MAX_FILES][200];  /* fil names of SDR files */
00142   /*  Some derived values that are convienient to carry here */
00143   int64 t58_day;      /* time of start of the day in microsec past 1/1/1958 */
00144   char cre_date[10];  /* string of the create data of file: YYYYMMDD */
00145   char cre_time[20];  /* string of the create time of file: HHMMSS.SSSSSSZ */
00146   int64 st_58_t;      /* data start in micro sec > 1958 */
00147   int64 en_58_t;      /* data end in micro sec > 1958 */
00148   char st_date[10];   /* data start as variable cre... (cre_date... above) */
00149   char st_time[20];   /* data start time as cre... */
00150   char en_date[10];   /* data end as cre... */
00151   char en_time[20];   /* data end time as cre... */
00152   float *geo_pos;     /* position data from geolocation file, in km */
00153   float *geo_vel;     /* velocity data from geolocation file, in km/s */
00154   float *geo_att;     /* sensor attitude data from geolocation file, 
00155                          in degrees */
00156   unsigned char *ham_side;  /* HAM half angle mirror side storage,
00157                       just set to 0 on 1st scan 1 on next ... */
00158   double *scan_time;  /* read from the geo file, scan time in sec of day */
00159   };
00160 
00161 typedef struct sdr_info_struc_d sdr_info_struc;
00162 
00163 struct in_rec_struc_d {
00164   int year;        /* year of data */
00165   int yday;        /* day of year */
00166   int msec;        /* millisec of day for the scan */
00167   int nbnd;        /* # bands */
00168   int npix;        /* # pixels  */
00169   int nlin;        /* # lines in whole file */
00170   int nscan;       /* # scans should be nlin / 16 */
00171   /*  input format info read from the geo file */
00172   int scn_fmt;     /* input scan format, found from geo file: 0 aggregated,
00173                       1 unaggregated - 6304 pix, 2 unaggregated with margin */
00174   int margin[2];      /* along-track and -scan margins around std scan */
00175   int ndet_scan;      /* convenience - NDET + 2 * margin[0] = # det in a scan */
00176   unsigned char ham_side;  /* HAM half angle mirror side for the scan */
00177   /*  */
00178   h5io_str geo_fid;  /*  file id for geo file */
00179   h5io_str geo_dat_id[6];  /* dataset ids for the input geo info:
00180                              lat, lon, sena, senz, sola, solz   */
00181   float *lat;     /*  the line[s] storage arrays  */
00182   float *lon;
00183   float *sena;
00184   float *senz;
00185   float *sola;
00186   float *solz;
00187   float *bnd_lt[MAX_BND]; /* for the radiances (reflectances made on
00188                            output) in W m^-2 sr^-1  or BBT in K */
00189   unsigned char *bnd_q[MAX_BND]; /* flags for bad values in radiances, */
00190                                /*  see bnd_q in out_rec */
00191   float *dn[MAX_BND]; /* if needed, storage for the count data in float */
00192   unsigned char *gain_bit[MAX_BND]; /* seperate gain state bit for counts */
00193   char *dn_sat[MAX_BND]; /* indicator that the dn resulting from the radiance
00194                             would have been saturated in VIIRS if 1 */
00195   float ll_lims[6];  /*  These are latitude, longitude limits for the 
00196                          input geo data: low lat, high, low (-)lon, high
00197                          low (+)lon, high */
00198   int lam_band[MAX_BND];  /* the band wavelength in nm */
00199   float f0[MAX_BND];  /* the season corrected F0 value for the bands */ 
00200   /* info for the input L2 file with TOA rads (and atmos info for rhos work) */
00201   l2_prod l2_str;  /*  The information structure for reading a l2 file */
00202   };
00203 typedef struct in_rec_struc_d in_rec_struc;
00204 
00205 struct out_rec_struc_d {
00206   int year;        /* year of data */
00207   int yday;        /* day of year */
00208   int msec;        /* millisec of day for the scan */
00209   int nbnd;        /* # bands */
00210   int npix;        /* # pixels  */
00211   int nlin;        /* # lines  */
00212   int nscan;       /* # scans should be nlin / 16 */
00213   /*  input format info read from the geo file */
00214   int scn_fmt;     /* input scan format, found from geo file: 0 aggregated,
00215                       1 unaggregated - 6304 pix, 2 unaggregated with margin */
00216   int margin[2];      /* along-track and -scan margins around std scan */
00217   int ndet_scan;      /* convenience - NDET + 2 * margin[0] = # det in a scan */
00218   /*    */
00219   h5io_str sdr_fid[MAX_FILES];  /*  file id for sdr files making output SDR */
00220   h5io_str sdr_dat_gid[2][MAX_FILES];  /* 2 groups under which lies the 
00221                                             geo or band datasets */
00222   h5io_str geo_dat_id[6];  /* dataset ids for the output geo info:
00223                              lat, lon, sena, senz, sola, solz   */
00224   h5io_str bnd_dat_id[2][MAX_BND]; /* dataset ids for the output band
00225                                            info: radiance, refl / BBT values */
00226   h5io_str qual1_m_id[MAX_BND];  /* QF1_VIIRSMBANDSDR dataset id */
00227   float *lat;     /*  the line[s] storage arrays  */
00228   float *lon;     /*  NOTE that for now, these will point to the input arrays */
00229   float *sena;
00230   float *senz;
00231   float *sola;
00232   float *solz;
00233   float *bnd_lt[MAX_BND]; /* for the radiances (reflectances made on
00234                            output) in W m^-2 sr^-1 or BBT in K */
00235   unsigned char *bnd_q[MAX_BND]; /* flags for bad values in radiances, 0 if */
00236     /*  none and 1 if bow tie deletion on this pixel */
00237     /*  2 if bad value resultant from rads and refl fill */
00238   unsigned char *qual1_m[MAX_BND]; /* This is the QF1_VIIRSMBANDSDR dataset
00239           information for the current scan.  For an aggregated output 
00240           (scn_fmt = 0) it is std def of bits 2 and 3 having saturatio:
00241           0 no samples saturated, 1 some, 2 all saturated.  For unaggregated,
00242           (scn_fmt = 1, 2) bit 2 is gain state: 0 hi, 1 low and bit 3 is 
00243           saturation: 0 unsat, 1 saturated */
00244   float *f0;  /* the season corrected F0 value for the bands */
00245   int *lam_band;  /* the band wavelength in microns = nm / 10^3 */
00246   float scale[MAX_BND];  /* scale and offset for the VIIRS output radiance */
00247   float offset[MAX_BND];
00248   float refl_scale[MAX_BND];  /* as above but for reflectivity (reflective 
00249                                  bands) or BBT (emissive bands) */
00250   float refl_offset[MAX_BND];
00251   char out_bnd_typ[MAX_BND]; /* 0 if this band stored as unsigned short, 1 
00252                                for float  */
00253   char meas_typ[MAX_BND];  /* measurement type: 0 - rad + refl, 
00254                              1 - BBT and rad */
00255   };
00256 typedef struct out_rec_struc_d out_rec_struc;
00257 
00258 /*
00259  *  This is an attribute description storage structure for outputting the 
00260  *  many attributes
00261  */
00262 struct h5attr_struc_d {
00263   int express;  /* a 0 to not output this entry as a attribute, 1 to do it */
00264   int type;   /*  type of data - 0 most types, 1 - char type  */
00265   char *name; /*  attrib name */
00266   hid_t typ;  /*  type of data, like H5T_NATIVE_INT, note for type = 1,
00267                   this is not used */
00268   int str_len;  /*  string length - if 1 string, this can be 0, but
00269                     for an array, length of the strings (all same length)
00270                     This is not used for type = 0  */
00271   int ndim;     /*  # dimensis of data array, 0 if a scalar */
00272   int *dim_siz; /*  lengths of the dimensions, not used if ndim = 0 */
00273   void *data;   /*  the actual data to write */
00274   };
00275 typedef struct h5attr_struc_d h5attr_struc;
00276 
00277 /*
00278  *  The next 2 structures contain the calibration information: the gain
00279  *  and the RVS information
00280  */
00281 struct vir_gain_struc_d {
00282   int nham;  /* # Half angle Mirror sides (2, I hope) */
00283   int ndet;  /* # detectors (16 for VIIRS)  */
00284   int ngain;  /* # gain states (2, I hope) */
00285   int nbnd;  /* # bands - should be 11 for all reflective M bands,
00286                  but could be 7 for just vis and NIR */
00287   float *c0;  /* zero order gain coefficient */
00288   float *c1;  /* first order gain coefficient */
00289   float *c2;  /* 2nd order gain coefficient */
00290               /* the lt is computed from counts, dn, with
00291                  lt = c0 + c1 * dn + c2 * dn^2  */
00292   float *dark;  /* dark count value, the dark-subtracted counts, dn, are
00293                    related to the raw counts, DN, by
00294                    dn = DN - dark */
00295         /* for c* and dark, the storage is f( HAM, det, gain, band )
00296            with band the fastest varying... */
00297   char gain_ct[16];
00298             /* gain_ct is the # gain states for the m band - it will
00299                determine if dn conversion will try to use the low gain
00300                or just stay with the high gain (more likely to
00301                saturate) */
00302   };
00303 typedef struct vir_gain_struc_d vir_gain_struc;
00304 
00305 struct vir_rvs_struc_d {
00306   int nham;  /* # Half angle Mirror sides (2, I hope) */
00307   int ndet;  /* # detectors (16 for VIIRS)  */
00308   int nbnd;  /* # bands - should be 11 for all reflective M bands,
00309                  but could be 7 for just vis and NIR */
00310   float aoi_range[2];  /* AOI range coefficients are applicable for  */
00311   float *a0;  /* zero order rvs coefficient*/
00312   float *a1;  /* 1st order rvs coefficient*/
00313   float *a2;  /* 2nd order rvs coefficient*/
00314               /* array storage: f( HAM, det, band ) 
00315                  with band the fastest varying... */
00316               /* RVS = a0 + a1 * AOI + a2 * AOI^2 */
00317   };
00318 typedef struct vir_rvs_struc_d vir_rvs_struc;
00319 
00320 struct vir_straylt_struc_d {
00321   int nbands;  /* # bands included in stray light table */
00322   int nrec_det;  /* # lines (= detectors) in stray light table */
00323                  /* note that a convolution array exists for each 
00324                     band and line of a standard scan */
00325   int nsamp;  /* # pixels in the convolution array */
00326   int ndet;   /* # lines (= detectors) in convcolution array */
00327   int csamp;  /* (0 origin) center sample of the convolution array */
00328   float *psf;  /* the set of convolution arrays for each band and 
00329                   detector, as a 4 d array of 
00330                   ( nsamp, ndet, nbands, nrec_det ) with nsamp the 
00331                   fastest dimension */
00332   };
00333 typedef struct vir_straylt_struc_d vir_straylt_struc;
00334 
00335 /*
00336  *  prototypes
00337  */
00338 int check_usage( int, char *[], ctl_struc * );
00339 int rd_sim_init( ctl_struc *, sdr_info_struc *, in_rec_struc * );
00340 int rd_geo_init( ctl_struc *, sdr_info_struc *, in_rec_struc * );
00341 int init_sdr( ctl_struc *, sdr_info_struc *, in_rec_struc *, out_rec_struc * );
00342 int init_sdr_top( int, sdr_info_struc *, out_rec_struc * );
00343 int init_geo_data( sdr_info_struc *, in_rec_struc *, out_rec_struc * );
00344 int init_bnd_data( int, sdr_info_struc *, in_rec_struc *, out_rec_struc * );
00345 int init_sdr_dpattr( int, h5io_str *, sdr_info_struc * );
00346 int init_sdr_agg( int, h5io_str *, sdr_info_struc * );
00347 int init_sdr_gran( int, h5io_str *, sdr_info_struc *, out_rec_struc * );
00348 int rd_sdr_scan( int, ctl_struc *, sdr_info_struc *, in_rec_struc * );
00349 int rd_geo_scan( int, sdr_info_struc *, in_rec_struc * );
00350 int gen_const_rad_scn( sdr_info_struc *, int, in_rec_struc * );
00351 int wr_sdr_scan( int, out_rec_struc * );
00352 int wr_geo_scan( int, out_rec_struc * );
00353 int wr_bnd_scan( int, out_rec_struc * );
00354 int fin_sdr( ctl_struc *, in_rec_struc *, out_rec_struc * );
00355 int wr_attr_seq( h5io_str *, int, h5attr_struc * );
00356 int gen_sdr_fname( int, char *, sdr_info_struc *, int, char * );
00357 int rd_rhos_scan( char *, int, int, int, float ** );
00358 int rhos_to_lt( int, float **, in_rec_struc *, int, sdr_info_struc * );
00359 int scan_cvt( in_rec_struc *, out_rec_struc * );
00360 int ang_avg( float, float, float, float, float *, float * );
00361 int mod_artifact( ctl_struc *, in_rec_struc * );
00362 int viirs_oxt( ctl_struc *, in_rec_struc * );
00363 int viirs_oxt_ib_read( char *,float *, float * );
00364 int viirs_oxt_comp( ctl_struc *, in_rec_struc * );
00365 float bbt_2_rad( float, float );
00366 int vir_xf_scan( float, int, int, float * );
00367 int vset_cal_gain( char *, vir_gain_struc * );
00368 int vset_cal_rvs( char *, vir_rvs_struc * );
00369 int viirs_cal( ctl_struc *, in_rec_struc * );
00370 int viirs_decal( ctl_struc *, in_rec_struc * );
00371 int viirs_sim_input( int, char *[], ctl_struc * );
00372 int32_t rdsensorinfo( int32_t, int32_t, char *, void ** );
00373 int bnd_ix_2_sen_info( char *, void * );
00374 int viirs_ext( ctl_struc *, in_rec_struc * );
00375 int viirs_noise( ctl_struc *, in_rec_struc *, int );
00376 int viirs_noise_coef_rd( char *, float *, float *, float *, float * );
00377 int viirs_straylt( ctl_struc *, in_rec_struc *, int );
00378 int viirs_straylt_rd( char *, vir_straylt_struc * );
00379 /*  */
00380 int jd_c( int, int, int );
00381 int day2mday( int, int, int *, int * );