40 size_t sdim,
int *ncid,
int *gid) {
47 ifstream hawkeye_data_structure;
49 string dataStructureFile;
51 dataStructureFile.assign( cdlfile);
54 hawkeye_data_structure.open( dataStructureFile.c_str(), ifstream::in);
55 if ( hawkeye_data_structure.fail() ==
true) {
56 cout <<
"\"" << dataStructureFile.c_str() <<
"\" not found" << endl;
62 getline( hawkeye_data_structure,
line);
63 size_t pos =
line.find(
"dimensions:");
71 getline( hawkeye_data_structure,
line);
73 if (
pos == string::npos)
break;
76 istringstream iss(
line.substr(
pos+2, string::npos));
81 iss >> skipws >>
line;
83 cout <<
"Dimension Name: " <<
line.c_str() <<
" Dimension Size: "
86 if (
line.compare(
"number_of_scans") == 0) {
90 status = nc_def_dim( *ncid,
line.c_str(), dimSize, &dimid[ndims++]);
97 getline( hawkeye_data_structure,
line);
98 size_t pos =
line.find(
"// global attributes");
103 getline( hawkeye_data_structure,
line);
105 if (
pos == string::npos)
break;
112 attValue.assign(
line.substr(
pos+4));
113 size_t posQuote = attValue.find(
"\"");
114 attValue.assign(attValue.substr(0, posQuote));
116 istringstream iss(
line.substr(
pos+2));
119 iss >> skipws >>
line;
122 if (
line.compare(
"//") == 0)
continue;
125 attName.assign(
line.substr(1).c_str());
128 if (attName.compare(
"orbit_number") == 0)
continue;
129 if (attName.compare(
"history") == 0)
continue;
130 if (attName.compare(
"format_version") == 0)
continue;
131 if (attName.compare(
"instrument_number") == 0)
continue;
132 if (attName.compare(
"pixel_offset") == 0)
continue;
133 if (attName.compare(
"number_of_filled_scans") == 0)
continue;
137 status = nc_put_att_text( *ncid, NC_GLOBAL, attName.c_str(),
138 strlen(attValue.c_str()), attValue.c_str());
147 getline( hawkeye_data_structure,
line);
151 if (
line.substr(0,1).compare(
"}") == 0) {
152 hawkeye_data_structure.close();
157 size_t pos =
line.find(
"group:");
163 istringstream iss(
line.substr(6, string::npos));
164 iss >> skipws >>
line;
167 status = nc_def_grp( *ncid,
line.c_str(), &gid[ngrps]);
173 int varDims[NC_MAX_DIMS];
174 size_t dimSize[NC_MAX_DIMS];
175 char dimName[NC_MAX_NAME+1];
181 string flag_meanings;
184 double fill_value=0.0;
185 float scale_factor=1.0;
186 float add_offset=0.0;
191 getline( hawkeye_data_structure,
line);
193 getline( hawkeye_data_structure,
line);
195 if (
line.length() == 0)
continue;
196 if (
line.substr(0,1).compare(
"\r") == 0)
continue;
197 if (
line.substr(0,1).compare(
"\n") == 0)
continue;
202 if (
pos == string::npos) {
207 sname.c_str(), lname.c_str(),
209 (
void *) &fill_value,
210 flag_values.c_str(), flag_meanings.c_str(),
213 flag_values.assign(
"");
214 flag_meanings.assign(
"");
224 if (
line.substr(0,10).compare(
"} // group") == 0)
break;
228 istringstream iss(
line);
229 iss >> skipws >> varType;
232 if ( varType.compare(
"char") == 0) ntype = NC_CHAR;
233 else if ( varType.compare(
"byte") == 0) ntype = NC_BYTE;
234 else if ( varType.compare(
"short") == 0) ntype = NC_SHORT;
235 else if ( varType.compare(
"int") == 0) ntype = NC_INT;
236 else if ( varType.compare(
"long") == 0) ntype = NC_INT;
237 else if ( varType.compare(
"float") == 0) ntype = NC_FLOAT;
238 else if ( varType.compare(
"real") == 0) ntype = NC_FLOAT;
239 else if ( varType.compare(
"double") == 0) ntype = NC_DOUBLE;
240 else if ( varType.compare(
"ubyte") == 0) ntype = NC_UBYTE;
241 else if ( varType.compare(
"ushort") == 0) ntype = NC_USHORT;
242 else if ( varType.compare(
"uint") == 0) ntype = NC_UINT;
243 else if ( varType.compare(
"int64") == 0) ntype = NC_INT64;
244 else if ( varType.compare(
"uint64") == 0) ntype = NC_UINT64;
248 size_t posSname =
line.substr(0,
pos).rfind(
" ");
249 sname.assign(
line.substr(posSname+1,
pos-posSname-1));
250 cout <<
"sname: " << sname.c_str() << endl;
254 &numDims, dimid, varDims);
255 for (
int i=0;
i<numDims;
i++) {
256 nc_inq_dim( *ncid, varDims[
i], dimName, &dimSize[
i]);
257 cout <<
line.c_str() <<
" " <<
i <<
" " << dimName
258 <<
" " << dimSize[
i] << endl;
263 size_t posEql =
line.find(
"=");
264 size_t pos1qte =
line.find(
"\"");
265 size_t pos2qte =
line.substr(pos1qte+1, string::npos).find(
"\"");
271 if (
attrName.compare(
"long_name") == 0) {
272 lname.assign(
line.substr(pos1qte+1, pos2qte));
277 else if (
attrName.compare(
"units") == 0) {
278 units.assign(
line.substr(pos1qte+1, pos2qte));
283 else if (
attrName.compare(
"_FillValue") == 0) {
285 iss.str(
line.substr(posEql+1, string::npos));
291 else if (
attrName.compare(
"flag_values") == 0) {
292 flag_values.assign(
line.substr(pos1qte+1, pos2qte));
296 else if (
attrName.compare(
"flag_meanings") == 0) {
297 flag_meanings.assign(
line.substr(pos1qte+1, pos2qte));
301 else if (
attrName.compare(
"valid_min") == 0) {
303 iss.str(
line.substr(posEql+1, string::npos));
309 else if (
attrName.compare(
"valid_max") == 0) {
311 iss.str(
line.substr(posEql+1, string::npos));
317 else if (
attrName.compare(
"scale_factor") == 0) {
319 iss.str(
line.substr(posEql+1, string::npos));
324 else if (
attrName.compare(
"add_offset") == 0) {
326 iss.str(
line.substr(posEql+1, string::npos));
340 int *numDims,
int *dimid,
int *varDims) {
342 size_t dimSize, curPos=0;
343 char dimName[NC_MAX_NAME+1];
348 size_t pos = dimString.find(
",", curPos);
349 if (
pos == string::npos)
350 pos = dimString.find(
")");
353 istringstream iss(dimString.substr(curPos,
pos-curPos));
354 iss >> skipws >> varDimName;
356 for (
int i=0;
i<ndims;
i++) {
357 int status = nc_inq_dim( ncid, dimid[
i], dimName, &dimSize);
359 if ( varDimName.compare(dimName) == 0) {
360 varDims[(*numDims)++] = dimid[
i];
364 if ( dimString.substr(
pos, 1).compare(
")") == 0)
break;
374 int createNCDF(
int ncid,
const char *sname,
const char *lname,
377 const char *flag_values,
const char *flag_meanings,
378 double low,
double high,
379 float scale_factor,
float add_offset,
380 int nt,
int rank,
int *dimids) {
388 status = nc_def_var(ncid, sname, nt,
rank, dimids, &varid);
390 printf(
"-E- %s %d: %s for %s\n",
391 __FILE__, __LINE__, nc_strerror(
status), sname);
396 double fill_value_dbl;
397 memcpy( &fill_value_dbl, fill_value,
sizeof(
double));
408 if ( low != fill_value_dbl) {
409 if ( nt == NC_BYTE) {
411 status = nc_def_var_fill( ncid, varid, 0, (
void *) &i8);
412 }
else if ( nt == NC_UBYTE) {
413 ui8 = fill_value_dbl;
414 status = nc_def_var_fill( ncid, varid, 0, (
void *) &ui8);
415 }
else if ( nt == NC_SHORT) {
416 i16 = fill_value_dbl;
417 status = nc_def_var_fill( ncid, varid, 0, (
void *) &i16);
418 }
else if ( nt == NC_USHORT) {
419 ui16 = fill_value_dbl;
420 status = nc_def_var_fill( ncid, varid, 0, (
void *) &ui16);
421 }
else if ( nt == NC_INT) {
422 i32 = fill_value_dbl;
423 status = nc_def_var_fill( ncid, varid, 0, (
void *) &i32);
424 }
else if ( nt == NC_UINT) {
425 ui32 = fill_value_dbl;
426 status = nc_def_var_fill( ncid, varid, 0, (
void *) &ui32);
427 }
else if ( nt == NC_FLOAT) {
428 f32 = fill_value_dbl;
429 status = nc_def_var_fill( ncid, varid, 0, (
void *) &
f32);
431 status = nc_def_var_fill( ncid, varid, 0, (
void *) &fill_value_dbl);
438 if (
rank == 3 && (strncmp(sname,
"EV_", 3) == 0)) {
439 status = nc_inq_dimlen(ncid, dimids[2], &dimlength);
442 chunksize[2] = dimlength/10;
449 status = nc_def_var_chunking(ncid, varid, NC_CHUNKED, chunksize);
451 printf(
"-E- %s %d: %s for %s\n", __FILE__, __LINE__,
452 nc_strerror(
status), sname);
458 status = nc_def_var_deflate(ncid, varid, NC_SHUFFLE, 1, 5);
460 printf(
"-E- %s %d: %s for %s\n", __FILE__, __LINE__,
461 nc_strerror(
status), sname);
468 status = nc_put_att_text(ncid, varid,
"long_name", strlen(lname), lname);
470 printf(
"-E- %s %d: %s for %s\n",
471 __FILE__, __LINE__, nc_strerror(
status),
"long_name");
477 if ( strcmp( flag_values,
"") != 0) {
482 fv.assign( flag_values);
483 size_t pos = fv.find(
"=", curPos);
484 fv = fv.substr(
pos+1);
486 size_t semicln = fv.find(
";");
491 while(
pos != semicln) {
492 pos = fv.find(
",", curPos);
493 if (
pos == string::npos)
497 istringstream iss(fv.substr(curPos,
pos-curPos));
498 iss >> skipws >> flag_value;
499 vec[n++] = atoi( flag_value.c_str());
503 status = nc_put_att_schar(ncid, varid,
"flag_values", NC_BYTE, n, vec);
505 printf(
"-E- %s %d: %s for %s\n",
506 __FILE__, __LINE__, nc_strerror(
status),
"flag_values");
512 if ( strcmp( flag_meanings,
"") != 0) {
513 status = nc_put_att_text(ncid, varid,
"flag_meanings",
514 strlen(flag_meanings), flag_meanings);
516 printf(
"-E- %s %d: %s for %s\n",
517 __FILE__, __LINE__, nc_strerror(
status),
"flag_meanings");
523 if ((scale_factor!=1) || (add_offset!=0) ) {
524 status = nc_put_att_float(ncid, varid,
"scale_factor",NC_FLOAT,1,&scale_factor);
526 printf(
"-E- %s %d: %s for %s\n",
527 __FILE__, __LINE__, nc_strerror(
status),
"scale_factor");
530 status = nc_put_att_float(ncid, varid,
"add_offset",NC_FLOAT,1,&add_offset);
532 printf(
"-E- %s %d: %s for %s\n",
533 __FILE__, __LINE__, nc_strerror(
status),
"add_offset");
544 vr[0] = (uint8_t)low;
545 vr[1] = (uint8_t)high;
546 status = nc_put_att_uchar(ncid, varid,
"valid_min",NC_BYTE,1,&vr[0]);
548 printf(
"-E- %s %d: %s for %s\n",
549 __FILE__, __LINE__, nc_strerror(
status),
"valid_min");
552 status = nc_put_att_uchar(ncid, varid,
"valid_max",NC_BYTE,1,&vr[1]);
554 printf(
"-E- %s %d: %s for %s\n",
555 __FILE__, __LINE__, nc_strerror(
status),
"valid_max");
563 vr[0] = (uint8_t)low;
564 vr[1] = (uint8_t)high;
565 status = nc_put_att_uchar(ncid, varid,
"valid_min",NC_UBYTE,1,&vr[0]);
567 printf(
"-E- %s %d: %s for %s\n",
568 __FILE__, __LINE__, nc_strerror(
status),
"valid_min");
571 status = nc_put_att_uchar(ncid, varid,
"valid_max",NC_UBYTE,1,&vr[1]);
573 printf(
"-E- %s %d: %s for %s\n",
574 __FILE__, __LINE__, nc_strerror(
status),
"valid_max");
582 vr[0] = (int16_t)low;
583 vr[1] = (int16_t)high;
584 status = nc_put_att_short(ncid, varid,
"valid_min",NC_SHORT,1,&vr[0]);
586 printf(
"-E- %s %d: %s for %s\n",
587 __FILE__, __LINE__, nc_strerror(
status),
"valid_min");
590 status = nc_put_att_short(ncid, varid,
"valid_max",NC_SHORT,1,&vr[1]);
592 printf(
"-E- %s %d: %s for %s\n",
593 __FILE__, __LINE__, nc_strerror(
status),
"valid_max");
601 vr[0] = (uint16_t)low;
602 vr[1] = (uint16_t)high;
603 status = nc_put_att_ushort(ncid, varid,
"valid_min",NC_USHORT,1,&vr[0]);
605 printf(
"-E- %s %d: %s for %s\n",
606 __FILE__, __LINE__, nc_strerror(
status),
"valid_min");
609 status = nc_put_att_ushort(ncid, varid,
"valid_max",NC_USHORT,1,&vr[1]);
611 printf(
"-E- %s %d: %s for %s\n",
612 __FILE__, __LINE__, nc_strerror(
status),
"valid_max");
620 vr[0] = (int32_t)low;
621 vr[1] = (int32_t)high;
622 status = nc_put_att_int(ncid, varid,
"valid_min",NC_INT,1,&vr[0]);
624 printf(
"-E- %s %d: %s for %s\n",
625 __FILE__, __LINE__, nc_strerror(
status),
"valid_min");
628 status = nc_put_att_int(ncid, varid,
"valid_max",NC_INT,1,&vr[1]);
630 printf(
"-E- %s %d: %s for %s\n",
631 __FILE__, __LINE__, nc_strerror(
status),
"valid_max");
639 vr[0] = (uint32_t)low;
640 vr[1] = (uint32_t)high;
641 status = nc_put_att_uint(ncid, varid,
"valid_min",NC_UINT,1,&vr[0]);
643 printf(
"-E- %s %d: %s for %s\n",
644 __FILE__, __LINE__, nc_strerror(
status),
"valid_min");
647 status = nc_put_att_uint(ncid, varid,
"valid_max",NC_UINT,1,&vr[1]);
649 printf(
"-E- %s %d: %s for %s\n",
650 __FILE__, __LINE__, nc_strerror(
status),
"valid_max");
660 status = nc_put_att_float(ncid, varid,
"valid_min",NC_FLOAT,1,&vr[0]);
662 printf(
"-E- %s %d: %s for %s\n",
663 __FILE__, __LINE__, nc_strerror(
status),
"valid_min");
666 status = nc_put_att_float(ncid, varid,
"valid_max",NC_FLOAT,1,&vr[1]);
668 printf(
"-E- %s %d: %s for %s\n",
669 __FILE__, __LINE__, nc_strerror(
status),
"valid_max");
679 status = nc_put_att_double(ncid, varid,
"valid_min",NC_DOUBLE,1,&vr[0]);
681 printf(
"-E- %s %d: %s for %s\n",
682 __FILE__, __LINE__, nc_strerror(
status),
"valid_min");
685 status = nc_put_att_double(ncid, varid,
"valid_max",NC_DOUBLE,1,&vr[1]);
687 printf(
"-E- %s %d: %s for %s\n",
688 __FILE__, __LINE__, nc_strerror(
status),
"valid_max");
694 fprintf(
stderr,
"-E- %s line %d: ",__FILE__,__LINE__);
695 fprintf(
stderr,
"Got unsupported number type (%d) ",nt);
696 fprintf(
stderr,
"while trying to create NCDF variable, \"%s\", ",sname);
705 printf(
"-E- %s %d: %s for %s\n",
706 __FILE__, __LINE__, nc_strerror(
status),
"units");
713 status = nc_put_att_text(ncid, varid,
"standard_name",
716 printf(
"-E- %s %d: %s for %s\n",
717 __FILE__, __LINE__, nc_strerror(
status),
"standard_name");
732 if (tepoch >= 15638401)
leap =
leap + 1;
733 if (tepoch >= 63158402)
leap =
leap + 1;
735 double tlocal = tepoch -
leap;
738 int32_t
jd = (long) (tlocal/86400) + 2457024;
742 *sec = tlocal - (
int) (tlocal / 86400) * 86400;
868 int readFrame ( ifstream *framefile, uint8_t frame[892],
int& prevFrameCnt,
873 if (!framefile->eof()) {
874 framefile->read( (
char *) frame, 892);
877 int frameCnt = (
int) frame[2];
881 int deltaCnt = frameCnt - prevFrameCnt;
884 if (deltaCnt != 1 && deltaCnt != -255) {
891 int first2bytes = 20*256 + 160;
892 while ((framePtr == 2046 || framePtr == 2047 || first2bytes != 5280) && \
894 framefile->read( (
char *) frame, 892);
895 framePtr = (frame[4] % 8)*256 + frame[5];
896 first2bytes = frame[0]*256 + frame[1];
900 frameCnt = (
int) frame[2];
902 ierror[1] = framePtr;
905 prevFrameCnt = frameCnt;
912 int getSHpacket( ifstream *framefile, uint8_t frame[892],
int& framePtr,
913 uint8_t **packet,
int& packetLength,
int& prevFrameCnt,
922 uint8_t secondHdr[6];
936 if (framePtr == 892) {
938 readFrame( framefile, frame, prevFrameCnt, ierror);
941 if (ierror[0] == 1) {
943 framePtr = ierror[1];
949 if (framePtr > (892-6)) {
962 if (framePtr > 892) {
963 long long int pos = framefile->tellg();
964 cout <<
"Bad frame pointer: " << framePtr <<
" " <<
pos << endl;
973 memcpy( secondHdr, &frame[framePtr], 892-framePtr);
976 readFrame( framefile, frame, prevFrameCnt, ierror);
979 if (ierror[0] == 1) {
981 framePtr = ierror[1];
987 prevFramePtr = framePtr;
991 bytesWritten = 892 - prevFramePtr;
992 toWrite2nd =
SHDRLEN - bytesWritten;
995 memcpy( &secondHdr[bytesWritten], &frame[framePtr], toWrite2nd);
998 framePtr += toWrite2nd;
1001 memcpy( pusHdr, &frame[framePtr],
PUSLEN);
1004 if (secondHdr[0]==7) {
1006 cout<<
"EOI packet detected type1"<<endl;
1011 dataLen = secondHdr[4]*256 + secondHdr[5] - 2;
1013 long long int pos = framefile->tellg();
1014 cout <<
"Bad dataLen type1: " << dataLen <<
" " <<
pos << endl;
1025 memcpy( &(*packet)[0], secondHdr,
SHDRLEN);
1029 apid = ((*packet)[0] % 8) * 256 + (*packet)[1];
1030 if (((*packet)[7] < 253 || (*packet)[7] > 255) && apid != 2047) {
1031 long long int pos = framefile->tellg();
1032 cout <<
"Bad PUS type1: " << (
int) (*packet)[7] <<
" " <<
pos << endl;
1041 prevFramePtr = framePtr +
PUSLEN;
1044 framePtr = prevFramePtr + dataLen;
1048 if (framePtr <= 892) {
1049 memcpy( &(*packet)[packetPtr], &frame[prevFramePtr], dataLen);
1054 }
else if (framePtr == (892-6)) {
1064 memcpy( secondHdr, &frame[framePtr],
SHDRLEN);
1067 readFrame( framefile, frame, prevFrameCnt, ierror);
1070 if (ierror[0] == 1) {
1072 framePtr = ierror[1];
1080 dataLen = secondHdr[4]*256 + secondHdr[5] - 2;
1082 long long int pos = framefile->tellg();
1083 cout <<
"Bad dataLen type2: " << dataLen <<
" " <<
pos << endl;
1094 memcpy( &(*packet)[0], secondHdr,
SHDRLEN);
1098 apid = ((*packet)[0] % 8) * 256 + (*packet)[1];
1099 if (((*packet)[7] < 253 || (*packet)[7] > 255) && apid != 2047) {
1100 long long int pos = framefile->tellg();
1101 cout <<
"Bad PUS type2: " << (
int) (*packet)[7] <<
" " <<
pos << endl;
1113 framePtr = prevFramePtr + dataLen;
1117 if (framePtr <= 892) {
1118 memcpy( &(*packet)[packetPtr], &frame[prevFramePtr], dataLen);
1123 }
else if (framePtr > (892-9)) {
1134 memcpy( secondHdr, &frame[framePtr],
SHDRLEN);
1137 memcpy( pusHdr, &frame[framePtr+
SHDRLEN], 892-(framePtr+
SHDRLEN));
1140 readFrame( framefile, frame, prevFrameCnt, ierror);
1143 if (ierror[0] == 1) {
1145 framePtr = ierror[1];
1151 prevFramePtr = framePtr +
SHDRLEN;
1155 bytesWritten = 892 - prevFramePtr;
1156 toWritePUS =
PUSLEN - bytesWritten;
1159 memcpy( &pusHdr[bytesWritten], &frame[framePtr], toWritePUS);
1162 dataLen = secondHdr[4]*256 + secondHdr[5] - 2;
1164 long long int pos = framefile->tellg();
1165 cout <<
"Bad dataLen type3: " << dataLen <<
" " <<
pos << endl;
1176 memcpy( &(*packet)[0], secondHdr,
SHDRLEN);
1180 apid = ((*packet)[0] % 8) * 256 + (*packet)[1];
1181 if (((*packet)[7] < 253 || (*packet)[7] > 255) && apid != 2047) {
1182 long long int pos = framefile->tellg();
1183 cout <<
"Bad PUS type3: " << (
int) (*packet)[7] <<
" " <<
pos << endl;
1192 prevFramePtr = framePtr + toWritePUS;
1195 framePtr = prevFramePtr + dataLen;
1199 if (framePtr <= 892) {
1200 memcpy( &(*packet)[packetPtr], &frame[prevFramePtr], dataLen);
1217 memcpy( secondHdr, &frame[framePtr],
SHDRLEN);
1230 dataLen = secondHdr[4]*256 + secondHdr[5] - 2;
1232 long long int pos = framefile->tellg();
1233 cout <<
"Bad dataLen type4: " << dataLen <<
" " <<
pos << endl;
1244 memcpy( &(*packet)[0], secondHdr,
SHDRLEN);
1248 apid = ((*packet)[0] % 8) * 256 + (*packet)[1];
1249 if (((*packet)[7] < 253 || (*packet)[7] > 255) && apid != 2047) {
1250 long long int pos = framefile->tellg();
1251 cout <<
"Bad PUS type4: " << (
int) (*packet)[7] <<
" " <<
pos << endl;
1263 framePtr = prevFramePtr + dataLen;
1267 if (framePtr <= 892) {
1268 memcpy( &(*packet)[packetPtr], &frame[prevFramePtr], dataLen);
1280 if (prevFramePtr < 892) {
1281 memcpy( &(*packet)[packetPtr], &frame[prevFramePtr], 892-prevFramePtr);
1282 packetPtr += 892-prevFramePtr;
1286 dataLeftToWrite = dataLen - (892 - prevFramePtr);
1288 while (dataLeftToWrite > 0) {
1291 readFrame( framefile, frame, prevFrameCnt, ierror);
1294 if (ierror[0] == 1) {
1296 framePtr = ierror[1];
1305 if (892 < prevFramePtr+dataLeftToWrite)
1308 framePtr = prevFramePtr + dataLeftToWrite;
1310 int dataToWrite = framePtr - prevFramePtr;
1313 memcpy( &(*packet)[packetPtr], &frame[prevFramePtr], dataToWrite);
1314 packetPtr += dataToWrite;
1317 dataLeftToWrite -= framePtr - prevFramePtr;