18 #include <gsl/gsl_errno.h>
19 #include <gsl/gsl_spline.h>
20 #include <gsl/gsl_sort_double.h>
26 #define SENSOR "aviris"
27 #define CDLFILE "AVIRIS_Level-1B_Data_Structure.cdl"
28 #define NBANDS 200 // number of output AVIRIS bands
34 #define VERSION "1.00"
36 size_t interpLt(
size_t startJ,
size_t npixels,
size_t k,
size_t nbMax,
37 float ncdfWavelength,
size_t iwave, aviris4ocia_t*
data,
38 gsl_interp_accel* acc,
float* Lt) {
40 size_t i0 = startJ, i1 = startJ + 5;
41 float minWave, maxWave;
43 double*
x = (
double*) (calloc(nbMax,
sizeof (
double)));
44 double*
y = (
double*) (calloc(nbMax,
sizeof (
double)));
46 while (i0 >= 0 && i1 < nbMax) {
50 for (
j = i0;
j < i1;
j++) {
51 if (
data->Lt[
j * npixels +
k] > 0) {
53 y[n] =
data->Lt[
j * npixels +
k];
55 if (minWave >
data->wave[
j]) minWave =
data->wave[
j];
56 if (maxWave < data->wave[
j]) maxWave =
data->wave[
j];
60 if (n > 2 && minWave <= ncdfWavelength && maxWave >= ncdfWavelength)
break;
61 if (minWave > ncdfWavelength || n < 3)
63 if (maxWave < ncdfWavelength || n < 3)
69 gsl_spline*
spline = gsl_spline_alloc(gsl_interp_cspline, n);
70 gsl_sort2(
x, 1,
y, 1, n);
71 if (ncdfWavelength >
x[0]) {
73 gsl_sort2(
x, 1,
y, 1, n);
77 double yi = gsl_spline_eval(
spline, ncdfWavelength, acc);
78 Lt[iwave * npixels +
k] = yi;
80 Lt[iwave * npixels +
k] = -9999;
99 int main(
int argc,
char* argv[]) {
101 size_t sline = 0, eline = 0;
102 char filename[FILENAME_MAX],
hdrfile[FILENAME_MAX], imgfile[FILENAME_MAX],
103 navfile[FILENAME_MAX], gainfile[FILENAME_MAX];
105 int interleave =
BIL;
106 double scantime_first, scantime_last;
107 static int first_good_scantime = 0;
108 char isodatetime_first[30], isodatetime_last[30];
110 cout <<
"avirisbil2ncdf " <<
VERSION <<
" ("
111 << __DATE__ <<
" " << __TIME__ <<
")" << endl;
115 "avirisbil2ncdf input_AVIRIS_file output_AVIRIS_file <sline (default=0)> <eline (default=last row,0=last row)> <img file(optional)> <nav file(optional)> <gain file(optional)>"
121 sscanf(argv[3],
"%ld", &sline);
122 if (sline < 0) sline = 0;
126 sscanf(argv[4],
"%ld", &eline);
127 if (eline < 0) eline = 0;
130 sscanf(argv[5],
"%s", imgfile);
131 if (eline < 0) eline = 0;
134 sscanf(argv[6],
"%s", navfile);
135 if (eline < 0) eline = 0;
138 sscanf(argv[7],
"%s", gainfile);
139 if (eline < 0) eline = 0;
143 if ((filedir = getenv(
"OCDATAROOT")) ==
NULL) {
144 printf(
"-E- %s: OCDATAROOT env variable undefined.\n", __FILE__);
155 aviris4ocia_t **
data, *temp;
161 data = (aviris4ocia_t **) malloc(
sizeof (aviris4ocia_t *));
163 size_t ip, nscans, npixels = temp->npix;
165 if (eline <= 0) eline = temp->nscans;
167 nscans = eline - sline;
171 const char *dimNames[] = {
"number_of_lines",
"pixels_per_line",
"number_of_bands"};
172 size_t dimSize[] = {nscans, npixels,
NBANDS};
185 size_t nbands_av = temp->numBands;
186 size_t nbands_aviris =
NBANDS;
188 size_t n = 0, iwave = 0;
191 static int *ibndx, *ipbndx;
192 int ipb_av, ib, ibl1, ipb;
196 gsl_interp_accel *acc = gsl_interp_accel_alloc();
198 float *Lt = (
float *) calloc(npixels*nbands_aviris,
sizeof (
float));
199 float *ncdfWavelength = (
float *) calloc(nbands_aviris,
sizeof (
float));
200 double* utc = (
double*) (calloc(nscans,
sizeof (
double)));
203 ipbndx = (
int*) malloc(npixels * nbands_aviris *
sizeof (
int));
205 fprintf(
stderr,
"-E- %s line %d: Out of Memory allocating buffer array in readl1_aviris.\n",
210 ibndx = (
int*) malloc(nbands_aviris *
sizeof (
int));
212 fprintf(
stderr,
"-E- %s line %d: Out of Memory allocating buffer array in readl1_aviris.\n",
220 for (ib = 0; ib < 31; ib++) {
224 for (ib = 33; ib < 95; ib++) {
228 for (ib = 97; ib < 159; ib++) {
232 for (ib = 161; ib < 206; ib++) {
238 for (ip = 0; ip < npixels; ip++) {
240 for (ib = 0; ib < 31; ib++) {
241 ipb = ip * nbands_aviris + ibl1;
242 switch (interleave) {
244 ipb_av = ip * nbands_av + ib;
247 ipb_av = ib * npixels + ip;
250 ipbndx[ipb] = ipb_av;
253 for (ib = 33; ib < 95; ib++) {
254 ipb = ip * nbands_aviris + ibl1;
255 switch (interleave) {
257 ipb_av = ip * nbands_av + ib;
260 ipb_av = ib * npixels + ip;
263 ipbndx[ipb] = ipb_av;
266 for (ib = 97; ib < 159; ib++) {
267 ipb = ip * nbands_aviris + ibl1;
268 switch (interleave) {
270 ipb_av = ip * nbands_av + ib;
273 ipb_av = ib * npixels + ip;
276 ipbndx[ipb] = ipb_av;
279 for (ib = 161; ib < 206; ib++) {
280 ipb = ip * nbands_aviris + ibl1;
281 switch (interleave) {
283 ipb_av = ip * nbands_av + ib;
286 ipb_av = ib * npixels + ip;
289 ipbndx[ipb] = ipb_av;
294 grpID = ncdffile.
getGid(
"observation_data");
302 cout <<
"Can't read file " << argv[1] << endl;
307 for (ip = 0; ip < npixels; ip++)
308 for (iwave = 0; iwave < nbands_aviris; iwave++) Lt[iwave * npixels + ip] =
FILL;
310 if ((
iscan % 100) == 0) {
311 cout <<
"Processing scan: " <<
iscan <<
" out of " << nscans << endl;
312 printf(
"year=%d doy=%d msec=%d month=%d day=%d hour=%d min=%d sec=%f\n ", temp->year, temp->doy, temp->msec, temp->month, temp->day, temp->hour, temp->min, temp->sec);
315 for (
size_t k = 0;
k < npixels;
k++) {
320 for (
j = 0;
j < nbands_av;
j++) {
321 if (temp->Lt[
j * npixels +
k] > 0) n++;
325 if (n == 0)
continue;
332 for (
j = 0;
j < nbands_aviris;
j++) {
333 ipb =
k * nbands_aviris +
j;
334 if (temp->Lt[ipbndx[ipb]] > 0) {
335 Lt[iwave * npixels +
k] = temp->Lt[ipbndx[ipb]];
337 Lt[iwave * npixels +
k] =
FILL;
339 ncdfWavelength[iwave] = temp->wave[ibndx[
j]];
340 fwhm[iwave] = temp->fwhm[ibndx[
j]];
349 printf(
"Oops, something's wrong iwave (%d) != nbands (%d) \n", (
int) iwave,
NBANDS);
357 status = nc_inq_varid(grpID,
"Lt", &varID);
361 size_t count[3] = {nbands_aviris, 1, npixels};
367 utc[
iscan] = temp->scantime;
370 if (temp->scantime > 0) {
371 scantime_last = temp->scantime;
372 if (!first_good_scantime) {
373 first_good_scantime = 1;
374 scantime_first = temp->scantime;
380 if (scantime_first < scantime_last) {
387 status = nc_put_att_text(ncid, NC_GLOBAL,
"time_coverage_start", strlen(isodatetime_first) + 1, isodatetime_first);
389 printf(
"-E- %s %d: %s for %s\n",
390 __FILE__, __LINE__, nc_strerror(
status),
"time_coverage_start");
393 status = nc_put_att_text(ncid, NC_GLOBAL,
"time_coverage_end", strlen(isodatetime_last) + 1, isodatetime_last);
395 printf(
"-E- %s %d: %s for %s\n",
396 __FILE__, __LINE__, nc_strerror(
status),
"time_coverage_end");
400 const char *navfieldsAVIRIS[] = {
"to-sensor_azimuth",
"to-sensor_zenith",
"to-sun_azimuth",
"to-sun_zenith",
"lat",
"lon"};
401 const char *navfieldsNCDF[] = {
"sena",
"senz",
"sola",
"solz",
"lat",
"lon"};
403 grpID = ncdffile.
getGid(
"navigation_data");
406 size_t startNCDF[2] = {0, 0};
407 size_t countNCDF[2] = {nscans, npixels};
410 for (
i = 0;
i < 6;
i++) {
411 cout <<
"Copying " << navfieldsAVIRIS[
i] <<
" to " <<
412 navfieldsNCDF[
i] << endl;
414 status = nc_inq_varid(grpID, navfieldsNCDF[
i], &varID);
419 status = nc_put_vara_float(grpID, varID, startNCDF, countNCDF, &temp->sena[sline * npixels]);
423 status = nc_put_vara_float(grpID, varID, startNCDF, countNCDF, &temp->senz[sline * npixels]);
427 status = nc_put_vara_float(grpID, varID, startNCDF, countNCDF, &temp->sola[sline * npixels]);
431 status = nc_put_vara_float(grpID, varID, startNCDF, countNCDF, &temp->solz[sline * npixels]);
435 status = nc_put_vara_double(grpID, varID, startNCDF, countNCDF, &temp->lat[sline * npixels]);
439 status = nc_put_vara_double(grpID, varID, startNCDF, countNCDF, &temp->lon[sline * npixels]);
447 gsl_interp_accel_free(acc);
449 size_t startw[2] = {0, 0};
450 size_t countw[2] = {nbands_aviris, 0};
452 grpID = ncdffile.
getGid(
"sensor_band_parameters");
454 status = nc_inq_varid(grpID,
"wavelength", &varID);
456 status = nc_put_vara_float(grpID, varID, startw, countw, ncdfWavelength);
459 status = nc_inq_varid(grpID,
"fwhm", &varID);
461 status = nc_put_vara_float(grpID, varID, startw, countw,
fwhm);
464 size_t starts[2] = {0, 0};
465 size_t counts[2] = {nscans, 0};
467 const char *nscanfieldsAVIRIS[] = {
"utc",
"alt",
"alt"};
468 const char *nscanfieldsNCDF[] = {
"scan_start_time",
"altitude",
"range"};
469 short* alt = (
short*) (calloc(nscans,
sizeof (
short)));
471 grpID = ncdffile.
getGid(
"scan_line_attributes");
473 for (
i = 0;
i < 3;
i++) {
474 cout <<
"Copying " << nscanfieldsAVIRIS[
i] <<
" to " <<
475 nscanfieldsNCDF[
i] << endl;
477 status = nc_inq_varid(grpID, nscanfieldsNCDF[
i], &varID);
482 status = nc_put_vara_double(grpID, varID, starts,
counts, utc);
486 for (
i = 0;
i < nscans;
i++) alt[
i] = temp->alt[sline +
i];
487 status = nc_put_vara_short(grpID, varID, starts,
counts, alt);
491 for (
i = 0;
i < nscans;
i++) alt[
i] = temp->alt[sline +
i]*1000;
492 status = nc_put_vara_short(grpID, varID, starts,
counts, alt);