17 using namespace netCDF;
18 using namespace netCDF::exceptions;
21 int32_t *iyear, int32_t *iday,
double *sec) {
26 memcpy( &ui32, cctime, 4);
29 double dsec = (
double) ccsec;
33 *iday = ccsec / 86400;
34 int32_t
jday = *iday + 2436205;
38 int32_t
msec = cctime[4]*256 + cctime[5];
39 int32_t isec = ccsec % 86400;
40 *sec = isec +
msec/65536.0;
46 #define VERSION "1.05.00_2022-06-24"
142 int main (
int argc,
char* argv[])
145 cout <<
"l1agen_oci " <<
VERSION <<
" ("
146 << __DATE__ <<
" " << __TIME__ <<
")" << endl;
150 "l1agen_oci OCI_packet_file granule_len " <<
151 "[-k SC_HKT_input_list] " <<
152 "[--hktlist SC_HKT_input_list] "
153 "[-s SWIR_LOFF_config] " <<
154 "[--swir_loff_set] " <<
155 "[-t YYYYmmddTHH:MM:SS] " <<
156 "[--start_time YYYYmmddTHH:MM:SS] " <<
157 "[-o output_list_file] " <<
158 "[--outlist output_list_file]" <<
165 string swir_loff_set =
"";
170 static struct option long_options[] = {
171 {
"hktlist", required_argument, 0,
'k'},
172 {
"swir_loff_set", required_argument,0,
's'},
173 {
"start_time", required_argument, 0,
't'},
174 {
"outlist", required_argument, 0,
'o'},
175 {
"nametag", required_argument, 0,
'p'},
180 int option_index = 0;
182 c = getopt_long( argc, argv,
"k:s:t:o:p:", long_options, &option_index);
191 hktlist.assign(optarg);
195 swir_loff_set.assign(optarg);
204 outlist.assign( optarg);
208 nametag.assign( optarg);
217 if ( outlist.compare(
"") != 0)
218 fout.open( outlist.c_str());
226 tfileStream.open( argv[optind+0], fstream::in | fstream::binary);
227 if ( tfileStream.fail()) {
228 cout << argv[optind+0] <<
" not found" << endl;
238 uint32_t maxpkts = 30000;
242 uint8_t **pbuffer0 =
new uint8_t *[maxpkts];
243 pbuffer0[0] =
new uint8_t[
PKTSIZE*maxpkts];
244 for (
size_t i=1;
i<maxpkts;
i++) pbuffer0[
i] = pbuffer0[
i-1] +
PKTSIZE;
246 uint8_t **pbuffer1 =
new uint8_t *[maxpkts];
247 pbuffer1[0] =
new uint8_t[
PKTSIZE*maxpkts];
248 for (
size_t i=1;
i<maxpkts;
i++) pbuffer1[
i] = pbuffer1[
i-1] +
PKTSIZE;
253 int32_t spn0, spn, spnp;
254 vector<int32_t> tlmind0;
260 cout <<
"Packet too big (" << len <<
") for buffer (" <<
PKTSIZE <<
")"
264 read_packet( &tfileStream, fpacket, len, apid, endfile);
265 apid = (fpacket[0] % 8)*256 + fpacket[1];
267 while (apid != 636 && apid != 700 && apid != 720 && !endfile) {
271 cout <<
"Packet too big (" << len <<
") for buffer (" <<
PKTSIZE <<
")"
275 read_packet( &tfileStream, fpacket, len, apid, endfile);
276 apid = (fpacket[0] % 8)*256 + fpacket[1];
279 cout <<
"No science packets found in file" << endl;
282 if (nsk > 0) cout << nsk <<
" packets skipped" << endl;
294 string str = argv[optind+1];
295 istringstream(
str) >> mper;
298 string dtypes[] = {
"",
"",
"_DARK",
"_SOL-D",
"_SOL-M",
"_LIN",
"_LUN",
299 "_DIAG",
"_STAT",
"_SPEC",
"",
"_SNAP-X",
"_SNAP-I",
301 string smodes[] = {
"",
"_SDIAG",
"_SRAW",
"_STEST"};
303 uint8_t **pbuffer =
new uint8_t *[maxpkts];
304 pbuffer[0] =
new uint8_t[
PKTSIZE*maxpkts];
305 for (
size_t i=1;
i<maxpkts;
i++) pbuffer[
i] = pbuffer[
i-1] +
PKTSIZE;
307 uint16_t ncps, nbbs, nrbs, nsps, ndcs, ndss, btaps[16], rtaps[16], msps;
309 uint8_t *linerr =
new uint8_t[maxpkts];
310 uint8_t *seqerr =
new uint8_t[maxpkts];
311 for (
size_t i=1;
i<maxpkts;
i++) {
319 while ((ancind0 == -1) && !endfile) {
321 (uint8_t (*)[
PKTSIZE]) &pbuffer0[0][0],
322 npkts0, spn0, ancind0, tlmind0, noseq, endfile);
325 cout <<
"No ancillary packets found in file" << endl;
343 memcpy( apacket0, &pbuffer0[ancind0][0],
ANCSIZE);
345 btaps, rtaps, itable);
348 while ((ncps == 1 || ancind0 == -1) && !endfile) {
349 if (ancind0 != -1) cout<<
"Ancillary packet has zero science pixels at spin "<<spn0<<endl;
351 (uint8_t (*)[
PKTSIZE]) &pbuffer0[0][0],
352 npkts0, spn0, ancind0, tlmind0, noseq, endfile);
354 memcpy( apacket0, &pbuffer0[ancind0][0],
ANCSIZE);
356 btaps, rtaps, itable);
361 cout <<
"No ancillary packets for last granule" << endl;
366 cout <<
"ncps - number of CCD band pixels (ccd_pixels): " << ncps << endl;
367 cout <<
"nsps - number of SWIR band pixels: " << nsps << endl;
368 cout <<
"ndcs - number of dark collect pixels: " << ndcs << endl;
369 cout <<
"ndss - number of dark SWIR pixels: " << ndss << endl;
370 cout <<
"nbbs - number of blue bands: " << nbbs << endl;
371 cout <<
"nrbs - number of red bands: " << nrbs << endl;
375 double scanp = 0.1755;
376 double stimp = stime - scanp;
383 uint32_t ltime,
mtime;
390 strptime(
time_start.c_str(),
"%Y-%m-%dT%H:%M:%S", &tm_start);
391 ltime = (int32_t)(tm_start.tm_hour*3600+tm_start.tm_min*60+tm_start.tm_sec);
392 mtime = mper>0?ltime + mper*60:ltime + 600;
393 while (((stime <ltime) || (ncps==1)) && !endfile) {
395 (uint8_t (*)[
PKTSIZE]) &pbuffer0[0][0],
396 npkts0, spn0, ancind0, tlmind0, noseq, endfile);
398 memcpy( apacket0, &pbuffer0[ancind0][0],
ANCSIZE);
402 btaps, rtaps, itable);
403 if (ncps==1) cout<<
"Ancillary packet has zero science pixels at spin "<<spn0<<endl;
407 if (endfile || (stime>
mtime)) {
408 cout <<
"No science data in time range" << endl;
418 ltime = (((int32_t) (stime)) / 60 / mper) * (mper*60);
419 mtime = ltime + mper*60;
420 maxsc = (uint16_t) ((mper*60/scanp) + 2);
422 cout <<
"Processing with single file option" << endl;
423 ltime = (int32_t) stime;
431 nsps = ((nsps+7)/8)*8;
432 unsigned short nswb = 9;
434 int16_t cindex[32768];
435 int16_t sindex[32768*nswb];
436 int16_t cdindex[32768];
437 int16_t sdindex[32768];
438 int16_t swir_loff[9];
440 for (
size_t i=0;
i<32768;
i++) {
442 for (
size_t j=0;
j<nswb;
j++) sindex[
i*nswb+
j] = -1;
447 for (
size_t i=0;
i<9;
i++) swir_loff[
i] = 0;
448 if (swir_loff_set.compare(
"ETU")==0){
465 uint16_t smodep = smode;
471 uint32_t jd0 =
jday(iyear, 1, iday);
472 int16_t yr16 = (int16_t) iyear;
473 int16_t
doy = (int16_t) iday;
477 int32_t
ih = (int32_t) (stime / 3600);
478 int32_t mn = (int32_t) ((stime -
ih*3600) / 60);
479 int32_t isec = (int32_t) (stime -
ih*3600 - mn*60);
481 stringstream timestr, datestr;
482 timestr << setfill(
'0') << setw(2) <<
ih
483 << setfill(
'0') << setw(2) << mn
484 << setfill(
'0') << setw(2) << isec;
486 datestr << setfill(
'0') << setw(4) << iyear
487 << setfill(
'0') << setw(2) << month
488 << setfill(
'0') << setw(2) << dom;
492 basenme.assign(
basename(argv[optind+0]));
494 if (nametag ==
"") nametag.assign(
"PACE_OCI");
497 for (
size_t i=0;
i<10;
i++) {
498 if (itable[
i].
dtype>maxdtype) maxdtype = itable[
i].
dtype;
501 if ((maxdtype != 2) && (maxdtype != 10))
dtype = maxdtype;
504 if ((jd0 < 2459000) &&
dtype == 11)
dtype = 9;
506 string l1a_name = nametag + dtypes[
dtype] + smodes[smode] +
507 "_" + basenme.substr(0,4) + basenme.substr(5,3) + basenme.substr(9,3);
508 l1a_name +=
"." + datestr.str() +
"T" + timestr.str() +
".L1A.nc";
513 uint16_t **dark_b =
new uint16_t *[maxsc];
514 dark_b[0] =
new uint16_t[ndcs*(nbbs>0?nbbs:1)*maxsc];
515 for (
size_t i=1;
i<maxsc;
i++) dark_b[
i] = dark_b[
i-1] + ndcs*(nbbs>0?nbbs:1);
518 uint16_t **dark_r =
new uint16_t *[maxsc];
519 dark_r[0] =
new uint16_t[ndcs*(nrbs>0?nrbs:1)*maxsc];
520 for (
size_t i=1;
i<maxsc;
i++) dark_r[
i] = dark_r[
i-1] + ndcs*(nrbs>0?nrbs:1);
523 uint32_t **dark_s =
new uint32_t *[maxsc];
524 dark_s[0] =
new uint32_t[ndss*(nswb>0?nswb:1)*maxsc];
525 for (
size_t i=1;
i<maxsc;
i++) dark_s[
i] = dark_s[
i-1] + ndss*(nswb>0?nswb:1);
527 uint16_t **bdark =
new uint16_t *[nbbs];
528 uint16_t **rdark =
new uint16_t *[nrbs];
529 uint32_t **sdark =
new uint32_t *[nswb];
531 uint16_t **bsci =
new uint16_t *[nbbs];
532 bsci[0] =
new uint16_t[ncps*(nbbs>0?nbbs:1)];
533 for (
size_t i=1;
i<nbbs;
i++) bsci[
i] = bsci[
i-1] + ncps;
535 uint16_t **rsci =
new uint16_t *[nrbs];
536 rsci[0] =
new uint16_t[ncps*(nrbs>0?nrbs:1)];
537 for (
size_t i=1;
i<nrbs;
i++) rsci[
i] = rsci[
i-1] + ncps;
539 uint32_t **ssci =
new uint32_t *[nswb];
540 ssci[0] =
new uint32_t[nsps*(nswb>0?nswb:1)];
541 for (
size_t i=1;
i<nswb;
i++) ssci[
i] = ssci[
i-1] + nsps;
544 uint8_t **ancdata =
new uint8_t *[maxsc+1];
545 ancdata[0] =
new uint8_t[
ANCSIZE*(maxsc+1)];
546 for (
size_t i=1;
i< (size_t) (maxsc+1);
i++)
549 uint32_t maxtlm = maxsc*10;
550 uint8_t **tlmdata =
new uint8_t *[maxtlm];
551 tlmdata[0] =
new uint8_t[
TLMSIZE*(maxtlm)];
552 for (
size_t i=1;
i< (size_t) (maxtlm);
i++)
557 uint16_t ncpt = ncps;
558 uint16_t nspt0 = nsps;
561 uint16_t nspt = ((nspt0 + 7) / 8) * 8;
564 uint16_t **bbands =
new uint16_t*[ncpt];
565 uint16_t **rbands =
new uint16_t*[ncpt];
566 for (
size_t i=0;
i<ncpt;
i++) {
571 int16_t *blines =
new int16_t[ncpt];
572 int16_t *rlines =
new int16_t[ncpt];
574 for (
size_t i=0;
i<ncpt;
i++) blines[
i] = -1;
575 for (
size_t i=0;
i<ncpt;
i++) rlines[
i] = -1;
579 uint32_t **sbands =
new uint32_t*[msps];
580 for (
size_t i=0;
i<msps;
i++) {
581 sbands[
i] =
new uint32_t[nswb];
584 int16_t *slines =
new int16_t[msps];
588 int8_t **sdfrms =
new int8_t *[maxsc];
589 sdfrms[0] =
new int8_t[ndss*maxsc];
590 for (
size_t i=1;
i<maxsc;
i++) sdfrms[
i] = sdfrms[
i-1] + ndss;
591 memset(sdfrms[0], -1,
sizeof(int8_t)*ndss*maxsc);
593 int8_t *sfrm =
new int8_t[msps];
594 int8_t *sfrms =
new int8_t[msps];
597 cout <<
"Creating: " << l1a_name.c_str() << endl;
598 cout <<
"Starting at spin number "<<spn0<<endl;
599 outfile.
createl1( (
char *) l1a_name.c_str(),
600 maxsc, ncps, nbbs, nrbs, nsps, ndcs);
604 if ( outlist.compare(
"") != 0) {
605 outlistpos = fout.tellp();
606 fout << l1a_name.c_str() <<
" ";
613 uint32_t npkts,npkt1;
615 vector<int32_t> tlmind;
621 uint16_t btype, rtype;
622 uint16_t bagg[16], ragg[16];
630 while ( stime <
mtime && acomp && !enddata && scomp && (dspn <= maxgap) && isc<maxsc) {
632 if ((isc % 100) == 0) cout <<
"Processing scan " << isc << endl;
636 memcpy( &pbuffer1[0][0], &pbuffer0[0][0],
PKTSIZE*maxpkts);
645 (uint8_t (*)[
PKTSIZE]) &pbuffer0[0][0],
646 npkts0, spn0, ancind0, tlmind0, seqerr[isc],
650 if (dspn >= 1 && npkt1 > 1) {
654 memcpy(ancdata[isc], pbuffer1[ancind],
ANCSIZE);
659 memcpy(&ancdata[isc][24], &ui32, 4);
663 memcpy(&ancdata[isc][0], &ui32, 4);
667 int ntind = tlmind.size();
668 if ( ntind > 0 && ntlm < maxtlm) {
670 while (itt < ntind && ntlm < maxtlm) {
671 memcpy(tlmdata[ntlm], pbuffer1[tlmind[itt]],
TLMSIZE);
676 cout <<
"Maximum number of telemetry packets exceeded at spin: "
681 for (
size_t i=0;
i<msps;
i++) {
684 for (
size_t j=0;
j<nswb;
j++) sbands[
i][
j] = 0;
688 npkts = npkt1 + npkts0;
690 cout<<
"Packets exceed max ["<<maxpkts<<
"]."<<endl;
692 npkts0 = npkts - npkt1;
694 memcpy( &pbuffer[0][0], &pbuffer1[0][0],
PKTSIZE*npkt1);
695 if (npkts0 > 0) memcpy( &pbuffer[npkt1][0], &pbuffer0[0][0],
PKTSIZE*npkts0);
698 (uint8_t (*)[
PKTSIZE]) &pbuffer[0][0],
699 bbands, rbands, sbands, blines, rlines, slines,
700 btype, bagg, rtype, ragg, sfrm, icheck);
701 if (icheck != 0) cout <<
"Science data unpacking error in spin: "
703 iret = iret | icheck;
705 bdark[0] = dark_b[isc];
706 for (
size_t i=1;
i<nbbs;
i++) bdark[
i] = bdark[
i-1] + ndcs;
708 rdark[0] = dark_r[isc];
709 for (
size_t i=1;
i<nrbs;
i++) rdark[
i] = rdark[
i-1] + ndcs;
711 sdark[0] = dark_s[isc];
712 for (
size_t i=1;
i<nswb;
i++) sdark[
i] = sdark[
i-1] + ndss;
715 std::fill(bsci[0], bsci[0]+ncps*((nbbs>0)?nbbs:1), 65535);
716 std::fill(rsci[0], rsci[0]+ncps*((nrbs>0)?nrbs:1), 65535);
717 std::fill(ssci[0],ssci[0]+nsps*((nswb>0)?nswb:1), 1048575);
718 memset(sfrms, -1,
sizeof(int8_t)*msps);
721 std::fill(bdark[0],bdark[0]+ndcs*((nbbs>0)?nbbs:1), 65535);
722 std::fill(rdark[0],rdark[0]+ndcs*((nrbs>0)?nrbs:1), 65535);
723 std::fill(sdark[0],sdark[0]+ndss*((nswb>0)?nswb:1), 1048575);
726 if ((blines[0] != -1) || (rlines[0] != -1) || (slines[0] != -1)) {
728 cindex, sindex, cdindex,sdindex, bbands, rbands, sbands,
729 blines, rlines, slines, bsci, rsci, ssci,
730 bdark, rdark, sdark, linerr[isc], icheck);
731 if (icheck >= 2) cout <<
"Science data checking error in spin: "
733 iret = iret | icheck;
736 for (
size_t i=0;
i<msps;
i++) {
737 if (sindex[slines[
i]*nswb] > -1) {
738 sfrms[sindex[slines[
i]*nswb]] = sfrm[
i];
742 for (
size_t i=0;
i<msps;
i++) {
743 if (sdindex[slines[
i]] > -1) {
744 sdfrms[isc][sdindex[slines[
i]]] = sfrm[
i];
750 ncps, nsps, bsci, rsci, ssci, sfrms);
756 endtime.
iyear = iyear;
762 ih = (int32_t) (stime / 3600);
763 mn = (int32_t) ((stime -
ih*3600) / 60);
764 isec = (int32_t) (stime -
ih*3600 - mn*60);
766 if(stime <= stimp && ancind != -1)
767 cout <<
"Scan " << spn <<
" out of order at" <<
768 ih <<
" " << mn <<
" " << isec << endl;
772 if (!enddata && ancind0 != -1) {
773 memcpy( apacket, &pbuffer0[ancind0][0],
ANCSIZE);
775 uint32_t
jd =
jday(iyear, 1, iday);
776 stime += (
jd - jd0) * 86400;
780 scomp = (smode == smodep);
783 ih = (int32_t) (stime / 3600);
784 mn = (int32_t) ((stime -
ih*3600) / 60);
785 isec = (int32_t) (stime -
ih*3600 - mn*60);
786 cout <<
"SWIR data mode change at: "
787 <<
ih <<
" " << mn <<
" " << isec << endl;
795 cout <<
"Spin number gap at spin " << spnp << endl;
802 if (mper > 0 && isc < (
size_t) (maxsc-10) && acomp) {
803 cout <<
"(mper > 0 && isc < (size_t) (maxsc-10) && acomp)" << endl;
806 cout <<
"Scans in file: " << isc << endl;
807 cout <<
"Complete flag: " << iret << endl;
812 if (ancind0 != -1) memcpy(ancdata[isc], apacket,
ANCSIZE);
816 dark_b[0], dark_r[0], dark_s[0],
821 int32_t *spinID =
new int32_t[isc+100];
822 for (
size_t i=0;
i<(isc+100);
i++) spinID[
i] = -999;
830 int ntind = tlmind0.size();
831 if (tlmind0.size() != 0 && dspn <= maxgap && !endfile) {
833 while (itt < ntind && ntlm < maxtlm) {
834 memcpy(tlmdata[ntlm], pbuffer0[tlmind0[itt]],
TLMSIZE);
841 cout << ntlm <<
" HKT packets" << endl;
843 (uint8_t (*)[
TLMSIZE]) &tlmdata[0][0],
844 spinID, cdsmode,isc);
849 if ( hktlist.compare(
"") != 0)
856 string sdir =
"Ascending";
857 string edir =
"Ascending";
859 sdir, edir, isc,
dtype,
860 smode, cdsmode, fout);
863 if ( outlist.compare(
"") != 0)
864 fout << iret <<
"\n";
874 cout <<
"Removing 0-scan file: " << l1a_name.c_str() << endl;
875 if ( outlist.compare(
"") != 0) fout.seekp( outlistpos);
877 cout <<
"Error removing " << l1a_name.c_str() << endl;
900 for (
size_t i=0;
i<ncps;
i++)
if ( bbands[
i] !=
NULL)
delete[] bbands[
i];
902 for (
size_t i=0;
i<ncps;
i++)
if ( rbands[
i] !=
NULL)
delete[] rbands[
i];
904 for (
size_t i=0;
i<msps;
i++)
delete[] sbands[
i];
920 if (stime>
mtime && sstop==1)
break;
928 if ( outlist.compare(
"") != 0) fout.close();
933 delete[] pbuffer0[0];
948 int16_t *cdindex, int16_t *sdindex, int16_t *swir_loff) {
950 uint16_t maxlines = 32768;
951 uint16_t nagg[4] = {1,2,4,8};
952 unsigned short nswb = 9;
962 for (
size_t i=0;
i<10;
i++) {
965 if ((itable[
i].
dtype != 0) && (itable[
i].
dtype != 10)) {
966 if (itable[
i].
lines > 0) {
969 uint16_t iagg = nagg[itable[
i].
iagg];
970 uint16_t
lines = itable[
i].lines;
972 if ((loff+
lines) > maxlines) {
973 cout <<
"Mode table entry " <<
i <<
" exceeds max lines" << endl;
974 lines = maxlines - loff - iagg;
977 uint16_t cp =
lines / iagg;
978 for (
size_t j=0;
j<cp;
j++) {
979 uint16_t cind = loff +
j*iagg;
980 cindex[cind] = cpix +
j;
985 uint16_t sp =
lines / 8;
986 for (
size_t j=0;
j<sp;
j++) {
987 uint16_t sind = (loff/8 +
j)*8;
989 for (
size_t k=0;
k<nswb;
k++) {
991 if (itable[
i].
dtype == 1) {
992 sloff = swir_loff[
k];
996 sindex[(sind-sloff)*nswb+
k] =
spix +
j;
1003 if (itable[
i].
dtype == 2) {
1004 for (
size_t j=0;
j<cp;
j++) {
1005 uint16_t cind = loff +
j*iagg;
1006 cdindex[cind] = cdpix +
j;
1010 for (
size_t j=0;
j<sp;
j++) {
1011 uint16_t sind = loff +
j*8;
1012 sdindex[sind] = sdpix +
j;
1020 cout <<
"Data zone " <<
i <<
" type " << itable[
i].
dtype <<
1021 " has zero lines" << endl;
1025 if ( loff >= maxlines)
break;
1035 uint16_t btaps[16], uint16_t rtaps[16],
1037 uint16_t **bbands, uint16_t **rbands, uint32_t **sbands,
1038 int16_t *blines, int16_t *rlines, int16_t *slines,
1039 uint16_t &btype, uint16_t bagg[16],
1040 uint16_t &rtype, uint16_t ragg[16],
1041 int8_t *sfrm,
int &iret) {
1045 for (
size_t i=0;
i<ncps;
i++) {
1049 for (
size_t i=0;
i<msps;
i++) slines[
i] = -1;
1051 std::vector<int> iswav = {0,1,2,3,4,5,6,7,8};
1062 uint16_t **ccddata =
new uint16_t*;
1063 uint16_t ossdata[16];
1080 iswav = {3,0,1,2,8,6,7,5,4};
1083 for (
size_t ipkt=0; ipkt<npkts; ipkt++) {
1084 apid = (pbuffer[ipkt][0] % 8)*256 + pbuffer[ipkt][1];
1085 uint16_t
dtype = (pbuffer[ipkt][12] % 64) / 4;
1086 memcpy( &ui32, &pbuffer[ipkt][6], 4);
1090 if (apid == 700 &&
dtype > 0 && spn ==
spin) {
1092 iagg, jagg,
nbands, ccddata, ossdata);
1102 memcpy( bagg, jagg, 16*
sizeof(uint16_t));
1103 for (
size_t i=0;
i<ncps;
i++) {
1104 bbands[
i] =
new uint16_t[
nbands];
1105 for (
size_t j=0;
j<
nbands;
j++) bbands[
i][
j] = 65535;
1111 for (
size_t i=0;
i<16;
i++)
1112 if (jagg[
i] != bagg[
i]) agg++;
1115 cout <<
"Data type or spectral aggregation error, CCDID: " <<
1116 ccdid <<
" Line: " <<
line << endl;
1122 memcpy( bbands[ibpix], &(*ccddata)[0],
nb*
sizeof(uint16_t));
1123 blines[ibpix] = (
line/iagg) * iagg;
1127 cout <<
"Number of blue pixels exceeded in spin: " <<
spin
1128 <<
" in packet (0-based): " << ipkt << endl;
1137 memcpy( ragg, jagg, 16*
sizeof(uint16_t));
1138 for (
size_t i=0;
i<ncps;
i++) {
1139 rbands[
i] =
new uint16_t[
nbands];
1140 for (
size_t j=0;
j<
nbands;
j++) rbands[
i][
j] = 65535;
1145 for (
size_t i=0;
i<16;
i++)
1146 if (jagg[
i] != ragg[
i]) agg++;
1149 cout <<
"Data type or spectral aggregation error, CCDID: " <<
1150 ccdid <<
" Line: " <<
line << endl;
1157 memcpy( rbands[irpix], &(*ccddata)[0],
nb*
sizeof(uint16_t));
1158 rlines[irpix] = (
line/iagg) * iagg;
1162 cout <<
"Number of red pixels exceeded in spin: " <<
spin
1163 <<
" in packet (0-based): " << ipkt << endl;
1176 if (apid == 720 && (ispix+7) < msps && spn ==
spin) {
1179 uint32_t swirdata[8*9];
1184 for (
size_t i=0;
i<8;
i++) {
1185 slines[ispix+
i] = (
lines[
i]/8) * 8;
1186 for (
size_t j=0;
j<9;
j++) sbands[ispix+
i][
j] = swirdata[iswav[
j]+9*
i];
1188 memcpy(&sfrm[ispix], swirfrm, 8*
sizeof(uint8_t));
1193 if ((ibpix <= 0) || (ibpix > ncps)) blines[0] = -1;
1194 if ((irpix <= 0) || (irpix > ncps)) rlines[0] = -1;
1195 if ((ispix <= 0) || (ispix > msps)) slines[0] = -1;
1207 uint16_t &ccdid, uint32_t &
line, uint16_t &
dtype,
1208 uint16_t &iagg, uint16_t jagg[16], uint16_t &
nbands,
1209 uint16_t **ccddata, uint16_t ossdata[16]) {
1212 ccdid = (packet[12] & 64) / 64;
1213 line = packet[10]*256 + packet[11];
1214 dtype = (packet[12] % 64) / 4;
1215 uint16_t oss = packet[17] % 16;
1216 iagg = packet[12] % 4;
1217 uint16_t agg[4] = {1,2,4,8};
1222 if (ccdid) ftaps = btaps;
else ftaps = rtaps;
1225 uint16_t its[4] = {0,4,8,12};
1227 for (
size_t i=0;
i<4;
i++) {
1228 jagg[its[
i]] = agg[(packet[13+
i] & 192) / 64];
1229 jagg[its[
i]+1] = agg[(packet[13+
i] & 48) / 16];
1230 jagg[its[
i]+2] = agg[(packet[13+
i] & 12) / 4];
1231 jagg[its[
i]+3] = agg[(packet[13+
i] & 3)];
1233 for (
size_t i=0;
i<16;
i++) taps[
i] = 32*ftaps[
i] / jagg[
i];
1237 for (
size_t i=0;
i<16;
i++)
nbands += taps[
i];
1240 *ccddata =
new uint16_t[
nbands];
1243 uint16_t ibnd =
nbands - 1;
1246 for (
size_t j=0;
j<16;
j++) {
1251 for (
size_t i=0;
i<taps[
j];
i++) {
1253 memcpy( &ui16, &packet[
ioff+2*
i], 2);
1255 memcpy( &(*ccddata)[ibnd-
i], &ui16, 2);
1263 for (
size_t j=0;
j<16;
j++) {
1265 memcpy( &ui16, &packet[
ioff+2*
j], 2);
1267 memcpy(&ossdata[
j], &ui16, 2);
1276 uint32_t *swirdata) {
1287 for (
size_t i=0;
i<8;
i++) {
1290 if ((
unsigned(packet[
ioff])==255) && (
unsigned(packet[
ioff+1])==255)) {
1294 slines[
i] = packet[
ioff]*256 + packet[
ioff+1];
1297 smeta = packet[
ioff+2];
1298 swirfrm[
i] = smeta % 8;
1311 uint16_t ndcs, uint16_t ndss,
1312 uint16_t nbbs, uint16_t nrbs, uint16_t nswb,
1313 int16_t *cindex, int16_t *sindex,
1314 int16_t *cdindex, int16_t *sdindex,
1315 uint16_t **bbands, uint16_t **rbands,
1317 int16_t *blines, int16_t *rlines, int16_t *slines,
1318 uint16_t **bsci, uint16_t **rsci, uint32_t **ssci,
1319 uint16_t **bdark, uint16_t **rdark, uint32_t **sdark,
1320 uint8_t &linerr,
int &icheck) {
1325 bool linchk = (
dtype != 5);
1329 bool bb = ((bbands[0] !=
NULL) && (blines[0] != -1));
1331 bool rb = ((rbands[0] !=
NULL) && (rlines[0] != -1));
1333 bool sb = ((sbands[0] !=
NULL) && (slines[0] != -1));
1337 if (bbands[0] ==
NULL) nbb = 0;
else nbb = nbbs;
1341 if ( bb && linchk) {
1342 for (
size_t i=0;
i<ncpt;
i++) {
1343 if (blines[
i] == -1)
continue;
1344 if (cindex[blines[
i]] == -1) mb++;
1346 for (
size_t i=0;
i<ncpt;
i++)
1347 if (cindex[blines[
i]] != (cindex[blines[0]] + (
int)
i)) {
1348 cout <<
"Blue CCD line sequence error" << endl;
1357 for (
size_t i=0;
i<ncpt;
i++) {
1358 if (blines[
i] == -1)
continue;
1360 if (cindex[blines[
i]] > -1) nbs++;
1362 if ((nbs < ncpt-ndcs) && linchk) {
1363 cout <<
"Missing blue band science pixels" << endl;
1367 for (
size_t i=0;
i<ncpt;
i++) {
1368 if (blines[
i] == -1)
continue;
1370 if (cindex[blines[
i]] > -1) {
1371 for (
size_t j=0;
j<nbb;
j++) {
1372 bsci[
j][cindex[blines[
i]]] = bbands[
i][
j];
1379 for (
size_t i=0;
i<ncpt;
i++) {
1380 if (blines[
i] == -1)
continue;
1382 if (cdindex[blines[
i]] > -1) nbd++;
1385 cout <<
"Missing blue band dark pixels" << endl;
1389 for (
size_t i=0;
i<ncpt;
i++) {
1390 if (blines[
i] == -1)
continue;
1392 if (cdindex[blines[
i]] > -1) {
1394 int k = cdindex[blines[
i]];
1395 if (
k >= ndcs ||
k < 0) {
1400 for (
size_t j=0;
j<nbb;
j++) {
1401 bdark[
j][
k] = bbands[
i][
j];
1409 if (rbands[0] ==
NULL) nrb = 0;
else nrb = nrbs;
1413 if ( rb && linchk) {
1414 for (
size_t i=0;
i<ncpt;
i++) {
1415 if (rlines[
i] == -1)
continue;
1416 if (cindex[rlines[
i]] == -1) mr++;
1418 for (
size_t i=0;
i<ncpt;
i++) {
1419 if (rlines[
i] == -1)
continue;
1420 if (cindex[rlines[
i]] != (cindex[rlines[0]] + (
int)
i)) {
1421 cout <<
"Red CCD line sequence error" << endl;
1431 for (
size_t i=0;
i<ncpt;
i++) {
1432 if (rlines[
i] == -1)
continue;
1434 if (cindex[rlines[
i]] > -1) nrs++;
1437 if ((nrs < ncpt-ndcs) && linchk) {
1438 cout <<
"Missing red band science pixels" << endl;
1442 for (
size_t i=0;
i<ncpt;
i++) {
1444 if (rlines[
i] == -1)
continue;
1445 if (rlines[
i] > -1 && cindex[rlines[
i]] > -1) {
1446 for (
size_t j=0;
j<nrb;
j++) {
1447 rsci[
j][cindex[rlines[
i]]] = rbands[
i][
j];
1454 for (
size_t i=0;
i<ncpt;
i++) {
1455 if (rlines[
i] == -1)
continue;
1457 if (cdindex[rlines[
i]] > -1) nrd++;
1461 cout <<
"Missing red band dark pixels" << endl;
1465 for (
size_t i=0;
i<ncpt;
i++) {
1466 if (rlines[
i] == -1)
continue;
1468 if (cdindex[rlines[
i]] > -1) {
1470 int k = cdindex[rlines[
i]];
1471 if (
k >= ndcs ||
k < 0) {
1476 for (
size_t j=0;
j<nrb;
j++) {
1477 rdark[
j][
k] = rbands[
i][
j];
1483 if (mb != -1 || mr !=-1) {
1485 cout <<
"Invalid line numbers" << endl;
1495 for (
size_t i=0;
i<nspt0;
i++) {
1496 if (slines[
i] > -1) {
1498 for (
size_t j=0;
j<nswb;
j++) {
1499 if (sindex[slines[
i]*nswb+
j] > -1) {
1500 ssci[
j][sindex[slines[
i]*nswb+
j]] = sbands[
i][
j];
1506 if (nss < nspt0-ndss) {
1507 cout <<
"Missing SWIR band science pixels" << endl;
1513 for (
size_t i=0;
i<nspt0;
i++) {
1514 if (slines[
i] == -1)
continue;
1516 if (sdindex[slines[
i]] > -1) nsd++;
1523 for (
size_t i=0;
i<nspt0;
i++) {
1525 if (slines[
i] == -1)
continue;
1526 if (sdindex[slines[
i]] > -1) {
1528 int k = sdindex[slines[
i]];
1529 if (
k >= ndss ||
k < 0) {
1530 cout <<
"sdark indice out of bounds: " <<
k << endl;
1534 for (
size_t j=0;
j<nswb;
j++) {
1535 sdark[
j][
k] = sbands[
i][
j];
1543 uint8_t
check_sum( int32_t nc, uint8_t *dat, uint8_t *chk) {
1548 uint8_t chks[4],
tmp[4];
1549 memcpy( chks, &dat[0], 4);
1551 for (
int i=1;
i<nc;
i++) {
1552 memcpy( &
tmp, &dat[4*
i], 4);
1553 for (
int j=0;
j<4;
j++) chks[
j] = chks[
j] ^
tmp[
j];
1557 for (
int i=0;
i<4;
i++)
check[
i] = (chk[
i] == chks[
i]);
1569 uint16_t ncps, uint16_t nbbs, uint16_t nrbs,
1570 uint16_t nsps, uint16_t ndcs) {
1573 l1afile =
new NcFile( l1_filename, NcFile::replace);
1575 catch ( NcException& e) {
1577 cerr <<
"Failure creating OCI L1A file: " << l1_filename << endl;
1581 fileName.assign( l1_filename);
1583 ifstream oci_l1a_data_structure;
1585 string dataStructureFile;
1586 dataStructureFile.assign(
"$OCDATAROOT/oci/OCI_Level-1A_Data_Structure.cdl");
1589 oci_l1a_data_structure.open( dataStructureFile.c_str(), ifstream::in);
1590 if ( oci_l1a_data_structure.fail() ==
true) {
1591 cout <<
"\"" << dataStructureFile.c_str() <<
"\" not found" << endl;
1597 getline( oci_l1a_data_structure,
line);
1598 size_t pos =
line.find(
"dimensions:");
1599 if (
pos == 0)
break;
1606 getline( oci_l1a_data_structure,
line);
1607 if (
line.substr(0,2) ==
"//")
continue;
1609 size_t pos =
line.find(
" = ");
1610 size_t semi =
line.find(
" ;");
1611 if (
pos == string::npos)
break;
1616 string dimString =
line.substr(
pos+2, semi-(
pos+2));
1617 if (dimString.find(
"UNLIMITED") == string::npos) {
1621 dimSize = NC_UNLIMITED;
1626 iss >> skipws >>
line;
1631 if (
line.compare(
"ccd_pixels") == 0) {
1635 if (
line.compare(
"SWIR_pixels") == 0) {
1639 if (
line.compare(
"DC_pixels") == 0) {
1643 if (
line.compare(
"blue_bands") == 0) {
1647 if (
line.compare(
"red_bands") == 0) {
1652 ncDims[ndims++] = l1afile->addDim(
line, dimSize);
1654 catch ( NcException& e) {
1656 cerr <<
"Failure creating dimension: " <<
line.c_str() << endl;
1664 getline( oci_l1a_data_structure,
line);
1665 size_t pos =
line.find(
"// global attributes");
1666 if (
pos == 0)
break;
1670 getline( oci_l1a_data_structure,
line);
1671 size_t pos =
line.find(
" = ");
1672 if (
pos == string::npos)
break;
1677 attValue.assign(
line.substr(
pos+4));
1678 size_t posQuote = attValue.find(
"\"");
1679 attValue.assign(attValue.substr(0, posQuote));
1681 istringstream iss(
line.substr(
pos+2));
1684 iss >> skipws >>
line;
1687 if (
line.substr(0,2) ==
"//")
continue;
1690 attName.assign(
line.substr(1).c_str());
1693 if (attName.compare(
"orbit_number") == 0)
continue;
1694 if (attName.compare(
"history") == 0)
continue;
1695 if (attName.compare(
"format_version") == 0)
continue;
1696 if (attName.compare(
"instrument_number") == 0)
continue;
1697 if (attName.compare(
"pixel_offset") == 0)
continue;
1698 if (attName.compare(
"number_of_filled_scans") == 0)
continue;
1702 l1afile->putAtt(attName, attValue);
1704 catch ( NcException& e) {
1706 cerr <<
"Failure creating attribute: " + attName << endl;
1716 getline( oci_l1a_data_structure,
line);
1720 if (
line.substr(0,1).compare(
"}") == 0) {
1721 oci_l1a_data_structure.close();
1726 size_t pos =
line.find(
"group:");
1732 istringstream iss(
line.substr(6, string::npos));
1733 iss >> skipws >>
line;
1735 ncGrps[ngrps++] = l1afile->addGroup(
line);
1743 string flag_meanings;
1747 double fill_value=0.0;
1749 vector<NcDim> varVec;
1755 getline( oci_l1a_data_structure,
line);
1757 getline( oci_l1a_data_structure,
line);
1759 if (
line.substr(0,2) ==
"//")
continue;
1760 if (
line.length() == 0)
continue;
1761 if (
line.substr(0,1).compare(
"\r") == 0)
continue;
1762 if (
line.substr(0,1).compare(
"\n") == 0)
continue;
1767 if (
pos == string::npos) {
1772 createNCDF( ncGrps[ngrps-1], sname.c_str(), lname.c_str(),
1774 (
void *) &fill_value,
1775 flag_values.c_str(), flag_meanings.c_str(),
1779 flag_values.assign(
"");
1780 flag_meanings.assign(
"");
1781 reference.assign(
"");
1790 if (
line.substr(0,10).compare(
"} // group") == 0)
break;
1794 istringstream iss(
line);
1795 iss >> skipws >> varType;
1798 if ( varType.compare(
"char") == 0) ntype = NC_CHAR;
1799 else if ( varType.compare(
"byte") == 0) ntype = NC_BYTE;
1800 else if ( varType.compare(
"short") == 0) ntype = NC_SHORT;
1801 else if ( varType.compare(
"int") == 0) ntype = NC_INT;
1802 else if ( varType.compare(
"long") == 0) ntype = NC_INT;
1803 else if ( varType.compare(
"float") == 0) ntype = NC_FLOAT;
1804 else if ( varType.compare(
"real") == 0) ntype = NC_FLOAT;
1805 else if ( varType.compare(
"double") == 0) ntype = NC_DOUBLE;
1806 else if ( varType.compare(
"ubyte") == 0) ntype = NC_UBYTE;
1807 else if ( varType.compare(
"ushort") == 0) ntype = NC_USHORT;
1808 else if ( varType.compare(
"uint") == 0) ntype = NC_UINT;
1809 else if ( varType.compare(
"ulong") == 0) ntype = NC_UINT;
1810 else if ( varType.compare(
"int64") == 0) ntype = NC_INT64;
1811 else if ( varType.compare(
"uint64") == 0) ntype = NC_UINT64;
1815 size_t posSname =
line.substr(0,
pos).rfind(
" ");
1816 sname.assign(
line.substr(posSname+1,
pos-posSname-1));
1820 this->
parseDims( line.substr(
pos+1, string::npos), varVec);
1821 numDims = varVec.size();
1825 size_t posEql =
line.find(
"=");
1826 size_t pos1qte =
line.find(
"\"");
1827 size_t pos2qte =
line.substr(pos1qte+1, string::npos).find(
"\"");
1833 if (
attrName.compare(
"long_name") == 0) {
1834 lname.assign(
line.substr(pos1qte+1, pos2qte));
1839 else if (
attrName.compare(
"units") == 0) {
1840 units.assign(
line.substr(pos1qte+1, pos2qte));
1845 else if (
attrName.compare(
"_FillValue") == 0) {
1847 iss.str(
line.substr(posEql+1, string::npos));
1852 else if (
attrName.compare(
"flag_values") == 0) {
1853 flag_values.assign(
line.substr(pos1qte+1, pos2qte));
1857 else if (
attrName.compare(
"flag_meanings") == 0) {
1858 flag_meanings.assign(
line.substr(pos1qte+1, pos2qte));
1862 else if (
attrName.compare(
"reference") == 0) {
1863 reference.assign(
line.substr(pos1qte+1, pos2qte));
1867 else if (
attrName.compare(
"valid_min") == 0) {
1869 iss.str(
line.substr(posEql+1, string::npos));
1875 else if (
attrName.compare(
"valid_max") == 0) {
1877 iss.str(
line.substr(posEql+1, string::npos));
1898 size_t pos = dimString.find(
",", curPos);
1899 if (
pos == string::npos)
1900 pos = dimString.find(
")");
1903 istringstream iss(dimString.substr(curPos,
pos-curPos));
1904 iss >> skipws >> varDimName;
1906 for (
int i=0;
i<ndims;
i++) {
1909 dimName = ncDims[
i].getName();
1911 catch ( NcException& e) {
1913 cerr <<
"Failure accessing dimension: " + dimName << endl;
1917 if ( varDimName.compare(dimName) == 0) {
1919 varDims.push_back(ncDims[
i]);
1923 if ( dimString.substr(
pos, 1).compare(
")") == 0)
break;
1933 uint16_t nbbs, uint16_t nrbs,
1935 uint16_t ncps, uint16_t nsps,
1936 uint16_t **bsci, uint16_t **rsci,
1937 uint32_t **ssci, int8_t *sfrms) {
1945 vector<size_t>
start;
1946 vector<size_t> count_b;
1947 vector<size_t> count_r;
1948 vector<size_t> count_s;
1949 vector<size_t> count_0;
1951 vector<size_t> start_frms;
1952 vector<size_t> count_frms;
1954 NcGroup gid = l1afile->getGroup(
"science_data");
1955 blu_bands = gid.getVar(
"sci_blue");
1956 red_bands = gid.getVar(
"sci_red");
1957 swir_bands = gid.getVar(
"sci_SWIR");
1958 swir_frms = gid.getVar(
"frm_type_SWIR");
1964 count_b.push_back(1);
1965 count_b.push_back(nbbs);
1966 count_b.push_back(ncps);
1968 count_r.push_back(1);
1969 count_r.push_back(nrbs);
1970 count_r.push_back(ncps);
1972 count_s.push_back(1);
1973 count_s.push_back(nswb);
1974 count_s.push_back(nsps);
1976 count_0.push_back(1);
1977 count_0.push_back(1);
1978 count_0.push_back(1);
1988 (count_b[1] > 0) ? blu_bands.putVar(
start, count_b, &bsci[0][0]) : blu_bands.putVar(
start, count_0, &bsci[0][0]);
1989 (count_r[1] > 0) ? red_bands.putVar(
start, count_r, &rsci[0][0]) : red_bands.putVar(
start, count_0, &rsci[0][0]);
1990 (count_s[1] > 0) ? swir_bands.putVar(
start, count_s, &ssci[0][0]) : swir_bands.putVar(
start, count_0, &ssci[0][0]);
1992 start_frms.push_back(isc);
1993 start_frms.push_back(0);
1995 count_frms.push_back(1);
1996 count_frms.push_back(nsps);
1998 if ( count_frms[1] > 0)
1999 swir_frms.putVar(start_frms, count_frms, sfrms);
2006 uint16_t nbbs, uint16_t nrbs, uint16_t nswb,
2007 uint16_t ndcs, uint16_t ndss,
2008 uint16_t *dark_b, uint16_t *dark_r,
2015 vector<size_t>
start;
2016 vector<size_t> count_b;
2017 vector<size_t> count_r;
2018 vector<size_t> count_s;
2019 vector<size_t> count_0;
2025 count_b.push_back(isc);
2026 count_b.push_back(nbbs);
2027 count_b.push_back(ndcs);
2029 count_r.push_back(isc);
2030 count_r.push_back(nrbs);
2031 count_r.push_back(ndcs);
2033 count_s.push_back(isc);
2034 count_s.push_back(nswb);
2035 count_s.push_back(ndss);
2037 count_0.push_back(1);
2038 count_0.push_back(1);
2039 count_0.push_back(1);
2041 NcGroup gid = l1afile->getGroup(
"onboard_calibration_data");
2042 blu_dark = gid.getVar(
"DC_blue");
2043 red_dark = gid.getVar(
"DC_red");
2044 swr_dark = gid.getVar(
"DC_SWIR");
2047 (count_b[1] > 0) ? blu_dark.putVar(
start, count_b, dark_b) : blu_dark.putVar(
start, count_0, dark_b);
2048 (count_r[1] > 0) ? red_dark.putVar(
start, count_r, dark_r) : red_dark.putVar(
start, count_0, dark_r);
2049 (count_s[1] > 0) ? swr_dark.putVar(
start, count_s, dark_s) : swr_dark.putVar(
start, count_0, dark_s);
2052 vector<size_t> start_sdfrm;
2053 vector<size_t> count_sdfrm;
2055 start_sdfrm.push_back(0);
2056 start_sdfrm.push_back(0);
2057 count_sdfrm.push_back(isc);
2058 count_sdfrm.push_back(ndss);
2060 sdfrm_dark = gid.getVar(
"frm_type_DC_SWIR");
2062 if ( count_sdfrm[1] > 0)
2063 sdfrm_dark.putVar(start_sdfrm, count_sdfrm, sdfrms);
2070 uint8_t *seqerr, uint8_t *linerr,
2073 vector<size_t>
start;
2074 vector<size_t>
count;
2076 count.push_back(isc);
2078 uint32_t ancap = 636;
2080 int32_t iyear, iday;
2085 double *stimes =
new double[isc];
2086 short int toff = 24;
2087 uint32_t *scss =
new uint32_t[isc];
2088 int32_t *scsu =
new int32_t[isc];
2090 for (
size_t i=0;
i<isc;
i++) {
2092 if (apid == ancap) {
2097 memcpy( &ui32, &ancdata[
i*
ANCSIZE+toff+4], 4);
2099 memcpy( &ui32, &ancdata[
i*
ANCSIZE+toff+8], 4);
2100 scsu[
i] =
SWAP_4( ui32) / 4096;
2108 NcGroup gid = l1afile->getGroup(
"scan_line_attributes");
2113 var = gid.getVar(
"scan_start_time");
2119 var = gid.getVar(
"scan_start_CCSDS_sec");
2123 var = gid.getVar(
"scan_start_CCSDS_usec");
2128 uint8_t *hamSide =
new uint8_t[isc];
2130 for (
size_t i=0;
i<isc;
i++) {
2138 hamSide[
i] = (uint8_t) ham;
2140 var = gid.getVar(
"HAM_side");
2147 for (
size_t i=0;
i<isc;
i++) {
2149 memcpy( &ui32, &ancdata[
i*
ANCSIZE+toff], 4);
2152 var = gid.getVar(
"spin_ID");
2157 var = gid.getVar(
"pseq_flag");
2159 var = gid.getVar(
"line_flag");
2174 vector<size_t>
start;
2175 vector<size_t>
count;
2182 int16_t *anctlm =
new int16_t[6*isc];
2183 for (
size_t i=0;
i<6*isc;
i++) anctlm[
i]=-999;
2184 for (
size_t i=0;
i<isc;
i++) {
2185 for (
size_t j=0;
j<6;
j++) {
2195 short nagg[4] = {1,2,4,8};
2198 int32_t iyear, iday;
2201 uint32_t
jd =
jday(iyear, 1, iday);
2203 for (
size_t i=0;
i<10;
i++) {
2206 iagg[
i] = ancdata[
ioff+2] % 4;
2207 if (
dtype[
i] != 0) iagg[
i] = nagg[iagg[
i]];
2216 uint16_t btap = ancdata[
ioff+2] * 256 + ancdata[
ioff+3];
2217 uint16_t rtap = ancdata[
ioff+0] * 256 + ancdata[
ioff+1];
2223 memcpy(&ui32, &ancdata[
ioff+8], 4);
2224 uint32_t bagg =
SWAP_4(ui32);
2226 memcpy(&ui32, &ancdata[
ioff+4], 4);
2227 uint32_t ragg =
SWAP_4(ui32);
2229 int16_t baggs[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2230 int16_t raggs[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2237 for (
size_t i=0;
i<16;
i++) {
2238 uint16_t btaps = (btap & ken) / ken;
2239 if (btaps) baggs[15-
i] = nagg[(bagg & kag) / lag];
2240 uint16_t rtaps = (rtap & ken) / ken;
2241 if (rtaps) raggs[15-
i] = nagg[(ragg & kag) / lag];
2254 gid = l1afile->getGroup(
"spatial_spectral_modes");
2256 count.push_back(10);
2259 var = gid.getVar(
"spatial_zone_data_type");
2263 var = gid.getVar(
"spatial_aggregation");
2267 var = gid.getVar(
"spatial_zone_lines");
2271 count.push_back(16);
2274 var = gid.getVar(
"blue_spectral_mode");
2278 var = gid.getVar(
"red_spectral_mode");
2285 gid = l1afile->getGroup(
"engineering_data");
2288 count.push_back(isc);
2293 var = gid.getVar(
"ancillary_tlm");
2300 int32_t *aggcon =
new int32_t[isc];
2301 for (
size_t i=0;
i<isc;
i++) {
2305 var = gid.getVar(
"agg_control");
2311 uint16_t *bagerr =
new uint16_t[isc];
2312 uint16_t *ragerr =
new uint16_t[isc];
2313 for (
size_t i=0;
i<isc;
i++) {
2320 var = gid.getVar(
"blue_agg_error");
2322 var = gid.getVar(
"red_agg_error");
2329 int32_t *digerr =
new int32_t[isc];
2330 for (
size_t i=0;
i<isc;
i++) {
2334 var = gid.getVar(
"dig_card_error");
2346 int32_t *spinID, uint16_t &cdsmode, uint32_t isc) {
2355 const uint16_t dauapid = 723;
2356 const uint16_t ddcapid = 701;
2357 const uint16_t mceapid = 713;
2358 const uint16_t encapid = 712;
2359 const uint16_t scaapid = 717;
2360 const uint16_t senapid = 716;
2361 const uint16_t tcapid = 656;
2362 const uint16_t ddctapid = 703;
2363 const uint16_t dauctapid = 744;
2364 const uint16_t icdumcetapid = 745;
2370 uint16_t nicthrm = 74;
2371 uint16_t ndctmps = 69;
2372 uint16_t nimtmps = 16;
2373 uint16_t nadclat = 4;
2384 int16_t *tlmzs =
new int16_t[2];
2385 int16_t *tlmzd =
new int16_t[2];
2386 uint16_t tditime = 0;
2388 double *dausec =
new double[ntlm];
2389 for (
size_t i=0;
i<ntlm;
i++) dausec[
i]=-999;
2390 int32_t *dauspin =
new int32_t[ntlm];
2391 for (
size_t i=0;
i<ntlm;
i++) dauspin[
i]=-999;
2392 uint8_t *dautlm =
new uint8_t[620*ntlm];
2393 for (
size_t i=0;
i<620*ntlm;
i++) dautlm[
i]=0;
2394 double *ddcsec =
new double[ntlm];
2395 for (
size_t i=0;
i<ntlm;
i++) ddcsec[
i]=-999;
2396 uint8_t *ddctlm =
new uint8_t[524*ntlm];
2397 for (
size_t i=0;
i<524*ntlm;
i++) ddctlm[
i]=0;
2398 int32_t *mcespin =
new int32_t[ntlm];
2399 for (
size_t i=0;
i<ntlm;
i++) mcespin[
i]=-999;
2400 uint8_t *mcetlm =
new uint8_t[480*ntlm];
2401 for (
size_t i=0;
i<480*ntlm;
i++) mcetlm[
i]=0;
2402 int32_t *encspin =
new int32_t[ntlm];
2403 for (
size_t i=0;
i<ntlm;
i++) encspin[
i]=-999;
2404 int16_t *encoder =
new int16_t[4*200*ntlm];
2405 for (
size_t i=0;
i<4*200*ntlm;
i++) encoder[
i]=-999;
2406 int16_t *auxparms =
new int16_t[33];
2407 for (
size_t i=0;
i<33;
i++) auxparms[
i] = -999;
2408 int32_t *scaspin =
new int32_t[ntlm];
2409 for (
size_t i=0;
i<ntlm;
i++) scaspin[
i]=-999;
2410 uint8_t *scatlm =
new uint8_t[480*ntlm];
2411 for (
size_t i=0;
i<480*ntlm;
i++) scatlm[
i]=0;
2412 int32_t *senspin =
new int32_t[ntlm];
2413 for (
size_t i=0;
i<ntlm;
i++) senspin[
i]=-999;
2414 int16_t *sencoder =
new int16_t[4*200*ntlm];
2415 for (
size_t i=0;
i<4*200*ntlm;
i++) sencoder[
i]=-999;
2416 double *tcsec =
new double[ntlm];
2417 for (
size_t i=0;
i<ntlm;
i++) tcsec[
i]=-999;
2418 uint8_t *tctlm =
new uint8_t[1216*ntlm];
2419 for (
size_t i=0;
i<1216*ntlm;
i++) tctlm[
i]=0;
2420 double *dauctsec =
new double[ntlm];
2421 for (
size_t i=0;
i<ntlm;
i++) dauctsec[
i]=-999;
2422 double *icdumcetsec =
new double[ntlm];
2423 for (
size_t i=0;
i<ntlm;
i++) icdumcetsec[
i]=-999;
2424 uint8_t *icdumcettlm =
new uint8_t[76*ntlm];
2425 for (
size_t i=0;
i<76*ntlm;
i++) icdumcettlm[
i]=0;
2436 float *icdutherm =
new float[nicthrm*ntlm];
2437 for (
size_t i=0;
i<nicthrm*ntlm;
i++) icdutherm[
i]=-999;
2438 float *dauctemp =
new float[ndctmps*ntlm];
2439 for (
size_t i=0;
i<ndctmps*ntlm;
i++) dauctemp[
i]=-999;
2440 float *icdumcetemp =
new float[nimtmps*ntlm];
2441 for (
size_t i=0;
i<nimtmps*ntlm;
i++) icdumcetemp[
i]=-999;
2442 uint8_t *adclat =
new uint8_t[nadclat*ntlm];
2443 for (
size_t i=0;
i<nadclat*ntlm;
i++) adclat[
i]=0;
2444 uint8_t *cdsdis =
new uint8_t[ntlm];
2445 for (
size_t i=0;
i<ntlm;
i++) cdsdis[
i]=0;
2446 uint8_t *hamside =
new uint8_t[ntlm];
2447 uint32_t *redmask =
new uint32_t[16];
2448 uint32_t *bluemask =
new uint32_t[16];
2450 for (
size_t i=0;
i<ntlm;
i++) {
2451 uint32_t apid = ((uint8_t) tlmdata[
i][0] % 8)*256 + (uint8_t) tlmdata[
i][1];
2454 uint8_t cctime[8]={0,0,0,0,0,0,0,0};
2465 memcpy(cctime, &tlmdata[
i][6], 6);
2468 memcpy( &ui32, &tlmdata[
i][12], 4);
2469 dauspin[ndau] =
SWAP_4(ui32);
2470 memcpy( &dautlm[ndau*620], &tlmdata[
i][16], 620);
2529 memcpy(cctime, &tlmdata[
i][6], 6);
2532 memcpy( &ddctlm[nddc*524], &tlmdata[
i][12], 524);
2533 cdsdis[nddc] = tlmdata[
i][29];
2534 memcpy( &adclat[nddc*nadclat], &tlmdata[
i][176], nadclat);
2536 for (
size_t j=0;
j<16;
j++) {
2537 memcpy( &ui32, &tlmdata[
i][196+
j*4], 4);
2538 redmask[
j] = (uint32_t)
SWAP_4(ui32);
2540 memcpy( &ui32, &tlmdata[
i][260+
j*4], 4);
2541 bluemask[
j] = (uint32_t)
SWAP_4(ui32);
2550 memcpy( &ui32, &tlmdata[
i][12], 4);
2551 mcespin[nmce] =
SWAP_4(ui32);
2552 memcpy( &mcetlm[nmce*480], &tlmdata[
i][16], 480);
2553 hamside[nmce] = (tlmdata[
i][49] & 8) / 8;
2560 memcpy( &ui32, &tlmdata[
i][12], 4);
2561 encspin[nenc] =
SWAP_4(ui32);
2563 for (
size_t j=0;
j<4*200;
j++) {
2564 memcpy( &i16, &tlmdata[
i][16+2*
j], 2);
2565 encoder[nenc*4*200+
j] =
SWAP_2(i16);
2573 memcpy( &ui32, &tlmdata[
i][12], 4);
2574 scaspin[nsca] =
SWAP_4(ui32);
2575 memcpy( &scatlm[nsca*480], &tlmdata[
i][16], 480);
2582 memcpy( &ui32, &tlmdata[
i][12], 4);
2583 senspin[nsen] =
SWAP_4(ui32);
2584 for (
size_t j=0;
j<4*200;
j++) {
2585 memcpy( &i16, &tlmdata[
i][16+2*
j], 2);
2586 sencoder[nsen*4*200+
j] =
SWAP_2(i16);
2594 memcpy(cctime, &tlmdata[
i][6], 6);
2597 memcpy( &tctlm[ntc*1216], &tlmdata[
i][12], 1216);
2599 for (
size_t j=0;
j<nicthrm;
j++) {
2600 memcpy( &
f32, &tlmdata[
i][184+4*
j], 4);
2611 for (
size_t j=0;
j<33;
j++) {
2612 memcpy( &ui32, &tlmdata[
i][64+
j*4], 4);
2613 auxparms[
j] = (int16_t)
SWAP_4(ui32);
2621 memcpy(cctime, &tlmdata[
i][6], 6);
2623 dauctsec[ndct] = sc;
2625 for (
size_t j=0;
j<ndctmps;
j++) {
2626 memcpy( &
f32, &tlmdata[
i][12+4*
j], 4);
2634 memcpy(cctime, &tlmdata[
i][6], 6);
2636 icdumcetsec[nimt] = sc;
2637 memcpy( &icdumcettlm[nimt*76], &tlmdata[
i][12], 76);
2638 for (
size_t j=0;
j<nimtmps;
j++) {
2639 memcpy( &
f32, &tlmdata[
i][24+4*
j], 4);
2651 vector<size_t>
start;
2652 vector<size_t>
count;
2656 gid = l1afile->getGroup(
"engineering_data");
2658 if (ndau==0) ndau = 1;
2662 count.push_back(ndau);
2664 var = gid.getVar(
"DAU_tlm_time");
2667 var = gid.getVar(
"DAU_spin_ID");
2671 count.push_back(620);
2673 var = gid.getVar(
"DAU_telemetry");
2703 tlmzs[0] = dautlm[123];
2704 tlmzs[1] = dautlm[125];
2705 tlmzd[0] = dautlm[122];
2706 tlmzd[1] = dautlm[124];
2710 for (
size_t i=0;
i<16;
i++) {
2711 redmask[
i] = 4294967295;
2712 bluemask[
i] = 4294967295;
2720 count.push_back(nddc);
2723 var = gid.getVar(
"DDC_tlm_time");
2727 var = gid.getVar(
"CDS_disable");
2731 count.push_back(524);
2734 var = gid.getVar(
"DDC_telemetry");
2738 count.push_back(nadclat);
2741 var = gid.getVar(
"ADC_latency");
2748 count.push_back(16);
2749 var = gid.getVar(
"blue_channel_mask");
2751 var = gid.getVar(
"red_channel_mask");
2755 cdsmode = cdsdis[0] * (adclat[0]-14);
2759 memcpy( &ui16, &ddctlm[346], 2);
2763 if (nmce==0) nmce=1;
2768 count.push_back(nmce);
2771 var = gid.getVar(
"MCE_spin_ID");
2776 uint8_t *
mside =
new uint8_t[isc];
2777 for (
size_t i=0;
i<isc;
i++)
mside[
i] = 255;
2778 for (
size_t i=0;
i<isc;
i++) {
2779 for (
size_t j=0;
j<ntlm;
j++) {
2780 if ( (
int) mcespin[
j] == spinID[
i]) {
2787 count.push_back(isc);
2788 NcGroup scgid = l1afile->getGroup(
"scan_line_attributes");
2790 var = scgid.getVar(
"HAM_side");
2798 count.push_back(nmce);
2799 count.push_back(480);
2802 var = gid.getVar(
"MCE_telemetry");
2806 if (nenc==0) nenc = 1;
2811 count.push_back(nenc);
2814 var = gid.getVar(
"encoder_spin_ID");
2818 count.push_back(200);
2823 var = gid.getVar(
"MCE_encoder_data");
2828 if (nsca==0) nsca = 1;
2832 count.push_back(nsca);
2834 var = gid.getVar(
"SCA_spin_ID");
2841 count.push_back(nsca);
2842 count.push_back(480);
2844 var = gid.getVar(
"SCA_telemetry");
2847 if (nsen==0) nsen = 1;
2851 count.push_back(nsen);
2854 var = gid.getVar(
"SCA_encoder_spin_ID");
2859 count.push_back(200);
2864 var = gid.getVar(
"SCA_encoder_data");
2867 if (ntc==0) ntc = 1;
2872 count.push_back(ntc);
2875 var = gid.getVar(
"TC_tlm_time");
2879 count.push_back(1216);
2882 var = gid.getVar(
"TC_telemetry");
2886 count.push_back(nicthrm);
2889 var = gid.getVar(
"ICDU_thermisters");
2894 gid = l1afile->getGroup(
"spatial_spectral_modes");
2900 count.push_back(33);
2902 var = gid.getVar(
"aux_param_table");
2906 if (ndct==0) ndct=1;
2910 count.push_back(ndct);
2912 gid = l1afile->getGroup(
"engineering_data");
2915 var = gid.getVar(
"DAUC_temp_time");
2919 count.push_back(ndctmps);
2922 var = gid.getVar(
"DAUC_temperatures");
2925 if (nimt==0) nimt=1;
2929 count.push_back(nimt);
2932 var = gid.getVar(
"ICDU_MCE_temp_time");
2936 count.push_back(76);
2939 var = gid.getVar(
"ICDU_MCE_temp_tlm");
2943 count.push_back(nimtmps);
2946 var = gid.getVar(
"ICDU_MCE_temperatures");
2952 string conflict =
"No";
2953 gid = l1afile->getGroup(
"engineering_data");
2955 if ((tlmzd[0] == 0) && (tlmzd[1] == 0)) {
2956 cout<<
"No telemetry zone fields in file"<<endl;
2957 gid.putAtt(
"science_telemetry_zone_conflict", conflict);
2960 float clock = 136000;
2961 float secpline = (tditime + 1)/clock;
2964 float *znd =
new float[2*10];
2965 float *zndcp =
new float[2*10];
2968 for (
size_t i=0;
i<10;
i++) {
2970 znd[ndz] =
line*secpline;
2971 znd[1*10+ndz] = (
line + itable[
i].
lines)*secpline;
2973 if ((ndz > 0) && (znd[ndz] == znd[1*10+ndz-1])) {
2974 znd[1*10+ndz-1] = znd[1*10+ndz];
2981 memcpy(zndcp,znd,20);
2983 for (
int i=0;
i<ndz;
i++) znd[ndz+
i]=znd[10+
i];
2986 if ((itable[0].
dtype == 0) || (itable[0].
dtype == 10)) znd[1*ndz+ndz-1] += znd[1*ndz+0];
2990 for (
size_t i=0;
i<2;
i++) {
2992 int16_t tlmze = tlmzs[
i] + tlmzd[
i] + 3;
2994 for (
k=ndz-1;
k>=0;
k--) {
2996 if (tlmze>znd[
k])
break;
2998 if ((
k == -1) || (tlmzs[
i] < znd[
k]) || (tlmze > znd[1*ndz+
k])) {
3000 cout<<
"Telemetry zone "<< tlmzs[
i]<<
", " << tlmze<<endl;
3001 cout<<
"No-data zone "<<znd[
k]<<
", "<<znd[1*ndz+
k]<<endl;
3004 }
else conflict =
"Yes";
3008 NcGroup gid = l1afile->getGroup(
"engineering_data");
3009 gid.putAtt(
"science_telemetry_zone_conflict", conflict);
3029 delete[] icdumcetsec;
3030 delete[] icdumcettlm;
3049 uint16_t nnavmax = 1000;
3052 double usec_l1a_start, usec_l1a_end, usec_hkt;
3057 ifstream
file(hktlist);
3060 double *atime =
new double[nnavmax];
3061 for (
size_t i=0;
i<nnavmax;
i++) atime[
i]=-999;
3062 double *otime =
new double[nnavmax];
3063 for (
size_t i=0;
i<nnavmax;
i++) otime[
i]=-999;
3064 double *ttime =
new double[nnavmax];
3065 for (
size_t i=0;
i<nnavmax;
i++) ttime[
i]=-999;
3067 float *arate =
new float[3*nnavmax];
3068 for (
size_t i=0;
i<3*nnavmax;
i++) arate[
i]=-999;
3069 float *quat =
new float[4*nnavmax];
3070 for (
size_t i=0;
i<4*nnavmax;
i++) quat[
i]=-999;
3071 float *
pos =
new float[3*nnavmax];
3072 for (
size_t i=0;
i<3*nnavmax;
i++)
pos[
i]=-9999999;
3073 float *vel =
new float[3*nnavmax];
3074 for (
size_t i=0;
i<3*nnavmax;
i++) vel[
i]=-9999999;
3075 float *tlt =
new float[nnavmax];
3076 for (
size_t i=0;
i<nnavmax;
i++) tlt[
i]=-999;
3080 while (getline(
file, strHKTfile)) {
3084 int atttid, attqid,attrid;
3085 int orbpid, orbtid, orbvid;
3088 char *hkt_t_start = (
char*)malloc(100 *
sizeof(
char));
3090 nc_open(strHKTfile.c_str(), NC_NOWRITE, &ncid);
3091 cout<<
"Reading PACE HKT file "<<strHKTfile<<
"..."<<endl;
3094 nc_get_att_text(ncid, NC_GLOBAL,
"time_coverage_start", hkt_t_start);
3096 int hkt_yr, hkt_mon, hkt_day;
3097 sscanf(hkt_t_start,
"%4d-%2d-%2d", &hkt_yr, &hkt_mon, &hkt_day);
3099 nc_inq_grp_ncid(ncid,
"navigation_data",&gid);
3103 int attt_dimids[NC_MAX_VAR_DIMS];
3105 nc_inq_varid (gid,
"att_time", &atttid);
3106 nc_inq_var (gid, atttid, 0, &attt_type, &attt_ndims, attt_dimids,&attt_natts);
3107 nc_inq_dimlen(gid, attt_dimids[0], &nHKTlen);
3114 double *at =
new double[nHKTlen];
3115 double *ot =
new double[nHKTlen];
3116 double *
tt =
new double[nHKTlen];
3118 float *
r =
new float[3*nHKTlen];
3119 float *
q =
new float[4*nHKTlen];
3120 float *
p =
new float[3*nHKTlen];
3121 float *
v =
new float[3*nHKTlen];
3122 float *
t =
new float[nHKTlen];
3124 nc_get_var(gid,atttid,at);
3127 int ind_start=1e6, ind_end=-1;
3128 for (
size_t i=0;
i<nHKTlen;
i++) {
3129 usec_hkt =
ymds2unix(hkt_yr,hkt_mon,hkt_day,at[
i]);
3130 if ((usec_hkt>usec_l1a_start-10) && (ind_start==1e6)) {
3135 for (
size_t i=nHKTlen-1;
i>=0;
i--) {
3136 usec_hkt =
ymds2unix(hkt_yr,hkt_mon,hkt_day,at[
i]);
3137 if ((usec_hkt<usec_l1a_end+10) && (ind_end==-1)) {
3143 if ((ind_end - ind_start)>0) {
3144 nc_inq_varid (gid,
"att_quat", &attqid);
3145 nc_get_var(gid,attqid,
q);
3146 nc_inq_varid (gid,
"att_rate", &attrid);
3147 nc_get_var(gid,attrid,
r);
3149 nc_inq_varid (gid,
"orb_pos", &orbpid);
3150 nc_get_var(gid,orbpid,
p);
3151 nc_inq_varid (gid,
"orb_time", &orbtid);
3152 nc_get_var(gid,orbtid,ot);
3153 nc_inq_varid (gid,
"orb_vel", &orbvid);
3154 nc_get_var(gid,orbvid,
v);
3156 nc_inq_varid (gid,
"tilt", &tid);
3157 nc_get_var(gid,tid,
t);
3158 nc_inq_varid (gid,
"tilt_time", &ttid);
3159 nc_get_var(gid,ttid,
tt);
3162 size_t nnav0 = ind_end - ind_start + 1;
3163 memcpy(atime+nnav,at+ind_start,nnav0*
sizeof(
double));
3164 memcpy(otime+nnav,ot+ind_start,nnav0*
sizeof(
double));
3165 memcpy(ttime+nnav,
tt+ind_start,nnav0*
sizeof(
double));
3166 memcpy(tlt+nnav,
t+ind_start,nnav0*
sizeof(
float));
3168 memcpy(quat+nnav*4,
q+ind_start*4,4*nnav0*
sizeof(
float));
3169 memcpy(arate+nnav*3,
r+ind_start*3,3*nnav0*
sizeof(
float));
3170 memcpy(
pos+nnav*3,
p+ind_start*3,3*nnav0*
sizeof(
float));
3171 memcpy(vel+nnav*3,
v+ind_start*3,3*nnav0*
sizeof(
float));
3179 delete[] hkt_t_start;
3189 catch (NcException& e){
3191 cout<<
"Error reading "<<strHKTfile<<
"!"<<endl;
3198 vector<size_t>
start;
3199 vector<size_t>
count;
3201 NcGroup l1a_gid = l1afile->getGroup(
"navigation_data");
3203 if (nnav==0) nnav = 1;
3205 count.push_back(nnav);
3206 var = l1a_gid.getVar(
"att_time");
3209 var = l1a_gid.getVar(
"orb_time");
3212 var = l1a_gid.getVar(
"tilt_time");
3215 var = l1a_gid.getVar(
"tilt");
3223 count.push_back(nnav);
3225 var = l1a_gid.getVar(
"att_quat");
3230 var = l1a_gid.getVar(
"att_rate");
3233 var = l1a_gid.getVar(
"orb_pos");
3236 var = l1a_gid.getVar(
"orb_vel");
3257 uint32_t isc,
short dtype,
3258 uint16_t smode, uint16_t cdsmode,
3259 std::ofstream &fout) {
3261 string dtypes[] = {
"",
"Earth Collect",
"Dark Collect",
"Solar Cal Daily",
3262 "Solar Cal Monthly",
"Response Curve",
"Lunar Cal",
3263 "Diagnostic",
"Static",
"Earth Spectral",
"",
3264 "External Snapshot Trigger",
"Internal Snapshot Trigger",
3265 "SPCA",
"Lunar Stare"};
3267 string smodes[] = {
"Science",
"Diagnostic",
"Single-image raw",
3270 string cdsmodes[] = {
"CDS",
"Reset",
"Video"};
3275 l1afile->putAtt(
"date_created", buf);
3291 ss = stringstream();
3292 ss << setw(4) << to_string(
starttime.iyear) <<
"-";
3293 ss << setw(2) << setfill(
'0') << mon <<
"-";
3294 ss << setw(2) << setfill(
'0') << idm <<
"T";
3296 ss << setw(2) << setfill(
'0') <<
ih <<
":";
3297 ss << setw(2) << setfill(
'0') << mn <<
":";
3298 ss << fixed << setw(6) << setprecision(3) <<
3301 cout << ss.str() << endl;
3302 l1afile->putAtt(
"time_coverage_start", ss.str().c_str());
3305 if ( fout.is_open())
3306 fout << ss.str().c_str() <<
" ";
3308 yd2md((int16_t) endtime.
iyear, (int16_t) endtime.
iday, &mon, &idm);
3310 endtime.
sec = floor(endtime.
sec*1000)/1000;
3312 endtime.
sec -=
ih * 3600;
3313 mn = (
int) (endtime.
sec/60);
3314 endtime.
sec -= mn * 60;
3317 ss = stringstream();
3318 ss << setw(4) << to_string(endtime.
iyear) <<
"-";
3319 ss << setw(2) << setfill(
'0') << mon <<
"-";
3320 ss << setw(2) << setfill(
'0') << idm <<
"T";
3322 ss << setw(2) << setfill(
'0') <<
ih <<
":";
3323 ss << setw(2) << setfill(
'0') << mn <<
":";
3324 ss << fixed << setw(6) << setprecision(3) <<
3325 setfill(
'0') << endtime.
sec;
3327 cout << ss.str() << endl;
3328 l1afile->putAtt(
"time_coverage_end", ss.str().c_str());
3331 l1afile->putAtt(
"product_name", l1a_name.c_str());
3334 l1afile->putAtt(
"startDirection", sdir.c_str());
3335 l1afile->putAtt(
"endDirection", edir.c_str());
3338 l1afile->putAtt(
"number_of_filled_scans", to_string(isc));
3341 l1afile->putAtt(
"data_collect_mode", dtypes[
dtype].c_str());
3342 l1afile->putAtt(
"SWIR_data_mode", smodes[smode].c_str());
3343 l1afile->putAtt(
"CDS_mode", cdsmodes[cdsmode].c_str());
3346 if ( fout.is_open())
3347 fout << ss.str().c_str() <<
" ";
3357 catch ( NcException& e) {
3358 cout << e.what() << endl;
3359 cerr <<
"Failure closing: " + fileName << endl;
3367 int createNCDF( NcGroup &ncGrp,
const char *sname,
const char *lname,
3369 void *fill_value,
const char *flag_values,
3370 const char *flag_meanings,
const char *reference,
3371 double low,
double high,
int nt, vector<NcDim>& varVec) {
3379 ncVar = ncGrp.addVar(sname, nt, varVec);
3381 catch ( NcException& e) {
3382 cout << e.what() << endl;
3383 cerr <<
"Failure creating variable: " << sname << endl;
3388 double fill_value_dbl;
3389 memcpy( &fill_value_dbl, fill_value,
sizeof(
double));
3399 if ( low != fill_value_dbl) {
3400 if ( nt == NC_BYTE) {
3401 i8 = fill_value_dbl;
3402 ncVar.setFill(
true, (
void *) &i8);
3403 }
else if ( nt == NC_UBYTE) {
3404 ui8 = fill_value_dbl;
3405 ncVar.setFill(
true, (
void *) &ui8);
3406 }
else if ( nt == NC_SHORT) {
3407 i16 = fill_value_dbl;
3408 ncVar.setFill(
true, (
void *) &i16);
3409 }
else if ( nt == NC_USHORT) {
3410 ui16 = fill_value_dbl;
3411 ncVar.setFill(
true, (
void *) &ui16);
3412 }
else if ( nt == NC_INT) {
3413 i32 = fill_value_dbl;
3414 ncVar.setFill(
true, (
void *) &i32);
3415 }
else if ( nt == NC_UINT) {
3416 ui32 = fill_value_dbl;
3417 ncVar.setFill(
true, (
void *) &ui32);
3418 }
else if ( nt == NC_FLOAT) {
3419 f32 = fill_value_dbl;
3420 ncVar.setFill(
true, (
void *) &
f32);
3422 ncVar.setFill(
true, (
void *) &fill_value_dbl);
3428 vector<size_t> chunkVec;
3429 if ( varVec.size() == 3 && (strncmp(sname,
"EV_", 3) == 0)) {
3430 dimlength = varVec[2].getSize();
3432 chunkVec.push_back(1);
3433 chunkVec.push_back(16);
3434 chunkVec.push_back(dimlength/10);
3443 ncVar.setChunking(ncVar.nc_CHUNKED, chunkVec);
3445 catch ( NcException& e) {
3447 cerr <<
"Failure setting chunking: " << sname << endl;
3452 ncVar.setCompression(
true,
true, 5);
3454 catch ( NcException& e) {
3456 cerr <<
"Failure setting compression: " << sname << endl;
3464 ncVar.putAtt(
"long_name", lname);
3466 catch ( NcException& e) {
3468 cerr <<
"Failure creating 'long_name' attribute: " << lname << endl;
3472 if ( strcmp( flag_values,
"") != 0) {
3477 fv.assign( flag_values);
3478 size_t pos = fv.find(
"=", curPos);
3479 fv = fv.substr(
pos+1);
3481 size_t semicln = fv.find(
";");
3486 while(
pos != semicln) {
3487 pos = fv.find(
",", curPos);
3488 if (
pos == string::npos)
3492 istringstream iss(fv.substr(curPos,
pos-curPos));
3493 iss >> skipws >> flag_value;
3494 vec[n++] = atoi( flag_value.c_str());
3499 ncVar.putAtt(
"flag_values", NC_BYTE, n, vec);
3501 catch ( NcException& e) {
3503 cerr <<
"Failure creating 'flag_values' attribute: " << lname << endl;
3509 if ( strcmp( flag_meanings,
"") != 0) {
3512 ncVar.putAtt(
"flag_meanings", flag_meanings);
3514 catch ( NcException& e) {
3516 cerr <<
"Failure creating 'flag_meanings' attribute: "
3517 << flag_meanings << endl;
3523 if ( strcmp( reference,
"") != 0) {
3526 ncVar.putAtt(
"reference", reference);
3528 catch ( NcException& e) {
3530 cerr <<
"Failure creating 'reference' attribute: "
3531 << reference << endl;
3542 vr[0] = (uint8_t)low;
3543 vr[1] = (uint8_t)high;
3546 ncVar.putAtt(
"valid_min", NC_BYTE, 1, &vr[0]);
3548 catch ( NcException& e) {
3550 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
3555 ncVar.putAtt(
"valid_max", NC_BYTE, 1, &vr[1]);
3557 catch ( NcException& e) {
3559 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
3567 vr[0] = (uint8_t)low;
3568 vr[1] = (uint8_t)high;
3571 ncVar.putAtt(
"valid_min", NC_UBYTE, 1, &vr[0]);
3573 catch ( NcException& e) {
3575 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
3580 ncVar.putAtt(
"valid_max", NC_UBYTE, 1, &vr[1]);
3582 catch ( NcException& e) {
3584 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
3592 vr[0] = (int16_t)low;
3593 vr[1] = (int16_t)high;
3596 ncVar.putAtt(
"valid_min", NC_SHORT, 1, &vr[0]);
3598 catch ( NcException& e) {
3600 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
3605 ncVar.putAtt(
"valid_max", NC_SHORT, 1, &vr[1]);
3607 catch ( NcException& e) {
3609 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
3617 vr[0] = (uint16_t)low;
3618 vr[1] = (uint16_t)high;
3621 ncVar.putAtt(
"valid_min", NC_USHORT, 1, &vr[0]);
3623 catch ( NcException& e) {
3625 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
3630 ncVar.putAtt(
"valid_max", NC_USHORT, 1, &vr[1]);
3632 catch ( NcException& e) {
3634 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
3642 vr[0] = (int32_t)low;
3643 vr[1] = (int32_t)high;
3646 ncVar.putAtt(
"valid_min", NC_INT, 1, &vr[0]);
3648 catch ( NcException& e) {
3650 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
3655 ncVar.putAtt(
"valid_max", NC_INT, 1, &vr[1]);
3657 catch ( NcException& e) {
3659 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
3668 vr[0] = (uint32_t)low;
3669 vr[1] = (uint32_t)high;
3672 ncVar.putAtt(
"valid_min", NC_UINT, 1, &vr[0]);
3674 catch ( NcException& e) {
3676 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
3681 ncVar.putAtt(
"valid_max", NC_UINT, 1, &vr[1]);
3683 catch ( NcException& e) {
3685 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
3695 vr[1] = (
float)high;
3698 ncVar.putAtt(
"valid_min", NC_FLOAT, 1, &vr[0]);
3700 catch ( NcException& e) {
3702 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
3707 ncVar.putAtt(
"valid_max", NC_FLOAT, 1, &vr[1]);
3709 catch ( NcException& e) {
3711 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
3723 ncVar.putAtt(
"valid_min", NC_DOUBLE, 1, &vr[0]);
3725 catch ( NcException& e) {
3727 cerr <<
"Failure creating 'valid_min' attribute: " << vr[0] << endl;
3732 ncVar.putAtt(
"valid_max", NC_DOUBLE, 1, &vr[1]);
3734 catch ( NcException& e) {
3736 cerr <<
"Failure creating 'valid_max' attribute: " << vr[1] << endl;
3742 fprintf(
stderr,
"-E- %s line %d: ",__FILE__,__LINE__);
3743 fprintf(
stderr,
"Got unsupported number type (%d) ",nt);
3744 fprintf(
stderr,
"while trying to create NCDF variable, \"%s\", ",sname);
3753 ncVar.putAtt(
"units",
units);
3755 catch ( NcException& e) {
3757 cerr <<
"Failure creating 'units' attribute: " <<
units << endl;
3765 ncVar.putAtt(
"standard_name",
units);
3767 catch ( NcException& e) {
3769 cerr <<
"Failure creating 'standard_name' attribute: "
3780 int eight20( uint8_t *inbytes, uint32_t *outsamples) {
3785 for (
size_t i=0;
i<5;
i++) {
3786 outsamples[
i*2] = 4096*inbytes[
i*5] + 16*inbytes[
i*5+1] + inbytes[
i*5+2]/16;
3789 for (
size_t i=0;
i<4;
i++) {
3790 outsamples[
i*2+1] = 65536*(inbytes[
i*5+2] % 16) + 256*inbytes[
i*5+3] +