Skip to content

Commit

Permalink
Add SWOB-ML Observation download function
Browse files Browse the repository at this point in the history
  • Loading branch information
openfirmware committed May 10, 2018
1 parent 64a4cae commit 558ecc7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ After the base entities have been created in the OGC SensorThings API service, t
$ transload get observations --source environment_canada --station XCM --cache /datastore/weather
```

In this example, observations for the Environment Canada station `XCM` (Cambridge Bay) are downloaded to a local cache in the `/datastore/weather/environment_canada/XCM/YYYY/MM/DD/HHMMSS.xml` file. The year/month/day and hour/minute/second are parsed from the observation file provided by the data source.
In this example, observations for the Environment Canada station `XCM` (Cambridge Bay) are downloaded to a local cache in the `/datastore/weather/environment_canada/XCM/YYYY/MM/DD/HHMMSS+0000.xml` file. The year/month/day and hour/minute/second/time zone offset are parsed from the observation file provided by the data source.

If a file already exists with the same name, it is **overwritten**.

Expand Down
19 changes: 19 additions & 0 deletions lib/transloader/environment_canada_station.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'date'
require 'fileutils'
require 'json'
require 'nokogiri'

Expand All @@ -19,6 +21,7 @@ def initialize(id, provider, properties)
@properties = properties
@metadata = {}
@metadata_path = "#{@provider.cache_path}/#{EnvironmentCanadaProvider::CACHE_DIRECTORY}/metadata/#{@id}.json"
@observations_path = "#{@provider.cache_path}/#{EnvironmentCanadaProvider::CACHE_DIRECTORY}/#{@id}"
end

# Parse metadata from the Provider properties and the SWOB-ML file for a
Expand Down Expand Up @@ -202,5 +205,21 @@ def put_metadata(server_url)
def save_metadata
IO.write(@metadata_path, JSON.pretty_generate(@metadata))
end

# Save the SWOB-ML file to file cache
def save_observations
xml = observation_xml

# Parse date from SWOB-ML
timestamp = DateTime.parse(xml.xpath('//po:identification-elements/po:element[@name="date_tm"]/@value', NAMESPACES).text)

# Create cache directory structure
date_path = timestamp.strftime('%Y/%m/%d')
time_path = timestamp.strftime('%H%M%S%z.xml')
FileUtils.mkdir_p("#{@observations_path}/#{date_path}")

# Dump XML to file
IO.write("#{@observations_path}/#{date_path}/#{time_path}", xml.to_s)
end
end
end
13 changes: 12 additions & 1 deletion transload
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ def get_station_metadata(source, station_id, cache)
end
end

def get_station_observations(source, station_id, cache)
case source
when "environment_canada"
provider = Transloader::EnvironmentCanadaProvider.new(cache)
station = provider.get_station(station_id)
station.save_observations
else
puts "Support for source not implemented"
end
end

def put_station_metadata(source, station_id, cache, destination)
case source
when "environment_canada"
Expand All @@ -37,7 +48,7 @@ when :get
when :metadata
get_station_metadata(args.source, args.station, args.cache)
when :observations
puts "Not yet implemented"
get_station_observations(args.source, args.station, args.cache)
end
when :put
case args.object
Expand Down

0 comments on commit 558ecc7

Please sign in to comment.