Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

include $(ENV)

PG_FLAGS=-h $(DB_HOST) -p $(DB_PORT) $(DB_NAME) -U $(DB_USERNAME)
PG_FLAGS=-h $(WDR_DB_HOST) -p $(WDR_DB_PORT) -U $(WDR_DB_USERNAME) -W $(WDR_DB_NAME)

help:
@echo
Expand Down Expand Up @@ -78,7 +78,7 @@ coverage:
coverage report -m

createdb:
createdb $(PG_FLAGS) -E UTF8 --template=template0
createdb $(PG_FLAGS) -E UTF8
psql $(PG_FLAGS) -c "create extension postgis;"

dropdb:
Expand Down
51 changes: 29 additions & 22 deletions woudc_data_registry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import click
import csv
import geoalchemy2
import json
from sqlalchemy import (Boolean, Column, create_engine, Date, DateTime,
Enum, ForeignKey, Integer, String, Time, UnicodeText,
UniqueConstraint)
Expand Down Expand Up @@ -83,17 +84,21 @@ class Country(base):
__tablename__ = 'countries'

identifier = Column(String, nullable=False, primary_key=True)
name = Column(String, nullable=False, unique=True)
wmo_region = Column(String, nullable=False)
country_name = Column(String, nullable=False, unique=True)
french_name = Column(String, nullable=False, unique=True)
wmo_region_id = Column(String, nullable=False)
regional_involvement = Column(String, nullable=False)
wmo_membership = Column(Date, nullable=True)
url = Column(String, nullable=True)
link = Column(String, nullable=True)

def __init__(self, dict_):
self.identifier = dict_['identifier']
self.name = dict_['name']
self.wmo_region = dict_['wmo_region']
self.wmo_region_since = dict_['wmo_membership']
self.url = dict_['url']
self.identifier = dict_['id']
self.country_name = dict_['country_name']
self.french_name = dict_['french_name']
self.wmo_region_id = dict_['wmo_region_id']
self.regional_involvement = dict_['regional_involvement']
self.wmo_membership = dict_['wmo_membership']
self.link = dict_['link']

def __repr__(self):
return 'Country ({}, {})'.format(self.identifier, self.name)
Expand Down Expand Up @@ -166,20 +171,19 @@ class Station(base):

__tablename__ = 'stations'
__table_args__ = (UniqueConstraint('active_start_date', 'active_end_date',
'contributor_id', 'stn_identifier'),)
'contributor_id', 'identifier'),)

stn_type_enum = Enum('STN', 'SHP', name='type')

identifier = Column(Integer, primary_key=True, autoincrement=True)
identifier = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
contributor_id = Column(String, ForeignKey('contributors.identifier'),
nullable=False)
stn_identifier = Column(String, nullable=False)
nullable=True)
stn_type = Column(stn_type_enum, nullable=False)
gaw_id = Column(String, nullable=True)
country_id = Column(String, ForeignKey('countries.identifier'),
nullable=False)
wmo_region = Column(WMO_REGION_ENUM, nullable=False)
wmo_region = Column(WMO_REGION_ENUM, nullable=True)
active = Column(Boolean, nullable=False, default=True)
active_start_date = Column(Date, nullable=False)
active_end_date = Column(Date, nullable=True)
Expand All @@ -197,7 +201,6 @@ class Station(base):
def __init__(self, dict_):
"""serializer"""

self.stn_identifier = dict_['stn_identifier']
self.name = dict_['name']
self.stn_type = dict_['stn_type']
self.gaw_id = dict_['gaw_id']
Expand Down Expand Up @@ -241,7 +244,8 @@ class DataRecord(base):
instrument_model = Column(String, nullable=False)
instrument_number = Column(String, nullable=False)

location = Column(Geometry(srid=0), nullable=False)
location = Column(Geometry(geometry_type='POINT', srid=4326),
nullable=False)

timestamp_utcoffset = Column(String, nullable=False)
timestamp_date = Column(Date, nullable=False)
Expand Down Expand Up @@ -400,7 +404,7 @@ def setup(ctx):

try:
click.echo('Generating models')
base.metadata.create_all(engine, checkfirst=False)
base.metadata.create_all(engine, checkfirst=True)
click.echo('Done')
except (OperationalError, ProgrammingError) as err:
click.echo('ERROR: {}'.format(err))
Expand All @@ -417,7 +421,7 @@ def teardown(ctx):

try:
click.echo('Deleting models')
base.metadata.drop_all(engine, checkfirst=False)
base.metadata.drop_all(engine, checkfirst=True)
click.echo('Done')
except (OperationalError, ProgrammingError) as err:
click.echo('ERROR: {}'.format(err))
Expand All @@ -436,18 +440,21 @@ def init(ctx, datadir):
if datadir is None:
raise click.ClickException('Missing required data directory')

countries = os.path.join(datadir, 'countries.csv')
countries = os.path.join(datadir, 'countries.json')
contributors = os.path.join(datadir, 'contributors.csv')
stations = os.path.join(datadir, 'stations.csv')
datasets = os.path.join(datadir, 'datasets.csv')

registry_ = registry.Registry()

click.echo('Loading countries metadata')
with open(countries) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
country = Country(row)
with open(countries) as jsonfile:
countries_data = json.load(jsonfile)
for row in countries_data['countries']:
country_data = countries_data['countries'][row]
if country_data['id'] == 'NUL':
continue
country = Country(country_data)
registry_.save(country)

click.echo('Loading datasets metadata')
Expand Down