18 #define MERIS_NBANDS 15
21 static float *
lon, *
lat, *senz, *sena, *solz, *sola;
24 static int16_t nline,
npix;
26 static int32_t
spix = 0;
28 static double time_interval;
41 float *insolz, *insola;
42 float *insenz, *insena;
50 float *x_ctl_ll, *y_ctl_ll, *z_ctl_ll;
51 float *x_ctl_sol, *y_ctl_sol, *z_ctl_sol;
52 float *x_ctl_sen, *y_ctl_sen, *z_ctl_sen;
53 float * in_ptr[3][3], *out_ptr[3][2], *tout_ptr[3][2];
55 float off_x, off_y, sub_x, sub_y;
56 float *tlon, *tlat, *tsenz, *tsena, *tsolz, *tsola;
60 inlon = (
float *) calloc(ncol*nrow,
sizeof (
float));
61 inlat = (
float *) calloc(ncol*nrow,
sizeof (
float));
62 insolz = (
float *) calloc(ncol*nrow,
sizeof (
float));
63 insola = (
float *) calloc(ncol*nrow,
sizeof (
float));
64 insenz = (
float *) calloc(ncol*nrow,
sizeof (
float));
65 insena = (
float *) calloc(ncol*nrow,
sizeof (
float));
67 xctl = (
float *) calloc(ncol,
sizeof (
float));
68 yctl = (
float *) calloc(nrow,
sizeof (
float));
69 in1 = (
float *) calloc(nrow,
sizeof (
float));
70 in2 = (
float *) calloc(nrow,
sizeof (
float));
73 nc_inq_varid(
fileID,
"longitude", &geo_id);
74 nc_get_var_float(
fileID, geo_id, inlon);
75 nc_get_att_float(
fileID, geo_id,
"offset_x", &off_x);
76 nc_get_att_float(
fileID, geo_id,
"offset_y", &off_y);
77 nc_get_att_float(
fileID, geo_id,
"subsampling_x", &sub_x);
78 nc_get_att_float(
fileID, geo_id,
"subsampling_y", &sub_y);
80 nc_inq_varid(
fileID,
"latitude", &geo_id);
81 nc_get_var_float(
fileID, geo_id, inlat);
82 nc_inq_varid(
fileID,
"sun_zenith", &geo_id);
83 nc_get_var_float(
fileID, geo_id, insolz);
84 nc_inq_varid(
fileID,
"sun_azimuth", &geo_id);
85 nc_get_var_float(
fileID, geo_id, insola);
86 nc_inq_varid(
fileID,
"view_zenith", &geo_id);
87 nc_get_var_float(
fileID, geo_id, insenz);
88 nc_inq_varid(
fileID,
"view_azimuth", &geo_id);
89 nc_get_var_float(
fileID, geo_id, insena);
94 printf(
"nrow,ncol = %d %d \n", nrow, ncol);
95 for (
i = 0;
i < ncol;
i++) {
96 xctl[
i] = ((
float)
i) * sub_x + off_x;
103 for (
i = 0;
i < nrow;
i++) {
104 yctl[
i] = ((
float)
i) * sub_y + off_y;
109 x_ctl_ll = (
float *) calloc(ncol*nrow,
sizeof (
float));
110 y_ctl_ll = (
float *) calloc(ncol*nrow,
sizeof (
float));
111 z_ctl_ll = (
float *) calloc(ncol*nrow,
sizeof (
float));
113 x_ctl_sol = (
float *) calloc(ncol*nrow,
sizeof (
float));
114 y_ctl_sol = (
float *) calloc(ncol*nrow,
sizeof (
float));
115 z_ctl_sol = (
float *) calloc(ncol*nrow,
sizeof (
float));
117 x_ctl_sen = (
float *) calloc(ncol*nrow,
sizeof (
float));
118 y_ctl_sen = (
float *) calloc(ncol*nrow,
sizeof (
float));
119 z_ctl_sen = (
float *) calloc(ncol*nrow,
sizeof (
float));
122 for (
i = 0;
i < nrow;
i++) {
123 for (
j = 0;
j < ncol;
j++) {
124 inlon[
i * ncol +
j] = inlon[
i * ncol +
j] /
RADEG;
125 inlat[
i * ncol +
j] = inlat[
i * ncol +
j] /
RADEG;
127 x_ctl_ll[
i * ncol +
j] = cos(inlat[
i * ncol +
j]) * cos(inlon[
i * ncol +
j]);
128 y_ctl_ll[
i * ncol +
j] = cos(inlat[
i * ncol +
j]) * sin(inlon[
i * ncol +
j]);
129 z_ctl_ll[
i * ncol +
j] = sin(inlat[
i * ncol +
j]);
132 insola[
i * ncol +
j] = insola[
i * ncol +
j] /
RADEG;
133 insolz[
i * ncol +
j] = insolz[
i * ncol +
j] /
RADEG;
135 x_ctl_sol[
i * ncol +
j] = cos(insolz[
i * ncol +
j]) * cos(insola[
i * ncol +
j]);
136 y_ctl_sol[
i * ncol +
j] = cos(insolz[
i * ncol +
j]) * sin(insola[
i * ncol +
j]);
137 z_ctl_sol[
i * ncol +
j] = sin(insolz[
i * ncol +
j]);
140 insena[
i * ncol +
j] = insena[
i * ncol +
j] /
RADEG;
141 insenz[
i * ncol +
j] = insenz[
i * ncol +
j] /
RADEG;
143 x_ctl_sen[
i * ncol +
j] = cos(insenz[
i * ncol +
j]) * cos(insena[
i * ncol +
j]);
144 y_ctl_sen[
i * ncol +
j] = cos(insenz[
i * ncol +
j]) * sin(insena[
i * ncol +
j]);
145 z_ctl_sen[
i * ncol +
j] = sin(insenz[
i * ncol +
j]);
149 in_ptr[0][0] = x_ctl_ll;
150 in_ptr[0][1] = y_ctl_ll;
151 in_ptr[0][2] = z_ctl_ll;
152 in_ptr[1][0] = x_ctl_sol;
153 in_ptr[1][1] = y_ctl_sol;
154 in_ptr[1][2] = z_ctl_sol;
155 in_ptr[2][0] = x_ctl_sen;
156 in_ptr[2][1] = y_ctl_sen;
157 in_ptr[2][2] = z_ctl_sen;
159 tlon = (
float *) calloc(
npix*nrow,
sizeof (
float));
160 tlat = (
float *) calloc(
npix*nrow,
sizeof (
float));
161 tsenz = (
float *) calloc(
npix*nrow,
sizeof (
float));
162 tsena = (
float *) calloc(
npix*nrow,
sizeof (
float));
163 tsolz = (
float *) calloc(
npix*nrow,
sizeof (
float));
164 tsola = (
float *) calloc(
npix*nrow,
sizeof (
float));
166 tout_ptr[0][0] = tlon;
167 tout_ptr[0][1] = tlat;
168 tout_ptr[1][0] = tsola;
169 tout_ptr[1][1] = tsolz;
170 tout_ptr[2][0] = tsena;
171 tout_ptr[2][1] = tsenz;
175 out_ptr[1][0] = sola;
176 out_ptr[1][1] = solz;
177 out_ptr[2][0] = sena;
178 out_ptr[2][1] = senz;
185 printf(
"Interpolating rows for longitude/azimuth\n");
186 spl_aux = (
float *) calloc(ncol,
sizeof (
float));
188 for (
i = 0;
i < nrow;
i++) {
189 for (l = 0; l < 3; l++) {
190 spline(xctl, in_ptr[l][0] +
i*ncol, ncol, 1e30, 1e30, spl_aux);
191 for (
j = 0;
j < nlon;
j++)
192 splint(xctl, in_ptr[l][0] +
i * ncol, spl_aux, ncol,
193 (
float)
j, tout_ptr[l][0] +
i *
npix +
j);
195 spline(xctl, in_ptr[l][1] +
i*ncol, ncol, 1e30, 1e30, spl_aux);
196 for (
j = 0;
j < nlon;
j++)
197 splint(xctl, in_ptr[l][1] +
i * ncol, spl_aux, ncol,
198 (
float)
j, tout_ptr[l][1] +
i *
npix +
j);
206 printf(
"Interpolating columns for longitude/azimuth\n");
207 spl_aux = (
float *) calloc(nrow,
sizeof (
float));
209 for (
i = 0;
i < nlon;
i++) {
210 for (l = 0; l < 3; l++) {
211 for (
k = 0;
k < nrow;
k++) {
212 in1[
k] = *(tout_ptr[l][0] +
k *
npix +
i);
213 in2[
k] = *(tout_ptr[l][1] +
k *
npix +
i);
215 spline(yctl, in1, nrow, 1e30, 1e30, spl_aux);
216 for (
j = 0;
j < nlat;
j++)
217 splint(yctl, in1, spl_aux, nrow, (
float)
j, (
float *) &out1[
j]);
219 spline(yctl, in2, nrow, 1e30, 1e30, spl_aux);
220 for (
j = 0;
j < nlat;
j++)
221 splint(yctl, in2, spl_aux, nrow, (
float)
j, (
float *) &out2[
j]);
223 for (
j = 0;
j < nlat;
j++) {
224 *(out_ptr[l][0] +
j *
npix +
i) = atan2(out2[
j], out1[
j]) *
RADEG;
225 if (l >= 1 && *(out_ptr[l][0] +
j *
npix +
i) < 0) {
226 *(out_ptr[l][0] +
j *
npix +
i) += 360;
234 printf(
"Interpolating rows for latitude/zenith\n");
235 spl_aux = (
float *) calloc(ncol,
sizeof (
float));
237 for (
i = 0;
i < nrow;
i++) {
238 for (l = 0; l < 3; l++) {
239 spline(xctl, in_ptr[l][2] +
i*ncol, ncol, 1e30, 1e30, spl_aux);
240 for (
j = 0;
j < nlon;
j++)
241 splint(xctl, in_ptr[l][2] +
i * ncol, spl_aux, ncol,
242 (
float)
j, tout_ptr[l][1] +
i *
npix +
j);
250 printf(
"Interpolating columns for latitude/zenith\n");
251 spl_aux = (
float *) calloc(nrow,
sizeof (
float));
253 for (
i = 0;
i < nlon;
i++) {
254 for (l = 0; l < 3; l++) {
255 for (
k = 0;
k < nrow;
k++) {
256 in1[
k] = *(tout_ptr[l][1] +
k *
npix +
i);
258 spline(yctl, in1, nrow, 1e30, 1e30, spl_aux);
259 for (
j = 0;
j < nlat;
j++)
260 splint(yctl, in1, spl_aux, nrow, (
float)
j, (
float *) &out1[
j]);
262 for (
j = 0;
j < nlat;
j++) {
263 *(out_ptr[l][1] +
j *
npix +
i) = asin(out1[
j]) *
RADEG;
306 static char months_list[12][4] ={
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
307 "AUG",
"SEP",
"OCT",
"NOV",
"DEC"};
308 size_t source_w, source_h;
310 int fileID, xid, yid, retval;
311 int minute, hour, month, sec,
i;
312 int year2, day2, msec2;
316 int16_t y16, m16, d16;
321 retval = nc_open(
file->name, NC_NOWRITE, &
fileID);
322 if (retval != NC_NOERR) {
323 fprintf(
stderr,
"-E- %s line %d: nc_open(%s) failed.\n",
324 __FILE__, __LINE__,
file->name);
329 retval = nc_inq_dimid(
fileID,
"x", &xid);
330 retval = nc_inq_dimid(
fileID,
"y", &yid);
331 retval = nc_inq_dimlen(
fileID, xid, &source_w);
332 retval = nc_inq_dimlen(
fileID, yid, &source_h);
335 printf(
"MERIS CC Npix :%d Nscans:%d\n", (
int) source_w, (
int) source_h);
337 npix = (int32_t) source_w;
338 nscan = (int32_t) source_h;
342 retval = nc_inq_attlen(
fileID, NC_GLOBAL,
"start_date", &att_len);
345 fltime = (
char *) malloc(att_len + 1);
348 retval = nc_get_att_text(
fileID, NC_GLOBAL,
"start_date", fltime);
350 sscanf(fltime,
"%02d-%3s-%04d %02d:%02d:%f", &
day, monthstr, &year,
351 &hour, &minute, &fsec);
354 for (
i = 0;
i < 12;
i++)
355 if (strncmp(monthstr, months_list[
i], 3) == 0)
358 milisec = (
float) (fsec - sec) * 1e3;
360 (int32_t *) & year, (int32_t *) &
day, (int32_t *) &
msec);
363 dblsec = hour * 3600 + minute * 60 + fsec;
371 retval = nc_inq_attlen(
fileID, NC_GLOBAL,
"stop_date", &att_len);
374 fltime = (
char *) malloc(att_len + 1);
375 prodtype = (
char *) malloc(att_len + 1);
378 retval = nc_get_att_text(
fileID, NC_GLOBAL,
"stop_date", fltime);
379 retval = nc_get_att_text(
fileID, NC_GLOBAL,
"product_type", prodtype);
381 sscanf(fltime,
"%02d-%3s-%04d %02d:%02d:%f", &day2, monthstr, &year2,
382 &hour, &minute, &fsec);
385 for (
i = 0;
i < 12;
i++)
386 if (strncmp(monthstr, months_list[
i], 3) == 0)
389 milisec = (
float) (fsec - sec) * 1e3;
391 (int32_t *) & year2, (int32_t *) & day2, (int32_t *) & msec2);
392 msec2 += (
int) milisec;
394 dblsec = hour * 3600 + minute * 60 + fsec;
406 if (strncmp(prodtype,
"MER_RR", 6) == 0)
411 retval = nc_inq_dimid(
file->sd_id,
"tp_x", &xid);
412 retval = nc_inq_dimlen(
file->sd_id, xid, &nctlpix);
415 retval = nc_inq_dimid(
file->sd_id,
"tp_y", &yid);
416 retval = nc_inq_dimlen(
file->sd_id, yid, &nctlscan);
419 lon = (
float *) calloc(
npix*nline,
sizeof (
float));
420 lat = (
float *) calloc(
npix*nline,
sizeof (
float));
421 senz = (
float *) calloc(
npix*nline,
sizeof (
float));
422 sena = (
float *) calloc(
npix*nline,
sizeof (
float));
423 solz = (
float *) calloc(
npix*nline,
sizeof (
float));
424 sola = (
float *) calloc(
npix*nline,
sizeof (
float));
435 static int firstCall = 1;
439 static char *l1_names[] = {
440 "radiance_1",
"radiance_2",
"radiance_3",
"radiance_4",
441 "radiance_5",
"radiance_6",
"radiance_7",
"radiance_8",
442 "radiance_9",
"radiance_10",
"radiance_11",
"radiance_12",
443 "radiance_13",
"radiance_14",
"radiance_15"
450 static float gain[15];
451 double recsec, sec70;
452 int16_t sh_year, sh_day;
458 printf(
"file->nbands = %d\n", (
int)
file->nbands);
461 for (ip = 0; ip <
npix; ip++)
l1rec->pixnum[ip] = ip;
464 retval = nc_inq_varid(
file->sd_id, l1_names[ib], &band_id);
465 if (retval != NC_NOERR) {
467 "-E- %s line %d: nc_inq_varid failed for file, %s band, %s.\n",
468 __FILE__, __LINE__,
file->name, l1_names[ib]);
472 retval = nc_get_att_float(
file->sd_id, band_id,
"scale_factor", &
gain[ib]);
473 if (retval != NC_NOERR) {
475 "-E- %s line %d: nc_get_att_float for file, %s attribute, %s.\n",
476 __FILE__, __LINE__,
file->name,
"scale_factor");
484 sec70 =
ymds2unix((int16_t) year, 1, (int16_t)
day, recsec);
485 unix2yds(sec70, &sh_year, &sh_day, &recsec);
486 l1rec->scantime = sec70;
499 rad_data = (int16_t *) calloc(
npix,
sizeof (int16_t));
506 for (ib = 0; ib <
nbands; ib++) {
508 retval = nc_inq_varid(
file->sd_id, l1_names[ib], &band_id);
509 retval = nc_get_vara_short(
file->sd_id, band_id,
start,
count, rad_data);
510 if (retval != NC_NOERR) {
512 "-E- %s line %d: nc_get_vara_short failed for file, %s field, %s.\n",
513 __FILE__, __LINE__,
file->name, l1_names[ib]);
519 l1rec->Lt[ipb] = rad_data[ip] *
gain[ib] / 10.0;
522 if (
l1rec->Lt[ipb] < 0.0)
523 l1rec->navfail[ip] = 1;
540 retval = nc_close(
file->sd_id);
541 if (retval != NC_NOERR) {
543 "-E- %s line %d: nc_close failed for file, %s.\n",
544 __FILE__, __LINE__,
file->name);