22 static int32 n_records;
24 static char pnames[
MAXCPROD][20] = {
"chlor_a",
"nLw_412",
"angstrom"};
25 static int loaded[
MAXCPROD] = {0, 0, 0};
31 static int nmonths = 12;
32 static int firstCall = 1;
33 static int clim_day [12] = {1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
34 static char clim_files[12][19] ={
"S001031_L3b_MC.hdf",
"S032059_L3b_MC.hdf",
"S060090_L3b_MC.hdf",
35 "S091120_L3b_MC.hdf",
"S121151_L3b_MC.hdf",
"S152181_L3b_MC.hdf",
36 "S182212_L3b_MC.hdf",
"S213243_L3b_MC.hdf",
"S244273_L3b_MC.hdf",
37 "S274304_L3b_MC.hdf",
"S305334_L3b_MC.hdf",
"S335365_L3b_MC.hdf"};
48 char file[FILENAME_MAX];
54 if (firstCall) printf(
"\nOpening L3 data file %s\n",
file);
58 for (
i = 1;
i < nmonths;
i++)
59 if (clim_day[
i] >
day)
63 if ((tmp_str = getenv(
"OCDATAROOT")) ==
NULL) {
64 printf(
"OCDATAROOT environment variable is not defined.\n");
68 strcat(
file,
"/common/");
69 strcat(
file, clim_files[
i]);
71 if (firstCall) printf(
"\nOpening climatology file %s\n",
file);
78 if ((fid = Hopen(
file, DFACC_RDONLY, 0)) < 0) {
79 printf(
"-E- %s line %d: Cannot open input HDF file on Hopen\n", __FILE__, __LINE__);
80 printf(
"-E- Level-3 data file - %s\n",
file);
84 if ((sdfid = SDstart(
file, DFACC_RDONLY)) < 0) {
85 printf(
"-E- %s line %d: Cannot open input HDF file on SDstart- %s\n", __FILE__, __LINE__,
file);
96 vg_ref = Vfind(fid,
"Level-3 Binned Data");
98 printf(
"-E- %s line %d: Cannot locate Level-3 Binned Data in %s\n", __FILE__, __LINE__,
file);
102 vgid = Vattach(fid, vg_ref,
"r");
108 for (
i = 0;
i < Vntagrefs(vgid);
i++) {
110 Vgettagref(vgid,
i, &tag, &
ref);
111 vdid = VSattach(fid,
ref,
"r");
114 printf(
"-E- %s line %d: Problem opening Vdata (reference # %d)\n", __FILE__, __LINE__,
ref);
118 VSgetname(vdid, nam_buf);
119 VSgetclass(vdid, cls_buf);
121 if (strcmp(cls_buf,
"DataMain") == 0) {
125 if (strcmp(cls_buf,
"Index") == 0) {
127 nrows = VSelts(vdata_id[1]);
130 if (strcmp(cls_buf,
"DataSubordinate") == 0) {
131 vdata_id[2 +
j++] = vdid;
150 if (vdata_id[
i] != -1) VSdetach(vdata_id[
i]);
167 int32 jl = -1, ju = n_records, jm = 0;
170 ascnd = (bins[n_records - 1] >= bins[0]);
171 while (ju - jl > 1) {
173 if (ascnd == (bin_num >= bins[jm]))
178 if (bin_num == bins[jl])
return (jl);
179 if (jl + 1 < n_records && bin_num == bins[jl + 1])
return (jl + 1);
180 if (jl > 0 && bin_num == bins[jl - 1])
return (jl - 1);
181 if (bin_num == bins[0])
return (0);
182 if (bin_num == bins[n_records - 1])
return (n_records - 1);
196 if ((numbin = (int32 *) calloc(nrows,
sizeof (int32))) ==
NULL) {
197 printf(
"-E- %s:%d Error allocating memory to numbin\n", __FILE__, __LINE__);
200 if ((basebin = (int32 *) calloc(nrows + 1,
sizeof (int32))) ==
NULL) {
201 printf(
"-E- %s:%d Error allocating memory to basebin\n", __FILE__, __LINE__);
205 for (row = 0; row < nrows; row++) {
206 latbin = ((row + 0.5)*180.0 / nrows) - 90.0;
207 numbin[row] = (int32) (2 * nrows * cos(latbin *
PI / 180.0) + 0.5);
209 basebin[row] = basebin[row - 1] + numbin[row - 1];
212 totbins = basebin[nrows - 1] + numbin[nrows - 1] - 1;
236 while (
lon < -180)
lon += 360;
237 while (
lon > 180)
lon -= 360;
247 row = (
int16) ((90 +
lat) * nrows / 180.0);
248 if (row >= nrows) row = nrows - 1;
260 col = (
int16) ((
lon + 180.0) * numbin[row] / 360.0);
261 if (col >= numbin[row]) col = numbin[row] - 1;
262 bin = basebin[row] + col;
278 col = (
int16) ((
lon + 180.0) * numbin[row] / 360.0);
279 if (col >= numbin[row]) col = numbin[row] - 1;
280 bin = basebin[row] + col;
288 static int firstCall = 1;
289 static char lastprod[20] =
"";
290 static int nofile = 0;
291 static int32 prodID = -1;
292 static int32 interlace0;
298 char vdata_name[H4_MAX_NC_NAME];
310 float32 *sum_buf =
NULL;
311 uint8 *bin_buf =
NULL;
314 if (strncmp(prodname, lastprod, 8) != 0 || prodID < 0) {
317 if (strncmp(prodname, pnames[
i], 8) == 0) {
322 printf(
"-E- %s line %d: Unknown climatology product\n", __FILE__, __LINE__);
332 printf(
"-E- Level-3 %s needed for the correction\n", pnames[prodID]);
338 status = VSsetfields(vdata_id[0],
"bin_num,nobs,weights,flags_set");
339 status = VSsetfields(vdata_id[1],
"start_num,begin,extent,max");
342 VSinquire(vdata_id[0], &n_records, &interlace0,
fields, &vdata_size, vdata_name);
343 if ((bins = (int32_t *) malloc(n_records *
sizeof (int32_t))) ==
NULL) {
344 printf(
"-E- %s line %d: Error allocating memory to L3 file bins\n", __FILE__, __LINE__);
349 vsize = 2 *
sizeof (int32) +
sizeof (float32) +
sizeof (
int16);
350 if ((bin_buf = (uint8 *) calloc(n_records, vsize)) ==
NULL) {
351 printf(
"-E- %s line %d: Error allocating memory for L3 file bin info\n", __FILE__, __LINE__);
356 nread = VSread(vdata_id[0], (uint8 *) bin_buf, n_records, interlace0);
358 printf(
"-E- %s line %d: Unable to read bin numbers.\n", __FILE__, __LINE__);
363 for (
i = 0;
i < n_records;
i++) {
364 memcpy((
void *) &bins[
i], (
const void *) &bin_buf[
i * vsize], 4);
373 if (nofile)
return (-999.0);
375 if (!loaded[prodID]) {
380 printf(
"Loading %s coverage.\n", pnames[prodID]);
382 printf(
"Loading %s climatology.\n", pnames[prodID]);
386 printf(
"-E- %s line %d: Error opening climatology file\n", __FILE__, __LINE__);
396 if (vdata_id[
i] == -1) nprod--;
400 printf(
"-E- %s: There are too few products in the L3 file\n", __FILE__);
404 status = VSsetfields(vdata_id[0],
"bin_num,nobs,weights,flags_set");
405 status = VSsetfields(vdata_id[1],
"start_num,begin,extent,max");
408 for (
i = 0;
i < nprod;
i++) {
409 VSgetname(vdata_id[2 +
i],
fields);
410 if (strncmp(
fields, pnames[prodID], 8) == 0) {
413 strcat(buf,
"_sum,");
415 strcat(buf,
"_sum_sq");
416 status = VSsetfields(vdata_id[2 +
i], buf);
418 printf(
"-E- %s: Failed to set field for %s.\n", __FILE__, pnames[prodID]);
425 printf(
"-E- %s: Failed to locate %s.\n", __FILE__, pnames[prodID]);
430 VSinquire(vdata_id[2 + prodinx], &n_records, &interlace,
fields, &vdata_size, vdata_name);
431 if ((
data[prodID] = (float32 *) malloc(n_records *
sizeof (float32))) ==
NULL) {
432 printf(
"-E- %s: Error allocating memory to L3 product %s\n", __FILE__, prodname);
437 if ((sum_buf = (float32 *) calloc(2 * n_records,
sizeof (float32))) ==
NULL) {
438 printf(
"-E- %s: Error allocating memory to L3 file products\n", __FILE__);
443 vsize = 2 *
sizeof (int32) +
sizeof (float32) +
sizeof (
int16);
444 if ((bin_buf = (uint8 *) calloc(n_records, vsize)) ==
NULL) {
445 printf(
"-E- %s: Error allocating memory for L3 file bin info\n", __FILE__);
450 nread = VSread(vdata_id[0], (uint8 *) bin_buf, n_records, interlace0);
452 printf(
"-E- %s line %d: Unable to read bin numbers.\n", __FILE__, __LINE__);
461 nread = VSread(vdata_id[2 + prodinx], (uint8 *) sum_buf, n_records, interlace);
463 printf(
"-E- %s line %d: Unable to read sum/sum_sq\n", __FILE__, __LINE__);
467 for (
i = 0;
i < n_records;
i++) {
468 memcpy((
void *) &wgt, (
const void *) &bin_buf[
i * vsize + 6], 4);
469 data[prodID][
i] = (float32) sum_buf[2 *
i] / wgt;
489 if (bin_idx >= 0)
return (
data[prodID][bin_idx]);
490 else return (-999.0);