OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
smeta_api.c
Go to the documentation of this file.
1 #include "smeta_api.h" /* SMETA structure and routines */
2 
3 static SMETA smeta; /* Enhanced metadata structure */
4 static WRS2 wrsparms; /* Nominal WRS-2 orbit parameters */
5 static double dellon; /* WRS-2 longitude offset */
6 static double delrow; /* Framed scene size in WRS rows */
7 static VECTOR ecfsun; /* Scene center ECEF solar vector */
8 static MTL_GRID_BAND bgrid[IAS_MAX_NBANDS]; /* Geometric grid for each band */
9 static MTL_GRID_BAND_LS bgrid_ls[IAS_MAX_NBANDS_LS]; /* Geometric grid for each band */
10 
11 int smeta_init( /* Returns num bands in SMETA */
12  char *smeta_filename) /* Product metadata file name */
13 {
14  int band_index; /* Loop index for bands */
15 
16  /* Read the input MTL file */
17  if ( smeta_read( &smeta, smeta_filename) != SUCCESS )
18  {
19  IAS_LOG_ERROR("Reading the input metadata file %s.", smeta_filename);
20  return(0);
21  }
22 
23  /* Load WRS parameters - these values are available in the CPF */
24  wrsparms.semimajor = 6378137.0;
25  wrsparms.semiminor = 6356752.3142;
26  wrsparms.inertialvel = 7.2921158553E-05;
27  wrsparms.orbitincl = 98.2; /* Nominal orbital inclination */
28  wrsparms.orbitrad = 7083137.0; /* Nominal 705 km orbital altitude */
29  wrsparms.path1long = -64.6;
30  wrsparms.numpath = 233;
31  wrsparms.numrow = 248;
32  wrsparms.cycle = 16;
33  wrsparms.row0lat = 60;
34 
35  /* Initialize the scene map projection */
36  if ( smeta_init_projection( smeta.projection ) != SUCCESS )
37  {
38  IAS_LOG_ERROR("Initializing the map projection transformation.");
39  return(0);
40  }
41 
42  /* Adjust nominal scene frame to fit metadata */
43  dellon = 0.0;
44  delrow = 1.2;
45 
46  if (smeta.sensor_mode == OLI)
47  {
48  if ( frame_scene( wrsparms, smeta, &dellon, &delrow ) != SUCCESS )
49  {
50  IAS_LOG_ERROR("Fitting scene frame to product metadata.\n");
52  return(0);
53  }
54  /* Iterate to refine fit */
55  if ( frame_scene( wrsparms, smeta, &dellon, &delrow ) != SUCCESS )
56  {
57  IAS_LOG_ERROR("Fitting scene frame to product metadata.\n");
59  return(0);
60  }
61  /* Compute ECEF solar vector */
62  if ( get_ecfsun( wrsparms, smeta, 0.0, &ecfsun ) != SUCCESS )
63  {
64  IAS_LOG_ERROR("Calculating ECEF solar vector.\n");
66  return(0);
67  }
68  }
69  else
70  {
71  int count;
72 
73  for( count=0 ; count<5 ; count++ )
74  {
75  if ( frame_scene_ls( wrsparms, smeta, &dellon, &delrow ) != SUCCESS )
76  {
77  IAS_LOG_ERROR("Fitting scene frame to product metadata.\n");
79  return(0);
80  }
81  }
82 
83  //printf("smeta.num_band before = %d\n", smeta.num_band);
84  /* If an "optical axis" band was added at the end, suppress it here. */
85  if ( smeta.band_smeta_ls[smeta.num_band-1].band == 0 ) smeta.num_band--;
86 
87  /* Compute ECEF solar vector */
88  if ( get_ecfsun_ls( wrsparms, smeta, 0.0, &ecfsun ) != SUCCESS )
89  {
90  IAS_LOG_ERROR("Calculating ECEF solar vector.\n");
92  return(0);
93  }
94  }
95 
96  //printf("smeta.num_band after = %d\n", smeta.num_band);
97  for ( band_index=0; band_index<smeta.num_band; band_index++ )
98  {
99  if (smeta.sensor_mode == OLI)
100  {
101  /* Generate grid for this band */
102  if ( calc_band_grid( wrsparms, smeta, dellon, ecfsun, band_index, &bgrid[band_index] ) != SUCCESS )
103  {
104  IAS_LOG_ERROR("Creating grid for band %d.", smeta.band_smeta[band_index].band);
106  return(0);
107  }
108  }
109  else
110  {
111  /* Generate grid for this band */
112  if ( calc_band_grid_ls( wrsparms, smeta, dellon, ecfsun, band_index, &bgrid_ls[band_index] ) != SUCCESS )
113  {
114  IAS_LOG_ERROR("Creating grid for band %d.", smeta.band_smeta_ls[band_index].band);
116  return(0);
117  }
118  }
119  }
120 
121  return(smeta.num_band);
122 }
123 
125 {
126  return( smeta.scene_id );
127 }
128 
130  int band_index, /* Input band index */
131  META_FRAME *frame ) /* Output image frame info */
132 {
133 
134  if ( band_index < 0 || band_index > smeta.num_band-1 )
135  {
136  IAS_LOG_ERROR("Invalid band requested");
137  return(ERROR);
138  }
139 
140  /* Establish the output image file frame */
141  if (smeta.sensor_mode == OLI)
142  {
143  frame->band = smeta.band_smeta[band_index].band;
144  frame->nlines = smeta.band_smeta[band_index].l1t_lines;
145  frame->nsamps = smeta.band_smeta[band_index].l1t_samps;
146  frame->pixsize = smeta.band_smeta[band_index].pixsize;
147  }
148  else
149  {
150  frame->band = smeta.band_smeta_ls[band_index].band;
151  frame->nlines = smeta.band_smeta_ls[band_index].l1t_lines;
152  frame->nsamps = smeta.band_smeta_ls[band_index].l1t_samps;
153  frame->pixsize = smeta.band_smeta_ls[band_index].pixsize;
154  }
155 
156  frame->code = smeta.projection.code;
157  frame->zone = smeta.projection.zone;
158  frame->ul_x = smeta.projection.corners.upleft.x;
159  frame->ul_y = smeta.projection.corners.upleft.y;
160 
161  return(SUCCESS);
162 }
163 
165  int in_line, /* L1T line coordinate */
166  int in_samp, /* L1T sample coordinate */
167  int in_band, /* Spectral band number */
168  /* Sudipta new addition for OLI to extract SCA and Det numbers */
169  short *sca_num, /* SCA number */
170  short *det_num, /* Detector number */
171  /* End Sudipta new addition for OLI */
172  double *sat_zn, /* Satellite zenith angle */
173  double *sat_az, /* Satellite azimuth angle */
174  double *sun_zn, /* Solar zenith angle */
175  double *sun_az ) /* Solar azimuth angle */
176 {
177  int band_index; /* Band index */
178  int status; /* Status return flag */
179 
180  // printf("SMETA_CALC: %d %d %d\n",in_line, in_samp, in_band);
181  /* Find the index for the current band */
182  band_index = smeta_band_number_to_index( &smeta, in_band );
183  if ( band_index < 0 )
184  {
185  IAS_LOG_ERROR("Invalid band requested: %d.", in_band);
186  smeta_close();
187  return(ERROR);
188  }
189 
190  /* Sudipta-added sca_num and det_num for getting SCA and det output for OLI */
191  if ( (status=smeta_angles( (double)in_line, (double)in_samp, bgrid[band_index],
192  sca_num, det_num, sat_zn, sat_az, sun_zn, sun_az )) != SUCCESS )
193  {
194  IAS_LOG_ERROR("Generating angles for band %d.", smeta.band_smeta[band_index].band);
195  smeta_close();
196  }
197 
198  /* Return status */
199  return( status );
200 }
201 
202 /* smeta_calc abive has custom changes for OLI to retrieve SCA and detector numbers hence created a clone of it for
203  * older L5/L7 sensors */
205  int in_line, /* L1T line coordinate */
206  int in_samp, /* L1T sample coordinate */
207  int in_band, /* Spectral band number */
208  short *sat_zn, /* Satellite zenith angle */
209  short *sat_az, /* Satellite azimuth angle */
210  short *sun_zn, /* Solar zenith angle */
211  short *sun_az ) /* Solar azimuth angle */
212 {
213  int band_index; /* Band index */
214  int status; /* Status return flag */
215 
216  // printf("SMETA_CALC: %d %d %d\n",in_line, in_samp, in_band);
217  /* Find the index for the current band */
218  band_index = smeta_band_number_to_index( &smeta, in_band );
219  if ( band_index < 0 )
220  {
221  IAS_LOG_ERROR("Invalid band requested: %d.", in_band);
222  smeta_close();
223  return(ERROR);
224  }
225 
226  if ( (status=smeta_angles_ls( (double)in_line, (double)in_samp, bgrid_ls[band_index],
227  sat_zn, sat_az, sun_zn, sun_az )) != SUCCESS )
228  {
229  IAS_LOG_ERROR("Generating angles for band %d.", smeta.band_smeta_ls[band_index].band);
230  smeta_close();
231  }
232 // printf("sat_az[%d]=%d\n",is,sat_az[is]);
233 
234  /* Return status */
235  return( status );
236 }
237 
239 {
240  int band_index; /* Index of current band */
241 
242  /* Release the projection transformation */
244 
245  /* Release the band grids */
246  if (smeta.sensor_mode == OLI)
247  {
248  for ( band_index=0; band_index<smeta.num_band; band_index++ )
249  free( bgrid[band_index].sgrid );
250  }
251  else
252  {
253  for ( band_index=0; band_index<smeta.num_band; band_index++ )
254  free( bgrid_ls[band_index].sgrid );
255  }
256 
257  return;
258 }
259 
int frame_scene_ls(WRS2 wrsparms, SMETA smeta, double *dellon, double *delrow)
Definition: mtl_geometry.c:591
int band
Definition: smeta.h:82
SMETA_SCENE_PROJ projection
Definition: smeta.h:130
#define SUCCESS
Definition: ObpgReadGrid.h:15
int smeta_band_number_to_index(SMETA *smeta, int band)
#define IAS_LOG_ERROR(format,...)
Definition: ias_logging.h:96
int status
Definition: l1_czcs_hdf.c:32
double inertialvel
Definition: mtl_geometry.h:25
int numrow
Definition: mtl_geometry.h:30
int calc_band_grid(WRS2 parms, SMETA smeta, double dellon, VECTOR ecfsun, int band_index, MTL_GRID_BAND *bgrid)
Definition: mtl_grid.c:14
int l1t_samps
Definition: smeta.h:99
int smeta_angles_ls(double line, double samp, MTL_GRID_BAND_LS bgrid, short *sat_zn, short *sat_az, short *sun_zn, short *sun_az)
Definition: mtl_grid.c:644
#define IAS_MAX_NBANDS
Definition: emeta.h:17
double semimajor
Definition: mtl_geometry.h:23
int num_band
Definition: smeta.h:131
int smeta_init(char *smeta_filename)
Definition: smeta_api.c:11
double ul_y
Definition: smeta_exploit.h:25
char * smeta_file_name()
Definition: smeta_api.c:124
int l1t_samps
Definition: smeta.h:84
int smeta_read(SMETA *smeta, const char *smeta_filename)
Definition: smeta_exploit.c:22
void smeta_release_projection()
double orbitincl
Definition: mtl_geometry.h:26
int l1t_lines
Definition: smeta.h:83
int smeta_calc_ls(int in_line, int in_samp, int in_band, short *sat_zn, short *sat_az, short *sun_zn, short *sun_az)
Definition: smeta_api.c:204
void smeta_close()
Definition: smeta_api.c:238
int smeta_get_frame(int band_index, META_FRAME *frame)
Definition: smeta_api.c:129
SMETA_BAND band_smeta[IAS_MAX_NBANDS]
Definition: smeta.h:132
double semiminor
Definition: mtl_geometry.h:24
int numpath
Definition: mtl_geometry.h:29
double pixsize
Definition: smeta.h:104
int band
Definition: smeta.h:97
double pixsize
Definition: smeta_exploit.h:26
int cycle
Definition: mtl_geometry.h:31
int frame_scene(WRS2 wrsparms, SMETA smeta, double *dellon, double *delrow)
Definition: mtl_geometry.c:419
int row0lat
Definition: mtl_geometry.h:32
double path1long
Definition: mtl_geometry.h:28
int calc_band_grid_ls(WRS2 parms, SMETA smeta, double dellon, VECTOR ecfsun, int band_index, MTL_GRID_BAND_LS *bgrid)
Definition: mtl_grid.c:91
#define IAS_MAX_NBANDS_LS
Definition: smeta.h:15
int smeta_angles(double line, double samp, MTL_GRID_BAND bgrid, short *sca_num, short *det_num, double *sat_zn, double *sat_az, double *sun_zn, double *sun_az)
Definition: mtl_grid.c:485
int get_ecfsun(WRS2 wrsparms, SMETA smeta, double dellon, VECTOR *ecfsun)
Definition: mtl_geometry.c:849
SMETA_BAND_LS band_smeta_ls[IAS_MAX_NBANDS_LS]
Definition: smeta.h:133
IAS_DBL_XY upleft
struct IAS_CORNERS corners
Definition: smeta.h:76
int get_ecfsun_ls(WRS2 wrsparms, SMETA smeta, double dellon, VECTOR *ecfsun)
Definition: mtl_geometry.c:900
int smeta_calc(int in_line, int in_samp, int in_band, short *sca_num, short *det_num, double *sat_zn, double *sat_az, double *sun_zn, double *sun_az)
Definition: smeta_api.c:164
int smeta_init_projection(SMETA_SCENE_PROJ proj)
double ul_x
Definition: smeta_exploit.h:24
double orbitrad
Definition: mtl_geometry.h:27
char scene_id[22]
Definition: smeta.h:120
Definition: smeta.h:118
SENSOR_MODE sensor_mode
Definition: smeta.h:123
@ OLI
Definition: smeta.h:53
int l1t_lines
Definition: smeta.h:98
#define ERROR
Definition: ancil.h:24
double pixsize
Definition: smeta.h:89
int count
Definition: decode_rs.h:79