Simple workflow using pyoscar to extract a station list from OSCAR into a CSV.
Last active
October 18, 2020 18:20
-
-
Save tomkralidis/cf9ff5744731efe0f9c418fe5fdfd7f8 to your computer and use it in GitHub Desktop.
OSCAR station list extraction using pyoscar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from datetime import date | |
from pyoscar import OSCARClient | |
today = date.today().isoformat() | |
CSV_FILENAME = 'oscar-station-list-{}.csv'.format(today) | |
FIELDNAMES = [ | |
'id', | |
'name', | |
'wigosStationIdentifier', | |
'dateEstablished', | |
'region', | |
'territory', | |
'declaredStatus', | |
'latitude', | |
'longitude', | |
'elevation', | |
'stationTypeName', | |
'stationTypeId', | |
'stationStatusCode', | |
'stationTypeCode', | |
'stationProgramsDeclaredStatuses' | |
] | |
client = OSCARClient() | |
can_stations = client.get_stations() | |
with open(CSV_FILENAME, 'w') as csvfile: | |
wigos_writer = csv.DictWriter(csvfile, fieldnames=FIELDNAMES) | |
wigos_writer.writeheader() | |
for s in can_stations: | |
csv_row = dict( | |
id=s.get('id'), | |
name=s.get('name'), | |
dateEstablished=s.get('dateEstablished'), | |
region=s.get('region'), | |
territory=s.get('territory'), | |
declaredStatus=s.get('declaredStatus'), | |
latitude=s.get('latitude'), | |
longitude=s.get('longitude'), | |
elevation=s.get('elevation'), | |
stationTypeName=s.get('stationTypeName'), | |
stationTypeId=s.get('stationTypeId'), | |
stationStatusCode=s.get('stationStatusCode'), | |
stationTypeCode=s.get('stationTypeCode'), | |
stationProgramsDeclaredStatuses=s.get( | |
'stationProgramsDeclaredStatuses') | |
) | |
try: | |
wsis = s.get('wigosStationIdentifiers') | |
wsi = wsis[0].get('wigosStationIdentifier') | |
csv_row['wigosStationIdentifier'] = wsi | |
except TypeError: | |
pass | |
wigos_writer.writerow(csv_row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment