63 #define NAME "pdsmerge"
69 #define USAGE "start_date end_date APID <input 1> [<input 2> [...]] output\nstart_date/end_date: YYYY/MM/DD,hh:mm:ss or -"
71 #define PRI_HDR_SIZE 6
73 #define MODIS_HDR_SIZE 12
75 #define DATA_SIZE 100000
77 #define MODIS_REF_DATE 2436205.0
126 void julday(
int minute,
int hour,
int day,
int month,
int year,
128 void caldat(
int *minute,
int *hour,
int *
day,
int *month,
int *
year,
137 int main(
int argc,
char *argv[]) {
147 unsigned char **buf_hdr;
149 unsigned char **buf_data;
168 unsigned long startmillisec;
171 unsigned long endmillisec;
175 int lastdays = 0, lastmicrosec = 0, lastpktcount = 0;
176 unsigned long int lastmillisec = 0;
182 fprintf(
stderr,
"%s V%d.%d ("__DATE__
")\n",
195 if (strcmp(argv[1],
"-") == 0) {
199 if (sscanf(argv[1],
" %d/%d/%d,%d:%d:%d",
200 &
year, &month, &
day, &hour, &
min, &sec) != 6) {
205 (month < 1) || (month > 12) ||
206 (
day < 1) || (
day > 31) ||
207 (hour < 0) || (hour > 23) ||
208 (
min < 0) || (
min > 59) ||
209 (sec < 0) || (sec > 59)) {
216 (
unsigned long) hour * 60
L * 60
L * 1000
L +
217 (
unsigned long)
min * 60
L * 1000
L +
218 (
unsigned long) sec * 1000
L;
222 if (strcmp(argv[2],
"-") == 0) {
224 endmillisec = 90000000
L;
226 if (sscanf(argv[2],
" %d/%d/%d,%d:%d:%d",
227 &
year, &month, &
day, &hour, &
min, &sec) != 6) {
232 (month < 1) || (month > 12) ||
233 (
day < 1) || (
day > 31) ||
234 (hour < 0) || (hour > 23) ||
235 (
min < 0) || (
min > 59) ||
236 (sec < 0) || (sec > 59)) {
243 (
unsigned long) hour * 60
L * 60
L * 1000
L +
244 (
unsigned long)
min * 60
L * 1000
L +
245 (
unsigned long) sec * 1000
L;
249 if ((endday < startday) ||
250 ((endday == startday) && (endmillisec <= startmillisec))) {
256 apid = atoi(argv[3]);
257 if ((apid < 64) || (apid > 127)) {
258 fprintf(
stderr,
"only APID 64 to 127 supported\n");
263 if (!(fin = malloc(
sizeof (FILE *) * n))) {
264 fprintf(
stderr,
"not enough memory\n");
267 if (!(hdr = malloc(
sizeof (
struct pri_hdr *) * n))) {
268 fprintf(
stderr,
"not enough memory\n");
271 if (!(mhdr = malloc(
sizeof (
struct modis_hdr *) * n))) {
272 fprintf(
stderr,
"not enough memory\n");
275 if (!(buf_hdr = malloc(
sizeof (
unsigned char *) * n))) {
276 fprintf(
stderr,
"not enough memory\n");
279 if (!(buf_data = malloc(
sizeof (
unsigned char *) * n))) {
280 fprintf(
stderr,
"not enough memory\n");
285 for (
i = 0;
i < n;
i++) {
286 if (!(hdr[
i] = malloc(
sizeof (
struct pri_hdr)))) {
287 fprintf(
stderr,
"not enough memory\n");
291 if (!(mhdr[
i] = malloc(
sizeof (
struct modis_hdr)))) {
292 fprintf(
stderr,
"not enough memory\n");
295 if (!(buf_hdr[
i] = malloc(
sizeof (
unsigned char) *
PRI_HDR_SIZE))) {
296 fprintf(
stderr,
"not enough memory\n");
299 if (!(buf_data[
i] = malloc(
sizeof (
unsigned char) *
DATA_SIZE))) {
300 fprintf(
stderr,
"not enough memory\n");
306 for (
i = 0;
i < n;
i++) {
307 if (!(fin[
i] = fopen(argv[
i + 4],
"rb"))) {
308 fprintf(
stderr,
"can't open input file (%s)\n", argv[
i + 4]);
314 if (!(fout = fopen(argv[n + 4],
"wb"))) {
315 fprintf(
stderr,
"can't create output file (%s)\n", argv[n + 4]);
322 for (
i = 0;
i < n;
i++) {
324 for (; hdr[
i]->
flag == 0;) {
333 "error reading input file (%s)\n",
345 "unsupported packet version (%d) in input file "
347 "file might be corrupted, trying to resyncronise\n",
354 "buffer overflow (%d), "
355 "please contact developer\n",
359 if (fread(buf_data[
i], hdr[
i]->pkt_length + 1, 1, fin[
i]) !=
362 "error reading input file (%s)\n",
369 "unknown error (%d) while decoding primary "
378 "buffer overflow (%d), "
379 "please contact developer\n",
383 if (fread(buf_data[
i], hdr[
i]->pkt_length + 1, 1, fin[
i]) !=
386 "error reading input file (%s)\n",
392 if (hdr[
i]->apid != apid)
397 hdr[
i]->pkt_length + 1,
411 if ((mhdr[
i]->
days < startday) ||
412 ((mhdr[
i]->
days == startday) &&
417 if ((mhdr[
i]->
days > endday) ||
418 ((mhdr[
i]->
days == endday) &&
428 for (
i = 0, valpkt = 0;
i < n;
i++) {
430 if (hdr[
i]->flag != 1)
446 if (mhdr[
i]->
days < mhdr[oldest]->
days) {
450 if (mhdr[
i]->
days > mhdr[oldest]->
days) {
474 if (pktdiff < -8191) pktdiff += 16384;
475 if (pktdiff > 8191) pktdiff -= 16384;
493 if (mhdr[oldest]->
days < lastdays) {
494 hdr[oldest]->
flag = 0;
495 lastdays = mhdr[oldest]->
days;
496 lastmillisec = mhdr[oldest]->
millisec;
497 lastmicrosec = mhdr[oldest]->
microsec;
501 if (mhdr[oldest]->
days == lastdays) {
502 if (mhdr[oldest]->
millisec < lastmillisec) {
503 hdr[oldest]->
flag = 0;
504 lastdays = mhdr[oldest]->
days;
505 lastmillisec = mhdr[oldest]->
millisec;
506 lastmicrosec = mhdr[oldest]->
microsec;
510 if (mhdr[oldest]->
millisec == lastmillisec) {
511 if (mhdr[oldest]->
microsec < lastmicrosec) {
512 hdr[oldest]->
flag = 0;
513 lastdays = mhdr[oldest]->
days;
514 lastmillisec = mhdr[oldest]->
millisec;
515 lastmicrosec = mhdr[oldest]->
microsec;
519 if (mhdr[oldest]->
microsec == lastmicrosec) {
520 pktdiff = hdr[oldest]->
pkt_count - lastpktcount;
521 if (pktdiff < -8191) pktdiff += 16384;
522 if (pktdiff > 8191) pktdiff -= 16384;
524 hdr[oldest]->
flag = 0;
525 lastdays = mhdr[oldest]->
days;
526 lastmillisec = mhdr[oldest]->
millisec;
527 lastmicrosec = mhdr[oldest]->
microsec;
537 lastdays = mhdr[oldest]->
days;
538 lastmillisec = mhdr[oldest]->
millisec;
539 lastmicrosec = mhdr[oldest]->
microsec;
545 "error writing to output file (%s)\n",
549 if (fwrite(buf_data[oldest],
550 hdr[oldest]->pkt_length + 1,
553 "error writing to output file (%s)\n",
559 hdr[oldest]->
flag = 0;
566 for (
i = 0;
i < n;
i++) {
571 for (
i = 0;
i < n;
i++) {
640 hdr->
version = (buf[0] & 0xE0) >> 5;
647 hdr->
type = (buf[0] & 0x10) >> 4;
653 hdr->
apid = (((
int) (buf[0] & 0x07)) << 8) + buf[1];
659 hdr->
pkt_count = (((
int) (buf[2] & 0x3F)) << 8) + buf[3];
685 (((
int) buf[0]) << 8) +
690 (((
unsigned long int) buf[2]) << 24) +
691 (((
unsigned long int) buf[3]) << 16) +
692 (((
unsigned long int) buf[4]) << 8) +
693 (((
unsigned long int) buf[5]));
697 (((
int) buf[6]) << 8) +
701 hdr->
ql = (buf[8] & 0x80) >> 7;
704 hdr->
pkt_type = (buf[8] & 0x70) >> 4;
713 hdr->
src1 = (buf[9] & 0x80) >> 7;
717 (((
int) buf[9] & 0x7F) << 4) +
718 (((
int) buf[10] & 0xF0) >> 4);
722 (((
int) buf[10] & 0x0F) << 6) +
723 (((
int) buf[11] & 0xFC) >> 2);
733 (((
int) buf[len - 2] & 0x0F) << 8) +
734 (((
int) buf[len - 1]));
771 ljul = (long) (floor(365.25 * jy) + floor(30.6001 * jm) +
day +
773 if (
day + 31
L * (month + 12
L *
year) >= (15 + 31
L * (10 + 12
L * 1582))) {
774 ja = (
int) (0.01 * jy);
775 ljul += 2 -
ja + (
int) (0.25 *
ja);
780 (
double) hour / 24.0 +
781 (
double) minute / 1440.0 +
804 long ja, jalpha,
jb, jc,
jd, je;
807 ljul = (long) floor(jul);
809 *hour = (
int) (floor(jul * 24.0));
810 jul -= (
double) *hour / 24.0;
811 *minute = (
int) (floor(jul * 1440.0));
813 if (ljul >= 2299161) {
814 jalpha = (long) (((
float) (ljul - 1867216) - 0.25) / 36524.25);
815 ja = ljul + 1 + jalpha - (long) (0.25 * jalpha);
819 jc = (long) (6680.0 + ((
float) (
jb - 2439870) - 122.1) / 365.25);
820 jd = (long) (365 * jc + (0.25 * jc));
821 je = (long) ((
jb -
jd) / 30.6001);
822 *
day =
jb -
jd - (long) (30.6001 * je);
853 for (
i = 0;
i < n;
i++) {
856 (((
unsigned long) buf[(
int) (1.5 *
i)]) << 4) +
857 (((
unsigned long) buf[(
int) (1.5 *
i) + 1] & 0xF0) >> 4);
871 (((
unsigned long) buf[(
int) (1.5 * (
i - 1)) + 1] & 0x0F) << 8) +
872 (((
unsigned long) buf[(
int) (1.5 * (
i - 1)) + 2]));