-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfcsvlogs.pl
executable file
·2148 lines (2010 loc) · 74.5 KB
/
cfcsvlogs.pl
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
#!/usr/bin/perl -w
# NAME: cfcsvlogs.pl, cfjsonlogs.pl (was: jsonfeeds.pl)
# AIM: Annalyse logs written by cfjsonlog.pl, in a target directory.
# Log file name will change each day, in the form - $out_dir/'flights-YYYY-MM-DD.csv'
# 2016-07-27 - Add the tide of usage - times at min/max usage... ie fliers(cs) on line
# 16/07/2016 - Beginning to mature the HTML output - more jumps...
# 12/07/2016 - Begin to add HTML output
# 2016-07-09 - Initial cut
######################################
use strict;
use warnings;
use File::Basename; # split path ($name,$dir,$ext) = fileparse($file [, qr/\.[^.]*/] )
use File::Spec; # File::Spec->rel2abs($rel); # get ABSOLUTE from REALTIVE get_full_path
use File::stat;
use Date::Parse;
use Time::gmtime;
use LWP::Simple;
use JSON;
use Data::Dumper;
use Cwd;
use Time::HiRes qw(gettimeofday tv_interval); # provide more accurate timings
my $begin_app = [ gettimeofday ];
my $os = $^O;
my $PATH_SEP = '/';
$PATH_SEP = "\\" if ($os =~ /win/i);
my ($pgmname,$perl_dir) = fileparse($0);
my $temp_dir = $perl_dir . "temp";
unshift(@INC, $perl_dir);
require 'lib_utils.pl' or die "Unable to load 'lib_utils.pl' Check paths in \@INC...\n";
require 'fg_wsg84.pl' or die "Unable to load fg_wsg84.pl ...\n";
# log file stuff
our ($LF);
my $outfile = $temp_dir."/temp.$pgmname.txt";
$outfile = path_u2d($outfile) if ($os =~ /win/i);
open_log($outfile);
# user variables
my $VERS = "0.0.9 2016-07-27";
##my $VERS = "0.0.8 2016-07-16";
##my $VERS = "0.0.7 2016-07-07";
##my $VERS = "0.0.6 2016-07-06";
##my $VERS = "0.0.5 2015-01-09";
my $load_log = 0;
my $in_file = '';
my $verbosity = 0;
my $out_file = '';
my $out_dir = $temp_dir.$PATH_SEP."temp-flights";
my $only_one_feed = 0;
my $show_each_flt = 0;
my $max_show_cs = 1000;
my $max_table_lines = 25;
my $min_table_lines = 8;
my $add_table_captions = 1;
my $use_utc_time = 1;
my $add_csv_header = 0; # add CSV header, to each **new** file
my $out_html = $temp_dir."/temphtml2.htm"; # DEFAULT out HTML
#############################################################
# crossfeed json feed - never fetch faster than 1 Hz!
my $feed1 = "http://crossfeed.freeflightsim.org/flights.json";
#############################################################
# ### DEBUG ###
my $debug_on = 0;
##my $def_file = 'C:\GTools\perl\scripts\temp\temp-flights\cflogs';
##my $def_file = 'C:\GTools\perl\scripts\temp\temp-flights\flights-2016-07-10.csv';
##my $def_file = 'C:\GTools\perl\scripts\temp\temp-flights\flights-2016-07-09.csv';
##my $def_file = 'C:\GTools\perl\scripts\temp\temp-flights\flights-2016-07-07.csv';
my $def_file = 'F:\FGx\crossfeed-dailies\flights-2016-07-30.csv';
### program variables
my @warnings = ();
my $cwd = cwd();
my $MPS2KT = 1.94384; # meters per second to knots
my $SG_EPSILON = 0.000001;
my $last_ymd = '';
my $g_log_period = "N/A";
my $g_min_flts = 999999;
my $g_max_flts = 0;
my $g_min_upd = 'N/A';
my $g_max_upd = 'N/A';
my $g_tot_files = 0;
my $g_tot_records = 0;
my @g_files = (); # set of CSV files used to create html summary
sub VERB1() { return $verbosity >= 1; }
sub VERB2() { return $verbosity >= 2; }
sub VERB5() { return $verbosity >= 5; }
sub VERB9() { return $verbosity >= 9; }
sub show_warnings($) {
my ($val) = @_;
if (@warnings) {
prt( "\nGot ".scalar @warnings." WARNINGS...\n" );
foreach my $itm (@warnings) {
prt("$itm\n");
}
prt("\n");
} else {
prt( "\nNo warnings issued.\n\n" ) if (VERB9());
}
}
sub prt_ran_for() {
my $end_app = [ gettimeofday ];
my $elap = tv_interval( $begin_app, $end_app );
my $tm = get_time_stg($elap);
prt("Ran for $tm ...\n");
}
sub pgm_exit($$) {
my ($val,$msg) = @_;
if (length($msg)) {
$msg .= "\n" if (!($msg =~ /\n$/));
prt($msg);
}
show_warnings($val);
prt_ran_for();
close_log($outfile,$load_log);
exit($val);
}
sub prtw($) {
my ($tx) = shift;
$tx =~ s/\n$//;
prt("$tx\n");
push(@warnings,$tx);
}
sub fetch_url($) { # see gettaf01.pl
my ($url) = shift;
prt( "Fetching: $url\n" ) if (VERB9());
my $txt = get($url);
my ($len);
if ($txt && length($txt)) {
$len = length($txt);
if (VERB9()) {
prt("Got $len chars...\n");
prt("$txt\n");
}
} else {
prt( "No text available from $url!\n" ) if (VERB5());
}
return $txt;
}
my $max_model = 14; # beech99-model
# "Aircraft/777/Models/777-200ER.xml
sub get_model($) {
my $mod = shift;
my @arr = split(/\//,$mod);
$mod = $arr[-1];
$mod =~ s/\.xml$//;
$mod =~ s/-model$//;
return $mod;
}
sub get_ll_double($) {
my $deg = shift;
my $dbl = sprintf("%12.6f",$deg);
return $dbl;
}
sub get_alt_stg($) {
my $alt = shift;
if ($alt > 10000) {
$alt = 'FL'.int($alt / 100);
#$alt = sprintf("%6d",$alt);
} else {
$alt = sprintf("%8d",$alt);
}
return $alt;
}
sub get_decimal_stg($$$) {
my ($dec,$il,$dl) = @_;
my (@arr);
if ($dec =~ /\./) {
@arr = split(/\./,$dec);
if (scalar @arr == 2) {
$arr[0] = " ".$arr[0] while (length($arr[0]) < $il);
$dec = $arr[0];
if ($dl > 0) {
$dec .= ".";
$arr[1] = substr($arr[1],0,$dl) if (length($arr[1]) > $dl);
$dec .= $arr[1];
}
}
} else {
$dec = " $dec" while (length($dec) < $il);
if ($dl) {
$dec .= ".";
while ($dl--) {
$dec .= "0";
}
}
}
return $dec;
}
sub get_sg_dist_stg($) {
my ($sg_dist) = @_;
my $sg_km = $sg_dist / 1000;
my $sg_im = int($sg_dist);
my $sg_ikm = int($sg_km + 0.5);
my $dlen = 5;
# if (abs($sg_pdist) < $CP_EPSILON)
my $sg_dist_stg = "";
if (abs($sg_km) > $SG_EPSILON) { # = 0.0000001; # EQUALS SG_EPSILON 20101121
if ($sg_ikm && ($sg_km >= 1)) {
$sg_km = int(($sg_km * 10) + 0.05) / 10;
#$sg_dist_stg .= get_decimal_stg($sg_km,5,1)." km";
$sg_dist_stg .= get_decimal_stg($sg_km,($dlen - 2),1)." km";
} else {
#$sg_dist_stg .= "$sg_im m, <1km";
#$sg_dist_stg .= get_decimal_stg($sg_im,7,0)." m.";
$sg_dist_stg .= get_decimal_stg($sg_im,$dlen,0)." m.";
}
} else {
#$sg_dist_stg .= "0 m";
#$sg_dist_stg .= get_decimal_stg('0',7,0)." m.";
$sg_dist_stg .= get_decimal_stg('0',$dlen,0)." m.";
}
return $sg_dist_stg;
}
sub display_flight($$$$$$$$$) {
my ($callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm,$type) = @_;
$callsign .= ' ' while (length($callsign) < 8);
$lat = get_ll_double($lat);
$lon = get_ll_double($lon);
$alt_ft = get_alt_stg($alt_ft);
$alt_ft = ' '.$alt_ft while (length($alt_ft) < 8);
$model = ' '.$model while (length($model) < $max_model);
$spd_kts = ' '.$spd_kts while (length($spd_kts) < 7);
$hdg = ' '.$hdg while (length($hdg) < 4);
$dist_nm = ' '.$dist_nm while (length($dist_nm) < 7);
my $line = "$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm - $type\n";
prt($line);
#return $line;
}
my $iocnt = 0;
my $csv_headers = "fid,callsign,lat,lon,alt_ft,model,spd_kts,hdg,dist_nm,update,tot_secs\n";
sub record_flight($$$$$$$$$$$) {
my ($fid,$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm,$tot_secs,$upd1) = @_;
my $epoch = time();
my $line = "$fid,$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm,$upd1,$tot_secs\n";
if (length($out_dir) && (-d $out_dir)) {
my $ymd = get_YYYYMMDD($epoch);
$ymd =~ s/\//-/g; # conver to a path compatible name
my $file = $out_dir.$PATH_SEP."flights-$ymd.csv";
if (-f $file) {
$last_ymd = $ymd if (length($last_ymd) == 0);
if ($ymd eq $last_ymd) {
prt("Appended to file $file...\n") if ($iocnt == 0);
append2file($line,$file);
$iocnt++;
} else {
$last_ymd = $ymd;
$line = $csv_headers.$line if ($add_csv_header);
write2file($line,$file);
prt("Created new file $file...\n");
$iocnt = 1;
}
} else {
$last_ymd = $ymd;
$line = $csv_headers.$line if ($add_csv_header);
write2file($line,$file);
prt("Created new file $file...\n");
$iocnt = 1;
}
} elsif ($iocnt == 0) {
if (length($out_dir)) {
$line = File::Spec->rel2abs($out_dir);
my ($n,$d) = fileparse($line);
prt("\nInfo: Can find dir '$d', but missing '$n'!\n") if (-d $d);
pgm_exit(1,"ERROR: Can NOT 'stat' output dir '$line'!\n".
"This directory MUST exist. Create and re-run...\n\n");
} else {
pgm_exit(1,"\nERROR: No valid output directory given!\n\n");
}
$iocnt++;
}
}
# crossfeed
# {"success":true,"source":"cf-client","last_updated":"2015-01-22 17:35:42","flights":[
# {"fid":1421695466000,"callsign":"Charlie","lat":45.360490,"lon":5.335270,"alt_ft":1288,"model":"Aircraft/777/Models/777-200ER.xml","spd_kts":0,"hdg":324,"dist_nm":379},
# {"fid":1421826354000,"callsign":"saphir","lat":51.242089,"lon":-0.834174,"alt_ft":3113,"model":"Aircraft/777/Models/777-200LR.xml","spd_kts":187,"hdg":146,"dist_nm":16384},
# fid callsign lat lon alt_ft model spd_kts hdg dist_nm
# php feed
# {"pilots":[
# {"callsign":"Charlie","aircraft":"777-200ER","latitude":"45.360490","longitude":"5.335270","altitude":"1287.707060","heading":323.8147125058},
# {"callsign":"saphir","aircraft":"777-200LR","latitude":"51.240209","longitude":"-0.832257","altitude":"3109.778402","heading":145.73179151024},
# {"callsign":"EGXU_TW","aircraft":"OpenRadar","latitude":"54.045074","longitude":"-1.250728","altitude":"53.001620","heading":0},
# callsign aircraft latitude longitude altitude heading
sub repeat_feeds() {
my $repeat = 1;
my $secs = 5;
my $header = '';
my $upd1 = "Unknown";
my ($fid,$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm);
my ($ra1,$cnt1,$i,$rh2,$line,$msg,$show,$rh3);
my ($lat2,$lon2,$alt_ft2,$model2,$spd_kts2,$hdg2,$dist_nm2,$type);
my ($res,$az1,$az2,$dist,$mps,$tsecs);
my $min_mps = 5;
my %hash = ();
$callsign = 'callsign';
$lat = 'latitude';
$lon = 'longitude';
$alt_ft = 'altitude';
$model = 'model';
$spd_kts = 'spd kts';
$hdg = 'hdg';
$dist_nm = 'dist nm';
# display header
$callsign .= ' ' while (length($callsign) < 8);
$lat = ' '.$lat while (length($lat) < 12);
$lon = ' '.$lon while (length($lon) < 12);
$alt_ft = ' '.$alt_ft while (length($alt_ft) < 8);
$model = ' '.$model while (length($model) < $max_model);
$spd_kts = ' '.$spd_kts while (length($spd_kts) < 7);
$hdg = ' '.$hdg while (length($hdg) < 4);
$dist_nm = ' '.$dist_nm while (length($dist_nm) < 7);
$msg = "$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm\n";
$header = $msg;
my $tot_secs = 0;
while ($repeat) {
my $txt1 = fetch_url($feed1); # "http://crossfeed.freeflightsim.org/flights.json";
my $json = JSON->new->allow_nonref;
my $rh1 = $json->decode( $txt1 );
###prt(Dumper($rh1));
my $upd1 = "Unknown";
my ($fid,$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm);
my ($ra1,$cnt1,$i,$rh2,$line,$msg);
if (defined ${$rh1}{last_updated}) {
$upd1 = ${$rh1}{last_updated};
}
if (defined ${$rh1}{flights}) {
$ra1 = ${$rh1}{flights};
$cnt1 = scalar @{$ra1};
# get all current FIDS in current hash
my @fids = keys %hash;
my $cnt2 = scalar @fids; # get COUNT in current db
my %hfids = ();
foreach $fid (@fids) {
$hfids{$fid} = 1;
}
# pre-process current cnt1, for missing FIDs
for ($i = 0; $i < $cnt1; $i++) {
$rh2 = ${$ra1}[$i]; # extract the hash
$fid = ${$rh2}{fid}; # get FID
if (defined $hfids{$fid}) {
delete $hfids{$fid}; # REMOVVE from list
}
}
# any remaining in FIDS need to DIE, have LEFT, no more
@fids = keys %hfids;
if (@fids) {
foreach $fid (@fids) {
$rh3 = $hash{$fid};
$callsign = ${$rh3}[0];
$lat = ${$rh3}[1];
$lon = ${$rh3}[2];
$alt_ft = ${$rh3}[3];
$model = ${$rh3}[4];
$spd_kts = ${$rh3}[5];
$hdg = ${$rh3}[6];
$dist_nm = ${$rh3}[7];
$dist = ${$rh3}[8];
$tsecs = ${$rh3}[9];
my $active = secs_HHMMSS($tsecs);
$type = "LEFT ".get_sg_dist_stg($dist).", after $active";
display_flight($callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm,$type);
delete $hash{$fid}; # remove it
}
}
@fids = keys %hash;
my $cnt3 = scalar @fids;
prt("Updated: $upd1, flights $cnt1... db $cnt2/$cnt3\n");
# now process THIS set of json flights
for ($i = 0; $i < $cnt1; $i++) {
$rh2 = ${$ra1}[$i]; # extract the hash
$fid = ${$rh2}{fid};
$callsign = ${$rh2}{callsign};
$lat = ${$rh2}{lat};
$lon = ${$rh2}{lon};
$alt_ft = ${$rh2}{alt_ft};
$model = get_model(${$rh2}{model});
$spd_kts = ${$rh2}{spd_kts};
$hdg = ${$rh2}{hdg};
$dist_nm = ${$rh2}{dist_nm};
record_flight($fid,$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm,$tot_secs,$upd1);
###prt("$fid,$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm\n");
if (defined $hash{$fid}) {
$show = 0;
$rh3 = $hash{$fid};
$lat2 = ${$rh3}[1];
$lon2 = ${$rh3}[2];
$type = 'SAME';
if (($lat != $lat2) || ($lon != $lon2)) {
$res = fg_geo_inverse_wgs_84($lat2,$lon2,$lat,$lon,\$az1,\$az2,\$dist);
$mps = $dist / $secs;
if ($mps > $min_mps) {
$type = "moved ".get_sg_dist_stg($dist);
$show = 1;
${$rh3}[1] = $lat;
${$rh3}[2] = $lon;
${$rh3}[8] += $dist;
}
}
${$rh3}[9] += $secs; # accumulate rough seconds - better based on update string
} else {
# 0 1 2 3 4 5 6 7 8 9 10
$hash{$fid} = [$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm,0,0,$upd1];
$show = 1;
$type = 'NEW';
}
if ($show) {
display_flight($callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm,$type);
#$msg .= $line;
}
}
#if (length($out_file)) {
# write2file($msg,$out_file);
# prt("JSON crossfeed flight list written to $out_file\n");
#}
} else {
prt("'flights' is NOT defined in hash 1!\n");
}
prt("Sleep for $secs second...\n");
sleep $secs;
$tot_secs += $secs;
}
}
sub get_one_feed() {
my $txt1 = fetch_url($feed1); # "http://crossfeed.freeflightsim.org/flights.json";
my $json = JSON->new->allow_nonref;
my $rh1 = $json->decode( $txt1 );
###prt(Dumper($rh1));
my $csv = '';
my $upd1 = "Unknown";
my ($fid,$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm);
my ($ra1,$cnt1,$i,$rh2,$line,$msg);
if (defined ${$rh1}{last_updated}) {
$upd1 = ${$rh1}{last_updated};
}
if (defined ${$rh1}{flights}) {
$ra1 = ${$rh1}{flights};
$cnt1 = scalar @{$ra1};
prt("Updated: $upd1, flights $cnt1...\n");
$callsign = 'callsign';
$lat = 'latitude';
$lon = 'longitude';
$alt_ft = 'altitude';
$model = 'model';
$spd_kts = 'spd kts';
$hdg = 'hdg';
$dist_nm = 'dist nm';
$csv .= "$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm,update\n";
# display header
$callsign .= ' ' while (length($callsign) < 8);
$lat = ' '.$lat while (length($lat) < 12);
$lon = ' '.$lon while (length($lon) < 12);
$alt_ft = ' '.$alt_ft while (length($alt_ft) < 8);
$model = ' '.$model while (length($model) < $max_model);
$spd_kts = ' '.$spd_kts while (length($spd_kts) < 7);
$hdg = ' '.$hdg while (length($hdg) < 4);
$dist_nm = ' '.$dist_nm while (length($dist_nm) < 7);
$msg = "$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm\n";
prt($msg);
for ($i = 0; $i < $cnt1; $i++) {
$rh2 = ${$ra1}[$i]; # extract the hash
$fid = ${$rh2}{fid};
$callsign = ${$rh2}{callsign};
$lat = ${$rh2}{lat};
$lon = ${$rh2}{lon};
$alt_ft = ${$rh2}{alt_ft};
$model = get_model(${$rh2}{model});
$spd_kts = ${$rh2}{spd_kts};
$hdg = ${$rh2}{hdg};
$dist_nm = ${$rh2}{dist_nm};
###prt("$fid,$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm\n");
$csv .= "$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm,$upd1\n";
###################################
# display - for spaced display only
$callsign .= ' ' while (length($callsign) < 8);
$lat = get_ll_double($lat);
$lon = get_ll_double($lon);
$alt_ft = get_alt_stg($alt_ft);
$alt_ft = ' '.$alt_ft while (length($alt_ft) < 8);
$model = ' '.$model while (length($model) < $max_model);
$spd_kts = ' '.$spd_kts while (length($spd_kts) < 7);
$hdg = ' '.$hdg while (length($hdg) < 4);
$dist_nm = ' '.$dist_nm while (length($dist_nm) < 7);
$line = "$callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm\n";
prt($line);
$msg .= $line;
###################################
}
if (length($out_file)) {
### No, do not output the spaced display lines
### write2file($msg,$out_file);
### Write instead the compact CSV collected as well
rename_2_old_bak($out_file);
write2file($csv,$out_file);
prt("JSON crossfeed CSV flight list written to $out_file\n");
} else {
prt("Compact CSV discarded. No -o out-file.csv given...\n");
}
} else {
prt("'flights' is NOT defined in hash 1!\n");
}
## $load_log = 1;
}
my $def_min_secs = 60;
my $def_min_dist = 2; # ignore a flight that does not move...
my %flt_fids = ();
my $htm = '';
my $href_txt = "<a href=\"#models\"><b>models</b></a>:\n".
"sorted (<a href=\"#mod.alpha\">alpha</a>, <a href=\"#mod.dist\">dist</a>, <a href=\"#mod.time\">time</a>)\n".
"<a href=\"#callsign\"><b>callsigns</b></a>:\n".
"sorted (<a href=\"#stat.alpha\">alpha</a>, <a href=\"#stat.dist\">dist</a>, <a href=\"#stat.time\">time</a>)\n".
"<a href=\"#stats\">stats</a>\n".
"page: <a href=\"#top\">top</a> <a href=\"#end\">end</a>\n";
sub get_warn_txt() {
my $txt = <<EOF;
<p><b>WARNING</b>: The times and distance flown can be greatly influenced by sim time warping,
and sim time speed up and slow down, so are always <b>only estimates</b>! And this is only the
results of one particular, specific crossfeed. That is, no prizes will be awarded on
these results. ;=)) And as such should not be taken as an general indication of
<a target="_blank" href="http://www.flightgear.org/">FlightGear</a> usage, and just a
sub-set of MultiPlayer usage. Also note the callsign <b>callsig</b> is the <b>DEFAULT</b>
callsign when the user does not enter his/her own callsign, so this makes the appearance
of callsign <b>callsig</b> misleading, in that it can be <b>multiple</b> users, probably sometimes
at the same time... very confusing...
</p>
EOF
return $txt;
}
sub get_body_html() {
my $txt = "<body>\n";
$txt .= "<a id=\"top\" name=\"top\"></a>\n";
$txt .= "<h1>Multiplayer Crossfeed Flights</h1>\n";
$txt .= "<p class=\"top\">$href_txt</p>\n";
$txt .= "<h2>$g_log_period</h2>\n";
$txt .= get_warn_txt();
return $txt;
}
sub get_html_head() {
my $txt = <<EOF;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Crossfeed Flights</title>
<style>
body {
display: block;
margin: 8px;
}
.vat {
vertical-align: top;
white-space: nowrap;
}
.ran {
text-align: right;
vertical-align: top;
}
em {
color: #204000;
font-weight: bold;
}
tr:nth-child(even) {background-color: #f2f2f2}
tr:hover {background-color: #ddd;}
h1 {
background : #efefef;
border-style : solid solid solid solid;
border-color : #d9e2e2;
border-width : 1px;
padding : 2px 2px 2px 2px;
font-size : 300%;
text-align : center;
}
h2 {
font-size : 16pt;
font-weight : bold;
background-color : #ccccff;
}
p.top {
margin : 0;
border-style : none;
padding : 0;
text-align : center;
}
caption {
display: table-caption;
text-align: center;
font-weight: bold;
font-size: 110%;
background-color: #ffffc0;
}
</style>
</head>
EOF
$txt .= get_body_html();
return $txt;
}
sub get_html_end($) {
my $utc = shift;
my $tm = "<!-- Generated $utc, by $pgmname -->";
my $txt = <<EOF;
<p class="top">$href_txt</p>
<a name="end" id="end"></a>
<p align="right">eof <a href="#top">top</a></p>
$tm
</body>
</html>
EOF
return $txt;
}
sub get_file_table() {
my $h = "<p class=\"top\">$href_txt</p>\n";
my $cnt = scalar @g_files;
$h .= "<table border='1' width='100%'>\n";
$h .= "<caption>Display of $cnt files scanned.</caption>\n"; # if ($add_table_captions);
# 0 1 2 3 4 5 6 7
#ush(@g_files,[$inf,$sz,$lncnt,$first_upd,$last_upd,$min_flts,$max_flts,$tm]);
my ($ff,$inf,$dir,$sz,$lncnt,$first_upd,$last_upd,$min_flts,$max_flts,$tm,$ra);
$h .= "<tr>\n";
$h .= "<th>file name</th>\n";
$h .= "<th>size</th>\n";
$h .= "<th>lines</th>\n";
$h .= "<th>begin</th>\n";
$h .= "<th>end</th>\n";
$h .= "<th>min</th>\n";
$h .= "<th>max</th>\n";
$h .= "<th>date</th>\n";
$h .= "</tr>\n";
foreach $ra (@g_files) {
$ff = ${$ra}[0];
$sz = ${$ra}[1];
$lncnt = ${$ra}[2];
$first_upd = ${$ra}[3];
$last_upd = ${$ra}[4];
$min_flts = ${$ra}[5];
$max_flts = ${$ra}[6];
$tm = ${$ra}[7];
my ($ctm);
if ($use_utc_time) {
$ctm = lu_get_YYYYMMDD_hhmmss_UTC($tm);
} else {
my @lt = localtime($tm);
$ctm = sprintf( "%02d/%02d/%04d %02d:%02d", $lt[3], $lt[4]+1, $lt[5]+1900, $lt[2], $lt[1]);
}
($inf,$dir) = fileparse($ff);
$h .= "<tr>\n";
$h .= "<td>$inf</td>\n";
$h .= "<td align='right'>".get_nn($sz)."</td>\n";
$h .= "<td align='right'>".get_nn($lncnt)."</td>\n";
$h .= "<td>$first_upd</td>\n";
$h .= "<td>$last_upd</td>\n";
# json groups $g_min_flts to $g_max_flts
if ($g_min_flts == $min_flts) {
$h .= "<td align='right'><em>$min_flts</em></td>\n";
} else {
$h .= "<td align='right'>$min_flts</td>\n";
}
if ($g_max_flts == $max_flts) {
$h .= "<td align='right'><em>$max_flts</em></td>\n";
} else {
$h .= "<td align='right'>$max_flts</td>\n";
}
$h .= "<td>$ctm</td>\n";
$h .= "</tr>\n";
}
$h .= "</table>\n";
return $h;
}
sub get_html_tail() {
my $utc = lu_get_YYYYMMDD_hhmmss_UTC(time());
my $rc = get_nn($g_tot_records);
my $end_app = [ gettimeofday ];
my $elap = tv_interval( $begin_app, $end_app );
my $pt = get_time_stg($elap);
my $txt = <<EOF;
<p><b>$utc UTC</b>: Information extracted from <b>crossfeed</b> json, connected to <b>mpserver14</b>, sampled every 5 seconds, over the perod of time
$g_log_period, collected into to a CSV file, by this <a target="_blank" href="https://github.com/geoffmcl/scripts/blob/master/cfjsonlog.pl">cfjsonlog.pl</a> script,
then analysed by the <a target="_blank" href="https://github.com/geoffmcl/scripts/blob/master/cfcsvlogs.pl">cfcsvlogs.pl</a> script.
This was from $g_tot_files file(s), $rc records, processed in $pt. json groups $g_min_flts to $g_max_flts.</p>
<p>The min <b>multiplayer/crossfeed</b> count $g_min_flts, first encountered on $g_min_upd,
and max $g_max_flts on $g_max_upd, <b>BUT</b> this is only one crossfeed sample... be assured,
the FGFS free sim has been flown <b>many</b> more times than shown here, without an mp connection...
rendering these stats totally invalid as <b>any</b> indication of true FGFS usage...
</p>
EOF
$txt .= get_warn_txt();
$txt .= get_file_table();
$txt .= get_html_end($utc);
return $txt;
}
sub prth($$) {
my ($txt,$flag) = @_;
prt($txt);
#$txt =~ s/\n$//;
$txt = trim_all($txt);
if ($flag & 8) {
$htm .= "<h3>$txt</h3>\n";
} elsif ($flag & 1) {
if ($flag & 2) {
$htm .= "<tr><td>$txt</td></tr>\n";
} else {
$htm .= "<td>$txt</td>\n";
}
} else {
$htm .= "<p>$txt</p>\n";
}
}
sub get_time_stg($) {
my $elap = shift;
my $negative = 0;
my $units = '';
if ($elap < 0) {
$negative = 1;
$elap = -$elap;
}
if ( !($elap > 0.0) ) {
return "0.0 s";
}
if ($elap < 1e-21) {
#// yocto - 10^-24
$elap *= 1e+21;
$units = "ys";
} elsif ($elap < 1e-18) {
#// zepto - 10^-21
$elap *= 1e+18;
$units = "zs";
} elsif ($elap < 1e-15) {
#// atto - 10^-18
$elap *= 1e+15;
$units = "as";
} elsif ($elap < 1e-12) {
#// femto - 10^-15
$elap *= 1e+12;
$units = "fs";
} elsif ($elap < 1e-9) {
#// pico - 10^-12
$elap *= 1e+9;
$units = "ps";
} elsif ($elap < 1e-6) {
#// nanosecond - one thousand millionth (10?9) of a second
$elap *= 1e+6;
$units = "ns";
} elsif ($elap < 1e-3) {
#// microsecond - one millionth (10?6) of a second
$elap *= 1e+3;
$units = "us";
} elsif ($elap < 1.0) {
#// millisecond
$elap *= 1000.0;
$units = "ms";
} elsif ($elap < 60.0) {
$units = "s";
} else {
my $secs = int($elap + 0.5);
my $mins = int($secs / 60);
$secs = ($secs % 60);
if ($mins >= 60) {
my $hrs = int($mins / 60);
$mins = $mins % 60;
if ($hrs >= 24) {
my $days = int($hrs / 24);
$hrs = $hrs % 24;
return sprintf("%d days %2d:%02d:%02d hh:mm:ss", $days, $hrs, $mins, $secs);
} else {
return sprintf("%2d:%02d:%02d hh:mm:ss", $hrs, $mins, $secs);
}
} else {
return sprintf("%2d:%02d mm:ss", $mins, $secs);
}
}
my $res = '';
if ($negative) {
$res = '-';
}
$res .= "$elap $units";
return $res;
}
sub mycmp_decend_n0 {
return 1 if (${$a}[0] < ${$b}[0]);
return -1 if (${$a}[0] > ${$b}[0]);
return 0;
}
sub mycmp_decend_n2 {
return 1 if (${$a}[2] < ${$b}[2]);
return -1 if (${$a}[2] > ${$b}[2]);
return 0;
}
sub mycmp_ascend {
return -1 if ($a > $b);
return 1 if ($a < $b);
return 0;
}
sub mycmp_decend {
return -1 if ($a < $b);
return 1 if ($a > $b);
return 0;
}
sub mycmp_nc_sort {
return -1 if (lc($a) lt lc($b));
return 1 if (lc($a) gt lc($b));
return 0;
}
# SHOW the collected set of flights, and
# TODO: Update a database, and gen html page
sub show_flt_fids() {
my @arr = sort keys %flt_fids;
my $max = scalar @arr;
prt("Have $max flights to analyse...\n");
my ($fid,$ra,$rma);
my ($callsign,$lat,$lon,$alt_ft,$model,$spd_kts,$hdg,$dist_nm,$tsecs,$upd,$upd2,$be,$ee,$elap,$tm,$cnt,$rcsa);
my ($i,$stt,$end,$msg,$lncnt);
my $skipped = 0;
my %models = ();
my %callsigns = ();
my $flt_cnt = 0;
my $first_ep = time();
my $last_ep = 0;
my $first_upd = '';
my $last_upd = '';
my $tot_secs = 0;
my $tot_dist = 0;
my ($nmph,$wrap,$tcols);
my $cols = 3;
my $tab_cnt = 0; # start TABLE counter
$stt = [ gettimeofday ];
# collection of information, by FID (unique)
foreach $fid (@arr) {
$ra = $flt_fids{$fid};
$callsign = ${$ra}[0];
$lat = ${$ra}[1];
$lon = ${$ra}[2];
$alt_ft = ${$ra}[3];
$model = ${$ra}[4];
$spd_kts = ${$ra}[5];
$hdg = ${$ra}[6];
$dist_nm = ${$ra}[7];
$tsecs = ${$ra}[8];
$upd = ${$ra}[10];
$upd2 = ${$ra}[11];
$be = str2time($upd);
$ee = str2time($upd2);
$elap = $ee - $be;
$tm = get_time_stg($elap);
### $tm .= " (".get_time_stg($tsecs).")";
########################################
#### Eliminate flights that -
#### Are NOT alive for very long
if ($elap < $def_min_secs) {
$skipped++;
next;
}
#### Did not move the min distance
if ($dist_nm < $def_min_dist) {
$skipped++;
next;
}
$flt_cnt++; # count this FLIGHT
# get the PERIOD, but if over multiple files???
if ($be < $first_ep) {
$first_ep = $be;
$first_upd = $upd;
}
if ($ee > $last_ep) {
$last_ep = $ee;
$last_upd = $upd2;
}
$tot_secs += $elap;
$tot_dist += $dist_nm;
# store by MODEL
$models{$model} = [] if (! defined $models{$model});
$rma = $models{$model};
push(@{$rma},[$callsign,$dist_nm,$elap]);
# store by CALLSIGN
$callsigns{$callsign} = [] if (! defined $callsigns{$callsign});
$rcsa = $callsigns{$callsign};
# model, dist, elap
# 0 1 2
push(@{$rcsa},[$model,$dist_nm,$elap]);
##################################################
### DISPLAY
if (VERB2()) {
$callsign .= ' ' while (length($callsign) < 8);
# $lat = get_ll_double($lat);
# $lon = get_ll_double($lon);
# $alt_ft = get_alt_stg($alt_ft);
# $alt_ft = ' '.$alt_ft while (length($alt_ft) < 8);
$model = ' '.$model while (length($model) < $max_model);
# $spd_kts = ' '.$spd_kts while (length($spd_kts) < 7);
# $hdg = ' '.$hdg while (length($hdg) < 4);
$dist_nm = ' '.$dist_nm while (length($dist_nm) < 7);
###prt("$callsign $model $dist_nm frm: $upd to: $upd2 $tm\n");
prt("$callsign $model $dist_nm $tm\n");
}
}
$end = [ gettimeofday ];
$elap = tv_interval( $stt, $end );
$tm = get_time_stg($elap);
prt("Collected $flt_cnt flights from the csv... in $tm\n");
if ($skipped) {
prt("Skipped $skipped with time LTT $def_min_secs secs, or dist_nm LTT $def_min_dist...\n");
}
$tm = get_time_stg($last_ep - $first_ep);
$g_log_period = "From: $first_upd, To: $last_upd - $tm";
prt("$g_log_period\n");
@arr = sort keys %models;
my $modcnt = scalar @arr;
@arr = sort keys %callsigns;
my $cscnt = scalar @arr;
my $tm_ts = get_time_stg($tot_secs);
prth("Sampled ".get_nn($flt_cnt)." flights, ".get_nn($tot_dist)." nm., ".
get_nn($modcnt)." models, ".get_nn($cscnt)." callsigns, est.time $tm_ts...\n",8);
########################################################
# Show extracted MODEL usage information
@arr = sort mycmp_nc_sort keys(%models);
# OOPS, need to collect into CALLSIGNS
# to avoid repeated models...
# ok, go through the list, 1 by 1
my %cs = ();
my %mods = ();
my ($rcsh2,$ra3,$tlines,$dnlines);
$cnt = scalar @arr;
# set, and add a JUMP
$htm .= "<a id=\"models\" id=\"models\"></a>\n";