18 #include <gsl/gsl_errno.h>
19 #include <gsl/gsl_spline.h>
20 #include <gsl/gsl_sort_double.h>
28 #define VERSION "0.10"
38 int main(
int argc,
char* argv[]) {
41 cout <<
"aviris2orca " <<
VERSION <<
" ("
42 << __DATE__ <<
" " << __TIME__ <<
")" << endl;
46 "aviris2orca input_AVIRIS_file output_ORCA_file ORCA_CDL_structure_file"
54 size_t nscans, npixels;
59 status = nc_inq_dimid(aviris_id.
fid,
"x", &dimid);
61 cout <<
"Can't find 'x' dimension in: " << argv[1] << endl;
64 status = nc_inq_dimlen(aviris_id.
fid, dimid, &npixels);
67 status = nc_inq_dimid(aviris_id.
fid,
"y", &dimid);
69 cout <<
"Can't find 'y' dimension in: " << argv[1] << endl;
72 status = nc_inq_dimlen(aviris_id.
fid, dimid, &nscans);
76 orcafile.
cdlCreate(argv[2], argv[3], nscans);
81 char *navfieldsAVIRIS[] = {
"to-sensor_azimuth",
"to-sensor_zenith",
82 "to-sun_azimuth",
"to-sun_zenith",
84 char *navfieldsORCA[] = {
"sena",
"senz",
"sola",
"solz",
"lat",
"lon"};
86 float *navbuffer = (
float *) calloc(nscans*npixels,
sizeof (
float));
87 grpID = orcafile.
getGid(
"navigation_data");
90 int startAVIRIS[2] = {0, 0};
91 int countAVIRIS[2] = {nscans, npixels};
92 size_t startORCA[2] = {0, 0};
93 size_t countORCA[2] = {nscans, npixels};
95 for (
size_t i = 0;
i < 6;
i++) {
96 cout <<
"Copying " << navfieldsAVIRIS[
i] <<
" to " <<
97 navfieldsORCA[
i] << endl;
100 startAVIRIS,
NULL, countAVIRIS, navbuffer));
102 status = nc_inq_varid(grpID, navfieldsORCA[
i], &varID);
105 status = nc_put_vara_float(grpID, varID, startORCA, countORCA, navbuffer);
110 float *plbuffer = (
float *) calloc(nscans*npixels,
sizeof (
float));
112 startAVIRIS,
NULL, countAVIRIS, plbuffer));
113 PTB(
readDS(aviris_id,
"to-sensor_zenith",
114 startAVIRIS,
NULL, countAVIRIS, navbuffer));
115 float deg2rad = 3.1415927 / 180;
116 for (
size_t j = 0;
j < nscans * npixels;
j++) {
117 if (navbuffer[
j] == -9999)
120 plbuffer[
j] *= cos(
deg2rad * navbuffer[
j]);
122 status = nc_inq_varid(grpID,
"altitude", &varID);
125 status = nc_put_vara_float(grpID, varID, startORCA, countORCA, plbuffer);
134 int *varLtid = (
int *) calloc(n_datasets,
sizeof (
int));
135 float *avirisWavelength = (
float *) calloc(n_datasets,
sizeof (
float));
136 double *scale_factor = (
double *) calloc(n_datasets,
sizeof (
double));
143 char nambuf[NC_MAX_NAME];
145 for (
size_t i = 0;
i < n_datasets;
i++) {
146 nc_inq_varname(aviris_id.
fid,
i, nambuf);
149 if (strncmp(nambuf,
"Lt_", 3) == 0) {
154 printf(
"Nambuf=%s %d\n", nambuf, varLtid[n]);
157 str.replace(
str.find_first_of(
'_', 3), 1,
".");
159 istr.str(
str.substr(3, string::npos));
160 istr >> avirisWavelength[n];
166 status = nc_get_att_double(aviris_id.
fid,
i,
"scale_factor",
171 int n_Lt_datasets = n;
174 int start[2] = {0, 0};
175 int count[2] = {1, npixels};
176 int16_t *sval = (int16_t *) calloc(npixels*n_Lt_datasets,
sizeof (int16_t));
178 gsl_interp_accel *acc = gsl_interp_accel_alloc();
180 size_t n_refl_bands = 91;
181 size_t n_ir_bands = 6;
183 int orcaWavelength[] ={350, 355, 360, 365, 370, 375, 380, 385, 390, 395, 400, 405, 410, 415,
184 420, 425, 430, 435, 440, 445, 450, 455, 460, 465, 470, 475, 480, 485,
185 490, 495, 500, 505, 510, 515, 520, 525, 530, 535, 540, 545, 550, 555,
186 560, 565, 570, 575, 580, 585, 590, 595, 600, 605, 610, 615, 620, 625,
187 630, 635, 640, 645, 650, 655, 660, 665, 670, 675, 680, 685, 690, 695,
188 700, 705, 710, 715, 720, 725, 730, 735, 740, 745, 750, 755, 760, 765,
189 770, 775, 780, 785, 790, 795, 800, 940, 1240, 1378, 1640, 2130, 2250, 0};
191 float *visnir = (
float *) calloc(npixels*n_refl_bands,
sizeof (
float));
192 float *ir = (
float *) calloc(npixels*n_ir_bands,
sizeof (
float));
194 grpID = orcafile.
getGid(
"earth_view_data");
198 if ((
iscan % 100) == 0)
199 cout <<
"Processing scan: " <<
iscan <<
" out of " << nscans << endl;
201 for (
size_t j = 0;
j < npixels * n_refl_bands;
j++) visnir[
j] = -999.0;
205 for (
size_t j = 0;
j < n_Lt_datasets;
j++) {
206 printf(
"max=%d j=%d varid=%d\n", n_Lt_datasets,
j, varLtid[
j]);
207 nc_inq_varname(aviris_id.
fid, varLtid[
j], nambuf);
212 for (
size_t k = 0;
k < npixels;
k++) {
216 for (
size_t j = 0;
j < n_Lt_datasets;
j++) {
217 if (sval[
j * npixels +
k] > 0) n++;
221 if (n == 0)
continue;
224 gsl_spline *
spline = gsl_spline_alloc(gsl_interp_cspline, n);
225 double *
x = (
double *) calloc(n,
sizeof (
double));
226 double *
y = (
double *) calloc(n,
sizeof (
double));
232 for (
size_t j = 0;
j < n_Lt_datasets;
j++) {
233 if (sval[
j * npixels +
k] > 0) {
234 x[n] = avirisWavelength[
j];
235 y[n] = sval[
j * npixels +
k] * scale_factor[
j];
241 gsl_sort2(
x, 1,
y, 1, n);
248 while (orcaWavelength[iwave] != 0) {
249 if (orcaWavelength[iwave] >
x[0] &&
250 orcaWavelength[iwave] <
x[n - 1]) {
251 double yi = gsl_spline_eval(
spline, orcaWavelength[iwave], acc);
252 if (orcaWavelength[iwave] <= 800)
253 visnir[iwave * npixels +
k] = yi;
255 ir[(iwave - n_refl_bands) * npixels +
k] = yi;
270 for (
size_t iwave = 0; iwave < n_refl_bands; iwave++) {
272 status = nc_inq_varid(grpID,
"Lt_visnir", &varID);
276 size_t count[3] = {1, 1, npixels};
278 &visnir[iwave * npixels]);
283 char *irLt[] = {
"Lt_940",
"Lt_1240",
"Lt_1378",
284 "Lt_1640",
"Lt_2130",
"Lt_2250"};
286 for (
size_t iwave = 0; iwave < n_ir_bands; iwave++) {
288 status = nc_inq_varid(grpID, irLt[iwave], &varID);
292 size_t count[2] = {1, npixels};
294 &ir[iwave * npixels]);
300 gsl_interp_accel_free(acc);
310 free(avirisWavelength);