-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_gigapan_capturetimes.rb
executable file
·74 lines (62 loc) · 2.26 KB
/
extract_gigapan_capturetimes.rb
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
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'ctlib/json.rb'
require 'rexml/document'
require 'time'
capture_times = []
if ARGV.length < 2
puts "Usage: ruby #{$0} path/to/.gigapans path/to/.json"
exit 1
end
unless File.exists?(ARGV[0])
puts "Input directory does not exist. Please check the name you typed and try again."
exit 1
end
unless File.exists?(ARGV[1])
puts "Output json file does not exist. Please check the name you typed and try again."
exit 1
end
dir = ARGV[0].dup
dir.chop! if dir[-1,1] == "/" || dir[-1,1] == "\\"
path = File.expand_path('*.gigapan', dir)
files = Dir.glob(path).sort
files.each do |file|
#read in the .gigapan file
gigapan = REXML::Document.new File.new(file)
#grab the notes section
notes = gigapan.elements.to_a("GigapanStitcher/notes")
if notes.length == 0
puts "#{file} did not have a notes section. NULL will be used for the capture time. Please go back and manually edit the output file in the end."
capture_times << "NULL"
next
end
#extract the capture time range from the notes section
notes_array = notes[0].to_s.split("\n")
capture_time_index = nil
notes_array.each_with_index do |x,i|
if x.include?("Capture time:")
capture_time_index = i
break
end
end
if capture_time_index.nil?
puts "The notes section in #{file} did not have a capture time field. NULL will be used for the capture time. Please go back and manually edit the output file in the end."
capture_times << "NULL"
next
end
begin
capture_time_range = notes_array[capture_time_index].scan(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} - \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/)[0].split(" - ")
capture_time_start = capture_time_range[0].strip
capture_time_end = capture_time_range[1].strip
rescue
puts "There was an error parsing the capture time field in #{file}. NULL will be used for the capture time. Please go back and manually edit the output file in the end."
capture_times << "NULL"
next
end
capture_times << capture_time_start
#puts "Successfuly processed #{file}."
end
json = open(ARGV[1]) {|fh| JSON.load(fh)}
json["capture-times"] = capture_times
open(ARGV[1], "w") {|fh| fh.puts(JSON.pretty_generate(json))}
puts "Successfully wrote capture times to #{ARGV[1]}"