-
Notifications
You must be signed in to change notification settings - Fork 1
/
pds_3.09.c
12320 lines (10425 loc) · 594 KB
/
pds_3.09.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*====================================================================================================================
* LAP PDS SOFTWARE
* ----------------
*
* See 00_README.TXT for more information.
*
*
* VERSION: 3.09
*
* AUTHOR: Original author is Reine Gill; many modifications by Erik P G Johansson
* Lines 4603-4604 modified from 2.98 by aie@irfu.se,
* Lines 6661 & 6806-6815 edited by fj@irfu.se
* DATE: 140710
* ...
*
*
* CHANGES FROM VERSION 3.07 TO 3.08
* =================================
* * Fixed bug that assigns command-line argument specified configuration file paths to the wrong string variables.
* /Erik P G Johansson 2015-02-xx
* * Changed the name of three PDS keywords to being probe-specific (in addition to four previously renamed PDS keywords).
* /Erik P G Johansson 2015-03-xx
* * Implemented functionality for ignoring data within specific time intervals (pds.dataexcludetimes).
* /Erik P G Johansson 2015-03-31
* * Corrected typos in PDS DESCRIPTION fields in LBL files.
* /Erik P G Johansson 2015-03-30
* * Fixed bug that sometimes terminates the DecodeScience thread prematurely thus omitting the last hour(s) of data.
* /Erik P G Johansson 2015-03-31
* * Specifically permit the faulty macro 515 to set new vbias2 in every macro loop (not just the first loop) thus
* permitting vbias2 to change value in every macro loop cycle, as described in the .mds file.
* /Erik P G Johansson 2015-04-10
* * Modified and simplified code that updates certain PDS keyword values in LBL/CAT files copied from
* the template directory so that it now updates more PDS keyword values.
* Thus modified WriteLabelFile and renamed it WriteUpdatedLabelFile.
* /Erik P G Johansson 2015-05-04
* * Fixed bug that made INDEX.LBL not adjust to the DATA_SET_ID column changing width.
* /Erik P G Johansson 2015-05-12
*
*
* CHANGES FROM VERSION 3.08 TO 3.09
* =================================
* * Fixed bug that made ADC20 always choose high-gain.
* /Erik P G Johansson 2015-06-03
* * Added code for calibrating ADC20 data relative to ADC16
* See Chapter 4, "LAP Offset Determination and Calibration", Anders Eriksson 2015-06-02.
* Also see added constants in "pds.h".
* /Erik P G Johansson 2015-06-10
* * Fixed bug that multiplied ccalf_ADC16 (later renamed to ccalf_ADC16_old and abolished) by 16 for
* * non-truncated ADC20 data, density mode, P1/P2. This led to calibration offsets being multiplied by 16.
* (Braces surrounding the statements after two if-then were missing.)
* /Erik P G Johansson 2015-08-31
* * Fixed potential bug that interpreted macro ID strings as decimal numbers rather than hexadecimal numbers.
* Code still worked since the result was only used to compare with decimal macro ID numbers
* and the code (seemed to) fail well for hexadecimal string representations.
* /Erik P G Johansson 2015-12-07
* * Added extra flags for overriding the MISSION_PHASE_NAME, period start date & duration, and description
* string used in DATA_SET_ID and DATA_SET_NAME.
* /Erik P G Johansson 2015-12-09
* * Start time & duration can now be specified with higher accuracy than days using CLI parameters.
* * All logs now display wall time the same way ("3pds_dds_progress.log" was previously different).
* * Bug fix: pds.modes can now handle colons in the description string.
* * Raised permitted string length for code interpreting "pds.modes". Can now (probably) handle all rows up
* to 1024 characters, including CR & LF.
* /Erik P G Johansson 2015-12-17
* * Bug fix: Reading LBL files failed to add CR at end-of-line for first line of multiline keyword values.
* Bug was in ReadLabelFile(..).
* This caused newlines without CR when pds modified
* CALIB/RPCLAP030101_CALIB_FRQ_D_P1.LBL
* CALIB/RPCLAP030101_CALIB_FRQ_E_P1.LBL
* CALIB/RPCLAP030101_CALIB_FRQ_D_P2.LBL
* CALIB/RPCLAP030101_CALIB_FRQ_E_P2.LBL
* /Erik P G Johansson 2015-12-10, 2016-03-21
* * Bug fix: StrucDir no longer alters parameter "date".
* /Erik P G Johansson 2016-03-21.
* * Bug fix: DecodeHK/ExitPDS:
* Last HK LBL file in data set: SPACECRAFT_CLOCK_STOP_COUNT was erroneously effectively the same as
* SPACECRAFT_CLOCK_START_COUNT. Every HK LBL file with only one row (in TAB file): STOP_TIME was only a year.
* /Erik P G Johansson 2016-03-21.
* * Bug fix: BUG: HK LBL files always had INSTRUMENT_MODE_DESC = "N/A". Now they use the macro descriptions.
* /Erik P G Johansson 2016-03-22
* * Updated to update LBL files under DOCUMENT/ (recursively).
* /Erik P G Johansson 2016-04-04
* * Bug fix: Corrected incorrect catching of errors when reading (some) calibration files: =+ --> +=
* /Erik P G Johansson 2016-04-11
* * Updated the way offset calibrations and TM conversion factors are selected, i.e. RPCLAPyymmdd_CALIB_MEAS.LBL/.TAB files.
* Uses the nearest calibration by default. Added a manual "calibration selection" list (override; later renamed).
* Removes unused calibration files (replaces the previous functionality for removing unused calibration files).
* /Erik P G Johansson 2016-04-13
* * Added tests for erroneous .mds files/macro descriptions to warn the user that he/she is probably using (incompatible) files generated
* with the older version of MEDS.
* /Erik P G Johansson 2016-04-25
* * ~Bug fix: Modified misconfigured SAMP_FREQ_ADC20 60.0 --> 57.8 (pds.h).
* /Erik P G Johansson 2016-04-26
* * Renamed the functionality "calibration selection" to "offset calibration exceptions" (OCE).
* Modified code to read the data from LBL+TAB file pair under CALIB/. Picks TAB file from LBL file.
* /Erik P G Johansson 2016-06-22
* * Modified the compressed output logs file name to contain the data set ID.
* /Erik P G Johansson 2016-06-22
* * Bug fix: WriteUpdatedLabelFile now updates TARGET_NAME. Needed for updating DATASET.DAT.
* /Erik P G Johansson 2016-07-21
* * Bug fix: Wrote illegal unquoted PDS keyword value containing dash/hyphen in P1-P2_CURRENT/VOLTAGE.
* /Erik P G Johansson 2016-07-22
* * Bug fix: Shortened RPCLAP_CALIB_MEAS_EXCEPTIONS.* to RPCLAP_CALIB_MEAS_EXCEPT.* too keep lengths of filenames
* within 27+1+3 (PDS requirement).
* /Erik P G Johansson 2016-10-06
* * Bug fix: HK label column "TMP12" BYTES=4 --> BYTES=5.
* /Erik P G Johansson 2016-10-25
* * Bug fix: Observed illegally changing P2_CURRENT TAB column widths. All EDITED current+voltage column widths (incl. for
* P3) changed from 6 to 7 bytes to accomodate for full range of ADC20 values.
* /Erik P G Johansson 2016-11-23
* * ~Bug fix: Removes CALIB_MEAS_EXCEPT files (LBL+TAB) for EDITED datasets. Simultaneously changed to prescribing TAB filename
* instead of reading it from the LBL file (since one now needs the TAB path twice in the code, rather than once).
* /Erik P G Johansson 2016-12-14
* * Bug fix: ADC16-to-ADC20 offset calibration bug. E-field at times when there were both P1 and P2 data (which was not P3 data) used
* the wrong offset (the P3 offset) which was far too small.
* /Erik P G Johansson 2017-03-01
* * Added offsets for 8 kHz filter (ADC16), compensation for moving average factor bug (in flight s/w).
* /Erik P G Johansson 2017-05-16/30.
* * Functionality for arbitrary ADC16 jump magnitude for EDITED (integer) and CALIB (decimal number) separately.
* /Erik P G Johansson 2017-06-01
* * Bugfix: Used offset for P1 when writing calibrated P2 fine sweeps. Changed p1_fine_offs --> p2_fine_offs.
* /Erik P G Johansson 2017-06-07
* * Experimentally using C_ADC20 := C_ADC16 / 16.0, where C=calibration factor, due to the ground calibration being faulty.
* /Erik P G Johansson 2017-06-09
* * No longer reads C_ADC20 from CALIB_MEAS files PDS keywords. Derives them from C_ADC16 values and hardcoded probe-specific
* C_ADC20/C_ADC16 ratios instead.
* /Erik P G Johansson 2017-0x-xx
* * Configures SPICE but does not use it.
* Loads SPICE metakernel (extra row in pds.conf specifies the path). Configures SPICE error behaviour. Does not use SPICE
* except for in test code.
* /Erik P G Johansson 2017-07-06
* * Uses SPICE for ConvertSccd2Utc and implicitly for LBL (PDS keywords) and TAB files (columns). (Other conversions UTC<-->time_ still do not use SPICE.)
* /Erik P G Johansson 2017-07-10
* * Will not write files for macro 710 P2 HF, and macro 910 P1 HF (hardcoded data exclusion).
* /Erik P G Johansson 2017-07-14
* * No longer read ADC16 calibration factors from PDS keywords in CALIB_MEAS files. Uses hardcoded values instead.
* /Erik P G Johansson 2017-08-21
* * Can now use CALIB_COEFF (or CALIB_MEAS) for bias-dependent current offsets.
* /Erik P G Johansson 2017-08-22
* * CALIB_COEFF LBL files are updated.
* /Erik P G Johansson 2017-08-31
* * Bugfix: Use SPICE mutex for ConvertUtc2Sccd_SPICE. Seems to prevent random crashes for CALIB+CALIB_COEFF_PRELOAD=FALSE.
* /Erik P G Johansson 2017-09-01
* * Bugfix: DecideWhetherToExcludeData now uses only TM timestamps (i.e. never corrected for group delay).
* /Erik P G Johansson 2017-09-19
* * Bugfix: DATASET.CAT:START_TIME first set according to mission calender (or arguments), but is then adjusted for those data products
* (only SCI data; not HK yet) which often begin before the official mission phase due to(?) not splitting data products.
* Note that data products in some mission phases, e.g. AST1, begin long __AFTER__ DATASET.CAT:START_TIME.
* /Erik P G Johansson 2017-11-17
* * Implemented saturation detection.
* /Erik P G Johansson 2018-02-23
* * Modified saturation detection: Use two special TM values per bit resolution (16/20) (instead of just one).
* /Erik P G Johansson 2018-03-12
* * Added flags for:
* - Setting arbitrary output dataset parent directory.
* - Setting end of time interval (an alternative to setting duration).
* - Overriding the remaining values in the mission calendar.
* /Erik P G Johansson 2018-07-06
* * Bugfix: Corrected the sweep timestamp increment.
* /Erik P G Johansson 2018-07-30
* * Bugfix: HK files used to NOT break at midnight, and the following HK files began where
* the last HK file ended, i.e. not at the beginning of the day if previous HK file
* stretched over midnight.
* /Erik P G Johansson 2018-08-01
* * ~Bugfix: Convert ROSETTA:LAP_P1/P2_ADC16_FILTER value to upper case (required by PSA/PDS). (Slightly ugly implementation.)
* NOTE: The original value is read from *.mds file which contains the wrong case.
* /Erik P G Johansson 2018-10-04
* * Bugfix: Completed the list of macros for which manually commanded bias should be ignored: Added all missing macros (macros which do not have LF).
* /Erik P G Johansson 2018-12-06
* * Bugfix: Fix clearing of last four bits in 20-bit data. (Bugfix on old bugfix.)
* /Erik P G Johansson 2019-01-11
* * Bugfix: Ignore manually commanded bias: Removed 304,305,306,307 + 204 from list.
* /Erik P G Johansson 2019-01-24, 2019-01-29
* * Bugfix: Moving average (ADC20, LF) timestamps are centered between first and last internal sample averaged over.
* /Erik P G Johansson 2019-02-18
* * ~Bugfix: Implemented use of special values for floating potential bias.
* /Erik P G Johansson 2019-03-07
* * Updated MISSING_CONSTANT value.
* /Erik P G Johansson 2019-04-05
* * Implemented MANUALLY_COMMANDED_BIAS_EFIELD_TIME_ADDITION_S to add 3 s to the time stamps of manually commanded E field bias.
* /Erik P G Johansson 2019-04-15
* * Bugfix: UTC timestamps incremented unevenly due to that conversion SCCD-->UTC passed over SCCS which has too low time resolution for HF.
* Now converts SCCD-->UTC using SPICE only for the first sample, and converts manually SCCD-->et (and SPICE for et-->UTC) for all other samples.
* /Erik P G Johansson 2019-04-16
* * ~Bugfix: Correct EDITED file format.
* Previously, the new MISSING_CONSTANT = -1e9 caused columns to grow wider than specified (when used).
* Sets all EDITED current and voltage columns to %11d in TAB, and BYTES=11 in LBL.
* /Erik P G Johansson 2019-06-13
*
*
* BUGS
* ====
* "BUG": INDEX.LBL contains keywords RELEASE_ID and REVISION_ID, and INDEX.TAB and INDEX.LBL contain columns
* RELEASE_ID and REVISION_ID which should all be omitted for Rosetta.
* NOTE: Changing this will/might cause backward-compatibility issues with lapdog and write_calib_meas.
* NOTE: Not important to fix. The INDEX is in practice generated separately with "pvv index".
*
* "BUG": Code requires output directory path in pds.conf to end with slash.
* BUG: HK label files do not use "fingerprinting" for identifying macros (only DecodeScience does) and can therefor
* not recognize all macros.
* BUG?: HK LBL files and INDEX.LBL have PRODUCT_ID without quotes. Uncertain if it is required but (1) the examples
* in "Planetary Data System Standards Referense, Version 3.6" imply that one probably should, and (2) it is
* inconsistent with the SCI LBL files and the CALIB/RPCLAP*.LBL files which do have quotes.
* Update 2017-10-03: Changed INDEX.LBL file to have quoted PRODUCT_ID.
* BUG?: Probably does not handle leap seconds correctly all the time. Has found a ~one second error at leap second in
* DATA/EDITED/2015/JUL/D01/RPCLAP150701_001_H.LBL : START_TIME-SPACECRAFT_CLOCK_START_COUNT.
* /Erik P G Johansson, 2016-10-17
* Guessing (but not verifying) that this is FIXED with SPICE.
* /Erik P G Johansson, 2017-08-16
*
* BUG: Rare instances of bad INSTRUMENT_MODE_ID values.
* RO-C-RPCLAP-2-ESC3-MTP020-V1.0/DATA/EDITED/2015/SEP/D03/RPCLAP150903_01A_H.LBL
* INSTRUMENT_MODE_ID = MCID0Xe2fc
* INSTRUMENT_MODE_DESC = "Open Sweep Test Calibration" // Corresponds to macro 104.
* NOTE: grep INSTRUMENT_MODE_ID *_01*_H.LBL ==> Overlaps with 104 data.
* RO-C-RPCLAP-2-ESC3-MTP020-V1.0/DATA/EDITED/2015/SEP/D21/RPCLAP150921_001_H.LBL
* INSTRUMENT_MODE_ID = MCID0Xe2fc
* INSTRUMENT_MODE_DESC = "Density P1P2 Burst Mode, Fix Bias -30/-30V, Cont. truncated no down, Sweeps P1 & P2 +-30V"
* // Corresponds to macro 914 (before correcting BM/NM typo in pds.modes).
* NOTE: grep INSTRUMENT_MODE_ID D21/RPCLAP*_00*_H.LBL ==> 914 data afterwards
* NOTE: First HK after several days without data.
* RO-C-RPCLAP-2-ESC3-MTP021-V1.0/DATA/EDITED/2015/OCT/D17/RPCLAP151017_03X_H.LBL
* INSTRUMENT_MODE_ID = MCID0Xe2fc
* INSTRUMENT_MODE_DESC = "EE float Cont. 20 bit, Every AQP 16 bit P1 & P2" // Fits macro 802
* NOTE: grep INSTRUMENT_MODE RPCLAP151017_0[34]*_H.LBL ==> Just when switching macro 802-->914
* NOTE: Odd INSTRUMENT_MODE_ID value. Usually MCID0Xe2fc (= 0xe2fc = 58108), but not always.
* NOTE: _Valid_ INSTRUMENT_MODE_DESC that is inherited from previous iteration.
* NOTE: Only HK-LBL files.
* NOTE: Appears that the macro_id value returned by AssembleHKLine is wrong. It in turn comes from the bitstream.
* NOTE: The time interval covered by the dataset influences when the bug shows up, probably because only a subset
* of the macro_id values in the HK bitstream are actually used for the HK-LBL, and which subset depends on the dataset start time.
* HK_NUM_LINES determines the number of HK packets(?) per HK TAB file, and (presumably) only one of these packets
* is used for the INSTRUMENT_MODE_ID in the corresponding HK-LBL file.
*
* BUG?: Some science data files in density mode do not contain ROSETTA:LAP_P1/P2_STRATEGY_OR_RANGE which specifies gain (among others).
* See data for 2015-05-20.
* /Erik P G Johansson =<2017
*
* ~BUG: MISSING_CONSTANT that does not fit in %7d (e.g. -10^-9) will extend the column width for EDITED1, making the pds LBL files incorrect.
*
* ~BUG: pds still requires the presence of CALIB_MEAS+CALIB_MEAS_EXCEPT files in CALIB/ even if when pds does not use that type of
* calibration (uses CALIB_COEFF instead). pds produces an empty data set if these files are missing.
* /Erik P G Johansson 2019-05-24
*
*
* NOTES
* =====
* NOTE: Source code indentation is largely OK, but with some exceptions. Some switch cases use different indentations.
* This is due to dysfunctional automatic indentation in the Kate editor.
* NOTE: Contains multiple occurrances of "0xCC" which one can suspect should really be replaced with S_HEAD.
* NOTE: time_t:
* (1) The code relies on that time_t can be interpreted as seconds since epoch 1970 which is true for POSIX and UNIX but not C in general.
* (Source: http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to)
* Code that uses this includes (incomplete list): function ConvertUtc2Timet_2 (used only once), WritePTAB_File,
* possibly the use of bias e.g. in LoadBias. Empirically (playing with TimeOfDatePDS/ConvertUtc2Timet, pds' default compiler),
* time_t appears to correspond to number of seconds after 1970-01-01 00:00.00, not counting leap seconds.
* (2) Code partly uses standard POSIX C functions that convert time_t <--> ~UTC which appear to NOT take leap seconds into account!
* Data as such seems to be correct wrt. leap seconds though.
*
*
* NAMING CONVENTIONS, DEFINITIONS OF TERMS
* ========================================
* Functions/variables for (1) interpreting time from byte streams, and (2) converting between different time formats, have been renamed to
* make their functions easier to understand. The names use the following naming conventions:
* UTC = A string on the form 2016-09-01T00:16:51.946, although the number of second decimals may vary (or be ignored on reading).
* Timet = Variable which value can be interpreted as time_t, although many times it is actually a double, int, or unsigned int.
* SCCS = Spacecraft clock count string, e.g. 1/0431309243.15680 (false decimals).
* SCCD = Spacecraft clock count double, i.e. approximately counting seconds (true decimals). Note that the reset counter has to be handled separately.
*
* TM : Telementry. Can refer to: (1) Quantity in TM units, (2) Time derived from TM without correcting for signal delay (ADC20).
* corrected : In variable name: Time adjusted for signal delay (ADC20).
* MC : Measurement/measured calibration(?)
* MA : Moving average
* tsweep = true sweep : Part of a raw sweep that contains the actual, intended science data. Samples taken on a well-defined sequence of bias plateaus. Subset of a raw sweep.
* This excludes the last sample on the last plateau, which according to pds source code comments (by Reine Gill) does not exist.
* rsweep = raw sweep : True sweep plus some samples taken before the true sweep.
* insmp = internal sample(s) : Samples taken internally by RPCLAP and which may or may not have been later downsampled, averaged over etc.
* tmsmp = TM sample(s) : Samples which are actually output from the RPCLAP instrument.
*
*
*====================================================================================================================
* PROPOSAL: Add check for mistakenly using quotes in MISSION_PHASE_NAME (CLI argument).
* PROPOSAL: Flag --test for triggering test code, instead of preprocessing variable.
* PROPOSAL: Add CALIB_COEFF file pattern to pds.conf, analogous to CALIB_COEFF file pattern.
* NOTE: lap_agility.sh will need to be updated?
* PROPOSAL: Be able to not use mission calendar data at all, including mission phase abbreviation which is still REQUIRED to be in the mission calendar.
* CON/PROBLEM: Only want this when specifying exactly ALL mission calendar values. If not, then one still wants the remaining
* value(s) read from the mission calendar.
*
* PROPOSAL: Let InitMissionPhaseStructFromMissionCalendar set DPL_number.
*====================================================================================================================
*/
// *************************************************************
// -= "Emancipate yourself from mental slavery..", Bob Marley =-
// *************************************************************
#include <string.h> // String handling
#include <ctype.h> // Character types
#include <sys/stat.h> // POSIX Standard file charateristics, fstat()
#include <errno.h> // Used for error handling, perror()
#include <sys/types.h> // POSIX Primitive System Data Types
#include <dirent.h> // POSIX Directory operations
#include <sys/wait.h> // POSIX Wait for process termination
#include <sys/time.h> // Time definitions, nanosleep()
#include <stdio.h> // Standard Input/output
#include <stdlib.h> // Standard General utilities
#include <limits.h> // Standard limits of integer types, PATH_MAX
#include <fcntl.h> // POSIX Standard File Control Operations
#include <fnmatch.h> // Filename matching types
#include <unistd.h> // POSIX Standard Symbolic Constants
#include <linux/unistd.h> // POSIX Standard Symbolic Constants
#include <signal.h> // Standard Signal handling
#include <stdarg.h> // Handle variable argument list
#include <time.h> // Standard date and time
#include <pthread.h> // Linux implementation of POSIX threads
#include <fts.h> // BSD library for traversing UNIX file hierachies
#include <libgen.h> // For basename and dirname
#include <sched.h> // For RT version
#include <stdint.h> // Standard types
#include <math.h> // Floor function
#include <SpiceUsr.h> // Required for calling CSPICE (SPICE for C) functions.
#include "id.h" // LAP Data ID codes
#include "esatm.h" // S/C TM Definitions
#include "pds.h" // PDS & LAP definitions and structures
#include "plnk.h" // Code for linked lists of property/value pairs
#include "plnkdec.h" // plnk declarations
#include "cirb.h" // Code for simple power of two circular buffers
#include "cirbdec.h" // cirb declarations
#include "nice.h" // Sleep definitions etc
#include "calib_coeff.h" // Functionality for loading, finding, and interpolating CALIB_COEFF coefficients.
// We skipped using OASWlib and ESA provided time cal. approach it produced weird results.
// We still use time calibration packets though.
//
// FORTRAN ROUTINES PROVIDED BY ESOC FROM DDS SYSTEM
//extern void dj2000_(double *DAY,int *I,int *J,int *K,int *JHR,int *MI,double *SEC);
void PrintUserHelpInfo(FILE *stream, char *executable_name); // Print user help info.
void InitSpice(char *metakernel_path);
int CheckSpiceError(char *caller_error_msg, int exit_pds, int print_to_stdout);
// Thread functions
//----------------------------------------------------------------------------------------------------------------------------------
void *SCDecodeTM(void *); // Decode S/C TM thread
void *DecodeHK(void *); // Decode HK data thread
void *DecodeScience(void *); // Decode Science data thread
// Signal handler
//----------------------------------------------------------------------------------------------------------------------------------
static void ExitWithGrace(int signo); // Graceful exit
// Logging and exit functions
//----------------------------------------------------------------------------------------------------------------------------------
void ExitPDS(int status); // Closes logging and exits with status
int YSPrintf(const char *fmt, ...);
int YPrintf(const char *fmt, ...); // Prints to pds system log
int YPrintf2(const char *fmt, va_list args);
int DPrintf(const char *fmt, ...); // Prints to DDS packet filter log
int PPrintf(const char *fmt, ...); // Prints to S/C packet filter log
int CPrintf(const char *fmt, ...); // Prints to Science decoding log
int HPrintf(const char *fmt, ...); // Prints to HK decoding log
int OpenLogFile(FILE **pfd,char *name,FILE *fderr); // Opens log file with name "name"
int AddPathsToSystemLog(pds_type *pds); // Adds paths to system log
// Program option functions
//----------------------------------------------------------------------------------------------------------------------------------
int GetOption(char *opt,int argc, char *argv[],char *arg); // Get an input option
int HasMoreArguments(int argc, char *argv[]); // Return true if-and-only-if argv[i] contains non-null components for i >= 1.
// Pointer to file desc pointer pfd is needed and an error stream
// Functions to load and test external information
//----------------------------------------------------------------------------------------------------------------------------------
int OpenFileCountDataRows(char *file_path, FILE **file_descr, int *N_rows);
int LoadConfig1(pds_type *p); // Loads configuration information first part
int LoadConfig2(pds_type *p, char *data_set_id); // Loads configuration information second part
int LoadAnomalies(prp_type *p,char *path); // Load anomaly file
int LoadModeDesc(prp_type *p,char *path); // Load human description of macro modes into a linked list of properties.
int LoadBias(unsigned int ***commanded_bias_table, unsigned int ***commanded_mode_table, int *bias_cnt_s, int *mode_cnt, char *path); // Load bias settings file
int LoadExclude(unsigned int **exclude,char *path); // Load exclude file
int LoadDataExcludeTimes(data_exclude_times_type **dataExcludeTimes, char *depath); // Load data exclude times file.
int DecideWhetherToExcludeData(
data_exclude_times_type *dataExcludeTimes,
curr_type curr,
int param_type,
sweep_type *sw_info,
unsigned int macro_id,
int dop,
int *shouldExcludeFilePair);
int LoadTimeCorr(pds_type *pds,tc_type *tcp); // Load time correlation packets
int LoadMacroDesc(prp_type macs[][MAX_MACROS_INBL],char *); // Loads all macro descriptions
int InitCalibMeas(char *rpath, char *fpath, char *pathocel, char *pathocet, m_type *m); // Get measured data calibration files
void FreeDirEntryList(struct dirent **dir_entry_list, int N_dir_entries);
// int InitMissionPhaseStructFromMissionCalendar(mp_type *mp, pds_type *pds); // Given a path, data set version and mission abbreviation (in mp)
int InitMissionPhaseStructFromMissionCalendar(mp_type *m, char *mission_calendar_path, int DPL_number, float data_set_version);
// Derive DATA_SET_ID and DATA_SET_NAME keyword values, INCLUDING QUOTES!
void DeriveDSIandDSN(
char* DATA_SET_ID, char* DATA_SET_NAME,
char* target_name_dsi, int DPLNumber, char* mpAbbreviation, char* descr, float data_set_version, char* target_name_dsn);
// void TestDumpMacs(); // Test dump of macro descriptions
// Label and Table functions
//----------------------------------------------------------------------------------------------------------------------------------
int UpdateDirectoryODLFiles(const char *dir_path, const char *filename_pattern, int update_PUBLICATION_DATE);
int UpdateODLFile(char *file_path, prp_type *odl_prp, int update_PUBLICATION_DATE);
int WriteUpdatedLabelFile(prp_type *pds, char *name, int update_PUBLICATION_DATE); // Write label file
int ReadLabelFile(prp_type *pds,char *name); // Read a label file
int ReadTableFile(prp_type *lbl_data,c_type *cal,char *path, char *msg); // Read "generic" table file
int UpdateDATASET(mp_type *mp_arg, pds_type *pds_arg); // Update DATASET.CAT
// Miscellaneous functions
//----------------------------------------------------------------------------------------------------------------------------------
char GetBiasMode(curr_type *curr, int dop);
int SelectCalibrationData(time_t, char*, m_type*);
int DestroyCalibMeas(char*, char *pathocel, char *pathocet, m_type*);
// Write data to data product table file.
int WritePTAB_File(
unsigned char *buff, char *fname, int data_type, int N_tmsmp, int id_code, int N_bytes, sweep_type *sw_info, adc20_type *a20_info,
curr_type *curr, int param_type, int ADC16_P1_insmp_per_tmsmp, int ADC16_P2_insmp_per_tmsmp, int dop,
m_type *m_conv, unsigned int **commanded_bias_table, int nbias, unsigned int **commanded_mode_table, int nmode, int N_non_tsweep_tmsmp, int N_plateau_tmsmp);
int handle_EDITED_floating_potential_bias(int current_bias_TM, int is_floating);
double handle_CALIB_floating_potential_bias( double current_bias, int is_floating);
void set_saturation_limits(
double* x_phys_min,
double* x_phys_max,
int* x_TM_saturation_1,
int* x_TM_saturation_2,
int is_ADC20_nontrunc,
int is_high_gain,
int is_Efield);
double handle_saturation(
double x_phys,
int x_TM,
double x_phys_min,
double x_phys_max,
int x_TM_saturation_1,
int x_TM_saturation_2);
// Write to data product label file .lbl
int WritePLBL_File(char *path,char *fname,curr_type *curr,int N_tmsmp,int id_code,int dop,int N_non_tsweep_tmsmp,int param_type);
// Buffer and TM functions
//----------------------------------------------------------------------------------------------------------------------------------
void FreeBuffs(buffer_struct_type *b0,buffer_struct_type *b1,buffer_struct_type *b2,buffer_struct_type *b3); // Free buffer memory
int GetBuffer(buffer_struct_type *cs,unsigned char *buff,int len); // Get data from circular buffer
int LookBuffer(buffer_struct_type *bs,unsigned char *buff,int len); // Look ahead in circular buffer
int GetHKPacket(buffer_struct_type *,unsigned char *,double *); // Get one packet of HK data
void DumpTMPacket(buffer_struct_type *cs,unsigned char packet_id); // Dump the non interesting SC TM packets
int SyncAhead(buffer_struct_type *cb,int len); // Test data synchronisation ahead.
// Functions handling/working with linked lists of property/value pairs
//----------------------------------------------------------------------------------------------------------------------------------
int ClearCommonPDS(prp_type *p); // Clear common PDS parameters
int ClearDictPDS(prp_type *p); // Clear dictionary PDS LAP parameters
int SetupHK(prp_type *p); // Setup HK label
int SetupIndex(prp_type *p); // Setup index label parameters
void WriteIndexLBL(prp_type *p,mp_type *m); // Write index label file
void WriteToIndexTAB(char* relative_LBL_file_path, char* product_ID, char* prod_creation_time); // Write one line in index label file.
int TotAQPs(prp_type *p,int n); // Return tot num of aqps since start for sequence n
int FindIDCode(prp_type *p,int n); // Find ID code in macro overriding ID code in data
// if anomaly correction is applicable
// Program state handler functions
//----------------------------------------------------------------------------------------------------------------------------------
void DispState(int,char *); // Display state changes for debugging
// String and alpha numeric handling functions
//----------------------------------------------------------------------------------------------------------------------------------
int Separate(char *,char *,char *,char,int); // Separate into left & right token out of many.
int SeparateOnce(char* str, char* strLeft, char* strRight, char separator); // Separate into left & right tokens.
int TrimWN(char *); // Trims initial and trailing whitespace and all newlines away
int TrimQN(char *); // Trims initial and trailing quotes and all newlines away
int ExtendStr(char *dest,char *src,int elen,char ch); // Make a new string dest using src extended with ch to elen length.
void ReplCh(char *str,char ch1,char ch2); // Replace all characters ch1 in str with characters ch2
int IncAlphaNum(char *n); // Increments the alphanumeric number stored in string n
// returns <0 on error
int GetAlphaNum(char *n,char *path,char *pattern); // Get largest alphanumeric value stored in file names matching
// a specific pattern
// In directory path
int Alpha2Num(char *n); // Convert a positive alpha numeric value to a number
// returns negative value on error
int IsNumber(char *str); // Check if string is a number
void convertToUpperCase(char *str);
// File handling functions
//----------------------------------------------------------------------------------------------------------------------------------
int FileLen(FILE *); // Get length of file
int FileStatus(FILE *fd,struct stat *sp); // Return FileStatus
int SetupPath(char *error_txt,char *path); // Resolves,tests and returns a usable path
int TestDir(char *name); // Test if directory exists
int TestFile(char *name); // Test if file exists and if we can read it.
int MakeDir(char *,char *,char *); // Make a directory for current YYMMDDD, if it's not already there!
int StrucDir(char *,char *,char *); // Test and create directory structure for data
void DumpDir(char *path); // Dump a directory. Mostly for debugging
int GetUnacceptedFName(char *name); // Get new filename for manual unaccepted file
void FTSDump(FTSENT *fe); // Dump FTSENT structure for debbuging
int Match(char *,char *); // Match filename to pattern
// HK Functions
//----------------------------------------------------------------------------------------------------------------------------------
void AssembleHKLine(unsigned char *, char *, double, char*, unsigned int *); // Assemble a HK line entry
// Low level data functions
//----------------------------------------------------------------------------------------------------------------------------------
void SignExt20(int *); // Sign extend 20 bit to 32 bit
double GetDBigE(unsigned char *buff); // Get bigendian double assuming we are on a little endian machine
unsigned int GetBitF(unsigned int word,int nb,int sb); // Returns nb number of bits starting at bit sb
// Time-related functions
//----------------------------------------------------------------------------------------------------------------------------------
double DecodeSCTime2Sccd(unsigned char *buff); // Decoding S/C time, returns raw S/C time in seconds as a double
double DecodeLAPTime2Sccd(unsigned char *buff); // Decoding lap time, returns raw S/C time in seconds as a double
//int DecodeRawTimeEst(double raw,char *stime); // Decodes raw S/C time (estimates UTC no calibration) and returns
// a PDS compliant time string. UNUSED
int ConvertSccd2Utc (double sccd, char *utc_3decimals, char *utc_6decimals); // Decodes raw S/C time (calibration included)
int ConvertSccd2Et_SPICE (double sccd, SpiceDouble* et);
void ConvertEt2Utc_SPICE (SpiceDouble et, char *utc_3decimals, char *utc_6decimals);
int ConvertSccd2Utc_nonSPICE(double sccd, char *utc_3decimals, char *utc_6decimals);
int ConvertSccd2Utc_SPICE (double sccd, char *utc_3decimals, char *utc_6decimals);
void ConvertUtc2Sccd_SPICE(char *utc, int *reset_counter, double *sccd);
int ConvertSccd2Sccs(double sccd, int n_resets, char *sccs, int quote_sccs); // Convert raw time to an OBT string (On Board Time)
int ConvertSccs2Sccd(char *sccs, int *reset_counter, double *sccd); // Convert OBT string to raw time.
int get_conversion_factor_sccd2et_SPICE(double sccd1, double sccd2, double* conversion_factor);
//int Scet2Date(double raw,char *stime,int lfrac); // Decodes SCET (Spacecraft event time, Calibrated OBT) into a date
// lfrac is long or short fractions of seconds.
// Replacement for Scet2Date ESA approach with OASWlib give dubious results. (dj2000 returns 60s instead of 59s etc.)
int ConvertTimet2Utc(double raw, char *utc, int use_6_decimals); // Decodes SCET (Spacecraft event time, Calibrated OBT) into a date
// use_6_digits is long or short fractions of seconds.
void ConvertTimet2Sccd_SPICE(time_t t, int *reset_counter, double *sccd);
int ConvertUtc2Timet(char *sdate,time_t *t); // Returns UTC time in seconds (since 1970) for a PDS date string
// NOTE: This is not the inverse of Scet2Date!
unsigned int ConvertUtc2Timet_2(char *utc); // Get seconds from 1970 epoch to epoch "epoch"
int ConvertUtc2Timet_midday(char *sdate, time_t *t);
int GetCurrentUtc0(char *); // Returns current UTC date and time as string CCYY-MM-DDThh:mm:ss
// LAP Logarithmic decompression functions
//----------------------------------------------------------------------------------------------------------------------------------
void DoILogTable(unsigned int *);
int LogDeComp(unsigned char *buff,int ilen,unsigned int *ilog);
int HighestBit(unsigned int value);
// DDS Archive functions (input archive)
//----------------------------------------------------------------------------------------------------------------------------------
void TraverseDDSArchive(pds_type *p); // Traverse DDS archive path
int Compare(const FTSENT **af, const FTSENT **bf); // Used for traversal of DDS archives using fts functions.
// Also used in DumpDir() ( Can be seen as adding a metric to a mathematical file space :) )
void ProcessDDSFile(unsigned char * ibuff,int len,struct stat *sp,FTSENT *fe); // Process DDS file
int DDSFileDuration(char *str); // Returns DDS file duration in seconds, computed on filename!
time_t DDSFileStartTime(FTSENT *f); // Returns the start time of entries in a DDS archive for sorting purposes
double DecodeDDSTime2Timet(unsigned char *ibuff); // Return DDS packet time
int DDSVirtualCh(unsigned char *ibuff); // Returns DDS packet virtual channel
void DDSGroundSN(unsigned short int gsid,char *str); // Get ground station name
// Dynamic allocation functions
//----------------------------------------------------------------------------------------------------------------------------------
void *CallocArray(int n,int s); // Allocate array of n entries each n bytes in size and clear
double **CallocDoubleMatrix (int rows, int cols); // Dynamically allocate two dimensional array of doubles
unsigned int **CallocIntMatrix (int rows, int cols); // Dynamically allocate two dimensional array of ints
void FreeIntMatrix(unsigned int **C, int rows, int cols); // Free two dim. array of integers
void FreeDoubleMatrix(double ** C, int rows, int cols); // Free two dim. array of doubles
// RT Version functions
//----------------------------------------------------------------------------------------------------------------------------------
// Set thread priority and scheduling policy, for RT version of PDS (not needed anymore, keep anyway)
int SetPRandSched(pthread_t thread,int priority,int policy);
// Test code
//----------------------------------------------------------------------------------------------------------------------------------
int main_TEST(int argc, char* argv[]);
//-=SOME GLOBAL VARIABLES AND STRUCTURES=-
//----------------------------------------------------------------------------------------------------------------------------------
static volatile sig_atomic_t exit_gracefully=0; // Used to cleanly exit then Ctrl-C is pressed
unsigned int sec_epoch; // Seconds from 1970 epoch to epoch "epoch"
int debug=0; // Debug message level. 0=Off; higher number=increasing verbosity
int macro_priority=0; // Priority of macros (Trust more or less than info in data). 0=Trust data, 1=Trust macro info
int calib=0; // Indicate whether we are creating a calibrated (1) or edited (0) archive.
extern char IDList[][33]; // ID array, string with short name of ID code. Defined in "id.c".
pthread_t sctmthread = 0; // S/C TM thread
pthread_t scithread = 0; // Science thread
pthread_t hkthread = 0; // HK thread
prp_type comm; // Linked property/value list for PDS Common Parameters
prp_type dict; // Linked property/value list for PDS LAP Dictionary
prp_type hkl; // Linked property/value list for PDS LAP HK
prp_type anom; // Linked property/value list for Anomalies
prp_type ind; // Linked property/value list for PDS INDEX Label
prp_type mdesc; // Linked property/value list for Macro Descriptions
// prp_type cc_lbl; // Linked property/value list for Coarse Bias Voltage Calibration Data Label
// prp_type ic_lbl; // Linked property/value list for Bias Current Calibration Data Label
// prp_type fc_lbl; // Linked property/value list for Fine Bias Voltage Calibration Data Label
c_type v_conv; // Coarse-voltage-bias-conversion-to-TM data structure (table).
c_type f_conv; // Fine -voltage-bias-conversion-to-TM data structure (table. (fine=fine sweep)
c_type i_conv; // Current -bias-conversion-to-TM data structure (table).
m_type m_conv; // CALIB_MEAS data structure.
calib_coeff_data_type calib_coeff_data; // CALIB_COEFF data structure
tc_type tcp={0,NULL,NULL,NULL}; // Time correlation packets structure
unsigned int ilogtab[256]; // Inverse logarithmic table for decoding of logarithmic sweeps
hk_info_type hk_info; // Some HK info needed to dump last HK label at exit
// Matrix of linked property/value lists containing macros
prp_type macros[MAX_MACRO_BLCKS][MAX_MACROS_INBL];
mp_type mp; // Mission phase data
extern FILE *stdout; // Keep track of standard output
// Initialize PDS configuration info, paths and file descriptors
pds_type pds =
{
0, // Number of times the spacecraft clock has been reset
"", // Date and time of last spacecraft clock reset example: 2003-01-01T00:00:00
"", // Path to LAP template PDS archive
"", // Path to LAP macro descriptions
0, // DPL Number
0.0, // Data Set Version
"", // Label revision note
"", // Release date
"", // Path to configuration file
"", // Path to anomaly file
"", // Path to bias settings file
"", // Path to (CALIB macro) exclude file
"", // Path to data exclude file
"", // Path to macro description file
"", // Mission calendar path and file name
"", // Dataset parent directory
"", // Archive path PDS (Out dataset)
"", // Archive path DDS (In data/TM)
"", // Path to time correlation packets
"", // Log path
"", // Data path PDS science edited
"", // Data path PDS science calibrated
"", // Root path to calibration data.
"", // Path to fine bias calibration data
"", // Path to coarse bias calibration data
"", // Path to current bias calibration data
"", // Path to offset calibration data
"", // Path to density frequency response probe 1
"", // Path to density frequency response probe 2
"", // Path to e-field frequency response probe 1
"", // Path to e-field frequency response probe 2
"", // Offset calibration exceptions data (LBL)
"", // Offset calibration exceptions data (TAB)
"", // Data subdirectory path for PDS science
"", // Data path PDS HK
"", // Data subdirectory path for PDS HK
"", // Path to data that has not been accepted
"", // Index table file path.
"", // Path to SPICE metakernel.
NULL, // Log file descriptor LAP PDS System log
NULL, // S/C packet filtering log
NULL, // Log file descriptor Science Decoding log
NULL, // Log file descriptor HK Decoding log
NULL, // Log file descriptor dds packet filter log
NULL, // File descriptor to recoverfile
NULL, // Science archive PDS data file descriptor
NULL, // Science data table file descriptor
NULL, // HK archive PDS data file descriptor
NULL, // HK data table file descriptor
NULL, // Index label file descriptor
NULL, // Index table file descriptor
NULL, // DDS Read file descriptor
NULL, // DDS progress file descriptor
};
// Can not place in pds.h, since pds.h is used in multiple files and this is a variable declaration.
gstype gstations[NGSTATIONS]=
{
{0x000D,"ESA Villafranca 2 "},
{0x0015,"ESA Kourou "},
{0x0016,"NDIULite (for SVTs) "},
{0x0017,"ESA New Norcia "},
{0x0022,"NASA Goldstone "},
{0x0023,"NASA Canberra "},
{0x0024,"NASA Madrid "},
{0x007F,"ESA/ESOC Test station "},
{0x0082,"NDIU classic (SVTs) "},
};
// Circular Buffers
//--------------------------------------------------------------------------
// Originally intended to support realtime stream processing.
// Now they are usefull to spread processing load on multicore machines.
buffer_struct_type cbtm; // Circular S/C TM buffer
buffer_struct_type cbs; // Circular in Science buffer
buffer_struct_type cbh; // Circular in HK buffer
buffer_struct_type cmb; // Circular mirror buffer
int sc_thread_cancel_threshold_timeout;
// Mutexes for protecting code which is not thread-safe.
static pthread_mutex_t protect_log = PTHREAD_MUTEX_INITIALIZER; // Added mutex protector for logging functions
// Mutex for SPICE functions. SPICE functions are not thread-safe.
// NOTE: Can not handle recursive locking(?) (mutex-protected blocks within mutex-protected blocks).
static pthread_mutex_t protect_spice = PTHREAD_MUTEX_INITIALIZER;
// -=MAIN FUNCTION=-
//----------------------------------------------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
if (RUN_TEST_CODE) {
return main_TEST(argc, argv); // INFORMAL TEST CODE. WILL (PROBABLY) EXIT PDS ON ITS OWN.
}
// ASSERTION
// Try to guarantee that MISSING_CONSTANT fits into int variables.
if (sizeof(int) < 4) {
fprintf(stderr, "ERROR: pds has been compiled for sizeof(int)=%i < 4. Therefore, pds is not guaranteed to work.\n\n", sizeof(int));
exit(1);
}
struct sigaction act;
int status; // Just a temporary status/error code variable
char tstr1[MAX_STR]; // Temporary string
char tstr2[MAX_STR]; // Temporary string
char tstr3[MAX_STR]; // Temporary string
prp_type tmp_lbl; // Linked property/value list for temporary use
arg_type scarg; // Argument structure S/C TM thread
arg_type sarg; // Argument structure Science thread
arg_type harg; // Argument structure HK thread
mp.t_start = 0; // Mission start time, small dummy
mp.t_stop = INT32_MAX; // Mission stop time, big dummy ~68 years
mp.sccd_start_data = NAN;
mp.sccd_stop_data = NAN;
unsigned int volume_id_nbr; // The four-digit number in VOLUME_ID, ROLAP_xxxx
if(GetOption("-h",argc,argv,NULL))
{
PrintUserHelpInfo(stdout, argv[0]);
exit(0);
}
// Basic check on number of input arguments.
if (argc>1+(2*21+1) || argc<1+6)
{
fprintf(stderr, "ERROR: pds called with too few or too many arguments.\n\n");
PrintUserHelpInfo(stdout, argv[0]); // NOTE: Prints to stderr.
exit(1);
}
printf("\n");
// Get options, note that we do no syntactic checks of the input!
// We assume it is correct, things like: pds -c or pds -c pds.conf -debug
// Will not work, since required parameter values -mp xx -vid yy -dsv zz are missing!!
// ----------------------------------------------------------------------------------------------------
//========================================
// Get path option for configuration file
//========================================
if(!GetOption("-c",argc,argv,pds.cpath)) {
sprintf(pds.cpath,"%s/%s",getenv("HOME"),"pds.conf");
}
//========================================
// Get path option for anomaly file
//========================================
if(!GetOption("-a",argc,argv,pds.apath)) {
sprintf(pds.apath,"%s/%s",getenv("HOME"),"pds.anomalies");
}
//========================================
// Get path option for bias file
//========================================
if(!GetOption("-b",argc,argv,pds.bpath)) {
sprintf(pds.bpath,"%s/%s",getenv("HOME"),"pds.bias");
}
//========================================
// Get path option for exclude file
//========================================
if(!GetOption("-e",argc,argv,pds.epath)) { // Erik P G Johansson 2015-03-24: Fixed bug. Store path in correct variable.
sprintf(pds.epath,"%s/%s",getenv("HOME"),"pds.exclude");
}
//===========================================
// Get path option for mode description file
//===========================================
if(!GetOption("-m",argc,argv,pds.mpath)) { // Erik P G Johansson 2015-03-24: Fixed bug. Store path in correct variable.
sprintf(pds.mpath,"%s/%s",getenv("HOME"),"pds.modes");
}
//=============================================
// Get path option for data exclude times file
//=============================================
// Erik P G Johansson 2015-03-25: Added
if(!GetOption("-d",argc,argv,pds.depath)) {
sprintf(pds.depath,"%s/%s",getenv("HOME"),"pds.dataexcludetimes");
}
//=============================================
// Get calibration option
//=============================================
if(GetOption("-calib",argc,argv,NULL))
{
printf("Producing calibrated archive\n");
calib=1;
}
//=============================================
// Get mission phase option
//=============================================
// Only process mission phase abbreviated as second column in mission calendar file.
if(GetOption("-mp",argc,argv,tstr1))
{
strncpy(mp.mission_phase_abbrev, tstr1, 4);
mp.mission_phase_abbrev[4]='\0';
printf("Processing mission phase with ID %s\n", mp.mission_phase_abbrev);
}
else
{
fprintf(stderr,"Mandatory mission phase abbreviation argument is missing.\n");
exit(1);
}
//=============================================
// Get volume ID option
//=============================================
if(GetOption("-vid",argc,argv,tstr1))
{
sscanf(tstr1,"%d\n",&volume_id_nbr);
printf("Volume ID: %d\n",volume_id_nbr);
if(volume_id_nbr==0 || volume_id_nbr>9999)
{
fprintf(stderr,"Can not interpret -vid <volume_id_nbr> argument.\n");
exit(1);
}
}
else
{
fprintf(stderr,"Mandatory volume ID argument is missing (Unique for each data set)\n");
exit(1);
}
//=======================
// Get data set version
//=======================
if(GetOption("-dsv",argc,argv,tstr1))
{
sscanf(tstr1,"%f\n",&pds.DataSetVersion);
printf("Data set version: %2.1f\n",pds.DataSetVersion);
if(pds.DataSetVersion<=0.0 || pds.DataSetVersion>9.9)
{
fprintf(stderr,"Can not interpret -dsv <data_set_version> argument.\n");
exit(1);
}
}
else
{
fprintf(stderr,"Mandatory data set version argument is missing (incremental for new versions of a data set)\n");
exit(1);
}
//==================
// Get debug option
//==================
if(GetOption("-debug",argc,argv,tstr1))
{
sscanf(tstr1,"%d",&debug);
printf("Debug level %d\n",debug);
}
printf("\n");
ProtectPlnkInit();
// Loads first part of configuration information into the PDS structure
if((status=LoadConfig1(&pds))<0)
{
fprintf(stderr,"Mangled configuration file (part 1): %d\n",status);
exit(1);
}
//==================
// Get debug option
//==================
if(GetOption("-mc", argc, argv, pds.mcpath))
{
printf("Mission calendar file : %s (command-line override)\n", pds.mcpath);
}
if(calib) {
pds.DPLNumber=3; // Calibrated data has DPL number 3
} else {
pds.DPLNumber=2; // Edited data has DPL number 2
}
sec_epoch=ConvertUtc2Timet_2(pds.SCResetClock); // Compute seconds from epoch 1970 to S/C Clock reset.
// Get mission phase data
// if(InitMissionPhaseStructFromMissionCalendar(&mp, &pds) < 0) {
if(InitMissionPhaseStructFromMissionCalendar(&mp, pds.mcpath, pds.DPLNumber, pds.DataSetVersion) < 0) {
exit(1);
}
/*================================================================================================
* Get options for overriding mission phase parameters (and description string)
*================================================================================================
* Overwrite mission phase values if values can be found among (optional) command-line arguments.
* NOTE: Current implementation requires 4 options and optionally another 3.
* NOTE: These options have to be read AFTER reading and interpreting the mission calendar
* so that those values are available if they are not overwritten here.
*================================================================================================*/
char descr_str[MAX_STR];
if (GetOption("-ds", argc, argv, descr_str))
{
if (GetOption("-mpn", argc, argv, tstr1)) {
sprintf(mp.mission_phase_name, "\"%s\"", tstr1); // NOTE: Surround with quotes since the archiving standard requires it.
} else {
fprintf(stderr, "Can not find option -mpn.\n");
exit(1);
}
if (GetOption("-pb", argc, argv, tstr1)) {
if((status=ConvertUtc2Timet(tstr1,&(mp.t_start))) < 0) {
fprintf(stderr, "Can not convert argument \"%s\" to a time: error code %i\n", tstr1, status);
exit(1);
}
} else {
fprintf(stderr, "Can not find option -pb.\n");
exit(1);
}
// Require exactly one of two options.
if (GetOption("-pd", argc, argv, tstr1))
{
float dataset_duration_days;
if (!sscanf(tstr1, "%e", &dataset_duration_days)) {
fprintf(stderr, "Can not interpret argument \"%s\".\n", tstr1); // NOTE: Periods shorter than one day are useful for debugging. Therefore permit decimal numbers.