19 #include <gsl/gsl_errno.h>
20 #include <gsl/gsl_spline.h>
21 #include <gsl/gsl_sort_double.h>
25 #define CDLFILE "OCIP_Level-1B_Data_Structure.cdl"
26 #define NBANDS 285 // number of OCI bands
42 #define VERSION "1.00"
44 size_t interpLt(
size_t startJ,
size_t npixels,
size_t k,
size_t nbMax,
45 float ociaWavelength,
size_t iwave, prism4ocia_t*
data,
46 gsl_interp_accel* acc,
float* Lt) {
48 size_t i0 = startJ, i1 = startJ + 5;
49 float minWave, maxWave;
51 double*
x = (
double*) (calloc(nbMax,
sizeof (
double)));
52 double*
y = (
double*) (calloc(nbMax,
sizeof (
double)));
54 while (i0 >= 0 && i1 < nbMax) {
58 for (
j = i0;
j < i1;
j++) {
59 if (
data->Lt[
j * npixels +
k] > 0) {
61 y[n] =
data->Lt[
j * npixels +
k];
63 if (minWave >
data->wave[
j]) minWave =
data->wave[
j];
64 if (maxWave < data->wave[
j]) maxWave =
data->wave[
j];
68 if (n > 2 && minWave <= ociaWavelength && maxWave >= ociaWavelength)
break;
69 if (minWave > ociaWavelength || n < 3)
71 if (maxWave < ociaWavelength || n < 3)
77 gsl_spline*
spline = gsl_spline_alloc(gsl_interp_cspline, n);
78 gsl_sort2(
x, 1,
y, 1, n);
79 if (ociaWavelength >
x[0]) {
81 gsl_sort2(
x, 1,
y, 1, n);
85 double yi = gsl_spline_eval(
spline, ociaWavelength, acc);
86 Lt[iwave * npixels +
k] = yi;
88 Lt[iwave * npixels +
k] = -9999;
98 int main(
int argc,
char* argv[]) {
100 size_t sline = 0, eline = 0;
103 double scantime_first, scantime_last;
104 static int first_good_scantime = 0;
105 char isodatetime_first[30], isodatetime_last[30];
107 cout <<
"prismbil2oci " <<
VERSION <<
" ("
108 << __DATE__ <<
" " << __TIME__ <<
")" << endl;
112 "prismbil2oci input_PRISM_file output_ORCA_file <sline (default=0)> <eline (default=last row)>"
118 sscanf(argv[3],
"%ld", &sline);
119 if (sline < 0) sline = 0;
123 sscanf(argv[4],
"%ld", &eline);
124 if (eline < 0) eline = 0;
128 if ((filedir = getenv(
"OCDATAROOT")) ==
NULL) {
129 printf(
"-E- %s: OCDATAROOT env variable undefined.\n", __FILE__);
140 prism4ocia_t **
data, *temp;
142 data = (prism4ocia_t **) malloc(
sizeof (prism4ocia_t *));
144 size_t nscans, npixels = temp->npix;
146 if (eline <= 0) eline = temp->nscan;
148 nscans = eline - sline;
152 const char *dimNames[] = {
"number_of_lines",
"pixels_per_line",
"number_of_bands"};
153 size_t dimSize[] = {nscans, npixels,
NBANDS};
155 char time_coverage_start[64] =
"yyyy-mm-ddThh:mm:ssZ";
168 sprintf(time_coverage_start,
"%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2dZ", temp->year, temp->month, temp->day, temp->hour, temp->min, (
int) temp->sec);
169 status = nc_put_att_text(ncid, NC_GLOBAL,
"time_coverage_start", strlen(time_coverage_start) + 1, time_coverage_start);
172 printf(
"-E- %s %d: %s for %s\n",
173 __FILE__, __LINE__, nc_strerror(
status),
"time_coverage_start");
177 sprintf(time_coverage_start,
"%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2dZ", temp->year, temp->month, temp->day, temp->hour, temp->min, (
int) temp->sec);
178 status = nc_put_att_text(ncid, NC_GLOBAL,
"time_coverage_end", strlen(time_coverage_start) + 1, time_coverage_start);
182 size_t nbands_av = temp->numBands;
183 size_t nbands_ocia =
NBANDS;
185 size_t n = 0, iwave = 0;
192 gsl_interp_accel *acc = gsl_interp_accel_alloc();
194 float *Lt = (
float *) calloc(npixels*nbands_ocia,
sizeof (
float));
195 float *ociaWavelength = (
float *) calloc(nbands_ocia,
sizeof (
float));
196 double* utc = (
double*) (calloc(nscans,
sizeof (
double)));
199 const char *navfieldsOCIP[] = {
"sena",
"senz",
"sola",
"solz",
"lat",
"lon"};
203 size_t startOCIP[2] = {0, 0};
204 size_t countOCIP[2] = {1, npixels};
210 grpID = ociafile.
getGid(
"observation_data");
215 cout <<
"Can't read file " << argv[1] << endl;
220 if ((
iscan % 100) == 0) {
221 cout <<
"Processing scan: " <<
iscan <<
" out of " << nscans << endl;
222 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);
225 for (
size_t k = 0;
k < npixels;
k++) {
230 for (
j = 0;
j < nbands_av;
j++) {
231 if (temp->Lt[
j * npixels +
k] > 0) n++;
235 if (n == 0)
continue;
242 for (
j = 0;
j < nbands_av;
j++) {
243 ociaWavelength[iwave] = temp->wave[
j];
244 Lt[iwave * npixels +
k] = temp->Lt[
k * nbands_av +
j];
246 fwhm[iwave] = temp->fwhm[
j];
281 printf(
"Oops, something's wrong iwave (%d) != nbands (%d) \n", (
int) iwave,
NBANDS);
285 for (
size_t iwave = 0; iwave < nbands_ocia; iwave++) {
289 status = nc_inq_varid(grpID,
"Lt", &varID);
293 size_t count[3] = {1, 1, npixels};
295 &Lt[iwave * npixels]);
299 utc[
iscan] = temp->scantime;
302 grpID = ociafile.
getGid(
"navigation_data");
304 startOCIP[0] =
iscan - sline;
306 for (
i = 0;
i < 6;
i++) {
310 status = nc_inq_varid(grpID, navfieldsOCIP[
i], &varID);
315 status = nc_put_vara_float(grpID, varID, startOCIP, countOCIP, &temp->sena[0]);
319 status = nc_put_vara_float(grpID, varID, startOCIP, countOCIP, &temp->senz[0]);
323 status = nc_put_vara_float(grpID, varID, startOCIP, countOCIP, &temp->sola[0]);
327 status = nc_put_vara_float(grpID, varID, startOCIP, countOCIP, &temp->solz[0]);
331 status = nc_put_vara_double(grpID, varID, startOCIP, countOCIP, &temp->lat[0]);
335 status = nc_put_vara_double(grpID, varID, startOCIP, countOCIP, &temp->lon[0]);
342 if (temp->scantime > 0) {
343 scantime_last = temp->scantime;
344 if (!first_good_scantime) {
345 first_good_scantime = 1;
346 scantime_first = temp->scantime;
352 if (scantime_first < scantime_last) {
359 status = nc_put_att_text(ncid, NC_GLOBAL,
"time_coverage_start", strlen(isodatetime_first) + 1, isodatetime_first);
361 printf(
"-E- %s %d: %s for %s\n",
362 __FILE__, __LINE__, nc_strerror(
status),
"time_coverage_start");
365 status = nc_put_att_text(ncid, NC_GLOBAL,
"time_coverage_end", strlen(isodatetime_last) + 1, isodatetime_last);
367 printf(
"-E- %s %d: %s for %s\n",
368 __FILE__, __LINE__, nc_strerror(
status),
"time_coverage_end");
372 gsl_interp_accel_free(acc);
374 size_t startw[2] = {0, 0};
375 size_t countw[2] = {nbands_ocia, 0};
377 grpID = ociafile.
getGid(
"sensor_band_parameters");
379 status = nc_inq_varid(grpID,
"wavelength", &varID);
381 status = nc_put_vara_float(grpID, varID, startw, countw, ociaWavelength);
384 status = nc_inq_varid(grpID,
"fwhm", &varID);
386 status = nc_put_vara_float(grpID, varID, startw, countw,
fwhm);
389 size_t starts[2] = {0, 0};
390 size_t counts[2] = {nscans, 0};
392 const char *nscanfieldsPRISM[] = {
"utc",
"alt"};
393 const char *nscanfieldsOCIP[] = {
"scan_start_time",
"altitude"};
394 short* alt = (
short*) (calloc(nscans,
sizeof (
short)));
396 for (
i = 0;
i < nscans;
i++) {
400 grpID = ociafile.
getGid(
"scan_line_attributes");
402 for (
i = 0;
i < 2;
i++) {
403 cout <<
"Copying " << nscanfieldsPRISM[
i] <<
" to " <<
404 nscanfieldsOCIP[
i] << endl;
406 status = nc_inq_varid(grpID, nscanfieldsOCIP[
i], &varID);
411 status = nc_put_vara_double(grpID, varID, starts,
counts, utc);
415 status = nc_put_vara_short(grpID, varID, starts,
counts, alt);