-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaddTimeDeltas.py
937 lines (685 loc) · 33.6 KB
/
addTimeDeltas.py
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
import csv
from datetime import datetime
def Rule3_add_time_delta(csv_file_path):
# Initialize an empty list to store the data
summary = []
# Open the CSV file and read its contents
with open(csv_file_path, 'r') as csvfile:
# Create a CSV reader object
csv_reader = csv.reader(csvfile)
# Read the header row to get the keys
header = next(csv_reader, None)
# Check if the header row exists
if header:
# Iterate through each row in the CSV file
for row in csv_reader:
# Create a dictionary for each row using the header as keys
row_dict = dict(zip(header, row))
# Append the dictionary to the list
summary.append(row_dict)
# Rule 3 calculations
rule3_absolute_time_sec = []
rule3_absolute_time_lost_mmss = []
# Iterate through each index in summary
for i in range(len(summary)):
task_distance_km = summary[i]['task_distance_km']
task_distance_nmi = float(task_distance_km) * 0.53996
speed_kts = summary[i]['Rule1_glide_avg_gs_kts']
speed = float(speed_kts) * 1.852
total_distance = summary[i]['Rule3_total_glide_distance_km']
# Calculate remaining distance
remaining_distance = float(total_distance) - float(task_distance_km)
# Calculate time in seconds (keeping negative values)
time_seconds = (remaining_distance / float(speed)) * 3600
# Convert time to MM:SS format while preserving sign
is_negative = time_seconds < 0
abs_seconds = abs(time_seconds)
minutes = int(abs_seconds // 60)
seconds = int(abs_seconds % 60)
time_str = f"{minutes:02}:{seconds:02}"
if is_negative:
time_str = "-" + time_str
# Store the results in lists (preserving negative values)
rule3_absolute_time_sec.append(str(int(time_seconds))) # Keep negative value
rule3_absolute_time_lost_mmss.append(time_str)
# Add values to summary
for i, time_sec in enumerate(rule3_absolute_time_sec):
summary[i]['rule3_absolute_time_sec'] = time_sec
for i, time_str in enumerate(rule3_absolute_time_lost_mmss):
summary[i]['rule3_absolute_time_mmss'] = time_str
# Convert the strings to seconds (integer), preserving negative values
time_values_sec = [int(time_str) for time_str in rule3_absolute_time_sec]
# Find the most negative time value
min_time_sec = min(time_values_sec) # This will now find the most negative value
# Calculate relative times (all positive values relative to most negative)
result_sec = [time - min_time_sec for time in time_values_sec]
# Format relative times with proper MM:SS
rule3_relative_time_lost_mmss = []
for time in result_sec:
minutes = abs(time) // 60
seconds = abs(time) % 60
time_str = f"{minutes:02}:{seconds:02}"
if time < 0: # Should not happen now since we subtract the minimum
time_str = "-" + time_str
rule3_relative_time_lost_mmss.append(time_str)
new_header = ['Rule3_absolute_time_lost_mmss', 'Rule3_time_behind_straightest_mmss']
# Read and update the CSV file
with open(csv_file_path, 'r') as file:
reader = csv.reader(file)
rows = list(reader)
# Add the new header to the end of the existing header row
header_row = rows[0]
for header in new_header:
header_row.append(header)
for i in range(len(rows) - 1):
rows[i + 1].append("'" + rule3_absolute_time_lost_mmss[i])
rows[i + 1].append("'" + rule3_relative_time_lost_mmss[i])
# Write the updated rows back to the CSV file
with open(csv_file_path, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)
print("Added [rule3_absolute_time_lost_mmss, rule3_relative_time_lost_mmss] to analysis")
###### MAKE THIS A DEF FOR RULE 2 CLIMB DIFF
#climb difference from best climb rate
#csv_file_path = 'summary.csv'
# Convert task_distance_km to nautical miles using the conversion factor
#task_distance_nmi = task_distance_km * 0.53996
def Rule2_add_time_delta(csv_file_path):
# Initialize an empty list to store the data
summary = []
# Open the CSV file and read its contents
with open(csv_file_path, 'r') as csvfile:
# Create a CSV reader object
csv_reader = csv.reader(csvfile)
# Read the header row to get the keys
header = next(csv_reader, None)
# Check if the header row exists
if header:
# Iterate through each row in the CSV file
for row in csv_reader:
# Create a dictionary for each row using the header as keys
row_dict = dict(zip(header, row))
# Append the dictionary to the list
summary.append(row_dict)
#print(summary)
#csv_file = 'summary.csv'
# Find the maximum climb rate
max_climb_rate = max(float(entry['Rule2_avg_climb_rate_kts']) for entry in summary)
# Iterate through the summary
for entry in summary:
# Remove the single quote at the beginning of 'mm:ss' format
mmss_format = entry['total_thermal_time_mmss'][1:]
# Calculate the average climb rate for the current entry
avg_climb_rate = float(entry['Rule2_avg_climb_rate_kts'])
# Calculate the climb rate loss
climb_rate_loss = max_climb_rate - avg_climb_rate
# Convert 'mm:ss' format to seconds for total thermal time
minutes, seconds = map(int, mmss_format.split(':'))
total_thermal_time_seconds = minutes * 60 + seconds
# Calculate the time you WOULD have spent climbing at the maximum rate
if climb_rate_loss == 0 or total_thermal_time_seconds == 0 or max_climb_rate == 0:
climb_rate_loss_seconds = 0
else:
climb_rate_loss_seconds = climb_rate_loss * total_thermal_time_seconds / max_climb_rate
# Convert back to 'mm:ss' format
climb_rate_loss_mmss = '{:02}:{:02}'.format(int(climb_rate_loss_seconds // 60), int(climb_rate_loss_seconds % 60))
# Add climb_rate_loss_mmss to the entry
entry['Rule2_time_behind_best_climb_mmss'] = "'"+climb_rate_loss_mmss
# Print or use climb_rate_loss_mmss as needed
#print(f"For entry {entry}: Climb Rate Loss: {climb_rate_loss_mmss}")
# Update the existing 'summary.csv' file with the new column
output_csv_file_path = csv_file_path
# Add the new column to the header if it doesn't exist
if 'Rule2_time_behind_best_climb_mmss' not in header:
header.append('Rule2_time_behind_best_climb_mmss')
# Add the new column value to each row
#for row in summary:
# Add the new column value to the row
#row['Rule2_relative_time_lost_mmss'] = next((entry['Rule2_relative_time_lost_mmss'] for entry in summary if entry['id'] == row['id']), '')
#print(row)
# Write the updated data back to 'summary.csv'
with open(output_csv_file_path, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=header)
writer.writeheader()
writer.writerows(summary)
print("Added [Rule2_relative_time_lost_mmss] to analysis")
#print(f"Updated data written to {output_csv_file_path}")
#add time delta behind first place
#csv_file_path = 'summary.csv'
def Task_time_behind_rank1(csv_file_path):
# Initialize an empty list to store the data
summary = []
# Open the CSV file and read its contents
with open(csv_file_path, 'r') as csvfile:
# Create a CSV reader object
csv_reader = csv.reader(csvfile)
# Read the header row to get the keys
header = next(csv_reader, None)
# Check if the header row exists
if header:
# Iterate through each row in the CSV file
for row in csv_reader:
# Create a dictionary for each row using the header as keys
row_dict = dict(zip(header, row))
# Append the dictionary to the list
summary.append(row_dict)
# Convert task_time_hmmss to seconds for each entry
time_seconds_list = []
for entry in summary:
task_time_hmmss = entry['task_time_hmmss']
time_object = datetime.strptime(task_time_hmmss, "%H:%M:%S")
time_seconds = time_object.hour * 3600 + time_object.minute * 60 + time_object.second
time_seconds_list.append(time_seconds)
# Find the fastest time
fastest_time = min(time_seconds_list)
# Calculate the time behind rank 1 for each entry
for i, entry in enumerate(summary):
task_time_behind_rank1 = time_seconds_list[i] - fastest_time
#entry['task_time_behind_rank1'] = task_time_behind_rank1
# Convert task_time_behind_rank1 to 'mm:ss' format
mm, ss = divmod(task_time_behind_rank1, 60)
entry['task_time_behind_rank1_mmss'] = "'"+'{:02}:{:02}'.format(mm, ss)
# Print the updated summary
#for entry in summary:
# print(entry)
# Update the existing 'summary.csv' file with the new column
output_csv_file_path = csv_file_path
# Add the new column to the header if it doesn't exist
if 'task_time_behind_rank1_mmss' not in header:
header.append('task_time_behind_rank1_mmss')
# Add the new column value to each row
#for row in summary:
# Add the new column value to the row
#row['Rule2_relative_time_lost_mmss'] = next((entry['Rule2_relative_time_lost_mmss'] for entry in summary if entry['id'] == row['id']), '')
#print(row)
# Write the updated data back to 'summary.csv'
with open(output_csv_file_path, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=header)
writer.writeheader()
writer.writerows(summary)
def calculate_height_lost(speed_kts, distance_km, glide_ratio):
speed_kts = float(speed_kts)
distance_km = float(distance_km)
glide_ratio = float(glide_ratio)
# Convert speed from knots to km/h
speed_kmh = speed_kts * 1.852 # 1 knot = 1.852 km/h
# Calculate time using the formula: time = distance / speed
time_hours = distance_km / speed_kmh
# Calculate height lost using the formula: height lost = distance / glide ratio
height_lost_km = distance_km / glide_ratio
# Convert height lost from kilometers to feet
height_lost_feet = height_lost_km * 3280.84 # 1 kilometer = 3280.84 feet
return height_lost_feet
def calculate_average_speed(distance, time):
return float(distance) / time
def calculate_time_saved(speed1, speed2, distance, time2):
time1 = float(distance) / float(speed1)
time_saved = time2 - time1
return time_saved
def hours_to_mmss(hours):
minutes = int(hours * 60)
seconds = int((hours * 60 - minutes) * 60)
return f"'{minutes:02d}:{seconds:02d}"
'''
task_distance_km = 170.56
csv_file_path = 'summary.csv'
# Initialize an empty list to store the data
summary = []
# Open the CSV file and read its contents
with open(csv_file_path, 'r') as csvfile:
# Create a CSV reader object
csv_reader = csv.reader(csvfile)
# Read the header row to get the keys
header = next(csv_reader, None)
# Check if the header row exists
if header:
# Iterate through each row in the CSV file
for row in csv_reader:
# Create a dictionary for each row using the header as keys
row_dict = dict(zip(header, row))
# Append the dictionary to the list
summary.append(row_dict)
height_lost_list = []
for entry in summary:
speed_kts = entry.get('Rule1_glide_avg_gs_kts', 0)
speed_kts = float(speed_kts)
distance_km = task_distance_km
glide_ratio = entry.get('Rule1_glide_ratio', 1)
glide_ratio = float(glide_ratio)
height_lost_feet = calculate_height_lost(speed_kts, distance_km, glide_ratio)
height_lost_list.append(height_lost_feet)
'''
#csv_file_path = 'summary.csv'
# Initialize an empty list to store the data
# Initialize an empty list to store the data
def AAT_Task_time_behind_rank1(csv_file_path):
summary = []
# Open the CSV file and read its contents
with open(csv_file_path, 'r') as csvfile:
# Create a CSV reader object
csv_reader = csv.reader(csvfile)
# Read the header row to get the keys
header = next(csv_reader, None)
# Check if the header row exists
if header:
# Iterate through each row in the CSV file
for row in csv_reader:
# Create a dictionary for each row using the header as keys
row_dict = dict(zip(header, row))
# Append the dictionary to the list
summary.append(row_dict)
# Convert task_time_hmmss to seconds for each entry
time_seconds_list = []
for entry in summary:
#task_time_hmmss = entry['task_time_hmmss']
#print('task_time_hmmss',task_time_hmmss)
#time_object = datetime.strptime(task_time_hmmss, "%H:%M:%S")
#time_seconds = time_object.hour * 3600 + time_object.minute * 60 + time_object.second
#time_seconds_list.append(time_seconds)
#print('entry[task_distance_km]',entry['task_distance_km'])
dist = float(entry['task_distance_km'])
speed = float(entry['task_speed_kmh'])
# Calculate task_time_h as a float value
task_time_h = dist / speed
print('task_time_h', task_time_h)
# Extract hours and fractional minutes directly from the float value
hours = int(task_time_h)
minutes = (task_time_h - hours) * 60
# Convert "h.hhhh" format to seconds
time_seconds = int((hours * 3600) + (minutes * 60))
# Append the result to the time_seconds_list
time_seconds_list.append(time_seconds)
print('time_seconds_list',time_seconds_list)
# Find rank 1 at the top
#rank1_time_seconds = min(time_seconds_list)
rank1_time_seconds = time_seconds_list[0]
# Assume there is at least one task in the summary
result_list = []
for i, task in enumerate(summary):
# Extract task details
distance1 = summary[0]['task_distance_km']
speed1 = summary[0]['task_speed_kmh']
time1 = time_seconds_list[0]
time1 - time1 / 3600
distance2 = task['task_distance_km']
speed2 = task['task_speed_kmh']
time2 = time_seconds_list[i]
time2 = time2 / 3600
# Calculate average speeds
average_speed1 = summary[0]['task_speed_kmh']
average_speed2 = task['task_speed_kmh']
# Calculate time saved if airplane 2 were traveling at the speed of airplane 1
time_saved_hours = calculate_time_saved(average_speed1, speed2, distance2, time2)
#print('time_saved_hours',time_saved_hours)
# Convert time saved to mm:ss format
time_saved_mmss = hours_to_mmss(time_saved_hours)
# Append the results to the result_list
result_list.append({
"average_speed1": average_speed1,
"average_speed2": average_speed2,
"time_saved_mmss": time_saved_mmss
})
# Extract the 'time_saved_mmss' values from the result_list
time_saved_values = [item['time_saved_mmss'] for item in result_list]
# Update the existing 'summary.csv' file with the new column
output_csv_file_path = csv_file_path
# New column header and data
new_column_header = 'AAT_time_behind_mmss'
new_column_data = time_saved_values
# Add new column header to each dictionary
for row in summary:
row[new_column_header] = None # Initialize with None
# Add new data to the corresponding dictionaries
for i, row in enumerate(summary):
row[new_column_header] = new_column_data[i]
# Extract headers from the first dictionary in the list
headers = list(summary[0].keys())
# Write the updated data back to the CSV file
with open(csv_file_path, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=headers)
# Write the header
writer.writeheader()
# Write the data
writer.writerows(summary)
print('Added AAT specific time behind leader')
# Add the new column to the header if it doesn't exist
#if 'AAT_task_time_behind_rank1_mmss' not in header:
#header.append('AAT_task_time_behind_rank1_mmss')
# Add the new column value to each row
#for row in summary:
# Add the new column value to the row
#row['Rule2_relative_time_lost_mmss'] = next((entry['Rule2_relative_time_lost_mmss'] for entry in summary if entry['id'] == row['id']), '')
#print(row)
# Write the updated data back to 'summary.csv'
#with open(output_csv_file_path, 'w', newline='') as csvfile:
#writer = csv.DictWriter(csvfile, fieldnames=header)
#writer.writeheader()
#writer.writerows(summary)
def solve_for_h(E, m, v):
#m in kg
#v in knots
#E in joules
m = float(m)
v_ms = float(v) * 0.514444
g = 9.8 # m/s^2, assuming Earth's gravitational acceleration
height_m = (E - (0.5 * m * (v_ms)**2)) / (m * g)
height_ft = height_m * 3.28084
return height_ft
def calculate_total_energy(m, v, h):
#m in kg
#v in knots
#h in feet
m_kg = float(m)
v_ms = float(v) * 0.514444
h_m = float(h) * 0.3048
g = 9.8 # m/s^2, assuming Earth's gravitational acceleration
KE = 0.5 * m_kg * (v_ms**2)
PE = m_kg * g * h_m
total_energy = KE + PE
return total_energy
def find_max_start_speed_kts(data_list):
max_start_speed = 0
for entry in data_list:
start_speed_str = entry.get('start_speed_gs_kts', '0') # Get the start altitude as a string, default to '0' if not present
start_speed = int(start_speed_str)
if start_speed > max_start_speed:
max_start_speed = start_speed
return max_start_speed
'''
#pass in
max_start_height_ft = 4951
mass_kg = 600 #18m is 600, 15m is 500, club is 250
csv_file_path = 'summary.csv'
summary = []
# Open the CSV file and read its contents
with open(csv_file_path, 'r') as csvfile:
# Create a CSV reader object
csv_reader = csv.reader(csvfile)
# Read the header row to get the keys
header = next(csv_reader, None)
# Check if the header row exists
if header:
# Iterate through each row in the CSV file
for row in csv_reader:
# Create a dictionary for each row using the header as keys
row_dict = dict(zip(header, row))
# Append the dictionary to the list
summary.append(row_dict)
start_speed_kts = float(summary[0]['start_speed_gs_kts'])
start_height_ft = float(summary[0]['start_altitude_ft'])
TE = calculate_total_energy(mass_kg, start_speed_kts, start_height_ft)
#calculated height diff from max start height
height_diff_ft = max_start_height_ft - start_height_ft
#find max start speed
max_start_speed_gs_kts = find_max_start_speed_kts(summary)
#find start speed
start_speed_diff_kts = max_start_speed_gs_kts - start_speed_kts
#find speed diff height equivalent
start_speed_equivalent_height_diff_ft = solve_for_h(TE, mass_kg, start_speed_diff_kts)
'''
'''
# Write the 'time_saved_mmss' values to a new CSV file
with open(output_csv_file_path, mode='a', newline='') as file:
writer = csv.writer(file)
# Write header row (if needed)
writer.writerow(['AAT_time_saved_mmss'])
# Write the values from the result_list to the CSV file
writer.writerows(map(lambda x: [x], time_saved_values))
print(f'CSV file "{output_csv_file_path}" created successfully.')
'''
import csv
csv_file_path = 'summary.csv'
def Rule1_add_time_delta(csv_file_path):
summary = []
# Open the CSV file and read its contents
with open(csv_file_path, 'r') as csvfile:
# Create a CSV reader object
csv_reader = csv.reader(csvfile)
# Read the header row to get the keys
header = next(csv_reader, None)
# Check if the header row exists
if header:
# Iterate through each row in the CSV file
for row in csv_reader:
# Create a dictionary for each row using the header as keys
row_dict = dict(zip(header, row))
# Append the dictionary to the list
summary.append(row_dict)
# Conversion factor from knots to km/h
knots_to_kmh = 1.852
# Keep summary[0] fixed
# Lists to store time_behind_rank1_from_gs_s, time_behind_rank1_from_gs_mmss, and time_behind_rank1_from_gs
time_behind_gs_s_values = []
time_behind_gs_mmss_values = []
time_behind_gs_values = []
# Lists to store ld_time_diff_s, ld_time_diff_mmss, composite_time_diff_s, and composite_time_diff_mmss
ld_time_diff_s_values = []
ld_time_diff_mmss_values = []
composite_time_diff_s_values = []
composite_time_diff_mmss_values = []
# Iterate through summary starting from index 1
for i in range(len(summary)):
# Calculate the expressions for each row
time_behind_rank1_from_gs = (
(float(summary[i]['task_distance_km']) / (float(summary[i]['Rule1_glide_avg_gs_kts']) * knots_to_kmh)) -
(float(summary[i]['task_distance_km']) / (float(summary[0]['Rule1_glide_avg_gs_kts']) * knots_to_kmh))
)
# Append the results to the respective lists
time_behind_gs_values.append(time_behind_rank1_from_gs)
# Calculate time_behind_rank1_from_gs_s and append to the list
time_behind_rank1_from_gs_s = time_behind_rank1_from_gs * 3600
time_behind_gs_s_values.append(int(time_behind_rank1_from_gs_s))
# Calculate time_behind_rank1_from_gs_mmss and append to the list
if time_behind_rank1_from_gs > 0:
hours = int(time_behind_rank1_from_gs)
minutes = int((time_behind_rank1_from_gs - hours) * 60)
seconds = int(((time_behind_rank1_from_gs - hours) * 60 - minutes) * 60)
time_behind_rank1_from_gs_mmss = "{:02d}:{:02d}".format(minutes, seconds)
else:
time_behind_rank1_from_gs = time_behind_rank1_from_gs * -1
hours = int(time_behind_rank1_from_gs)
minutes = int((time_behind_rank1_from_gs - hours) * 60)
seconds = int(((time_behind_rank1_from_gs - hours) * 60 - minutes) * 60)
time_behind_rank1_from_gs = time_behind_rank1_from_gs * -1
time_behind_rank1_from_gs_mmss = "-{:02d}:{:02d}".format(minutes, seconds)
time_behind_gs_mmss_values.append("'"+time_behind_rank1_from_gs_mmss)
# Step 1: Calculate height loss for summary[0] and summary[1]
height_loss_rank1_ft = (float(summary[i]['task_distance_km']) * 3280.84) / float(summary[0]['Rule1_glide_ratio'])
height_loss_rank2_ft = (float(summary[i]['task_distance_km']) * 3280.84) / float(summary[i]['Rule1_glide_ratio'])
# Step 2: Find the difference between height loss
height_loss_difference_ft = height_loss_rank2_ft - height_loss_rank1_ft
# Step 3: Convert climb rate from kts to ft for summary[0]
climb_rate_fps = float(summary[i]['Rule2_avg_climb_rate_kts']) * 1.68781
# Step 4: Calculate rank1 climb rate / difference
if climb_rate_fps != 0 and height_loss_difference_ft != 0:
ld_time_diff_s = height_loss_difference_ft / climb_rate_fps
else:
ld_time_diff_s = 0
#print('ld_time_diff_s',ld_time_diff_s)
# Append the results to the respective lists
ld_time_diff_s_values.append(int(ld_time_diff_s))
# Calculate ld_time_diff_mmss and append to the list
if ld_time_diff_s > 0:
hours = ld_time_diff_s // 3600
minutes = (ld_time_diff_s) // 60
seconds = ld_time_diff_s % 60
ld_time_diff_mmss = "{:02}:{:02}".format(int(minutes), int(seconds))
else:
ld_time_diff_s = ld_time_diff_s * -1
hours = ld_time_diff_s // 3600
minutes = (ld_time_diff_s) // 60
seconds = ld_time_diff_s % 60
ld_time_diff_s = ld_time_diff_s * -1
ld_time_diff_mmss = "-{:02}:{:02}".format(int(minutes), int(seconds))
ld_time_diff_mmss_values.append("'"+ld_time_diff_mmss)
# Calculate composite_time_diff_s and append to the list
composite_time_diff_s = time_behind_rank1_from_gs_s + ld_time_diff_s
print('time_behind_rank1_from_gs_s',time_behind_rank1_from_gs_s)
print('ld_time_diff_s',ld_time_diff_s)
print('composite_time_diff_s',composite_time_diff_s)
composite_time_diff_s_values.append(int(composite_time_diff_s))
# Check if the time difference is negative
is_negative = composite_time_diff_s < 0
# Convert the time difference into a positive value
composite_time_diff_s = abs(composite_time_diff_s)
hours = composite_time_diff_s // 3600
minutes = (composite_time_diff_s) // 60
seconds = composite_time_diff_s % 60
# Format the time difference as a string
composite_time_diff_mmss = "{:02}:{:02}".format(int(minutes), int(seconds))
# If the original time difference was negative, add a negative sign to the output string
if is_negative:
composite_time_diff_mmss = "-" + composite_time_diff_mmss
print('composite_time_diff_mmss', composite_time_diff_mmss)
composite_time_diff_mmss_values.append("'"+composite_time_diff_mmss)
# Print the lists of time_behind values
print("List of time_behind_rank1_from_gs_s values:", time_behind_gs_s_values)
print("List of time_behind_rank1_from_gs_mmss values:", time_behind_gs_mmss_values)
print("List of time_behind_rank1_from_gs values:", time_behind_gs_values)
# Print the lists of ld_time_diff and composite_time_diff values
print("List of ld_time_diff_s values:", ld_time_diff_s_values)
print("List of ld_time_diff_mmss values:", ld_time_diff_mmss_values)
print("List of composite_time_diff_s values:", composite_time_diff_s_values)
print("List of composite_time_diff_mmss values:", composite_time_diff_mmss_values)
# Assuming you want to use the 'Rule1_time_behind_rank1_mmss' as the new column header
#new_column_header = 'Rule1_time_behind_rank1_mmss'
new_column_headers = ['Rule1_time_behind_rank1_mmss','Rule1_time_behind_rank1_from_gs_mmss','Rule1_time_behind_rank1_from_ld_mmss']
# Update the existing 'summary.csv' file with the new column
output_csv_file_path = 'summary.csv' # Use a different file path if needed
# New column data
#new_column_data = composite_time_diff_mmss_values
# New column data
new_column_data1 = composite_time_diff_mmss_values
new_column_data2 = time_behind_gs_mmss_values
new_column_data3 = ld_time_diff_mmss_values
# Add new column header to each dictionary
#for row in summary:
#row[new_column_header] = None # Initialize with None
# Add new columns headers to each dictionary
for row in summary:
for header in new_column_headers:
row[header] = None # Initialize with None
# Add new data to the corresponding dictionaries
for i, row in enumerate(summary):
row[new_column_headers[0]] = new_column_data1[i]
row[new_column_headers[1]] = new_column_data2[i]
row[new_column_headers[2]] = new_column_data3[i]
# Extract headers from the first dictionary in the list
headers = list(summary[0].keys())
# Write the updated data back to the CSV file
with open(output_csv_file_path, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=headers)
# Write the header
writer.writeheader()
# Write the data
writer.writerows(summary)
print('Added glide time behind rank 1')
"""
csv_file_path = 'summary.csv'
summary = []
# Open the CSV file and read its contents
with open(csv_file_path, 'r') as csvfile:
# Create a CSV reader object
csv_reader = csv.reader(csvfile)
# Read the header row to get the keys
header = next(csv_reader, None)
# Check if the header row exists
if header:
# Iterate through each row in the CSV file
for row in csv_reader:
# Create a dictionary for each row using the header as keys
row_dict = dict(zip(header, row))
# Append the dictionary to the list
summary.append(row_dict)
# Conversion factor from knots to km/h
knots_to_kmh = 1.852
# Keep summary[0] fixed
reference_row = summary[0]
# List to store time_behind_rank1_from_gs values
time_behind_values = []
# Calculate the expressions
time_behind_rank1_from_gs = (
(float(summary[1]['task_distance_km']) / (float(summary[1]['Rule1_glide_avg_gs_kts']) * knots_to_kmh)) -
(float(summary[0]['task_distance_km']) / (float(summary[0]['Rule1_glide_avg_gs_kts']) * knots_to_kmh))
)
print(time_behind_rank1_from_gs)
time_behind_rank1_from_gs_s = time_behind_rank1_from_gs * 3600
print('time_behind_rank1_from_gs_s',time_behind_rank1_from_gs_s)
if time_behind_rank1_from_gs > 0:
# Convert the result to hours, minutes, and seconds
hours = int(time_behind_rank1_from_gs)
minutes = int((time_behind_rank1_from_gs - hours) * 60)
seconds = int(((time_behind_rank1_from_gs - hours) * 60 - minutes) * 60)
time_behind_rank1_from_gs_mmss = "{:02d}:{:02d}".format(minutes, seconds)
else:
time_behind_rank1_from_gs = time_behind_rank1_from_gs * -1
# Convert the result to hours, minutes, and seconds
hours = int(time_behind_rank1_from_gs)
minutes = int((time_behind_rank1_from_gs - hours) * 60)
seconds = int(((time_behind_rank1_from_gs - hours) * 60 - minutes) * 60)
time_behind_rank1_from_gs_mmss = "-{:02d}:{:02d}".format(minutes, seconds)
# Format the result as "hh:mm:ss"
# Print the formatted result
print('time_behind_rank1_from_gs_mmss',time_behind_rank1_from_gs_mmss)
# Step 1: Calculate height loss for summary[0] and summary[1]
height_loss_rank1_ft = (float(summary[0]['task_distance_km']) * 3280.84) / float(summary[0]['Rule1_glide_ratio'])
height_loss_rank2_ft = (float(summary[1]['task_distance_km']) * 3280.84) / float(summary[1]['Rule1_glide_ratio'])
#print('height_loss_rank2',height_loss_rank2)
# Step 2: Find the difference between height loss
height_loss_difference_ft = height_loss_rank2_ft - height_loss_rank1_ft
print('height_loss_difference_ft',height_loss_difference_ft)
# Step 3: Convert climb rate from kts to ft for summary[0]
climb_rate_fps = float(summary[0]['Rule2_avg_climb_rate_kts']) * 1.68781
print('climb_rate_fps',climb_rate_fps)
# Step 4: Calculate rank1 climb rate / difference
if climb_rate_fps != 0 and height_loss_difference_ft != 0:
ld_time_diff_s = height_loss_difference_ft / climb_rate_fps
else:
ld_time_diff_s = 0
# Print the result
print("time_diff_s:", ld_time_diff_s)
#time_diff_s = 12345 # replace this with your actual time difference in seconds
if ld_time_diff_s > 0:
hours = ld_time_diff_s // 3600
minutes = (ld_time_diff_s) // 60
seconds = ld_time_diff_s % 60
ld_time_diff_mmss = "{:02}:{:02}".format(int(minutes), int(seconds))
else:
ld_time_diff_s = ld_time_diff_s * -1
hours = ld_time_diff_s // 3600
minutes = (ld_time_diff_s) // 60
seconds = ld_time_diff_s % 60
ld_time_diff_mmss = "-{:02}:{:02}".format(int(minutes), int(seconds))
print('ld_time_diff_mmss',ld_time_diff_mmss)
composite_time_diff_s = time_behind_rank1_from_gs_s + ld_time_diff_s
print('composite_time_diff_s',composite_time_diff_s)
hours = composite_time_diff_s // 3600
minutes = (composite_time_diff_s) // 60
seconds = composite_time_diff_s % 60
composite_time_diff_mmss = "{:02}:{:02}".format(int(minutes), int(seconds))
print('composite_time_diff_mmss',composite_time_diff_mmss)
"""
"""
# Update the existing 'summary.csv' file with the new column
output_csv_file_path = csv_file_path
# New column header and data
new_column_header = 'Rule1_time_behind_rank1_mmss'
new_column_data = time_saved_values
#now loop composite through all, keep rank 1 as rank 1 but the other as i
# Add new column header to each dictionary
for row in summary:
row[new_column_header] = None # Initialize with None
# Add new data to the corresponding dictionaries
for i, row in enumerate(summary):
row[new_column_header] = new_column_data[i]
# Extract headers from the first dictionary in the list
headers = list(summary[0].keys())
# Write the updated data back to the CSV file
with open(csv_file_path, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=headers)
# Write the header
writer.writeheader()
# Write the data
writer.writerows(summary)
print('Added glide time behind rank 1')
"""