-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-tl-dir
More file actions
executable file
·170 lines (141 loc) · 4.34 KB
/
process-tl-dir
File metadata and controls
executable file
·170 lines (141 loc) · 4.34 KB
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
#!/usr/bin/env ruby
#
# 1. read directory with jpgs
# 2. extract datetime from first and last
# 3. load the weather data for this timeframe
#
require 'csv'
require 'fileutils'
require 'date'
require 'awesome_print'
require 'optparse'
require 'optparse/time'
$verbose=1
class Weather
CSV_DATA_DIR = '/var/lib/weather/goc'
# data points are pegged to the full hour
def normalize_instant(datum)
instant = DateTime.new(datum.year, datum.month, datum.day, datum.hour, 0, 0).strftime('%Y-%m-%dT%H:%M:%S')
instant
end
def initialize(year, month)
@weather = Hash.new({temperature: -999.9, wind: 0.0})
csv_file = "#{CSV_DATA_DIR}/weather-#{'%04d' % year}-#{'%02d' % month}.csv"
CSV.foreach(csv_file, col_sep: ';') do |row|
inst = row[0]
@weather[inst] = {temperature: row[1],
wind: row[2]}
end
end
def messwert(inst, messreihe)
begin
wert = @weather[normalize_instant(inst)][messreihe]
if wert.nil?
case messreihe
when :wind
wert = 0.0
else
wert = -9999.99
end
end
rescue StandardError => e
puts e
wert = -9999.99
end
wert
end
def temperature(inst)
messwert(inst, :temperature)
end
def wind(inst)
messwert(inst, :wind)
end
def precipitation(inst)
messwert(inst, :precipitation)
end
end
def exif_time(jpg)
s = `identify -format "%[EXIF:DateTimeOriginal]" #{jpg}`
DateTime.strptime(s, '%Y:%m:%d %H:%M:%S')
end
options = {target_dir: '.',
source_dir: '.',
interval: 0,
start_time: '000000',
end_time: '235959'
}
OptionParser.new do |opts|
opts.banner = "Usage: process-tl-dir [options]"
opts.on('-i', '--interval seconds', 'Interval in seconds, default: 0') do |interval|
options[:interval] = interval
end
opts.on('-v', '--verbose [LEVEL]', Integer, 'logging on or off') do |verbose|
$verbose=verbose || 1
STDERR.puts "verbose on, level=#{verbose}" if $verbose != 0
end
opts.on('-s', '--start [IME]', 'Start time HH:MM') do |start|
if start =~ /([02][0-9]):([05][0-9])/
options[:start_time] = "#{$~[1]}#{$~[2]}00"
else
STDERR.puts "time format HH:MM"
exit
end
end
opts.on('-e', '--end [TIME]', 'End time HH:MM') do |ende|
if ende =~ /([02][0-9]):([05][0-9])/
options[:end_time] = "#{$~[1]}#{$~[2]}00"
else
STDERR.puts "time format HH:MM"
exit
end
end
opts.on('-h', '--help', 'displays help') do
puts opts
exit
end
end.parse!
puts "running script #{File.expand_path(__FILE__)}" if $verbose > 0
ap options if $verbose > 0
source_dir = options[:source_dir]
target_dir = options[:target_dir]
interval = options[:interval]
start_time = options[:start_time]
end_time = options[:end_time]
target_dir = "#{target_dir}/exif-#{Process.pid}"
puts "source dir = #{source_dir}" if $verbose > 0
jpgs = Dir["#{source_dir}/**/tl*.jpg"].sort
puts "found #{jpgs.size} jpgs" if $verbose > 0
unless jpgs.size > 0
STDERR.puts "not jpgs found in source dir #{source_dir}"
exit 1
end
puts "process year=#{exif_time(jpgs[0]).year}, month=#{exif_time(jpgs[0]).month}" if $verbose > 0
tag = Weather.new(exif_time(jpgs[0]).year, exif_time(jpgs[0]).month)
puts "tag=#{tag}" if $verbose > 0
FONT = "Envy-Code-R"
begin
FileUtils.mkdir_p target_dir
rescue StandardError => e
STDERR.puts "#{target_dir}: #{e}"
exit 1
end
puts "found #{jpgs.size}" if $verbose > 0
index=0
jpgs.each do |jpg|
s = `identify -format "%[EXIF:DateTimeOriginal]" #{jpg}`
if $? == 0
timestamp = DateTime.strptime(s, '%Y:%m:%d %H:%M:%S')
# puts timestamp.strftime("%Y-%m-%d %H:%M")
temperature = tag.temperature(timestamp)
wind = tag.wind(timestamp)
ftime = File.stat(jpg)
target_jpg = "#{target_dir}/tl#{'%06d' % index}.jpg"
puts "#{jpg} #{timestamp.strftime("%Y-%m-%d %H:%M")} #{"%.1f" % temperature}°C #{"%.1f" % wind} km/h #{target_jpg}" if $verbose > 0
`convert #{jpg} -font \"#{FONT}\" -gravity NorthEast -undercolor graya\\(50\\%,0.5\\) -pointsize 24 -fill White -annotate +12+12 \"#{timestamp.strftime("%Y-%m-%d %H:%M")}\" -annotate +12+42 \"temp: #{"%.1f" % temperature}°C\" -annotate +12+72 \"wind: #{"%.1f km/h" % wind}\" #{target_jpg}`
File.utime(ftime.atime, ftime.mtime, target_jpg)
index += 1
else
puts "#{jpg} cannot be read"
# break
end
end