File size: 1,640 Bytes
49abca9 |
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 |
require 'csv'
require 'json'
require 'nokogiri'
require 'open-uri'
def create_unless_exists(path)
unless File.exists?(path)
`mkdir -p #{ path }`
end
end
def filename_from_uri(uri)
File.basename(URI(uri).path)
end
raw_sources_path = '/data/raw_sources'
sources_path = '/data/sources'
create_unless_exists(raw_sources_path)
create_unless_exists(sources_path)
cutouts = []
CSV.foreach("/data/metadata.csv", headers: true) do |datum|
cutout = {}
html = Nokogiri::HTML(open("https://hirise.lpl.arizona.edu/#{ datum['OBSERVATION_ID'] }"))
download_link = html.css('.image-details-container td:nth-child(2) a[href]').select { |link| link['href'].match(/RGB\.NOMAP\.JP2/) }.first['href']
filename = filename_from_uri(download_link)
`curl -L -o #{ raw_sources_path }/#{ filename } #{ download_link }`
`j2k_to_image -i #{ raw_sources_path }/#{ filename } -o #{ sources_path }/#{ datum['OBSERVATION_ID'] }.bmp`
cutout = {
name: datum['OBSERVATION_ID'],
acquisition_date: datum['START_TIME'],
local_mars_time: datum['LOCAL_TIME'],
latitude: datum['MEAN_LATITUDE'],
longitude: datum['MEAN_LONGITUDE'],
range_to_target: datum['GET_CENTER_DISTANCE'],
original_image_scale: datum['MAP_SCALE'],
emission_angle: datum['EMISSION_ANGLE'],
phase_angle: datum['PHASE_ANGLE'],
solar_incidence: datum['INCIDENCE_ANGLE'],
solar_longitude: datum['SOLAR_LONGITUDE'],
north_azimuth: datum['NORTH_AZIMUTH'],
south_azimuth: datum['SUB_SOLAR_AZIMUTH'],
download_link: download_link
}
cutouts << cutout
end
File.open("/data/sources.json", "w"){ |f| f.puts(JSON.dump(cutouts)) } |