11 static int16_t nline,
npix;
12 static int32_t
spix = 0;
14 static void scaleFloat(
float *buf,
int num,
float scale,
float offset) {
17 for (
i = 0;
i < num;
i++) {
18 if (buf[
i] != -32767.0) {
26 size_t source_w, source_h;
28 int fileID, xid, yid, retval;
31 retval = nc_open(
file->name, NC_NOWRITE, &
fileID);
32 if (retval != NC_NOERR) {
33 fprintf(
stderr,
"-E- %s line %d: nc_open(%s) failed.\n",
34 __FILE__, __LINE__,
file->name);
38 retval = nc_inq_dimid(
fileID,
"number_of_lines", &yid);
39 retval = nc_inq_dimid(
fileID,
"pixels_per_line", &xid);
40 retval = nc_inq_dimlen(
fileID, xid, &source_w);
41 retval = nc_inq_dimlen(
fileID, yid, &source_h);
44 printf(
"L1B Npix :%d Nscans:%d\n", (
int) source_w,
47 npix = (int32_t) source_w;
48 nscan = (int32_t) source_h;
54 file->terrain_corrected = 1;
59 static int firstCall = 1;
61 static int scanGrp = -1;
62 static int dataGrp = -1;
63 static int navigationGrp = -1;
64 static int ancillaryGrp = -1;
66 static int scantimeVar = -1;
67 static int yearVar = -1;
68 static int dayVar = -1;
69 static int msecVar = -1;
70 static int lonVar = -1;
71 static int latVar = -1;
72 static int senaVar = -1;
73 static int senzVar = -1;
74 static int solaVar = -1;
75 static int solzVar = -1;
76 static int wsVar = -1;
77 static int wdVar = -1;
78 static int prVar = -1;
79 static int ozVar = -1;
80 static int rhVar = -1;
81 static int wvVar = -1;
84 static float senaScale = 1.0;
85 static float senaOffset = 0.0;
86 static float senzScale = 1.0;
87 static float senzOffset = 0.0;
88 static float solaScale = 1.0;
89 static float solaOffset = 0.0;
90 static float solzScale = 1.0;
91 static float solzOffset = 0.0;
94 static float *rad_data;
103 int scan_year, scan_day;
107 printf(
"file->nbands = %d\n", (
int)
file->nbands);
112 retval = nc_inq_ncid(
file->sd_id,
"sensor_band_parameters", &sensorGrp);
113 if (retval == NC_NOERR) {
114 retval = nc_inq_varid(sensorGrp,
"pixnum", &pixnumVar);
115 if (retval == NC_NOERR) {
116 pixnum = (
int*) malloc(
npix *
sizeof (
int));
119 retval = nc_get_vara_int(sensorGrp, pixnumVar,
start,
count, pixnum);
120 if (retval != NC_NOERR) {
122 "-E- %s line %d: nc_get_vara_int failed for file, %s group, %s var, %s.\n",
123 __FILE__, __LINE__,
file->name,
"sensor_band_parameters",
"pixnum");
130 retval = nc_inq_ncid(
file->sd_id,
"scan_line_attributes", &scanGrp);
131 if (retval != NC_NOERR) {
133 "-E- %s line %d: nc_inq_ncid failed for file, %s group, %s.\n",
134 __FILE__, __LINE__,
file->name,
"scan_line_attributes");
138 retval = nc_inq_ncid(
file->sd_id,
"geophysical_data", &dataGrp);
139 if (retval != NC_NOERR) {
141 "-E- %s line %d: nc_inq_ncid failed for file, %s group, %s.\n",
142 __FILE__, __LINE__,
file->name,
"geophysical_data");
146 retval = nc_inq_ncid(
file->sd_id,
"navigation_data", &navigationGrp);
147 if (retval != NC_NOERR) {
149 "-E- %s line %d: nc_inq_ncid failed for file, %s group, %s.\n",
150 __FILE__, __LINE__,
file->name,
"navigation_data");
154 retval = nc_inq_ncid(
file->sd_id,
"ancillary_data", &ancillaryGrp);
155 if (retval != NC_NOERR)
160 retval = nc_inq_varid(scanGrp,
"scantime", &scantimeVar);
161 if (retval != NC_NOERR) {
163 retval = nc_inq_varid(scanGrp,
"year", &yearVar);
164 if (retval != NC_NOERR) {
166 "-E- %s line %d: variable \"scantime\" or \"year,day,msec\" required in group \"%s\"\n",
167 __FILE__, __LINE__,
"scan_line_attributes");
170 retval = nc_inq_varid(scanGrp,
"day", &dayVar);
171 if (retval != NC_NOERR) {
173 "-E- %s line %d: variable \"scantime\" or \"year,day,msec\" required in group \"%s\"\n",
174 __FILE__, __LINE__,
"scan_line_attributes");
177 retval = nc_inq_varid(scanGrp,
"msec", &msecVar);
178 if (retval != NC_NOERR) {
180 "-E- %s line %d: variable \"scantime\" or \"year,day,msec\" required in group \"%s\"\n",
181 __FILE__, __LINE__,
"scan_line_attributes");
186 retval = nc_inq_varid(navigationGrp,
"longitude", &lonVar);
187 if (retval != NC_NOERR) {
189 "-E- %s line %d: nc_inq_varid failed for file, %s field, %s.\n",
190 __FILE__, __LINE__,
file->name,
"longitude");
193 retval = nc_inq_varid(navigationGrp,
"latitude", &latVar);
194 if (retval != NC_NOERR) {
196 "-E- %s line %d: nc_inq_varid failed for file, %s field, %s.\n",
197 __FILE__, __LINE__,
file->name,
"latitude");
200 retval = nc_inq_varid(navigationGrp,
"sena", &senaVar);
201 if (retval != NC_NOERR) {
203 "-E- %s line %d: nc_inq_varid failed for file, %s field, %s.\n",
204 __FILE__, __LINE__,
file->name,
"sena");
207 retval = nc_inq_varid(navigationGrp,
"senz", &senzVar);
208 if (retval != NC_NOERR) {
210 "-E- %s line %d: nc_inq_varid failed for file, %s field, %s.\n",
211 __FILE__, __LINE__,
file->name,
"senz");
214 retval = nc_inq_varid(navigationGrp,
"sola", &solaVar);
215 if (retval != NC_NOERR)
217 retval = nc_inq_varid(navigationGrp,
"solz", &solzVar);
218 if (retval != NC_NOERR) {
223 "-E- %s line %d: nc_inq_varid failed for file, %s field, %s.\n",
224 __FILE__, __LINE__,
file->name,
"solz");
225 fprintf(
stderr,
" solz must exist if sola is defined.\n");
231 "-E- %s line %d: nc_inq_varid failed for file, %s field, %s.\n",
232 __FILE__, __LINE__,
file->name,
"sola");
233 fprintf(
stderr,
" sola must exist if solz is defined.\n");
238 if (ancillaryGrp != -1) {
239 retval = nc_inq_varid(ancillaryGrp,
"windspeed", &wsVar);
240 if (retval != NC_NOERR) {
243 retval = nc_inq_varid(ancillaryGrp,
"windangle", &wdVar);
244 if (retval != NC_NOERR) {
247 retval = nc_inq_varid(ancillaryGrp,
"pressure", &prVar);
248 if (retval != NC_NOERR) {
251 retval = nc_inq_varid(ancillaryGrp,
"ozone", &ozVar);
252 if (retval != NC_NOERR) {
255 retval = nc_inq_varid(ancillaryGrp,
"relhumid", &rhVar);
256 if (retval != NC_NOERR) {
259 retval = nc_inq_varid(ancillaryGrp,
"watervapor", &wvVar);
260 if (retval != NC_NOERR) {
266 ltVar = (
int*) malloc((
nbands +
file->nbandsir) *
sizeof (
int));
267 rad_data = (
float *) malloc(
npix *
sizeof (
float));
269 sprintf(bandName,
"Lt_%d",
file->iwave[
i]);
270 retval = nc_inq_varid(dataGrp, bandName, ltVar +
i);
271 if (retval != NC_NOERR) {
273 "-E- %s line %d: could not find \"%s\" variable in file, %s\n",
274 __FILE__, __LINE__, bandName,
file->name);
279 sprintf(bandName,
"Lt_%d",
file->iwave[
i]);
280 retval = nc_inq_varid(dataGrp, bandName, ltVar +
i);
281 if (retval != NC_NOERR) {
287 nc_get_att_float(navigationGrp, senaVar,
"scale_factor", &senaScale);
288 nc_get_att_float(navigationGrp, senaVar,
"add_offset", &senaOffset);
290 nc_get_att_float(navigationGrp, senzVar,
"scale_factor", &senzScale);
291 nc_get_att_float(navigationGrp, senzVar,
"add_offset", &senzOffset);
294 nc_get_att_float(navigationGrp, solaVar,
"scale_factor", &solaScale);
295 nc_get_att_float(navigationGrp, solaVar,
"add_offset", &solaOffset);
299 nc_get_att_float(navigationGrp, solzVar,
"scale_factor", &solzScale);
300 nc_get_att_float(navigationGrp, solzVar,
"add_offset", &solzOffset);
307 for (ip = 0; ip <
npix; ip++)
308 l1rec->pixnum[ip] = pixnum[ip];
314 if (scantimeVar != -1) {
315 nc_get_vara_double(scanGrp, scantimeVar,
start,
count, &
l1rec->scantime);
317 retval = nc_get_vara_int(scanGrp, yearVar,
start,
count, &scan_year);
318 if (retval != NC_NOERR) {
320 "-E- %s line %d: nc_get_vara_int failed for file, %s field, %s.\n",
321 __FILE__, __LINE__,
file->name,
"year");
324 retval = nc_get_vara_int(scanGrp, dayVar,
start,
count, &scan_day);
325 if (retval != NC_NOERR) {
327 "-E- %s line %d: nc_get_vara_int failed for file, %s field, %s.\n",
328 __FILE__, __LINE__,
file->name,
"day");
332 if (retval != NC_NOERR) {
334 "-E- %s line %d: nc_get_vara_int failed for file, %s field, %s.\n",
335 __FILE__, __LINE__,
file->name,
"msec");
339 l1rec->scantime =
yds2unix((int16_t) scan_year, (int16_t) scan_day, (
double) (
msec / 1.e3));
349 retval = nc_get_vara_float(navigationGrp, lonVar,
start,
count,
l1rec->lon);
350 if (retval != NC_NOERR) {
352 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
353 __FILE__, __LINE__,
file->name,
"longitude");
356 retval = nc_get_vara_float(navigationGrp, latVar,
start,
count,
l1rec->lat);
357 if (retval != NC_NOERR) {
359 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
360 __FILE__, __LINE__,
file->name,
"latitude");
363 retval = nc_get_vara_float(navigationGrp, senaVar,
start,
count,
l1rec->sena);
364 if (retval != NC_NOERR) {
366 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
367 __FILE__, __LINE__,
file->name,
"sena");
370 scaleFloat(
l1rec->sena,
npix, senaScale, senaOffset);
371 retval = nc_get_vara_float(navigationGrp, senzVar,
start,
count,
l1rec->senz);
372 if (retval != NC_NOERR) {
374 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
375 __FILE__, __LINE__,
file->name,
"senz");
378 scaleFloat(
l1rec->senz,
npix, senzScale, senzOffset);
380 retval = nc_get_vara_float(navigationGrp, solaVar,
start,
count,
l1rec->sola);
381 if (retval != NC_NOERR) {
383 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
384 __FILE__, __LINE__,
file->name,
"sola");
387 scaleFloat(
l1rec->sola,
npix, solaScale, solaOffset);
388 retval = nc_get_vara_float(navigationGrp, solzVar,
start,
count,
l1rec->solz);
389 if (retval != NC_NOERR) {
391 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
392 __FILE__, __LINE__,
file->name,
"solz");
395 scaleFloat(
l1rec->solz,
npix, solzScale, solzOffset);
416 retval = nc_get_vara_float(ancillaryGrp, wsVar,
start,
count,
l1rec->ws);
417 if (retval != NC_NOERR) {
419 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
420 __FILE__, __LINE__,
file->name,
"windspeed");
426 retval = nc_get_vara_float(ancillaryGrp, wdVar,
start,
count,
l1rec->wd);
427 if (retval != NC_NOERR) {
429 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
430 __FILE__, __LINE__,
file->name,
"windangle");
436 retval = nc_get_vara_float(ancillaryGrp, prVar,
start,
count,
l1rec->pr);
437 if (retval != NC_NOERR) {
439 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
440 __FILE__, __LINE__,
file->name,
"pressure");
446 retval = nc_get_vara_float(ancillaryGrp, ozVar,
start,
count,
l1rec->oz);
447 if (retval != NC_NOERR) {
449 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
450 __FILE__, __LINE__,
file->name,
"ozone");
456 retval = nc_get_vara_float(ancillaryGrp, rhVar,
start,
count,
l1rec->rh);
457 if (retval != NC_NOERR) {
459 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
460 __FILE__, __LINE__,
file->name,
"relhumid");
466 retval = nc_get_vara_float(ancillaryGrp, wvVar,
start,
count,
l1rec->wv);
467 if (retval != NC_NOERR) {
469 "-E- %s line %d: nc_get_vara_float failed for file, %s field, %s.\n",
470 __FILE__, __LINE__,
file->name,
"watervapor");
478 for (ib = 0; ib <
nbands; ib++) {
480 retval = nc_get_vara_float(dataGrp, ltVar[ib],
start,
count, rad_data);
481 if (retval != NC_NOERR) {
483 "-E- %s line %d: nc_get_vara_float failed for file, %s Lt %d nm.\n",
484 __FILE__, __LINE__,
file->name,
file->iwave[ib]);
491 l1rec->Lt[ipb] = rad_data[ip] / 10.0;
493 if (
l1rec->Lt[ipb] <= 0.0)
494 l1rec->Lt[ipb] = 0.0001;
500 if (ltVar[ib] != -1) {
501 retval = nc_get_vara_float(dataGrp, ltVar[ib],
start,
count, rad_data);
502 if (retval == NC_NOERR) {
506 l1rec->Ltir[ipb] = rad_data[ip] / 10.0;
508 if (
l1rec->Ltir[ipb] <= 0.0)
509 l1rec->Ltir[ipb] = 0.0001;
524 retval = nc_close(
file->sd_id);
525 if (retval != NC_NOERR) {
526 fprintf(
stderr,
"-E- %s line %d: nc_close failed for file, %s.\n",
527 __FILE__, __LINE__,
file->name);