17 using namespace netCDF;
18 using namespace netCDF::exceptions;
20 #define VERSION "0.045"
36 int main (
int argc,
char* argv[])
39 cout <<
"l1bgen_oci " <<
VERSION <<
" ("
40 << __DATE__ <<
" " << __TIME__ <<
")" << endl;
44 "l1bgen_oci OCI_L1A_file, cal_LUT_file temp_GEO_LUT_file OCI_L1B_file"
52 NcFile *calLUTfile =
new NcFile( argv[2], NcFile::read);
54 NcGroup gidCommon, gidBlue, gidRed, gidSWIR;
55 gidCommon = calLUTfile->getGroup(
"common");
56 gidBlue = calLUTfile->getGroup(
"blue");
57 gidRed = calLUTfile->getGroup(
"red");
58 gidSWIR = calLUTfile->getGroup(
"SWIR");
63 float spass[
NIWAVE] = {45, 80, 30, 30, 15, 75, 75, 50, 75};
69 var = gidCommon.getVar(
"blue_wavelength");
72 var = gidCommon.getVar(
"red_wavelength");
75 var = gidCommon.getVar(
"SWIR_wavelength");
78 var = gidCommon.getVar(
"K2t");
81 var = gidCommon.getVar(
"K3T");
90 uint32_t bbanddim, rbanddim, sbanddim;
102 float hysttime[9][3];
105 var = gidSWIR.getVar(
"hyst_time_const");
106 var.getVar( &hysttime[0][0]);
107 var = gidSWIR.getVar(
"hyst_amplitude");
108 var.getVar( &hystamp[0][0]);
118 NcFile *geoLUTfile =
new NcFile( argv[3], NcFile::read);
121 NcGroup gidTime, gidCT, gidRTA_HAM;
123 gidTime = geoLUTfile->getGroup(
"time_params");
124 var = gidTime.getVar(
"master_clock");
126 var = gidTime.getVar(
"MCE_clock");
129 gidCT = geoLUTfile->getGroup(
"coord_trans");
130 var = gidCT.getVar(
"sc_to_tilt");
132 var = gidCT.getVar(
"tilt_axis");
134 var = gidCT.getVar(
"tilt_angles");
136 var = gidCT.getVar(
"tilt_to_oci_mech");
138 var = gidCT.getVar(
"oci_mech_to_oci_opt");
141 gidRTA_HAM = geoLUTfile->getGroup(
"RTA_HAM_params");
142 var = gidRTA_HAM.getVar(
"RTA_axis");
144 var = gidRTA_HAM.getVar(
"HAM_axis");
146 var = gidRTA_HAM.getVar(
"HAM_AT_angles");
148 var = gidRTA_HAM.getVar(
"HAM_CT_angles");
150 var = gidRTA_HAM.getVar(
"RTA_enc_scale");
152 var = gidRTA_HAM.getVar(
"HAM_enc_scale");
154 var = gidRTA_HAM.getVar(
"RTA_nadir");
160 NcFile *l1afile =
new NcFile( argv[1], NcFile::read);
164 ncGrps[0] = l1afile->getGroup(
"scan_line_attributes");
165 ncGrps[1] = l1afile->getGroup(
"spatial_spectral_modes");
166 ncGrps[2] = l1afile->getGroup(
"engineering_data");
167 ncGrps[3] = l1afile->getGroup(
"science_data");
173 att = l1afile->getAtt(
"time_coverage_start");
174 att.getValues(tstart);
175 cout << tstart << endl;
177 att = l1afile->getAtt(
"time_coverage_end");
179 cout << tend << endl;
184 iss.str(tstart.substr(0,4));
185 iss >>
iyr; iss.clear();
186 iss.str(tstart.substr(5,2));
187 iss >>
imn; iss.clear();
188 iss.str(tstart.substr(8,2));
192 int32_t iyr32, idy32;
196 NcDim blue_dim = l1afile->getDim(
"blue_bands");
197 uint32_t bbands = blue_dim.getSize();
198 NcDim red_dim = l1afile->getDim(
"red_bands");
199 uint32_t rbands = red_dim.getSize();
202 NcDim nscan_dim = l1afile->getDim(
"number_of_scans");
203 uint32_t
nscan = nscan_dim.getSize();
205 double *sstime =
new double[
nscan];
206 var = ncGrps[0].getVar(
"scan_start_time");
210 var = ncGrps[0].getVar(
"spin_ID");
213 uint8_t *hside =
new uint8_t[
nscan];
214 var = ncGrps[0].getVar(
"HAM_side");
217 uint32_t nscan_good=0;
220 sstime[nscan_good] = sstime[
i];
221 hside[nscan_good] = hside[
i];
233 NcDim ntaps_dim = l1afile->getDim(
"number_of_taps");
234 uint32_t ntaps = ntaps_dim.getSize();
235 NcDim spatzn_dim = l1afile->getDim(
"spatial_zones");
236 uint32_t spatzn = spatzn_dim.getSize();
238 int16_t *
dtype =
new int16_t[spatzn];
239 var = ncGrps[1].getVar(
"spatial_zone_data_type");
242 int16_t *
lines =
new int16_t[spatzn];
243 var = ncGrps[1].getVar(
"spatial_zone_lines");
246 int16_t *iagg =
new int16_t[spatzn];
247 var = ncGrps[1].getVar(
"spatial_aggregation");
250 int16_t *bagg =
new int16_t[ntaps];
251 var = ncGrps[1].getVar(
"blue_spectral_mode");
254 int16_t *ragg =
new int16_t[ntaps];
255 var = ncGrps[1].getVar(
"red_spectral_mode");
264 double revpsec, secpline;
265 int32_t *mspin =
new int32_t[
nscan];
266 int32_t *ot_10us =
new int32_t[
nscan];
267 uint8_t *enc_count =
new uint8_t[
nscan];
269 NcDim nenc_dim = l1afile->getDim(
"encoder_samples");
270 uint32_t nenc = nenc_dim.getSize();
272 float **hamenc =
new float *[
nscan];
273 hamenc[0] =
new float[nenc*
nscan];
274 for (
size_t i=1;
i<
nscan;
i++) hamenc[
i] = hamenc[
i-1] + nenc;
276 float **rtaenc =
new float *[
nscan];
277 rtaenc[0] =
new float[nenc*
nscan];
278 for (
size_t i=1;
i<
nscan;
i++) rtaenc[
i] = rtaenc[
i-1] + nenc;
281 mspin, ot_10us, enc_count, &hamenc[0], rtaenc);
283 float clines[32400], slines[4050];
284 uint16_t pcdim, psdim;
286 double ev_toff, deltc[32400], delts[4050];
287 get_ev( secpline,
dtype,
lines, iagg, pcdim, psdim, ev_toff, clines, slines,
290 cout <<
"No science collect in file: " << argv[1] << endl;
295 double *evtime =
new double[
nscan];
296 for (
size_t i=0;
i<nscan_good;
i++) evtime[
i] = sstime[
i] + ev_toff;
299 for (
size_t i=0;
i<spatzn;
i++) {
313 ia =
new size_t[ntaps];
317 for (
size_t i=0;
i<ntaps;
i++) {
324 uint32_t bib=1, bbb=1;
326 cout <<
"All blue taps disabled" << endl;
328 for (
size_t i=0;
i<16;
i++) ntb[
i] = 0;
329 for (
size_t i=0;
i<iia;
i++) ntb[ia[
i]] = 32 / bagg[ia[
i]];
331 for (
size_t i=0;
i<16;
i++) bib += ntb[
i];
335 float **bamat =
new float*[bib];
336 float **bgmat =
new float*[512];
339 get_agg_mat( ia, iagg[ka], bagg, bib, bbb, ntb, bamat, bgmat);
342 cout <<
"Number of blue bands in file: " << argv[1] <<
343 " not consistent with spectral aggregation" << endl;
346 }
else if ( bib < 4) cout <<
"No blue bands in file: " << argv[1] << endl;
350 for (
size_t i=0;
i<ntaps;
i++) {
357 uint32_t rib=1, rbb=1;
359 cout <<
"All red taps disabled" << endl;
361 for (
size_t i=0;
i<16;
i++) ntb[
i] = 0;
362 for (
size_t i=0;
i<iia;
i++) ntb[ia[
i]] = 32 / ragg[ia[
i]];
364 for (
size_t i=0;
i<16;
i++) rib += ntb[
i];
367 float **ramat =
new float*[rib];
368 float **rgmat =
new float*[512];
371 get_agg_mat( ia, iagg[ka], ragg, rib, rbb, ntb, ramat, rgmat);
374 cout <<
"Number of red bands in file: " << argv[1] <<
375 " not consistent with spectral aggregation" << endl;
378 }
else if ( rib < 4) cout <<
"No red bands in file: " << argv[1] << endl;
387 for (
size_t i=0;
i<spatzn;
i++) {
393 cout <<
"No dark collect in file: " << argv[1] << endl;
398 int16_t ldc=0, lds=0;
399 for (
size_t i=0;
i<(size_t) kd;
i++) {
405 int16_t ndc =
lines[kd] / iagg[kd];
406 int16_t nds =
lines[kd] / 8;
430 float **sgmat =
new float*[swb];
431 for (
size_t i=0;
i<swb;
i++) {
432 sgmat[
i] =
new float[swb];
433 for (
size_t j=0;
j<swb;
j++) {
434 if (
i ==
j) sgmat[
i][
j] = 1.0;
else sgmat[
i][
j] = 0.0;
441 float **caltemps =
new float *[nscan_good];
442 caltemps[0] =
new float[30*nscan_good];
443 for (
size_t i=1;
i<nscan_good;
i++) caltemps[
i] = caltemps[
i-1] + 30;
446 for (
size_t i=0;
i<nscan_good;
i++)
447 for (
size_t j=1;
j<30;
j++)
448 caltemps[
i][
j] = 0.0;
454 start.push_back(ldc);
457 dims[0] = nscan_good; dims[1] = bib; dims[2] = ndc;
458 uint16_t ***bdark = make3dT<uint16_t>(dims);
459 dims[0] = nscan_good; dims[1] = rib; dims[2] = ndc;
460 uint16_t ***rdark = make3dT<uint16_t>(dims);
466 count.push_back(nscan_good);
467 count.push_back(bib);
468 count.push_back(ndc);
470 var = ncGrps[3].getVar(
"sci_blue");
473 var.getAtt(
"_FillValue").getValues(&bfill);
478 count.push_back(nscan_good);
479 count.push_back(rib);
480 count.push_back(ndc);
482 var = ncGrps[3].getVar(
"sci_red");
485 var.getAtt(
"_FillValue").getValues(&rfill);
488 dims[0] = nscan_good; dims[1] = swb; dims[2] = nds;
489 uint32_t ***sdark = make3dT<uint32_t>(dims);
496 start.push_back(lds);
499 count.push_back(nscan_good);
500 count.push_back(swb);
501 count.push_back(nds);
503 var = ncGrps[3].getVar(
"sci_SWIR");
506 var.getAtt(
"_FillValue").getValues(&sfill);
518 outfile.
createl1b( (
char *) argv[4], nscan_good, pcdim, bbb, rbb, psdim, swb);
527 uint32_t bicount[3] = {1,bib,(uint32_t) pcdim};
528 uint32_t ricount[3] = {1,rib,(uint32_t) pcdim};
529 uint32_t sicount[3] = {1,swb,(uint32_t) psdim};
530 uint32_t bcount[3] = {bbb,1,(uint32_t) pcdim};
531 uint32_t rcount[3] = {rbb,1,(uint32_t) pcdim};
532 uint32_t scount[3] = {swb,1,(uint32_t) psdim};
535 float **bdn =
new float *[bib];
536 bdn[0] =
new float[pcdim*bib];
537 for (
size_t i=1;
i<bib;
i++) bdn[
i] = bdn[
i-1] + pcdim;
539 float **rdn =
new float *[rib];
540 rdn[0] =
new float[pcdim*rib];
541 for (
size_t i=1;
i<rib;
i++) rdn[
i] = rdn[
i-1] + pcdim;
543 float **sdn =
new float *[swb];
544 sdn[0] =
new float[psdim*swb];
545 for (
size_t i=1;
i<swb;
i++) sdn[
i] = sdn[
i-1] + psdim;
547 float **bcal =
new float *[bib];
548 bcal[0] =
new float[pcdim*bib];
549 for (
size_t i=1;
i<bib;
i++) bcal[
i] = bcal[
i-1] + pcdim;
551 float **rcal =
new float *[rib];
552 rcal[0] =
new float[pcdim*rib];
553 for (
size_t i=1;
i<rib;
i++) rcal[
i] = rcal[
i-1] + pcdim;
555 float **scal =
new float *[swb];
556 scal[0] =
new float[psdim*swb];
557 for (
size_t i=1;
i<swb;
i++) scal[
i] = scal[
i-1] + psdim;
559 double *thetap =
new double[pcdim];
560 double *thetas =
new double[psdim];
562 float **pview =
new float *[pcdim];
563 pview[0] =
new float[3*pcdim];
564 for (
size_t i=1;
i<pcdim;
i++) pview[
i] = pview[
i-1] + 3;
566 float **sview =
new float *[psdim];
567 sview[0] =
new float[3*psdim];
568 for (
size_t i=1;
i<psdim;
i++) sview[
i] = sview[
i-1] + 3;
574 for (
size_t iscn=0; iscn<nscan_good; iscn++) {
576 if ((iscn % 50) == 0) cout <<
"Calibrating scan: " << iscn << endl;
579 if ( hside[iscn] == 0 || hside[iscn] == 1) {
583 start.push_back(iscn);
588 var = outfile.
ncGrps[1].getVar(
"time");
591 var = outfile.
ncGrps[1].getVar(
"HAM_side");
597 revpsec, ppr_off, mspin, ot_10us, enc_count, &hamenc[0],
598 &rtaenc[0], pview, thetap, iret);
601 tempOut.write((
char *) &pview[0][0],
sizeof(
float)*3*pcdim);
605 tempOut.write((
char *) &thetap[0],
sizeof(
double)*pcdim);
609 revpsec, ppr_off, mspin, ot_10us, enc_count, &hamenc[0],
610 &rtaenc[0], sview, thetas, iret);
613 tempOut.write((
char *) &sview[0][0],
sizeof(
float)*3*psdim);
617 tempOut.write((
char *) &thetas[0],
sizeof(
double)*psdim);
624 start.push_back(iscn);
629 count.push_back(bicount[0]);
630 count.push_back(bicount[1]);
631 count.push_back(bicount[2]);
633 uint16_t **bsci =
new uint16_t *[bib];
634 bsci[0] =
new uint16_t[pcdim*bib];
635 for (
size_t i=1;
i<bib;
i++) bsci[
i] = bsci[
i-1] + pcdim;
637 var = ncGrps[3].getVar(
"sci_blue");
641 tempOut.write((
char *) bsci[0],
sizeof(uint16_t)*pcdim*bib);
646 float *bdc =
new float[bib];
650 get_oci_dark<uint16_t>( iscn, nscan_good, hside, ndsc, nskp,
651 iagg[ka], iagg[kd], ntaps, bagg, fill32,
652 ndc, bdark, bib, bdc, iret);
654 float *k3 =
new float[bib];
656 for (
size_t j=0;
j<bib;
j++) {
657 for (
size_t k=0;
k<pcdim;
k++) {
660 if (bsci[
j][
k] == bfill) {
667 bdn[
j][
k] = bsci[
j][
k] - bdc[
j];
668 bcal[
j][
k] = k3[
j] * bgains.
K1K2[
j][hside[iscn]] * bdn[
j][
k];
677 float **k4 =
new float *[bib];
678 k4[0] =
new float[pcdim*bib];
679 for (
size_t i=1;
i<bib;
i++) k4[
i] = k4[
i-1] + pcdim;
683 float **k5 =
new float *[bib];
684 k5[0] =
new float[pcdim*bib];
685 for (
size_t i=1;
i<bib;
i++) k5[
i] = k5[
i-1] + pcdim;
689 for (
size_t j=0;
j<bib;
j++) {
690 for (
size_t k=0;
k<pcdim;
k++) {
691 bcal[
j][
k] *= k4[
j][
k] * k5[
j][
k];
700 float **bcalb =
new float *[bib];
701 bcalb[0] =
new float[pcdim*bib];
702 for (
size_t i=1;
i<bib;
i++) bcalb[
i] = bcalb[
i-1] + pcdim;
706 for (
size_t j=0;
j<bib;
j++) {
707 for (
size_t k=0;
k<pcdim;
k++) {
709 for (
size_t l=0; l<bib; l++)
710 if (bcal[l][
k] != -999) sum += bamat[
j][l]*bcal[l][
k];
717 start.push_back(iscn);
721 count.push_back(bcount[0]);
722 count.push_back(bcount[1]);
723 count.push_back(bcount[2]);
726 var = outfile.
ncGrps[4].getVar(
"Lt_blue");
741 start.push_back(iscn);
746 count.push_back(ricount[0]);
747 count.push_back(ricount[1]);
748 count.push_back(ricount[2]);
750 uint16_t **rsci =
new uint16_t *[rib];
751 rsci[0] =
new uint16_t[pcdim*rib];
752 for (
size_t i=1;
i<rib;
i++) rsci[
i] = rsci[
i-1] + pcdim;
754 var = ncGrps[3].getVar(
"sci_red");
758 tempOut.write((
char *) rsci[0],
sizeof(uint16_t)*pcdim*rib);
763 float *rdc =
new float[rib];
766 get_oci_dark<uint16_t>( iscn, nscan_good, hside, ndsc, nskp,
767 iagg[ka], iagg[kd], ntaps, ragg, fill32,
768 ndc, rdark, rib, rdc, iret);
771 tempOut.write((
char *) rdc,
sizeof(
float)*rib);
773 float *k3 =
new float[rib];
776 tempOut.write((
char *) k3,
sizeof(
float)*rib);
778 for (
size_t j=0;
j<rib;
j++) {
779 for (
size_t k=0;
k<pcdim;
k++) {
782 if (rsci[
j][
k] == rfill) {
789 rdn[
j][
k] = rsci[
j][
k] - rdc[
j];
790 rcal[
j][
k] = k3[
j] * rgains.
K1K2[
j][hside[iscn]] * rdn[
j][
k];
794 tempOut.write((
char *) rdn[0],
sizeof(
float)*pcdim*rib);
802 float **k4 =
new float *[rib];
803 k4[0] =
new float[pcdim*rib];
804 for (
size_t i=1;
i<rib;
i++) k4[
i] = k4[
i-1] + pcdim;
807 tempOut.write((
char *) k4[0],
sizeof(
float)*pcdim*rib);
810 float **k5 =
new float *[rib];
811 k5[0] =
new float[pcdim*rib];
812 for (
size_t i=1;
i<rib;
i++) k5[
i] = k5[
i-1] + pcdim;
816 tempOut.write((
char *) k5[0],
sizeof(
float)*pcdim*rib);
818 for (
size_t j=0;
j<rib;
j++) {
819 for (
size_t k=0;
k<pcdim;
k++) {
820 rcal[
j][
k] *= k4[
j][
k] * k5[
j][
k];
825 tempOut.write((
char *) rcal[0],
sizeof(
float)*pcdim*rib);
833 float **rcalb =
new float *[rib];
834 rcalb[0] =
new float[pcdim*rib];
835 for (
size_t i=1;
i<rib;
i++) rcalb[
i] = rcalb[
i-1] + pcdim;
838 for (
size_t j=0;
j<rib;
j++) {
839 for (
size_t k=0;
k<pcdim;
k++) {
841 for (
size_t l=0; l<rib; l++)
842 if (rcal[l][
k] != -999) sum += ramat[
j][l]*rcal[l][
k];
848 tempOut.write((
char *) rcalb[0],
sizeof(
float)*pcdim*rib);
853 start.push_back(iscn);
857 count.push_back(rcount[0]);
858 count.push_back(rcount[1]);
859 count.push_back(rcount[2]);
862 var = outfile.
ncGrps[4].getVar(
"Lt_red");
873 start.push_back(iscn);
878 count.push_back(sicount[0]);
879 count.push_back(sicount[1]);
880 count.push_back(sicount[2]);
882 uint32_t **ssci =
new uint32_t *[swb];
883 ssci[0] =
new uint32_t[psdim*swb];
884 for (
size_t i=1;
i<swb;
i++) ssci[
i] = ssci[
i-1] + psdim;
886 var = ncGrps[3].getVar(
"sci_SWIR");
891 float *sdc =
new float[swb];
895 get_oci_dark<uint32_t>( iscn, nscan_good, hside, ndsc, nskp,
896 1, 1, 1, &sagg, sfill, nds, sdark,
899 float *k3 =
new float[swb];
903 for (
size_t j=0;
j<swb;
j++) {
904 for (
size_t k=0;
k<psdim;
k++) {
907 if (ssci[
j][
k] == sfill) {
914 sdn[
j][
k] = ssci[
j][
k] - sdc[
j];
920 for (
size_t l=0; l<3; l++) {
922 float e = exp(-1.0/hysttime[
j][l]);
925 hc[l] = hc_prev[l]*e + sdn[
j][
k-1]*hystamp[
j][l];
932 k3[
j] * sgains.
K1K2[
j][hside[iscn]] * (sdn[
j][
k] - hyst);
941 float **k4 =
new float *[swb];
942 k4[0] =
new float[psdim*swb];
943 for (
size_t i=1;
i<swb;
i++) k4[
i] = k4[
i-1] + psdim;
947 float **k5 =
new float *[swb];
948 k5[0] =
new float[psdim*swb];
949 for (
size_t i=1;
i<swb;
i++) k5[
i] = k5[
i-1] + psdim;
954 for (
size_t j=0;
j<swb;
j++) {
955 for (
size_t k=0;
k<psdim;
k++) {
956 if (scal[
j][
k] != -999)
957 scal[
j][
k] *= k4[
j][
k] * k5[
j][
k];
968 start.push_back(iscn);
972 count.push_back(scount[0]);
973 count.push_back(scount[1]);
974 count.push_back(scount[2]);
977 var = outfile.
ncGrps[4].getVar(
"Lt_SWIR");
984 cout <<
"No mirror side index for scan: " << iscn << endl;
996 float *b1bwave =
new float[bib];
997 float *sum =
new float[bib];
998 for (
size_t i=0;
i<bib;
i++) {
1000 for (
size_t j=0;
j<512;
j++) {
1001 sum[
i] += bwave[
j]*bgmat[
j][
i];
1006 for (
size_t i=0;
i<bib;
i++) {
1008 for (
size_t j=0;
j<bib;
j++) {
1009 b1bwave[
i] += bamat[
i][
j]*sum[
j];
1017 count.push_back(bbands);
1018 var = outfile.
ncGrps[0].getVar(
"blue_wavelength");
1026 float *r1bwave =
new float[rib];
1027 float *sum =
new float[rib];
1028 for (
size_t i=0;
i<rib;
i++) {
1030 for (
size_t j=0;
j<512;
j++) {
1031 sum[
i] += rwave[
j]*rgmat[
j][
i];
1035 for (
size_t i=0;
i<rib;
i++) {
1037 for (
size_t j=0;
j<rib;
j++) {
1038 r1bwave[
i] += ramat[
i][
j]*sum[
j];
1046 count.push_back(rbands);
1047 var = outfile.
ncGrps[0].getVar(
"red_wavelength");
1060 var = outfile.
ncGrps[0].getVar(
"SWIR_wavelength");
1062 var = outfile.
ncGrps[0].getVar(
"SWIR_bandpass");
1066 string l1b_filename;
1067 l1b_filename.assign(argv[4]);
1082 delete [] enc_count;
1089 delete [] hamenc[0];
1091 delete [] rtaenc[0];
1093 delete [] caltemps[0];
1112 delete [] blue_lut.
K1[0];
1113 delete [] blue_lut.
K1;
1114 delete [] blue_lut.
K5[0];
1115 delete [] blue_lut.
K5;
1119 if (bgains.
K5 !=
NULL)
delete [] bgains.
K5[0];
1120 if (bgains.
K5 !=
NULL)
delete [] bgains.
K5;
1124 delete [] red_lut.
K1[0];
1125 delete [] red_lut.
K1;
1126 delete [] red_lut.
K5[0];
1127 delete [] red_lut.
K5;
1131 if (rgains.
K5 !=
NULL)
delete [] rgains.
K5[0];
1132 if (rgains.
K5 !=
NULL)
delete [] rgains.
K5;
1134 delete [] swir_lut.
K1[0];
1135 delete [] swir_lut.
K1;
1136 delete [] swir_lut.
K5[0];
1137 delete [] swir_lut.
K5;
1139 delete [] sgains.
K1K2[0];
1140 delete [] sgains.
K1K2;
1141 delete [] sgains.
K5[0];
1142 delete [] sgains.
K5;
1150 uint32_t
nscan, uint32_t nenc, int32_t& ppr_off,
1151 double& revpsec,
double&secpline, int32_t *mspin,
1152 int32_t *ot_10us, uint8_t *enc_count,
1153 float **hamenc,
float **rtaenc) {
1155 NcDim mce_dim = l1afile->getDim(
"MCE_block");
1156 uint32_t mce_blk = mce_dim.getSize();
1157 NcDim ddc_dim = l1afile->getDim(
"DDC_tlm");
1158 uint32_t nddc = ddc_dim.getSize();
1159 NcDim tlm_dim = l1afile->getDim(
"tlm_packets");
1160 uint32_t ntlm = tlm_dim.getSize();
1162 uint8_t **mtlm =
new uint8_t *[
nscan];
1163 mtlm[0] =
new uint8_t[mce_blk*
nscan];
1164 for (
size_t i=1;
i<
nscan;
i++) mtlm[
i] = mtlm[
i-1] + mce_blk;
1166 int16_t **enc =
new int16_t *[
nscan];
1167 enc[0] =
new int16_t[4*nenc*
nscan];
1168 for (
size_t i=1;
i<
nscan;
i++) enc[
i] = enc[
i-1] + 4*nenc;
1170 uint8_t **ddctlm =
new uint8_t *[ntlm];
1171 ddctlm[0] =
new uint8_t[nddc*ntlm];
1172 for (
size_t i=1;
i<ntlm;
i++) ddctlm[
i] = ddctlm[
i-1] + nddc;
1196 var = egid.getVar(
"MCE_telemetry");
1197 count.push_back(mce_blk);
1198 var.getVar( &mtlm[0][0]);
1200 var = egid.getVar(
"MCE_encoder_data");
1202 count.push_back(4*nenc);
1203 var.getVar( &enc[0][0]);
1205 var = egid.getVar(
"MCE_spin_ID");
1208 var = egid.getVar(
"DDC_telemetry");
1209 var.getVar( &ddctlm[0][0]);
1211 int32_t max_enc_cts = 131072;
1212 double clock[2] = {1.36e8,1.0e8};
1216 uint32_t ref_pulse_div[2];
1217 memcpy( &ui32, &mtlm[0][0], 4);
1218 ref_pulse_div[0] =
SWAP_4( ui32) % 16777216;
1219 memcpy( &ui32, &mtlm[0][4], 4);
1220 ref_pulse_div[1] =
SWAP_4( ui32) % 16777216;
1222 int32_t ref_pulse_sel = mtlm[0][9] / 128;
1224 revpsec = clock[ref_pulse_sel] / 2 / max_enc_cts /
1225 (ref_pulse_div[ref_pulse_sel]/256.0 + 1);
1228 memcpy( &ui32, &mtlm[0][8], 4);
1229 ppr_off =
SWAP_4( ui32) % max_enc_cts;
1231 memcpy( &ui32, &mtlm[
i][368], 4);
1232 ot_10us[
i] =
SWAP_4( ui32) % 4;
1237 memcpy( &ui16, &ddctlm[0][346], 2);
1238 int32_t tditime =
SWAP_2( ui16);
1239 secpline = (tditime+1) / clock[0];
1242 for (
size_t i=0;
i<
nscan;
i++) enc_count[
i] = mtlm[
i][475];
1243 float enc_s = 81.0 / 2560;
1245 for (
size_t j=0;
j<nenc;
j++) {
1246 hamenc[
i][
j] = enc[
i][4*
j+0] * enc_s;
1247 rtaenc[
i][
j] = enc[
i][4*
j+1] * enc_s;
1257 delete [] ddctlm[0];
1264 uint16_t& pcdim, uint16_t& psdim,
double& ev_toff,
1265 float *clines,
float *slines,
double *deltc,
double *delts,
1269 int16_t iz=0, line0=0, linen;
1271 while (
dtype[iz] == 0) {
1275 if (iz == 10)
return 0;
1281 for (
size_t i=iz;
i<9;
i++) {
1285 uint16_t np =
lines[
i] / iagg[
i];
1286 for (
size_t j=0;
j<np;
j++) {
1287 clines[pcdim+
j] = linen +
j*iagg[
i] + 0.5*(iagg[
i]-1);
1290 uint16_t ns =
lines[
i] / 8;
1291 for (
size_t j=0;
j<ns;
j++) {
1292 slines[psdim+
j] = linen +
j*8 + 3.5;
1301 for (
size_t i=0;
i<(size_t) pcdim;
i++) deltc[
i] = secpline * clines[
i];
1302 ev_toff = 0.5 * (deltc[0] + deltc[pcdim-1]);
1303 for (
size_t i=0;
i<(size_t) pcdim;
i++) deltc[
i] -= ev_toff;
1305 for (
size_t i=0;
i<(size_t) psdim;
i++)
1306 delts[
i] = secpline * slines[
i] - ev_toff;
1312 int get_agg_mat(
size_t *ia, int16_t iagg, int16_t jagg[16], uint16_t nib,
1313 uint32_t& nbb, uint32_t ntb[16],
float **amat,
float **gmat) {
1317 for (
size_t i=0;
i<512;
i++) {
1318 gmat[
i] =
new float[nib];
1319 for (
size_t j=0;
j<nib;
j++) gmat[
i][
j] = 0.0;
1324 for (
size_t i=0;
i<16;
i++) {
1327 if ( iagg*jagg[
i] < iaf) iaf = iagg*jagg[
i];
1328 for (
size_t k=0;
k<ntb[
i];
k++) {
1330 size_t kj =
k*jagg[
i];
1331 for (
size_t j=0;
j<(size_t) jagg[
i];
j++)
1332 gmat[ic+kj+
j][ii+
k] = (1.0/jagg[
i]) * (4/iaf);
1341 nbb = (ntb[0]*3) / 4 + 1;
1343 for (
size_t i=1;
i<16;
i++) {
1344 if (jagg[
i] >= jagg[
i-1])
1347 nbb += (ntb[
i]*3) / 4 + ntb[
i-1]/4;
1350 for (
size_t i=0;
i<nib;
i++) {
1351 amat[
i] =
new float[nbb];
1352 for (
size_t j=0;
j<nbb;
j++) amat[
i][
j] = 0;
1357 for (
size_t k=0;
k<=(ntb[ia[0]]*3)/4;
k++) {
1358 amat[
k+ntb[ia[0]]/4-1][
k] = jagg[ia[0]]/8.0;
1360 ib = (ntb[ia[0]]*3)/4 + 1;
1364 for (
size_t i=ia[1];
i<16;
i++) {
1366 if (ntb[
i] >= ntb[
i-1]) {
1368 nr = ntb[
i-1]/4 - 1;
1371 for (
size_t k=0;
k<nr;
k++) {
1372 uint16_t k1 = nr -
k - 1;
1373 uint16_t k2 = ((
k+1)*ntb[
i]) / ntb[
i-1] - 1;
1374 for (
size_t j=0;
j<k1;
j++)
1375 amat[itt[
i-1][1]-k1+
j][ib+
k] = jagg[
i-1] / 8.0;
1376 for (
size_t j=0;
j<k2;
j++)
1377 amat[itt[
i][0]+
j][ib+
k] = jagg[
i] / 8.0;
1386 for (
size_t k=0;
k<nr;
k++) {
1387 uint16_t k1 = ((nr-
k)*ntb[
i-1]) / ntb[
i] - 1;
1389 for (
size_t j=0;
j<k1;
j++)
1390 amat[itt[
i-1][1]-k1+
j][ib+
k] = jagg[
i-1] / 8.0;
1391 for (
size_t j=0;
j<k2;
j++)
1392 amat[itt[
i][0]+
j][ib+
k] = jagg[
i] / 8.0;
1398 for (
size_t k=0;
k<=(ntb[
i]*3)/4;
k++) {
1399 for (
size_t j=0;
j<ntb[
i]/4;
j++)
1400 amat[itt[
i][0]+
k+
j][ib+
k] = jagg[
i] / 8.0;
1402 ib += (ntb[
i]*3) / 4 + 1;
1414 uint32_t timedim, tempdim, tcdim, rvsdim, nldim, msdim=2;
1416 bandname.assign( tag);
1417 bandname.append(
"_bands");
1419 ncDIM = calLUTfile->getDim(bandname.c_str());
1420 banddim = ncDIM.getSize();
1422 ncDIM = calLUTfile->getDim(
"number_of_times");
1423 timedim = ncDIM.getSize();
1424 ncDIM = calLUTfile->getDim(
"number_of_temperatures");
1425 tempdim = ncDIM.getSize();
1426 ncDIM = calLUTfile->getDim(
"number_of_T_coefficients");
1427 tcdim = ncDIM.getSize();
1428 ncDIM = calLUTfile->getDim(
"number_of_RVS_coefficients");
1429 rvsdim = ncDIM.getSize();
1430 ncDIM = calLUTfile->getDim(
"number_of_nonlinearity_coefficients");
1431 nldim = ncDIM.getSize();
1433 float **
K1 =
new float *[banddim];
1434 K1[0] =
new float[msdim*banddim];
1435 for (
size_t i=1;
i<banddim;
i++)
K1[
i] =
K1[
i-1] + msdim;
1438 dims[0] = banddim; dims[1] = msdim; dims[2] = timedim;
1439 float ***K2 = make3dT<float>(dims);
1441 dims[0] = banddim; dims[1] = tempdim; dims[2] = tcdim;
1442 float ***K3_coef = make3dT<float>(dims);
1444 dims[0] = banddim; dims[1] = msdim; dims[2] = rvsdim;
1445 float ***K4_coef = make3dT<float>(dims);
1448 double **K5 =
new double *[banddim];
1449 K5[0] =
new double[nldim*banddim];
1450 for (
size_t i=1;
i<banddim;
i++) K5[
i] = K5[
i-1] + nldim;
1453 cal_lut.
ldims[0] = timedim; cal_lut.
ldims[1] = tempdim;
1454 cal_lut.
ldims[2] = tcdim; cal_lut.
ldims[3] = rvsdim;
1455 cal_lut.
ldims[4] = nldim; cal_lut.
ldims[5] = msdim;
1459 var = gidLUT.getVar(
"K1");
1460 var.getVar( &cal_lut.
K1[0][0]);
1461 var = gidLUT.getVar(
"K2");
1462 var.getVar( &cal_lut.
K2[0][0][0]);
1463 var = gidLUT.getVar(
"K3_coef");
1464 var.getVar( &cal_lut.
K3_coef[0][0][0]);
1465 var = gidLUT.getVar(
"K4_coef");
1466 var.getVar( &cal_lut.
K4_coef[0][0][0]);
1467 var = gidLUT.getVar(
"K5");
1468 var.getVar( &cal_lut.
K5[0][0]);
1484 uint16_t timedim = gains.
ldims[0];
1485 uint16_t tempdim = gains.
ldims[1];
1486 uint16_t tcdim = gains.
ldims[2];
1487 uint16_t rvsdim = gains.
ldims[3];
1488 uint16_t nldim = gains.
ldims[4];
1489 uint16_t msdim = gains.
ldims[5];
1493 float **K1K2 =
new float *[nib];
1494 K1K2[0] =
new float[nib*msdim];
1495 for (
size_t i=1;
i<nib;
i++) K1K2[
i] = K1K2[
i-1] + msdim;
1498 dims[0] = nib; dims[1] = tempdim; dims[2] = tcdim;
1499 float ***K3_coef = make3dT<float>(dims);
1501 dims[0] = nib; dims[1] = msdim; dims[2] = rvsdim;
1502 float ***K4_coef = make3dT<float>(dims);
1505 double **K5 =
new double *[nib];
1506 K5[0] =
new double[nib*nldim];
1507 for (
size_t i=1;
i<nib;
i++) K5[
i] = K5[
i-1] + nldim;
1511 double *K2 =
new double[banddim];
1512 for (
size_t ms=0; ms<msdim; ms++) {
1515 double d2 =
jday(
iyr, 1, idom) - 2451545 + stime/864.0;
1524 if ( kd < (
size_t) (timedim-1)) {
1525 double ff = (d2 - K2t[kd]) / (K2t[kd+1] - K2t[kd]);
1526 for (
size_t j=0;
j<banddim;
j++)
1527 K2[
j] = cal_lut.
K2[
j][ms][kd]*(1.0-ff) + cal_lut.
K2[
j][ms][kd+1]*ff;
1529 for (
size_t j=0;
j<banddim;
j++) K2[
j] = cal_lut.
K2[
j][ms][kd];
1531 for (
size_t j=0;
j<banddim;
j++) K2[
j] *= cal_lut.
K1[
j][ms];
1532 for (
size_t i=0;
i<nib;
i++) {
1533 gains.
K1K2[
i][ms] = 0;
1534 for (
size_t j=0;
j<banddim;
j++)
1535 gains.
K1K2[
i][ms] += gmat[
j][
i] * cal_lut.
K1[
j][ms];
1539 for (
size_t i=0;
i<nib;
i++) {
1540 for (
size_t k=0;
k<rvsdim;
k++) {
1542 for (
size_t j=0;
j<banddim;
j++)
1550 for (
size_t i=0;
i<nib;
i++) {
1551 for (
size_t k=0;
k<tcdim;
k++) {
1552 for (
size_t l=0; l<tempdim; l++) {
1554 for (
size_t j=0;
j<banddim;
j++)
1561 for (
size_t i=0;
i<nib;
i++) {
1562 for (
size_t k=0;
k<nldim;
k++) {
1564 for (
size_t j=0;
j<banddim;
j++)
1565 gains.
K5[
i][
k] += gmat[
j][
i] * cal_lut.
K5[
j][
k];
1579 double ev_toff, int32_t
spin,
float *slines,
double *delt,
1580 double revpsec, int32_t ppr_off, int32_t *mspin,
1581 int32_t *ot_10us, uint8_t *enc_count,
float **hamenc,
1582 float **rtaenc,
float **pview,
double *theta, int16_t& iret) {
1590 int32_t max_enc_cts = 131072;
1591 double dtenc = 0.001;
1592 constexpr
double pi = 3.14159265358979323846;
1594 double rad2asec = (180/
pi) * 3600;
1597 float pprang = 2 *
pi * (ppr_off - geoLUT.
rta_nadir) / max_enc_cts;
1598 if (pprang >
pi) pprang -= 2*
pi;
1601 double *toff =
new double[pdim];
1602 for (
size_t i=0;
i<pdim;
i++) toff[
i] = delt[
i] + ev_toff;
1604 for (
size_t i=0;
i<pdim;
i++)
1605 theta[
i] = pprang + 2 *
pi * revpsec * toff[
i];
1609 double *thetacor =
new double[pdim];
1613 if ( mspin[
i] ==
spin) {
1619 cout <<
"No MCE encoder data for spin: " <<
spin << endl;
1624 while ( ip < pdim) {
1626 for (
size_t j=0;
j<enc_count[isp];
j++) {
1627 double tenc =
j*dtenc;
1628 if ( tenc < toff[ip] && (tenc+dtenc) > toff[ip]) {
1635 for (
size_t i=0;
i<pdim;
i++) {
1636 if ( toff[
i] >= tenc_ke && toff[
i] < tenc_ke+dtenc) {
1637 double ft = (toff[
i] - tenc_ke) / dtenc;
1638 thetacor[
i] = (1-ft)*rtaenc[isp][ke] + ft*rtaenc[isp][ke+1];
1647 for (
size_t i=0;
i<pdim;
i++) {
1648 theta[
i] = theta[
i] + thetacor[
i] / rad2asec;
1649 pview[
i][1] = sin(theta[
i]);
1650 pview[
i][2] = cos(theta[
i]);
1660 template <
typename T>
1662 uint16_t nskp, int16_t iags, int16_t iagd, uint32_t ntaps,
1663 int16_t *jagg, uint32_t dfill, int16_t ndc, T ***dark,
1664 uint32_t nib,
float *dc, int16_t& iret) {
1670 int16_t *nbndt =
new int16_t[ntaps];
1674 for (
size_t i=0;
i<ntaps;
i++)
1675 if ( jagg[
i] > 0) nbndt[
i] = 32 / jagg[
i];
else nbndt[
i] = 0;
1677 for (
size_t i=0;
i<ntaps;
i++) nbndt[
i] = 9;
1680 for (
size_t i=0;
i<ntaps;
i++)
nbnd += nbndt[
i];
1683 int32_t *kh =
new int32_t[
nscan];
1686 if ( hside[
i] == hside[iscn]) {
1687 kh[
i] = (int32_t)
i;
1696 if ( kh[
i] == (int32_t) iscn) {
1703 uint16_t ndscl = ndsc;
1704 bool valid_dark_found =
false;
1705 int32_t is1=js, is2=js;
1707 while (!valid_dark_found && ndscl <= nkh) {
1723 for (
size_t i=is1;
i<=(size_t) is2;
i++) {
1724 for (
size_t j=nskp;
j<(size_t) ndc;
j++) {
1725 if ( dark[kh[
i]][0][
j] != dfill) {
1726 valid_dark_found =
true;
1731 if ( !valid_dark_found) ndscl += 2;
1734 if ( !valid_dark_found) {
1741 for (
size_t i=0;
i<ntaps;
i++) {
1745 if ( iags*jagg[
i] > 4) {
1746 ddiv = iagd * jagg[
i] / 4.0;
1747 doff = (ddiv-1) / (2*ddiv);
1750 for (
size_t j=0;
j<(size_t) nbndt[
i];
j++) {
1753 for (
size_t k=kh[is1];
k<=(size_t) kh[is2];
k++) {
1754 for (
size_t l=nskp; l<(size_t) ndc; l++) {
1755 if (dark[
k][ibnd+
j][l] != dfill) {
1756 sum += dark[
k][ibnd+
j][l];
1762 dc[ibnd+
j] = ((sum/(nv))/ddiv) - doff;
1772 if (ndscl > ndsc) iret = 1;
1779 float *caltemps, uint32_t
nscan,
float *k3) {
1781 uint16_t tempdim = gains.
ldims[1];
1782 uint16_t tcdim = gains.
ldims[2];
1784 for (
size_t i=0;
i<nib;
i++) k3[
i] = 1.0;
1786 for (
size_t i=0;
i<tempdim;
i++) {
1787 float td = caltemps[
i] - K3T[
i];
1788 for (
size_t j=0;
j<tcdim;
j++) {
1789 for (
size_t k=0;
k<nib;
k++) {
1803 uint16_t rvsdim = gains.
ldims[3];
1805 for (
size_t i=0;
i<nib;
i++)
1806 for (
size_t j=0;
j<pdim;
j++)
1809 for (
size_t i=0;
i<rvsdim;
i++)
1810 for (
size_t j=0;
j<nib;
j++)
1811 for (
size_t k=0;
k<pdim;
k++)
1812 k4[
j][
k] += gains.
K4_coef[
j][hside][
i] * powf(theta[
k],
j+1);
1818 float K3T[
NTEMPS],
float *caltemps,
float **dn,
1828 int16_t ibtmp[3] = {1,2,3};
1830 for (
size_t i=0;
i<3;
i++) td[
i] = caltemps[ibtmp[
i]] - K3T[ibtmp[
i]];
1832 for (
size_t i=0;
i<pdim;
i++) {
1834 for (
size_t j=0;
j<nib;
j++) k5[
j][
i] = gains.
K5[
j][0];
1837 for (
size_t k=1;
k<=2;
k++) {
1838 for (
size_t l=0; l<nib; l++) {
1839 k5[l][
i] += (gains.
K5[l][
k] + gains.
K5[l][
k+3]*powf(td[0],
k) +
1840 gains.
K5[l][
k+5]*powf(td[1],
k) +
1841 gains.
K5[l][
k+7]*powf(td[2],
k))*powf(dn[l][
i],
k);
1844 for (
size_t l=0; l<nib; l++)
1845 k5[l][
i] += gains.
K5[l][3]*powf(dn[l][
i], 3);
1857 uint16_t pcdim, uint16_t bbb, uint16_t rbb,
1858 uint16_t psdim, uint16_t swb) {
1868 l1bfile =
new NcFile( l1b_filename, NcFile::replace);
1870 catch ( NcException& e) {
1872 cerr <<
"Failure creating OCI L1B file: " << l1b_filename << endl;
1876 fileName.assign( l1b_filename);
1878 ifstream oci_l1b_data_structure;
1880 string dataStructureFile;
1881 dataStructureFile.assign(
"$OCDATAROOT/oci/OCI_Level-1B_Data_Structure.cdl");
1884 oci_l1b_data_structure.open( dataStructureFile.c_str(), ifstream::in);
1885 if ( oci_l1b_data_structure.fail() ==
true) {
1886 cout <<
"\"" << dataStructureFile.c_str() <<
"\" not found" << endl;
1892 getline( oci_l1b_data_structure,
line);
1893 size_t pos =
line.find(
"dimensions:");
1894 if (
pos == 0)
break;
1901 getline( oci_l1b_data_structure,
line);
1902 if (
line.substr(0,2) ==
"//")
continue;
1904 size_t pos =
line.find(
" = ");
1905 size_t semi =
line.find(
" ;");
1906 if (
pos == string::npos)
break;
1911 string dimString =
line.substr(
pos+2, semi-(
pos+2));
1912 if (dimString.find(
"UNLIMITED") == string::npos) {
1916 dimSize = NC_UNLIMITED;
1921 iss >> skipws >>
line;
1923 if (
line.compare(
"ccd_pixels") == 0) {
1927 if (
line.compare(
"SWIR_pixels") == 0) {
1931 if (
line.compare(
"blue_bands") == 0) {
1935 if (
line.compare(
"red_bands") == 0) {
1939 if (
line.compare(
"SWIR_bands") == 0) {
1944 ncDims[ndims++] = l1bfile->addDim(
line, dimSize);
1946 catch ( NcException& e) {
1948 cerr <<
"Failure creating dimension: " <<
line.c_str() << endl;
1952 cout <<
"Dimension Name: " <<
line.c_str() <<
" Dimension Size: "
1959 getline( oci_l1b_data_structure,
line);
1960 size_t pos =
line.find(
"// global attributes");
1961 if (
pos == 0)
break;
1965 getline( oci_l1b_data_structure,
line);
1966 size_t pos =
line.find(
" = ");
1967 if (
pos == string::npos)
break;
1972 attValue.assign(
line.substr(
pos+4));
1973 size_t posQuote = attValue.find(
"\"");
1974 attValue.assign(attValue.substr(0, posQuote));
1976 istringstream iss(
line.substr(
pos+2));
1979 iss >> skipws >>
line;
1982 if (
line.substr(0,2) ==
"//")
continue;
1985 attName.assign(
line.substr(1).c_str());
1988 if (attName.compare(
"orbit_number") == 0)
continue;
1989 if (attName.compare(
"history") == 0)
continue;
1990 if (attName.compare(
"format_version") == 0)
continue;
1991 if (attName.compare(
"instrument_number") == 0)
continue;
1992 if (attName.compare(
"pixel_offset") == 0)
continue;
1993 if (attName.compare(
"number_of_filled_scans") == 0)
continue;
1997 l1bfile->putAtt(attName, attValue);
1999 catch ( NcException& e) {
2001 cerr <<
"Failure creating attribute: " + attName << endl;
2011 getline( oci_l1b_data_structure,
line);
2015 if (
line.substr(0,1).compare(
"}") == 0) {
2016 oci_l1b_data_structure.close();
2021 size_t pos =
line.find(
"group:");
2027 istringstream iss(
line.substr(6, string::npos));
2028 iss >> skipws >>
line;
2030 ncGrps[ngrps++] = l1bfile->addGroup(
line);
2038 string flag_meanings;
2041 double fill_value=0.0;
2043 vector<NcDim> varVec;
2049 getline( oci_l1b_data_structure,
line);
2051 getline( oci_l1b_data_structure,
line);
2053 if (
line.substr(0,2) ==
"//")
continue;
2054 if (
line.length() == 0)
continue;
2055 if (
line.substr(0,1).compare(
"\r") == 0)
continue;
2056 if (
line.substr(0,1).compare(
"\n") == 0)
continue;
2061 if (
pos == string::npos) {
2066 createField( ncGrps[ngrps-1], sname.c_str(), lname.c_str(),
2068 (
void *) &fill_value,
2069 flag_values.c_str(), flag_meanings.c_str(),
2072 flag_values.assign(
"");
2073 flag_meanings.assign(
"");
2082 if (
line.substr(0,10).compare(
"} // group") == 0)
break;
2086 istringstream iss(
line);
2087 iss >> skipws >> varType;
2090 if ( varType.compare(
"char") == 0) ntype = NC_CHAR;
2091 else if ( varType.compare(
"byte") == 0) ntype = NC_BYTE;
2092 else if ( varType.compare(
"short") == 0) ntype = NC_SHORT;
2093 else if ( varType.compare(
"int") == 0) ntype = NC_INT;
2094 else if ( varType.compare(
"long") == 0) ntype = NC_INT;
2095 else if ( varType.compare(
"float") == 0) ntype = NC_FLOAT;
2096 else if ( varType.compare(
"real") == 0) ntype = NC_FLOAT;
2097 else if ( varType.compare(
"double") == 0) ntype = NC_DOUBLE;
2098 else if ( varType.compare(
"ubyte") == 0) ntype = NC_UBYTE;
2099 else if ( varType.compare(
"ushort") == 0) ntype = NC_USHORT;
2100 else if ( varType.compare(
"uint") == 0) ntype = NC_UINT;
2101 else if ( varType.compare(
"ulong") == 0) ntype = NC_UINT;
2102 else if ( varType.compare(
"int64") == 0) ntype = NC_INT64;
2103 else if ( varType.compare(
"uint64") == 0) ntype = NC_UINT64;
2107 size_t posSname =
line.substr(0,
pos).rfind(
" ");
2108 sname.assign(
line.substr(posSname+1,
pos-posSname-1));
2109 cout <<
"sname: " << sname.c_str() << endl;
2112 this->
parseDims( line.substr(
pos+1, string::npos), varVec);
2113 numDims = varVec.size();
2117 size_t posEql =
line.find(
"=");
2118 size_t pos1qte =
line.find(
"\"");
2119 size_t pos2qte =
line.substr(pos1qte+1, string::npos).find(
"\"");
2125 if (
attrName.compare(
"long_name") == 0) {
2126 lname.assign(
line.substr(pos1qte+1, pos2qte));
2131 else if (
attrName.compare(
"units") == 0) {
2132 units.assign(
line.substr(pos1qte+1, pos2qte));
2137 else if (
attrName.compare(
"_FillValue") == 0) {
2139 iss.str(
line.substr(posEql+1, string::npos));
2144 else if (
attrName.compare(
"flag_values") == 0) {
2145 flag_values.assign(
line.substr(pos1qte+1, pos2qte));
2149 else if (
attrName.compare(
"flag_meanings") == 0) {
2150 flag_meanings.assign(
line.substr(pos1qte+1, pos2qte));
2154 else if (
attrName.compare(
"valid_min") == 0) {
2156 iss.str(
line.substr(posEql+1, string::npos));
2162 else if (
attrName.compare(
"valid_max") == 0) {
2164 iss.str(
line.substr(posEql+1, string::npos));
2178 int createField( NcGroup &ncGrp,
const char *sname,
const char *lname,
2180 void *fill_value,
const char *flag_values,
2181 const char *flag_meanings,
2182 double low,
double high,
int nt, vector<NcDim>& varVec) {
2190 ncVar = ncGrp.addVar(sname, nt, varVec);
2192 catch ( NcException& e) {
2193 cout << e.what() << endl;
2194 cerr <<
"Failure creating variable: " << sname << endl;
2199 double fill_value_dbl;
2200 memcpy( &fill_value_dbl, fill_value,
sizeof(
double));
2210 if ( low != fill_value_dbl) {
2211 if ( nt == NC_BYTE) {
2212 i8 = fill_value_dbl;
2213 ncVar.setFill(
true, (
void *) &i8);
2214 }
else if ( nt == NC_UBYTE) {
2215 ui8 = fill_value_dbl;
2216 ncVar.setFill(
true, (
void *) &ui8);
2217 }
else if ( nt == NC_SHORT) {
2218 i16 = fill_value_dbl;
2219 ncVar.setFill(
true, (
void *) &i16);
2220 }
else if ( nt == NC_USHORT) {
2221 ui16 = fill_value_dbl;
2222 ncVar.setFill(
true, (
void *) &ui16);
2223 }
else if ( nt == NC_INT) {
2224 i32 = fill_value_dbl;
2225 ncVar.setFill(
true, (
void *) &i32);
2226 }
else if ( nt == NC_UINT) {
2227 ui32 = fill_value_dbl;
2228 ncVar.setFill(
true, (
void *) &ui32);
2229 }
else if ( nt == NC_FLOAT) {
2230 f32 = fill_value_dbl;
2231 ncVar.setFill(
true, (
void *) &
f32);
2233 ncVar.setFill(
true, (
void *) &fill_value_dbl);
2239 vector<size_t> chunkVec;
2240 if ( varVec.size() == 3 && (strncmp(sname,
"EV_", 3) == 0)) {
2241 dimlength = varVec[2].getSize();
2243 chunkVec.push_back(1);
2244 chunkVec.push_back(16);
2245 chunkVec.push_back(dimlength/10);
2254 ncVar.setChunking(ncVar.nc_CHUNKED, chunkVec);
2256 catch ( NcException& e) {
2258 cerr <<
"Failure setting chunking: " << sname << endl;
2263 ncVar.setCompression(
true,
true, 5);
2265 catch ( NcException& e) {
2267 cerr <<
"Failure setting compression: " << sname << endl;
2275 ncVar.putAtt(
"long_name", lname);
2277 catch ( NcException& e) {
2279 cerr <<
"Failure creating 'long_name' attribute: " << lname << endl;
2283 if ( strcmp( flag_values,
"") != 0) {
2288 fv.assign( flag_values);
2289 size_t pos = fv.find(
"=", curPos);
2290 fv = fv.substr(
pos+1);
2292 size_t semicln = fv.find(
";");
2297 while(
pos != semicln) {
2298 pos = fv.find(
",", curPos);
2299 if (
pos == string::npos)
2303 istringstream iss(fv.substr(curPos,
pos-curPos));
2304 iss >> skipws >> flag_value;
2305 vec[n++] = atoi( flag_value.c_str());
2310 ncVar.putAtt(
"flag_values", NC_BYTE, n, vec);
2312 catch ( NcException& e) {
2314 cerr <<
"Failure creating 'flag_values' attribute: " << lname << endl;
2320 if ( strcmp( flag_meanings,
"") != 0) {
2323 ncVar.putAtt(
"flag_meanings", flag_meanings);
2325 catch ( NcException& e) {
2327 cerr <<
"Failure creating 'flag_meanings' attribute: "
2328 << flag_meanings << endl;
2339 vr[0] = (uint8_t)low;
2340 vr[1] = (uint8_t)high;
2343 ncVar.putAtt(
"valid_min", NC_BYTE, 1, &vr[0]);
2345 catch ( NcException& e) {
2347 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
2352 ncVar.putAtt(
"valid_max", NC_BYTE, 1, &vr[1]);
2354 catch ( NcException& e) {
2356 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
2364 vr[0] = (uint8_t)low;
2365 vr[1] = (uint8_t)high;
2368 ncVar.putAtt(
"valid_min", NC_UBYTE, 1, &vr[0]);
2370 catch ( NcException& e) {
2372 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
2377 ncVar.putAtt(
"valid_max", NC_UBYTE, 1, &vr[1]);
2379 catch ( NcException& e) {
2381 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
2389 vr[0] = (int16_t)low;
2390 vr[1] = (int16_t)high;
2393 ncVar.putAtt(
"valid_min", NC_SHORT, 1, &vr[0]);
2395 catch ( NcException& e) {
2397 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
2402 ncVar.putAtt(
"valid_max", NC_SHORT, 1, &vr[1]);
2404 catch ( NcException& e) {
2406 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
2414 vr[0] = (uint16_t)low;
2415 vr[1] = (uint16_t)high;
2418 ncVar.putAtt(
"valid_min", NC_USHORT, 1, &vr[0]);
2420 catch ( NcException& e) {
2422 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
2427 ncVar.putAtt(
"valid_max", NC_USHORT, 1, &vr[1]);
2429 catch ( NcException& e) {
2431 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
2439 vr[0] = (int32_t)low;
2440 vr[1] = (int32_t)high;
2443 ncVar.putAtt(
"valid_min", NC_INT, 1, &vr[0]);
2445 catch ( NcException& e) {
2447 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
2452 ncVar.putAtt(
"valid_max", NC_INT, 1, &vr[1]);
2454 catch ( NcException& e) {
2456 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
2465 vr[0] = (uint32_t)low;
2466 vr[1] = (uint32_t)high;
2469 ncVar.putAtt(
"valid_min", NC_UINT, 1, &vr[0]);
2471 catch ( NcException& e) {
2473 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
2478 ncVar.putAtt(
"valid_max", NC_UINT, 1, &vr[1]);
2480 catch ( NcException& e) {
2482 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
2492 vr[1] = (
float)high;
2495 ncVar.putAtt(
"valid_min", NC_FLOAT, 1, &vr[0]);
2497 catch ( NcException& e) {
2499 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
2504 ncVar.putAtt(
"valid_max", NC_FLOAT, 1, &vr[1]);
2506 catch ( NcException& e) {
2508 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
2520 ncVar.putAtt(
"valid_min", NC_DOUBLE, 1, &vr[0]);
2522 catch ( NcException& e) {
2524 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
2529 ncVar.putAtt(
"valid_max", NC_DOUBLE, 1, &vr[1]);
2531 catch ( NcException& e) {
2533 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
2539 fprintf(
stderr,
"-E- %s line %d: ",__FILE__,__LINE__);
2540 fprintf(
stderr,
"Got unsupported number type (%d) ",nt);
2541 fprintf(
stderr,
"while trying to create NCDF variable, \"%s\", ",sname);
2550 ncVar.putAtt(
"units",
units);
2552 catch ( NcException& e) {
2554 cerr <<
"Failure creating 'units' attribute: " <<
units << endl;
2562 ncVar.putAtt(
"standard_name",
units);
2564 catch ( NcException& e) {
2566 cerr <<
"Failure creating 'standard_name' attribute: "
2583 size_t pos = dimString.find(
",", curPos);
2584 if (
pos == string::npos)
2585 pos = dimString.find(
")");
2588 istringstream iss(dimString.substr(curPos,
pos-curPos));
2589 iss >> skipws >> varDimName;
2591 for (
int i=0;
i<ndims;
i++) {
2594 dimName = ncDims[
i].getName();
2596 catch ( NcException& e) {
2598 cerr <<
"Failure accessing dimension: " + dimName << endl;
2602 if ( varDimName.compare(dimName) == 0) {
2603 cout <<
" " << dimName <<
" " << ncDims[
i].getSize() << endl;
2604 varDims.push_back(ncDims[
i]);
2608 if ( dimString.substr(
pos, 1).compare(
")") == 0)
break;
2623 l1bfile->putAtt(
"date_created", buf);
2625 l1bfile->putAtt(
"time_coverage_start", tstart.c_str());
2627 l1bfile->putAtt(
"time_coverage_end", tend.c_str());
2630 l1bfile->putAtt(
"product_name", l1b_name.c_str());
2641 catch ( NcException& e) {
2642 cout << e.what() << endl;
2643 cerr <<
"Failure closing: " + fileName << endl;
2651 template <
typename T>
2654 T ***arr3d =
new T **[dims[0]];
2656 arr3d[0] =
new T*[dims[0]*dims[1]];
2657 arr3d[0][0] =
new T[dims[0]*dims[1]*dims[2]];
2659 for (
size_t i=1;
i<dims[0];
i++) arr3d[
i] = arr3d[
i-1] + dims[1];
2661 for (
size_t i=0;
i<dims[0];
i++) {
2662 if (
i > 0) arr3d[
i][0] = arr3d[
i-1][0] + dims[1]*dims[2];
2663 for (
size_t j=1;
j<dims[1];
j++)
2664 arr3d[
i][
j] = arr3d[
i][
j-1] + dims[2];