-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSiedler_main.py
132 lines (111 loc) · 4.87 KB
/
Siedler_main.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
# imports
import csv
import os
from datetime import datetime, timedelta
from pathlib import Path
from time import sleep
import reverse_geocoder
from exif import Image
from orbit import ISS
from picamera import PiCamera
from skyfield.api import load
# variables:
cam = PiCamera() # defining the camera
cam.resolution = (4056, 3040) # defining the resolution
base_folder = Path(__file__).parent.resolve() # Pathing variable to define the path in which the pictures are saved in
start_time = datetime.now() # Create a `datetime` variable to store the start time
now_time = datetime.now() # Create a `datetime` variable to store the current time
num = 0 # defines the loop count to zero
count = 0 # counter for data that is saved
exif_file = f'{base_folder}/coordinates.csv'
ephemeris = load('de421.bsp')
timescale = load.timescale()
end_time = datetime.now() + timedelta(minutes=179) # Create a `datetime` variable to store the end time
max_filesize_in_gb = 2.9 # Define the maximum filesize in GB
print(end_time)
def convert(angle):
"""
Convert a `skyfield` Angle to an EXIF-appropriate
representation (rationals)
e.g. 98° 34' 58.7 to "98/1,34/1,587/10"
Return a tuple containing a boolean and the converted angle,
with the boolean indicating if the angle is negative.
"""
sign, degrees, minutes, seconds = angle.signed_dms()
exif_angle = f'{degrees:.0f}/1,{minutes:.0f}/1,{seconds * 10:.0f}/10'
return sign < 0, exif_angle
def capture(camera, image):
# Use `camera` to capture an `image` file with lat/long EXIF data.
point = ISS.coordinates()
camera.capture(image)
# Convert the latitude and longitude to EXIF-appropriate representations
south, exif_latitude = convert(point.latitude)
west, exif_longitude = convert(point.longitude)
# Set the EXIF tags specifying the current location
cam.exif_tags['GPS.GPSLatitude'] = exif_latitude
cam.exif_tags['GPS.GPSLatitudeRef'] = "S" if south else "N" # north-south latitude
cam.exif_tags['GPS.GPSLongitude'] = exif_longitude
cam.exif_tags['GPS.GPSLongitudeRef'] = "W" if west else "E" # east-west longitude
return point
def get_remaining_space(path):
size = 0 # Bytes
for path, dirs, files in os.walk(path):
for f in files:
fp = os.path.join(path, f)
size += os.stat(fp).st_size
size = size / (1000 * 1000 * 1000) # Filesize in GB
return size
with open(exif_file, 'w') as file: # defining the csv coordinate data file
writer = csv.writer(file)
header = (
'Datetime',
'North-South',
'East-West',
'Nearest city longitude',
'Nearest city latitude',
'Nearest city name',
'Nearest city admin1',
'Nearest city admin2',
'Nearest city cc',
)
writer.writerow(header)
enough_time = enough_space = True
while enough_time and enough_space:
enough_space = get_remaining_space(base_folder) < max_filesize_in_gb # check if there is enough space in the folder
enough_time = datetime.now() < end_time # check if the time is still running
t = timescale.now()
if ISS.at(t).is_sunlit(ephemeris):
num = num + 1 # counting the loop number to name the pictures
capture_point = capture(cam, f'{base_folder}/gps{num:0>5n}.jpg')
image_file = f'{base_folder}/gps{num:0>5n}.jpg' # saving the pictures every loop named after the loop number
capture(cam, image_file)
coordinates = ISS.coordinates()
coordinate_pair = (coordinates.latitude.degrees, coordinates.longitude.degrees)
location = reverse_geocoder.search(coordinate_pair)
lat, lon, name, admin1, admin2, cc = location[0].values()
print(name)
row = (
datetime.now(),
capture_point.latitude.degrees,
capture_point.longitude.degrees,
lat,
lon,
name,
admin1,
admin2,
cc,
) # saving the coordinate data into a csv file
writer.writerow(row)
count = count + 1 # increase the data counter
file.flush()
sleep(8) # take a picture every 8 seconds (+ ~2s time for taking the picture)
remaining_time = max((end_time - datetime.now()), timedelta(0))
print(f"Remaining time: {remaining_time}")
else:
print("In darkness, starting loop again")
sleep(0)
print("Timer ended, program stopped" if not enough_time else "No more space left, program stopped")
print(f"{count} pictures taken")
print(f"Space used: {get_remaining_space(base_folder)} GB")
print(f"Runtime: {datetime.now() - start_time}")
# Program end