17 #define VERSION "2.13"
209 #define MAX_PKTS_PER_SCAN 513
211 int main (
int argc,
char* argv[])
233 cout <<
"l1agen_viirs " <<
VERSION <<
" ("
234 << __DATE__ <<
" " << __TIME__ <<
")" << endl;
238 "l1agen_viirs VIIRS_packet_file S/C_diary_packet_file " <<
239 "ADCS_file bus_telemetry_file granule_len " <<
240 "[-o|--outlist output_list_file] " <<
241 "[-g|--gpsfilename gps_file] " <<
242 "[-n|--new_naming] " <<
243 "[-d|--doi doi_string]" <<
250 string gpsfilename =
"";
251 bool new_naming =
false;
254 static struct option long_options[] = {
255 {
"outlist", required_argument, 0,
'o'},
256 {
"gpsfilename", required_argument, 0,
'g'},
257 {
"new_naming", no_argument, 0,
'n'},
258 {
"doi", required_argument, 0,
'd'},
263 int option_index = 0;
265 c = getopt_long( argc, argv,
"o:g:n:d:", long_options, &option_index);
275 outlist.assign( optarg);
280 gpsfilename.assign( optarg);
297 if ( outlist.compare(
"") != 0)
298 fout.open( outlist.c_str());
300 fstream vfileStream, sfileStream, afileStream, bfileStream, gfileStream;
303 vfileStream.open( argv[optind+0], fstream::in | fstream::binary);
304 if ( vfileStream.fail()) {
305 cout << argv[optind+0] <<
" not found" << endl;
310 sfileStream.open( argv[optind+1], fstream::in | fstream::binary);
311 if ( sfileStream.fail()) {
312 cout << argv[optind+1] <<
" not found" << endl;
317 sfileStream.seekg (0, ios::end);
318 int sFileSize = sfileStream.tellg();
319 sfileStream.seekg (0, ios::beg);
320 int32_t
const nscd = sFileSize/71;
323 uint8_t **scdpkts =
new uint8_t*[nscd];
324 scdpkts[0] =
new uint8_t[71*nscd];
325 for (
int i=1;
i<nscd;
i++) scdpkts[
i] = scdpkts[
i-1] + 71;
328 sfileStream.read( (
char *) &scdpkts[0][0], sFileSize);
340 switch (scdpkts[0][14]) {
357 plat.assign(
"JPSS1");
371 plat.assign(
"JPSS2");
384 cout <<
"Unknown platform type" << endl;
389 double *otime =
new double[nscd];
390 double *atime =
new double[nscd];
393 float **orb =
new float *[nscd];
394 orb[0] =
new float[6*nscd];
395 for (
int i=1;
i<nscd;
i++) orb[
i] = orb[
i-1] + 6;
398 float **quat =
new float *[nscd];
399 quat[0] =
new float[4*nscd];
400 for (
int i=1;
i<nscd;
i++) quat[
i] = quat[
i-1] + 4;
402 int32_t iyrsc, idysc;
404 convert_diary( nscd, (uint8_t (*)[71]) &scdpkts[0][0], &iyrsc, &idysc,
405 otime, (
float (*)[6]) &orb[0][0],
406 atime, (
float (*)[4]) &quat[0][0]);
409 afileStream.open( argv[optind+2], fstream::in | fstream::binary);
410 if ( afileStream.fail()) {
411 cout << argv[optind+2] <<
" not found" << endl;
416 afileStream.seekg (0, ios::end);
417 int aFileSize = afileStream.tellg();
418 afileStream.seekg (0, ios::beg);
419 int32_t nadc = aFileSize/apktsize;
422 uint8_t **adcpkts =
new uint8_t*[nadc];
424 adcpkts[0] =
new uint8_t[apktsize*nadc];
425 for (
int i=1;
i<nadc;
i++) adcpkts[
i] = adcpkts[
i-1] + apktsize;
428 afileStream.read( (
char *) &adcpkts[0][0], aFileSize);
433 bfileStream.open( argv[optind+3], fstream::in | fstream::binary);
434 if ( bfileStream.fail()) {
435 cout << argv[optind+3] <<
" not found" << endl;
440 bfileStream.seekg (0, ios::end);
441 int bFileSize = bfileStream.tellg();
442 bfileStream.seekg (0, ios::beg);
443 int32_t nbus = bFileSize/bpktsize;
446 uint8_t **buspkts =
new uint8_t*[nbus];
447 buspkts[0] =
new uint8_t[bpktsize*nbus];
448 for (
int i=1;
i<nbus;
i++) buspkts[
i] = buspkts[
i-1] + bpktsize;
451 bfileStream.read( (
char *) &buspkts[0][0], bFileSize);
457 double *gpstime =
NULL;
458 uint8_t **gpspkts =
NULL;
459 if (
platform.compare(
"JPSS-2") == 0) {
460 gfileStream.open( gpsfilename, fstream::in | fstream::binary);
461 if ( gfileStream.fail()) {
462 cout << argv[optind+3] <<
" not found" << endl;
467 gfileStream.seekg (0, ios::end);
468 int gFileSize = gfileStream.tellg();
469 gfileStream.seekg (0, ios::beg);
470 ngps = gFileSize/gpktsize;
473 gpspkts =
new uint8_t*[ngps];
474 gpspkts[0] =
new uint8_t[gpktsize*ngps];
475 for (
int i=1;
i<ngps;
i++) gpspkts[
i] = gpspkts[
i-1] + gpktsize;
478 gfileStream.read( (
char *) &gpspkts[0][0], gFileSize);
481 gpstime =
new double[ngps];
484 int32_t iyrad, idyad;
487 double *adctime =
new double[nadc];
488 double *bustime =
new double[nbus];
489 uint8_t *adstate =
new uint8_t[nbus];
490 uint8_t *adsolution =
new uint8_t[nbus];
491 uint8_t *admandone =
new uint8_t[nadc];
492 int16_t *adfftid =
new int16_t[nadc];
500 &iyrad, &idyad, adctime, bustime,
501 adstate, admandone, adfftid, adsolution,
platform,
502 ngps, gpspkts, gpstime);
507 uint8_t epacket[9318];
514 cout <<
"Start of scan not found in file" << endl;
518 if ( apid == 826 && len == 9318) {
519 vfileStream.seekg( -6, ios_base::cur);
523 vfileStream.seekg( len-6, ios_base::cur);
527 int vfilePos = vfileStream.tellg();
530 int32_t toff, iyear, iday,
iyr,
idy;
532 double stime, ptime, etime;
548 string str = argv[optind+4];
549 istringstream(
str) >> mper;
561 cout <<
"Determining number of scans in " << argv[optind+0] <<
": ";
564 uint8_t epacket0[9318];
565 memcpy ( epacket0, epacket, 9318);
584 if ( npkts == 1)
continue;
589 mper = isc * (scan_time/60) + 1;
592 vfileStream.seekg( vfilePos, ios::beg);
598 int32_t ltime = (((int32_t) (stime)) / 60 / mper) * (mper*60);
599 if (mper > 10) ltime = (int32_t) (stime);
600 int32_t
mtime = ltime + mper*60;
603 char datetime[NC_MAX_NAME];
606 time_t unixtime = (time_t)
yds2unix(iyear,iday,ltime);
607 struct tm* trec = gmtime(&unixtime);
610 strftime(datetime, NC_MAX_NAME,
"%Y%m%dT%H%M%S", trec);
611 l1a_name.assign(plat +
"_VIIRS." +
string(datetime) +
".L1A.nc");
613 strftime(datetime, NC_MAX_NAME,
"%Y%j%H%M%S", trec);
614 l1a_name.assign(
string(
"V") +
string(datetime) +
".L1A_" + plat +
".nc");
616 cout << endl << l1a_name.c_str() << endl;
626 int32_t maxsc = mper*60/scan_time + 1;
627 cout <<
"maxsc: " << maxsc << endl;
632 int16_t sdm[17][16][48], svm[17][16][48],
bbm[17][16][48];
635 int16_t sdi[5][32][96], svi[5][32][96], bbi[5][32][96];
638 int16_t sdd[16][64], svd[16][64], bbd[16][64];
642 uint8_t **engdata =
new uint8_t *[maxsc];
643 engdata[0] =
new uint8_t[9318*maxsc];
644 for (
int i=1;
i<maxsc;
i++) engdata[
i] = engdata[
i-1] + 9318;
647 uint8_t **
p1 =
new uint8_t *[maxsc];
648 p1[0] =
new uint8_t[180*maxsc];
649 for (
int i=1;
i<maxsc;
i++)
p1[
i] =
p1[
i-1] + 180;
652 uint8_t **hrmets =
new uint8_t *[maxsc];
653 hrmets[0] =
new uint8_t[146*
EV_APIDs*maxsc];
654 for (
int i=1;
i<maxsc;
i++) hrmets[
i] = hrmets[
i-1] + 146*
EV_APIDs;
659 hrmet[0] =
new uint8_t[146*
EV_APIDs];
663 uint8_t **calmets =
new uint8_t *[maxsc];
664 calmets[0] =
new uint8_t[134*maxsc];
665 for (
int i=1;
i<maxsc;
i++) calmets[
i] = calmets[
i-1] + 134;
668 int16_t **sd_m =
new int16_t *[17*maxsc];
669 sd_m[0] =
new int16_t[17*maxsc*48*16];
670 for (
int i=1;
i<17*maxsc;
i++) {
671 sd_m[
i] = sd_m[
i-1] + 48*16;
672 for (
int j=0;
j<48*16;
j++) sd_m[
i][
j] = -999;
676 int16_t **sv_m =
new int16_t *[17*maxsc];
677 sv_m[0] =
new int16_t[17*maxsc*48*16];
678 for (
int i=1;
i<17*maxsc;
i++) {
679 sv_m[
i] = sv_m[
i-1] + 48*16;
680 for (
int j=0;
j<48*16;
j++) sv_m[
i][
j] = -999;
684 int16_t **bb_m =
new int16_t *[17*maxsc];
685 bb_m[0] =
new int16_t[17*maxsc*48*16];
686 for (
int i=1;
i<17*maxsc;
i++) {
687 bb_m[
i] = bb_m[
i-1] + 48*16;
688 for (
int j=0;
j<48*16;
j++) bb_m[
i][
j] = -999;
692 int16_t **sd_i =
new int16_t *[5*maxsc];
693 sd_i[0] =
new int16_t[5*maxsc*96*32];
694 for (
int i=1;
i<5*maxsc;
i++) {
695 sd_i[
i] = sd_i[
i-1] + 96*32;
696 for (
int j=0;
j<96*32;
j++) sd_i[
i][
j] = -999;
700 int16_t **sv_i =
new int16_t *[5*maxsc];
701 sv_i[0] =
new int16_t[5*maxsc*96*32];
702 for (
int i=1;
i<5*maxsc;
i++) {
703 sv_i[
i] = sv_i[
i-1] + 96*32;
704 for (
int j=0;
j<96*32;
j++) sv_i[
i][
j] = -999;
708 int16_t **bb_i =
new int16_t *[5*maxsc];
709 bb_i[0] =
new int16_t[5*maxsc*96*32];
710 for (
int i=1;
i<5*maxsc;
i++) {
711 bb_i[
i] = bb_i[
i-1] + 96*32;
712 for (
int j=0;
j<96*32;
j++) bb_i[
i][
j] = -999;
716 int16_t **sd_d =
new int16_t *[maxsc];
717 sd_d[0] =
new int16_t[maxsc*64*16];
718 for (
int i=1;
i<maxsc;
i++) {
719 sd_d[
i] = sd_d[
i-1] + 64*16;
720 for (
int j=0;
j<64*16;
j++) sd_d[
i][
j] = -999;
724 int16_t **sv_d =
new int16_t *[maxsc];
725 sv_d[0] =
new int16_t[maxsc*64*16];
726 for (
int i=1;
i<maxsc;
i++) {
727 sv_d[
i] = sv_d[
i-1] + 64*16;
728 for (
int j=0;
j<64*16;
j++) sv_d[
i][
j] = -999;
732 int16_t **bb_d =
new int16_t *[maxsc];
733 bb_d[0] =
new int16_t[maxsc*64*16];
734 for (
int i=1;
i<maxsc;
i++) {
735 bb_d[
i] = bb_d[
i-1] + 64*16;
736 for (
int j=0;
j<64*16;
j++) bb_d[
i][
j] = -999;
739 typedef uint16_t mbands_array[16][6304];
740 typedef uint16_t ibands_array[32][6400];
741 typedef uint16_t dnb_array[16][4064];
743 mbands_array *mbands;
744 mbands =
new mbands_array[16];
745 for (
int i=0;
i<16;
i++)
746 for (
int j=0;
j<16;
j++)
747 for (
int k=0;
k<6304;
k++)
748 mbands[
i][
j][
k] = -999;
750 ibands_array *ibands;
751 ibands =
new ibands_array[5];
752 for (
int i=0;
i<5;
i++)
753 for (
int j=0;
j<32;
j++)
754 for (
int k=0;
k<6400;
k++)
755 ibands[
i][
j][
k] = -999;
758 dnb =
new dnb_array[5];
759 for (
int i=0;
i<5;
i++)
760 for (
int j=0;
j<16;
j++)
761 for (
int k=0;
k<4064;
k++)
766 uint32_t orbitStartYearDay[128];
767 double orbitStartSecOfDay[128];
768 double orbitPeriods[128];
769 uint32_t orbitNumbers[128];
770 ifstream orbitStartTimesFile;
772 char *ocvarroot_str = getenv(
"OCVARROOT");
773 if (ocvarroot_str == 0x0) {
774 printf(
"Environment variable OCVARROOT not defined.\n");
778 string orbitStartTimesFilename = ocvarroot_str;
779 if (
platform.compare(
"SNPP") == 0) {
780 orbitStartTimesFilename +=
"/viirsn/viirs_orbit_times.txt";
781 }
else if (
platform.compare(
"JPSS-1") == 0) {
782 orbitStartTimesFilename +=
"/viirsj1/viirs_j1_orbit_times.txt";
783 }
else if (
platform.compare(
"JPSS-2") == 0) {
784 orbitStartTimesFilename +=
"/viirsj2/viirs_j2_orbit_times.txt";
789 orbitStartTimesFile.open( orbitStartTimesFilename.c_str());
790 if ( !orbitStartTimesFile.is_open()) {
791 cout << orbitStartTimesFilename.c_str() <<
" not found." << endl;
794 for (
size_t i=0;
i<128;
i++) {
795 orbitStartTimesFile >> orbitNumbers[
i];
796 orbitStartTimesFile >> orbitStartYearDay[
i];
797 orbitStartTimesFile >> orbitStartSecOfDay[
i];
798 orbitStartTimesFile >> orbitPeriods[
i];
799 if ( orbitNumbers[
i] == 0)
break;
801 orbitStartTimesFile.close();
807 bool exit_101 =
false;
811 outfile.
createl1( (
char *) l1a_name.c_str(), maxsc);
817 while ( stime <
mtime) {
820 cout <<
"Bad granule -- Times incompatible with scan period." << endl;
824 if ((isc % 10) == 0) cout <<
"Processing scan " << isc << endl;
828 apid = (epacket[0] % 8)*256 + epacket[1];
830 memcpy( engdata[isc], epacket, 9318);
832 memset( engdata[isc], 0, 9318);
841 if ( npkts == 1 && endfile == 0) {
853 while( pbuffer[ip][0] < 8) ip++;
854 memcpy( &
p1[isc][0], &pbuffer[ip][0], 180);
857 uint8_t mqfl[16][16];
861 for (
int i=0;
i<
EV_APIDs;
i++) memset( &hrmet[
i][0], 0, 146);
863 memset( &mqfl, 127, 16*16);
864 memset( &iqfl, 127, 5*32);
865 memset( &dqfl, 127, 5*16);
870 (uint16_t (*) [16][6304]) &mbands[0][0][0],
871 (uint16_t (*) [32][6400]) &ibands[0][0][0],
872 (uint16_t (*) [16][4064]) &dnb[0][0][0],
873 (uint8_t (*) [16]) &mqfl[0][0],
874 (uint8_t (*) [32]) &iqfl[0][0],
875 (uint8_t (*) [16]) &dqfl[0][0],
876 (uint8_t (*)[146]) &hrmet[0][0]);
881 (uint16_t (*) [16][6304]) &mbands[0][0][0],
882 (uint16_t (*) [32][6400]) &ibands[0][0][0],
883 (uint16_t (*) [16][4064]) &dnb[0][0][0],
884 (uint8_t (*) [16]) &mqfl[0][0],
885 (uint8_t (*) [32]) &iqfl[0][0],
886 (uint8_t (*) [16]) &dqfl[0][0]);
892 (int16_t (*) [16][48]) &sdm[0][0][0],
893 (int16_t (*) [16][48]) &svm[0][0][0],
894 (int16_t (*) [16][48]) &
bbm[0][0][0],
895 (int16_t (*) [32][96]) &sdi[0][0][0],
896 (int16_t (*) [32][96]) &svi[0][0][0],
897 (int16_t (*) [32][96]) &bbi[0][0][0],
898 (int16_t (*) [64]) &sdd[0][0],
899 (int16_t (*) [64]) &svd[0][0],
900 (int16_t (*) [64]) &bbd[0][0],
904 for (
int i=0;
i<17;
i++) {
906 memcpy( &sd_m[
i*maxsc+isc][0], &sdm[
i][0][0], 48*16*
sizeof(int16_t));
907 memcpy( &sv_m[
i*maxsc+isc][0], &svm[
i][0][0], 48*16*
sizeof(int16_t));
908 memcpy( &bb_m[
i*maxsc+isc][0], &
bbm[
i][0][0], 48*16*
sizeof(int16_t));
911 for (
int i=0;
i<5;
i++) {
912 memcpy( &sd_i[
i*maxsc+isc][0], &sdi[
i][0][0], 96*32*
sizeof(int16_t));
913 memcpy( &sv_i[
i*maxsc+isc][0], &svi[
i][0][0], 96*32*
sizeof(int16_t));
914 memcpy( &bb_i[
i*maxsc+isc][0], &bbi[
i][0][0], 96*32*
sizeof(int16_t));
917 memcpy( &sd_d[isc][0], &sdd[0][0], 64*16*
sizeof(int16_t));
918 memcpy( &sv_d[isc][0], &svd[0][0], 64*16*
sizeof(int16_t));
919 memcpy( &bb_d[isc][0], &bbd[0][0], 64*16*
sizeof(int16_t));
922 memcpy( &hrmets[isc][0], &hrmet[0][0], 146*
EV_APIDs*
sizeof(uint8_t));
924 memcpy( &calmets[isc][0], &calmet[0], 134*
sizeof(uint8_t));
931 double prev_stime = stime;
938 if (stime == prev_stime) {
939 cout <<
"stime: " << stime <<
" equal to previous stime: " <<
945 if (iday ==
idy && stime < prev_stime) {
946 cout <<
"stime: " << stime <<
" less than previous stime: " <<
947 prev_stime <<
" for same day" << endl;
952 if (
idy < iday)
idy = iday + 1;
953 stime = stime + (
idy-iday)*86400;
959 cout <<
"Number of scans to write: " << isc << endl;
966 int16_t minus999 = -999;
967 for (
int k=0;
k<17;
k++) {
968 for (
int j=isc;
j<maxsc;
j++) {
969 for (
int i=0;
i<48*16;
i++) {
970 memcpy( &sd_m[
k*maxsc+
j][
i], &minus999,
sizeof(int16_t));
971 memcpy( &sv_m[
k*maxsc+
j][
i], &minus999,
sizeof(int16_t));
972 memcpy( &bb_m[
k*maxsc+
j][
i], &minus999,
sizeof(int16_t));
977 for (
int k=0;
k<5;
k++) {
978 for (
int j=isc;
j<maxsc;
j++) {
979 for (
int i=0;
i<96*32;
i++) {
980 memcpy( &sd_i[
k*maxsc+
j][
i], &minus999,
sizeof(int16_t));
981 memcpy( &sv_i[
k*maxsc+
j][
i], &minus999,
sizeof(int16_t));
982 memcpy( &bb_i[
k*maxsc+
j][
i], &minus999,
sizeof(int16_t));
987 for (
int j=isc;
j<maxsc;
j++) {
988 for (
int i=0;
i<64*16;
i++) {
989 memcpy( &sd_d[
j][
i], &minus999,
sizeof(int16_t));
990 memcpy( &sv_d[
j][
i], &minus999,
sizeof(int16_t));
991 memcpy( &bb_d[
j][
i], &minus999,
sizeof(int16_t));
997 uint8_t *
mode =
new uint8_t[isc];
999 int mn_scn = (
int) ((mper*60) / scan_time);
1000 int mx_scn = mn_scn + 1;
1001 if (isc != mn_scn && isc != mx_scn) iret = iret | 1;
1004 (uint8_t (*)[180]) &
p1[0][0],
1005 (uint8_t (*)[146*26]) &hrmets[0][0],
1006 (uint8_t (*)[134]) &calmets[0][0],
1007 mode, iret, (
const char *) l1a_name.c_str(),
1011 outfile.
write_eng_data( isc, (uint8_t (*)[9318]) &engdata[0][0]);
1015 (int16_t (*) [48*16]) &sd_m[0][0],
1016 (int16_t (*) [48*16]) &sv_m[0][0],
1017 (int16_t (*) [48*16]) &bb_m[0][0],
1018 (int16_t (*) [96*32]) &sd_i[0][0],
1019 (int16_t (*) [96*32]) &sv_i[0][0],
1020 (int16_t (*) [96*32]) &bb_i[0][0],
1021 (int16_t (*) [64*16]) &sd_d[0][0],
1022 (int16_t (*) [64*16]) &sv_d[0][0],
1023 (int16_t (*) [64*16]) &bb_d[0][0]);
1027 char sdir[16], edir[16];
1028 strcpy(sdir,
"Undetermined");
1029 strcpy(edir,
"Undetermined");
1031 otime, (
float (*)[6]) &orb[0][0],
1032 atime, (
float (*)[4]) &quat[0][0],
1036 nadc, nbus, ngps, adctime, admandone, adfftid,
1037 bustime, adstate, adsolution, gpstime,
1038 &adcpkts[0], &buspkts[0], &gpspkts[0]);
1043 uint32_t orbitStartYearDay0 = iyear*1000+iday;
1045 double time_tai, dbl_ltime;
1046 dbl_ltime = (
double) ltime;
1047 time_tai =
yds2tai93( iyear, iday, dbl_ltime);
1049 for (
size_t i=0;
i<128;
i++) {
1051 if ( orbitStartYearDay[
i] > orbitStartYearDay0 ||
1052 (orbitStartYearDay[
i] == orbitStartYearDay0 &&
1053 orbitStartSecOfDay[
i] > ltime)) {
1057 uint32_t
iyr = (uint32_t) orbitStartYearDay[
i]/1000;
1058 uint32_t
idy = orbitStartYearDay[
i] -
iyr*1000;
1061 orbit = uint32_t ((time_tai - tai) / orbitPeriods[
i]) +
1071 (
const char *) l1a_name.c_str(),
1074 isc,
mode, argc, argv, doi.c_str());
1084 cout <<
"Removing 0-scan file: " << l1a_name.c_str() << endl;
1086 cout <<
"Error removing " << l1a_name.c_str() << endl;
1093 fout << l1a_name.c_str();
1095 if (isc == mn_scn || isc == mx_scn)
1096 fout <<
" 1" << endl;
1098 fout <<
" 0" << endl;
1104 if ( ltime >= 86400) {
1105 ltime = ltime - 86400;
1106 stime = stime - 86400;
1122 mtime = ltime + mper*60;
1125 unixtime = (time_t)
yds2unix(iyear,iday,ltime);
1126 trec = gmtime(&unixtime);
1129 strftime(datetime, NC_MAX_NAME,
"%Y%m%dT%H%M%S", trec);
1130 l1a_name.assign(plat +
"_VIIRS." +
string(datetime) +
".L1A.nc");
1132 strftime(datetime, NC_MAX_NAME,
"%Y%j%H%M%S", trec);
1133 l1a_name.assign(
string(
"V") +
string(datetime) +
".L1A_" + plat +
".nc");
1135 cout << endl << l1a_name.c_str() << endl;
1142 if ( outlist.compare(
"") != 0) fout.close();
1144 vfileStream.close();
1147 delete[] scdpkts[0];
1159 delete[] engdata[0];
1171 delete[] calmets[0];
1201 delete[] adcpkts[0];
1204 delete[] buspkts[0];
1210 if ( gpstime !=
NULL)
delete[] gpstime;
1211 if ( gpspkts !=
NULL) {
1212 delete[] gpspkts[0];
1224 delete[] pbuffer[0];
1227 if (exit_101)
return 101;
else return 0;
1231 int32_t *
iyr, int32_t *iday,
1232 double *otime,
float (*orb)[6],
1233 double *atime,
float (*quat)[4]) {
1242 memcpy( &ui16, (uint16_t *) &dstore[0][6], 2);
1243 int32_t jd0 =
SWAP_2( ui16) + 2436205;
1248 for (
int i=0;
i<npkts;
i++) {
1253 int16_t iy16, idy16;
1254 int32_t iy,
idy,
jd;
1260 jd =
jday( iy16, 1, idy16);
1261 ot = sec + (
jd - jd0)*86400;
1262 if ((ot > -10.e0) && (ot < 1728.0e2)) otime[io++]= ot;
1266 for (
int j=0;
j<=5;
j++) {
1267 memcpy( &ui32, (uint32_t *) &dstore[
i][
ioff], 4);
1269 memcpy( &orb[
i][
j], &
k,
sizeof(
float));
1277 jd =
jday( iy16, 1, idy16);
1278 at = sec + (
jd - jd0)*86400;
1279 if ((at > -10.e0) && (at < 1728.0e2)) atime[ia++]= at;
1283 for (
int j=0;
j<=3;
j++) {
1284 memcpy( &ui32, (uint32_t *) &dstore[
i][
ioff], 4);
1286 memcpy( &quat[
i][
j], &
k,
sizeof(
float));
1296 int32_t nadc, uint8_t *astore[],
1297 int32_t nbus, uint8_t *bstore[],
1298 int32_t *
iyr, int32_t *iday,
1299 double *adctime,
double *bustime,
1300 uint8_t *adstate, uint8_t *admandone,
1301 int16_t *adfftid, uint8_t *adsolution,
1303 uint8_t *gstore[],
double *gpstime) {
1308 memcpy( &ui16, (uint16_t *) &astore[0][6], 2);
1309 int32_t jd0 =
SWAP_2( ui16) + 2436205;
1314 int16_t iy16, idy16;
1315 int32_t iy,
idy,
jd;
1318 for (
int i=0;
i<nadc;
i++) {
1322 jd =
jday( iy16, 1, idy16);
1323 adctime[
i] = sec + (
jd - jd0)*86400;
1326 admandone[
i] = (astore[
i][adoffsets[2]] % 2);
1327 adfftid[
i] = astore[
i][adoffsets[3]] * 256 + astore[
i][adoffsets[3]+1];
1329 admandone[
i] = (astore[
i][adoffsets[2]] % 32) / 16;
1330 adfftid[
i] = astore[
i][adoffsets[3]] * 256 + astore[
i][adoffsets[3]+1];
1332 admandone[
i] = astore[
i][adoffsets[2]];
1333 adfftid[
i] = astore[
i][adoffsets[3]];
1338 for (
int i=0;
i<nbus;
i++) {
1342 jd =
jday( iy16, 1, idy16);
1343 bustime[
i] = sec + (
jd - jd0)*86400;
1346 adstate[
i] = (bstore[
i][adoffsets[0]] % 32) / 4;
1347 adsolution[
i] = (bstore[
i][adoffsets[1]] % 32);
1349 adstate[
i] = bstore[
i][adoffsets[0]] / 32;
1350 adsolution[
i] = (bstore[
i][adoffsets[1]] % 64) / 2;
1352 adstate[
i] = bstore[
i][adoffsets[0]];
1353 adsolution[
i] = (astore[
i][adoffsets[1]] & 3);
1359 for (
int i=0;
i<ngps;
i++) {
1363 jd =
jday( iy16, 1, idy16);
1364 gpstime[
i] = sec + (
jd - jd0)*86400;
1374 int32_t *npkts, int32_t *endfile) {
1377 int32_t iyear, iday;
1383 if ( epacket[1] == 58) {
1384 memcpy( &ui32, &epacket[48], 4);
1387 memcpy( &ui32, &epacket[34], 4);
1392 memcpy( &pbuffer[0], epacket, 9317);
1399 int32_t apid, len = 0;
1402 if ( *endfile)
return 0;
1404 cout <<
"Packet size " << len <<
" greater than buffer size: ";
1408 uint8_t *packet =
new uint8_t[len];
1410 vfileStream->seekg( -6, ios_base::cur);
1414 uint8_t first = (packet[0] & 8) / 8;
1420 if ( *endfile)
return 0;
1422 cout <<
"Packet size " << len <<
" greater than buffer size: ";
1428 packet =
new uint8_t[len];
1430 vfileStream->seekg( -6, ios_base::cur);
1433 first = (packet[0] & 8) / 8;
1438 memcpy( &ui32, &packet[34], 4);
1439 uint32_t scn =
SWAP_4( ui32);
1442 while ( (scn == scnum) && (
stm == stime)) {
1445 apid = (packet[0] % 8)*256 + packet[1];
1446 uint8_t npkg = packet[14];
1449 cout <<
"L1A Algorithm: Number of packets exceeds in scan"<<
MAX_PKTS_PER_SCAN << endl;
1453 memcpy( &pbuffer[*npkts][0], packet, len);
1462 if ( *endfile)
return 0;
1465 cout <<
"Packet size " << len <<
" greater than buffer size: ";
1470 packet =
new uint8_t[len];
1472 vfileStream->seekg( -6, ios_base::cur);
1479 cout <<
"L1A Algorithm: Number of packets exceeds in scan"<<
MAX_PKTS_PER_SCAN << endl;
1482 memcpy( &pbuffer[*npkts][0], packet, len);
1492 first = (packet[0] & 8) / 8;
1498 if ( *endfile)
return 0;
1500 cout <<
"Packet size " << len <<
" greater than buffer size: ";
1505 packet =
new uint8_t[len];
1507 vfileStream->seekg( -6, ios_base::cur);
1509 if ( *endfile)
return 0;
1510 first = (packet[0] & 8) / 8;
1512 memcpy( &ui32, &packet[34], 4);
1517 memcpy( epacket, packet, len);
1528 uint16_t (*mbands)[16][6304],
1529 uint16_t (*ibands)[32][6400],
1530 uint16_t (*dnb)[16][4064],
1531 uint8_t (*mqfl)[16],
1532 uint8_t (*iqfl)[32],
1533 uint8_t (*dqfl)[16],
1534 uint8_t (*hrmet)[146]) {
1539 int32_t ibnds[] = {4, 5, 3, 2, 1, 6, 7, 9, 10, 8, 11, 13, 12, 34,
1540 16, 15, 14, 35, 31, 32, 33, 21, 22, 23, 24, 25};
1543 while (ipkt < npkts) {
1545 int32_t apid = (pbuffer[ipkt][0] % 8)*256 + pbuffer[ipkt][1];
1548 if (apid != 825 && apid != 826) {
1551 uint8_t jhd = (pbuffer[ipkt][0] & 8) / 8;
1555 int32_t iap = apid - 800;
1556 if (iap > 23) iap = iap - 3;
1558 int32_t ibnd = ibnds[iap];
1562 int32_t nsamp = 3200;
1563 if (ibnd <= 5 || ibnd == 7 || ibnd == 13) nsamp = 6304;
1564 if (ibnd >= 21 && ibnd <= 25) nsamp = 4064;
1565 if (ibnd > 30) nsamp = 6400;
1568 memcpy( &hrmet[iap][0], &pbuffer[ipkt][32], 146);
1571 int32_t ngpk = pbuffer[ipkt][14];
1572 int32_t insno = pbuffer[ipkt][29];
1578 if (ibnd >= 22 && ibnd <= 25) {
1579 if (pbuffer[ipkt][96] == 0 && pbuffer[ipkt][97] == 8) {
1581 cout <<
"M-band data in DNB packets" << endl;
1586 while (idet < ngpk && ipkt < npkts) {
1589 int32_t apin = (pbuffer[ipkt][0] % 8)*256 + pbuffer[ipkt][1];
1591 int16_t *
scan =
new int16_t[nsamp];
1592 for (
int i=0;
i<nsamp;
i++)
scan[
i] = -999;
1595 int32_t ins = pbuffer[ipkt][19];
1596 idet = pbuffer[ipkt][25];
1600 if ((idet >= 0) && (idet < ngpk) && (ins == insno)) {
1606 memcpy( &mbands[ibnd-1][idet][0],
scan, nsamp*
sizeof(int16_t));
1607 mqfl[ibnd-1][idet] = qfl;
1611 if (ibnd >= 21 && ibnd <= 25) {
1612 memcpy( &dnb[ibnd-21][idet][0],
scan, nsamp*
sizeof(int16_t));
1613 dqfl[ibnd-21][idet] = qfl;
1616 memcpy( &ibands[ibnd-31][idet][0],
scan, nsamp*
sizeof(int16_t));
1617 iqfl[ibnd-31][idet] = qfl;
1621 cout <<
"Incorrect instrument number for APID " << apid << endl;
1640 int32_t *jj =
new int32_t[3200];
1641 for (
int i=0;
i<3200;
i++) jj[
i] = 2*
i;
1643 int32_t ms1[] = {0,640,1008,1600,2192,2560};
1644 int32_t ms2[] = {639,1007,1599,2191,2559,3199};
1645 int32_t md1[] = {0,640,1376,3152,4928,5664};
1646 int32_t md2[] = {639,1375,3151,4927,5663,6303};
1648 int32_t i1[6], i2[6];
1649 for (
int i=0;
i<6;
i++) {
1651 i2[
i] = ms2[
i] * 2 + 1;
1656 for (
int k=0;
k<=5;
k++) {
1657 for (
int i=0;
i<=15;
i++) {
1660 if ((mqfl[3][
i] & ik) == 0 &&
1661 (mqfl[2][
i] & ik) == 0) {
1662 for (
int kk=md1[
k]; kk<=md2[
k]; kk++) {
1663 mbands[2][
i][kk] += (mbands[3][
i][kk] - 16383);
1666 mqfl[2][
i] = mqfl[2][
i] | ik;
1667 for (
int kk=md1[
k]; kk<=md2[
k]; kk++) {
1668 mbands[2][
i][kk] = -999;
1672 if ((mqfl[3][
i] & ik) == 0 &&
1673 (mqfl[4][
i] & ik) == 0) {
1674 for (
int kk=md1[
k]; kk<=md2[
k]; kk++) {
1675 mbands[4][
i][kk] += (mbands[3][
i][kk] - 16383);
1678 mqfl[4][
i] = mqfl[4][
i] | ik;
1679 for (
int kk=md1[
k]; kk<=md2[
k]; kk++) {
1680 mbands[4][
i][kk] = -999;
1685 if ((mqfl[2][
i] & ik) == 0 &&
1686 (mqfl[1][
i] & ik) == 0) {
1687 for (
int kk=md1[
k]; kk<=md2[
k]; kk++) {
1688 mbands[1][
i][kk] += (mbands[2][
i][kk] - 16383);
1691 mqfl[1][
i] = mqfl[1][
i] | ik;
1692 for (
int kk=md1[
k]; kk<=md2[
k]; kk++) {
1693 mbands[1][
i][kk] = -999;
1698 if ((mqfl[1][
i] & ik) == 0 &&
1699 (mqfl[0][
i] & ik) == 0) {
1700 for (
int kk=md1[
k]; kk<=md2[
k]; kk++) {
1701 mbands[0][
i][kk] += (mbands[1][
i][kk] - 16383);
1704 mqfl[0][
i] = mqfl[0][
i] | ik;
1705 for (
int kk=md1[
k]; kk<=md2[
k]; kk++) {
1706 mbands[0][
i][kk] = -999;
1713 if ((mqfl[9][
i] & ik) == 0 &&
1714 (mqfl[7][
i] & ik) == 0) {
1715 for (
int kk=ms1[
k]; kk<=ms2[
k]; kk++) {
1716 int16_t adj = mbands[9][
i][kk] - 16383;
1717 mbands[ 7][
i][kk] += adj;
1720 mqfl[ 7][
i] = mqfl[ 7][
i] | ik;
1721 for (
int kk=ms1[
k]; kk<=ms2[
k]; kk++) {
1722 mbands[ 7][
i][kk] = -999;
1727 if ((mqfl[9][
i] & ik) == 0 &&
1728 (mqfl[10][
i] & ik) == 0) {
1729 for (
int kk=ms1[
k]; kk<=ms2[
k]; kk++) {
1730 int16_t adj = mbands[9][
i][kk] - 16383;
1731 mbands[10][
i][kk] += adj;
1734 mqfl[10][
i] = mqfl[10][
i] | ik;
1735 for (
int kk=ms1[
k]; kk<=ms2[
k]; kk++) {
1736 mbands[10][
i][kk] = -999;
1741 if ((mqfl[14][
i] & ik) == 0 &&
1742 (mqfl[13][
i] & ik) == 0) {
1743 for (
int kk=ms1[
k]; kk<=ms2[
k]; kk++) {
1745 memcpy( &ii, &mbands[14][
i][kk], 2);
1746 mbands[13][
i][kk] += (mbands[14][
i][kk] - 16383);
1749 mqfl[13][
i] = mqfl[13][
i] | ik;
1750 for (
int kk=ms1[
k]; kk<=ms2[
k]; kk++) {
1751 mbands[13][
i][kk] = -999;
1757 for (
int p=0;
p<2;
p++) {
1758 if ((mqfl[11][
i] & ik) == 0 &&
1759 (iqfl[3][2*
i+
p] & ik) == 0) {
1760 for (
int j=0;
j<=1;
j++) {
1761 for (
int kk=ms1[
k]; kk<=ms2[
k]; kk++) {
1762 int16_t adj = mbands[11][
i][kk] - 16383;
1763 ibands[3][2*
i+
p] [jj[kk]+
j] += adj;
1767 iqfl[3][2*
i+
p] = iqfl[3][2*
i+
p] | ik;
1768 for (
int j=0;
j<=1;
j++) {
1769 for (
int kk=ms1[
k]; kk<=ms2[
k]; kk++) {
1770 ibands[3][2*
i+
p] [jj[kk]+
j] = -999;
1778 for (
int p=0;
p<2;
p++) {
1779 if ((mqfl[14][
i] & ik) == 0 &&
1780 (iqfl[4][2*
i+
p] & ik) == 0) {
1781 for (
int j=0;
j<=1;
j++) {
1782 for (
int kk=ms1[
k]; kk<=ms2[
k]; kk++) {
1784 memcpy( &ii, &mbands[14][
i][kk], 2);
1785 int16_t adj = mbands[14][
i][kk] - 16383;
1786 ibands[4][2*
i+
p] [jj[kk]+
j] += adj;
1790 iqfl[4][2*
i+
p] = iqfl[4][2*
i+
p] | ik;
1791 for (
int j=0;
j<=1;
j++) {
1792 for (
int kk=ms1[
k]; kk<=ms2[
k]; kk++) {
1793 ibands[4][2*
i+
p] [jj[kk]+
j] = -999;
1803 for (
int i=0;
i<=31;
i++) {
1805 if ((iqfl[0][
i] & ik) == 0 &&
1806 (iqfl[1][
i] & ik) == 0) {
1807 for (
int kk=i1[
k]; kk<=i2[
k]; kk++) {
1808 ibands[1][
i][kk] += (ibands[0][
i][kk] - 16383);
1811 iqfl[1][
i] = iqfl[1][
i] | ik;
1812 for (
int kk=i1[
k]; kk<=i2[
k]; kk++) {
1813 ibands[1][
i][kk] = -999;
1818 if ((iqfl[1][
i] & ik) == 0 &&
1819 (iqfl[2][
i] & ik) == 0) {
1820 for (
int kk=i1[
k]; kk<=i2[
k]; kk++) {
1821 ibands[2][
i][kk] += (ibands[1][
i][kk] - 16383);
1824 iqfl[2][
i] = iqfl[2][
i] | ik;
1825 for (
int kk=i1[
k]; kk<=i2[
k]; kk++) {
1826 ibands[2][
i][kk] = -999;
1841 int16_t (*sdm)[16][48],
1842 int16_t (*svm)[16][48],
1843 int16_t (*
bbm)[16][48],
1844 int16_t (*sdi)[32][96],
1845 int16_t (*svi)[32][96],
1846 int16_t (*bbi)[32][96],
1865 for (
int i=0;
i<17;
i++) {
1866 for (
int j=0;
j<16;
j++) {
1867 for (
int k=0;
k<48;
k++) {
1868 sdm[
i][
j][
k] = -999;
1869 svm[
i][
j][
k] = -999;
1875 for (
int i=0;
i<5;
i++) {
1876 for (
int j=0;
j<32;
j++) {
1877 for (
int k=0;
k<96;
k++) {
1878 sdi[
i][
j][
k] = -999;
1879 svi[
i][
j][
k] = -999;
1880 bbi[
i][
j][
k] = -999;
1885 for (
int j=0;
j<16;
j++) {
1886 for (
int k=0;
k<64;
k++) {
1895 {0,21,1,2,4,3,31,32,7,5,6,13,12,34,33,10,11,8,9,35,16,17,15,14};
1901 while (ipkt < (npkts-1) && apid != 825) {
1903 apid = (pbuffer[ipkt][0] % 8)*256 + pbuffer[ipkt][1];
1907 memset( calmet, 255, 134);
1908 if ((pbuffer[ipkt][0] & 8) == 8) {
1909 memcpy( calmet, &pbuffer[ipkt][32], 134);
1911 apid = (pbuffer[ipkt][0] % 8)*256 + pbuffer[ipkt][1];
1918 while (ipkt < npkts && apid == 825) {
1921 int32_t ibid = pbuffer[ipkt][24];
1922 if (ibid < 1 || ibid > 23) {
1923 cout <<
"ibid out of bounds: " << ibid << endl;
1927 int32_t ibnd = ibnds[ibid];
1933 lpd = pbuffer[ipkt][4]*256 + pbuffer[ipkt][5] + 7;
1938 if (ibnd == 1 || ibnd == 2 || ibnd == 4 || ibnd == 5) {
1939 int32_t lpkt = pbuffer[ipkt][4]*256 + pbuffer[ipkt][5] + 7;
1943 for (
int i=98;
i<lpd;
i++) {
1944 if (pbuffer[ipd][
i] != pbuffer[ipkt][
i]) {
1949 if (test) ibnd = ibnd + 21;
1955 if (ibnd != 22 && ibnd != 23 && ibnd != 25 && ibnd != 26) {
1957 if (ibnd == 21) nsamps = pbuffer[ipkt][25];
1958 if (ibnd > 30) nsamps = 96;
1961 if (nsamps == 96) ndet = 32;
1963 int16_t *sv =
new int16_t[nsamps*ndet];
1964 int16_t *bb =
new int16_t[nsamps*ndet];
1965 int16_t *sd =
new int16_t[nsamps*ndet];
1970 nsamps, ndet, sv, bb, sd);
1975 memcpy( &sdm[ibnd-1][0][0], sd, nsamps*ndet*
sizeof(int16_t));
1976 memcpy( &svm[ibnd-1][0][0], sv, nsamps*ndet*
sizeof(int16_t));
1977 memcpy( &
bbm[ibnd-1][0][0], bb, nsamps*ndet*
sizeof(int16_t));
1982 for (
int i=0;
i<ndet;
i++) {
1983 memcpy( &sdd[
i][0], &sd[
i*nsamps], nsamps*
sizeof(int16_t));
1984 memcpy( &svd[
i][0], &sv[
i*nsamps], nsamps*
sizeof(int16_t));
1985 memcpy( &bbd[
i][0], &bb[
i*nsamps], nsamps*
sizeof(int16_t));
1988 memcpy( &sdd[0][0], sd, nsamps*ndet*
sizeof(int16_t));
1989 memcpy( &svd[0][0], sv, nsamps*ndet*
sizeof(int16_t));
1990 memcpy( &bbd[0][0], bb, nsamps*ndet*
sizeof(int16_t));
1995 memcpy( &sdi[ibnd-31][0][0], sd, nsamps*ndet*
sizeof(int16_t));
1996 memcpy( &svi[ibnd-31][0][0], sv, nsamps*ndet*
sizeof(int16_t));
1997 memcpy( &bbi[ibnd-31][0][0], bb, nsamps*ndet*
sizeof(int16_t));
2005 memcpy( &calmet[4*ibid+18], &pbuffer[ipkt][94], 4);
2008 apid = (pbuffer[ipkt][0] % 8)*256 + pbuffer[ipkt][1];
2019 int32_t lsegs[] = {640,736,1776,1776,736,640};
2020 int32_t lsegs_3200[] = {640,368,592,592,368,640};
2021 int32_t lsegs_4064[] = {784,488,760,760,488,784};
2022 int32_t lsegs_6400[] = {1280,736,1184,1184,736,1280};
2023 if (nsamp == 3200) memcpy( lsegs, lsegs_3200, 6*
sizeof(int32_t));
2024 if (nsamp == 4064) memcpy( lsegs, lsegs_4064, 6*
sizeof(int32_t));
2025 if (nsamp == 6400) memcpy( lsegs, lsegs_6400, 6*
sizeof(int32_t));
2030 int32_t np = idat[4]*256 + idat[5];
2037 memcpy( synp, &idat[26], 4);
2041 for (
int id=0;
id<6;
id++) {
2043 int32_t lp = idat[ip]*256 + idat[ip+1] - 4;
2044 if ((ip+lp) > np || lp < 4) {
2053 uint8_t chksm[4], sync[4];
2054 for (
int i=0;
i<4;
i++) sync[
i] = idat[ip+lp+6+
i];
2056 memcpy(
tmp, &idat[ip+2], lp);
2061 for (
int i=0;
i<4;
i++) totchksum += (sync[
i] == synp[
i]);
2062 if (totchksum != 4) {
2067 for (
int i=0;
i<4;
i++) chksm[
i] = idat[ip+lp+2+
i];
2076 int32_t nbytes = lp;
2077 if ( idat[ip-2] >= 8) nbytes -= 2;
2078 if ( (
tmp[nbytes-1] == 0) && (
tmp[nbytes-2] == 0)) nbytes -= 2;
2082 if ( nbytes > 5 &&
check) {
2084 static unsigned int packet_count=0;
2094 int options_mask=128+32+2;
2097 int pixels_per_scanline=lsegs[
id];
2098 uint16_t *decryptArrRice =
new uint16_t[lsegs[
id]+32*16];
2101 long bytes_written =
2103 pixels_per_scanline, (
const char*)
tmp, nbytes,
2104 decryptArrRice, 2*(lsegs[
id]+32*16));
2106 if ( bytes_written < 0) {
2107 cout <<
"Error in rice uncompress" << endl;
2114 char *argv[] = { (
char *)
"Usds",
2115 (
char *)
"-n", (
char *)
"15",
2116 (
char *)
"-j", (
char *)
"8",
2117 (
char *)
"-br", (
char *)
"128"};
2119 uint16_t *decryptArr =
new uint16_t[lsegs[
id]+32*16];
2120 usds(argc, argv, nbytes,
tmp, decryptArr);
2122 for (
int i=0;
i<lsegs[
id];
i++) {
2124 memcpy( &ui16, &decryptArr[
i],
sizeof(uint16_t));
2125 decryptArr[
i] =
SWAP_2( ui16);
2128 for (
int i=0;
i<lsegs[
id];
i++) {
2129 if (decryptArr[
i] != decryptArrRice[
i]) {
2130 printf(
"%d %d %d %d\n",
2131 i, lsegs[
id], decryptArr[
i], decryptArrRice[
i]);
2139 memcpy( &
scan[
is], decryptArrRice, lsegs[
id]*
sizeof(int16_t));
2141 for (
int i=0;
i<
id;
i++) idpow *= 2;
2143 delete[] decryptArrRice;
2158 int16_t *sp, int16_t *bl, int16_t *sd) {
2180 uint16_t *decryptArrRice =
new uint16_t[nsamp+32*16];
2183 int32_t nsamps = idat[25];
2184 if (nsamps <= 0)
return 8;
2187 int32_t np = idat[4]*256 + idat[5];
2194 memcpy( synp, &idat[26], 4);
2197 for (
int id=0;
id<ndet;
id++) {
2200 int32_t lp = idat[ip]*256+idat[ip+1] - 4;
2202 if (ip+lp > np || lp < 4)
return 8;
2206 for (
int i=0;
i<4;
i++) sync[
i] = idat[ip+lp+6+
i];
2208 for (
int i=0;
i<4;
i++)
if (sync[
i] == synp[
i]) cnt++;
2209 if (cnt != 4)
return 8;
2211 memcpy(
tmp, &idat[ip+2], lp*
sizeof(int8_t));
2212 int32_t nbytes = lp;
2214 if (idat[ip-2] >= 8) nbytes = nbytes - 2;
2217 for (
int i=0;
i<4;
i++) chksm[
i] = idat[ip+lp+2+
i];
2219 if ( !
check)
return 4;
2223 char *argv_sp[] = { (
char *)
"Usds",
2224 (
char *)
"-n", (
char *)
"15",
2225 (
char *)
"-j", (
char *)
"8",
2226 (
char *)
"-br", (
char *)
"128"};
2228 uint16_t *decryptArr =
new uint16_t[nsamp+32*16];
2230 usds(argc, argv_sp, nbytes,
tmp, decryptArr);
2232 for (
int i=0;
i<nsamp;
i++) {
2234 memcpy( &ui16, &decryptArr[
i],
sizeof(uint16_t));
2235 decryptArr[
i] =
SWAP_2( ui16);
2242 int options_mask=128+32+2;
2245 int pixels_per_scanline=nsamp;
2248 if (nbytes > 4 &&
check) {
2251 pixels_per_scanline, (
const char*)
tmp, nbytes,
2252 decryptArrRice, 200);
2254 if ( bytes_written < 0) {
2255 cout <<
"Error in rice uncompress" << endl;
2259 memcpy( &sp[
id*nsamp], decryptArrRice, nsamp*
sizeof(int16_t));
2265 lp = idat[ip]*256+idat[ip+1] - 4;
2267 if ((ip+lp > np) || (lp < 4))
return 8;
2270 for (
int i=0;
i<4;
i++) sync[
i] = idat[ip+lp+6+
i];
2272 for (
int i=0;
i<4;
i++)
if (sync[
i] == synp[
i]) cnt++;
2273 if (cnt != 4)
return 8;
2275 memcpy(
tmp, &idat[ip+2], lp*
sizeof(int8_t));
2278 if (idat[ip-2] >= 8) nbytes = nbytes - 2;
2279 for (
int i=0;
i<4;
i++) chksm[
i] = idat[ip+lp+2+
i];
2284 for (
int i=0;
i<4;
i++) chksm[
i] = idat[ip+lp+2+
i];
2285 if ( !
check)
return 4;
2288 if (nbytes > 4 &&
check) {
2291 pixels_per_scanline, (
const char*)
tmp, nbytes,
2292 decryptArrRice, 200);
2294 if ( bytes_written < 0) {
2295 cout <<
"Error in rice uncompress" << endl;
2299 memcpy( &bl[
id*nsamp], decryptArrRice, nsamp*
sizeof(int16_t));
2305 lp = idat[ip]*256+idat[ip+1] - 4;
2307 if (ip+lp > np || lp < 4)
return 8;
2310 for (
int i=0;
i<4;
i++) sync[
i] = idat[ip+lp+6+
i];
2312 for (
int i=0;
i<4;
i++)
if (sync[
i] == synp[
i]) cnt++;
2313 if (cnt != 4)
return 8;
2315 memcpy(
tmp, &idat[ip+2], lp*
sizeof(int8_t));
2318 if (idat[ip-2] >= 8) nbytes = nbytes - 2;
2319 for (
int i=0;
i<4;
i++) chksm[
i] = idat[ip+lp+2+
i];
2322 for (
int i=0;
i<4;
i++) chksm[
i] = idat[ip+lp+2+
i];
2323 if ( !
check)
return 4;
2325 if (nbytes > 4 &&
check) {
2328 pixels_per_scanline, (
const char*)
tmp, nbytes,
2329 decryptArrRice, 200);
2331 if ( bytes_written < 0) {
2332 cout <<
"Error in rice uncompress" << endl;
2336 memcpy( &sd[
id*nsamp], decryptArrRice, nsamp*
sizeof(int16_t));
2341 delete[] decryptArrRice;
2347 int32_t *len, int32_t *apid, int32_t *endfile) {
2350 if ( vfileStream->eof()) {
2353 cout <<
"End of packet file" << endl;
2359 if ( packet ==
NULL) {
2360 vfileStream->read( (
char *) &phead, 6);
2363 *len = phead[4]*256 + phead[5] + 1 + 6;
2364 *apid = (phead[0] % 8)*256 + phead[1];
2366 if ( vfileStream->tellg() == -1) *endfile = 1;
2370 vfileStream->read( (
char *) packet, *len);
2376 uint8_t
check_sum( int32_t nc, uint8_t *dat, uint8_t *chk) {
2381 uint8_t chks[4],
tmp[4];
2382 memcpy( chks, &dat[0], 4);
2384 for (
int i=1;
i<nc;
i++) {
2385 memcpy( &
tmp, &dat[4*
i], 4);
2386 for (
int j=0;
j<4;
j++) chks[
j] = chks[
j] ^
tmp[
j];
2390 for (
int i=0;
i<4;
i++)
check[
i] = (chk[
i] == chks[
i]);
2405 while ( (pbuffer[
i][0] != 11) || (pbuffer[
i][1] == 58))
i++;
2408 uint8_t
mode = pbuffer[
i][46];
2414 for (
int i=0;
i<npkts;
i++) {
2415 if (pbuffer[
i][0] == 11) {
2416 if (pbuffer[
i][1] == 42) mpkts = mpkts + 17;
2417 if (pbuffer[
i][1] == 54) mpkts = mpkts + 17;
2418 if (pbuffer[
i][1] == 55) mpkts = mpkts + 17;
2419 if (pbuffer[
i][1] == 59) mpkts = mpkts + 17;
2420 if (pbuffer[
i][1] == 60) mpkts = mpkts + 17;
2429 if (npkts != mpkts) icmp = 2;