Skip to content

Commit 48af0f1

Browse files
committedApr 17, 2019
update countries model, add doc, add exceptions for search
1 parent 32e95ae commit 48af0f1

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed
 

‎Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ help:
5454
@echo " createdb: create PostgreSQL/PostGIS database"
5555
@echo " dropdb: drop PostgreSQL/PostGIS database"
5656
@echo " setup: create models and search index"
57+
@echo " setup_data: download core metadata
5758
@echo " teardown: delete models and search index"
5859
@echo " test: run tests"
5960
@echo " coverage: run code coverage"
@@ -72,6 +73,7 @@ clean:
7273
rm -f debian/woudc-data-registry.prerm.debhelper
7374
rm -f debian/woudc-data-registry.substvars
7475
rm -fr debian/woudc-data-registry
76+
@echo "NOT removing data/"
7577

7678
coverage:
7779
coverage run --source=woudc_data_registry -m unittest woudc_data_registry.tests.test_data_registry
@@ -94,11 +96,15 @@ setup:
9496
woudc-data-registry manage setup
9597
woudc-data-registry search create-index
9698

99+
setup_data:
100+
mkdir -p data
101+
curl -o data/wmo-countries.json https://www.wmo.int/cpdb/data/membersandterritories.json
102+
97103
teardown:
98104
woudc-data-registry manage teardown
99105
woudc-data-registry search delete-index
100106

101107
test:
102108
python setup.py test
103109

104-
.PHONY: clean coverage createdb dropdb flake8 help package setup teardown test
110+
.PHONY: clean coverage createdb dropdb flake8 help package setup setup_data teardown test

‎README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ woudc-data-registry manage setup
5555
woudc-data-registry search create-index
5656

5757
# load core metadata
58-
woudc-data-registry manage init
58+
59+
# fetch WMO country list
60+
mkdir data
61+
curl -o data/wmo-countries.json https://www.wmo.int/cpdb/data/membersandterritories.json
62+
woudc-data-registry manage init -d data/
5963

6064
# cleanups
6165

‎woudc_data_registry/models.py

+30-10
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363

6464
LOGGER = logging.getLogger(__name__)
6565

66-
WMO_REGION_ENUM = Enum('I', 'II', 'III', 'IV', 'V', 'VI', name='wmo_region')
66+
WMO_REGION_ENUM = Enum('I', 'II', 'III', 'IV', 'V', 'VI', 'Antarctica',
67+
name='wmo_region')
6768

6869

6970
class Geometry(geoalchemy2.types.Geometry):
@@ -79,17 +80,22 @@ def get_col_spec(self):
7980

8081

8182
class Country(base):
82-
"""Data Registry Country"""
83+
"""
84+
Data Registry Country
85+
86+
https://www.wmo.int/cpdb/data/membersandterritories.json
87+
88+
"""
8389

8490
__tablename__ = 'countries'
8591

8692
identifier = Column(String, nullable=False, primary_key=True)
8793
country_name = Column(String, nullable=False, unique=True)
8894
french_name = Column(String, nullable=False, unique=True)
89-
wmo_region_id = Column(String, nullable=False)
90-
regional_involvement = Column(String, nullable=False)
91-
wmo_membership = Column(Date, nullable=True)
92-
link = Column(String, nullable=True)
95+
wmo_region_id = Column(WMO_REGION_ENUM, nullable=False)
96+
regional_involvement = Column(WMO_REGION_ENUM, nullable=False)
97+
wmo_membership = Column(Date, nullable=False)
98+
link = Column(String, nullable=False)
9399

94100
def __init__(self, dict_):
95101
self.identifier = dict_['id']
@@ -101,7 +107,7 @@ def __init__(self, dict_):
101107
self.link = dict_['link']
102108

103109
def __repr__(self):
104-
return 'Country ({}, {})'.format(self.identifier, self.name)
110+
return 'Country ({}, {})'.format(self.identifier, self.country_name)
105111

106112

107113
class Contributor(base):
@@ -175,7 +181,7 @@ class Station(base):
175181

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

178-
identifier = Column(Integer, primary_key=True)
184+
identifier = Column(String, primary_key=True)
179185
name = Column(String, nullable=False)
180186
contributor_id = Column(String, ForeignKey('contributors.identifier'),
181187
nullable=True)
@@ -201,6 +207,7 @@ class Station(base):
201207
def __init__(self, dict_):
202208
"""serializer"""
203209

210+
self.identifier = dict_['identifier']
204211
self.name = dict_['name']
205212
self.stn_type = dict_['stn_type']
206213
self.gaw_id = dict_['gaw_id']
@@ -212,7 +219,7 @@ def __init__(self, dict_):
212219
self.location = util.point2ewkt(dict_['x'], dict_['y'], dict_['z'])
213220

214221
def __repr__(self):
215-
return 'Station ({}, {})'.format(self.stn_identifier, self.name)
222+
return 'Station ({}, {})'.format(self.identifier, self.name)
216223

217224

218225
class DataRecord(base):
@@ -440,7 +447,7 @@ def init(ctx, datadir):
440447
if datadir is None:
441448
raise click.ClickException('Missing required data directory')
442449

443-
countries = os.path.join(datadir, 'countries.json')
450+
countries = os.path.join(datadir, 'wmo-countries.json')
444451
contributors = os.path.join(datadir, 'contributors.csv')
445452
stations = os.path.join(datadir, 'stations.csv')
446453
datasets = os.path.join(datadir, 'datasets.csv')
@@ -456,6 +463,19 @@ def init(ctx, datadir):
456463
continue
457464
country = Country(country_data)
458465
registry_.save(country)
466+
# Antarctica is not a recognized country per se but is
467+
# provided in WOUDC
468+
antarctica = {
469+
'id': 'ATA',
470+
'country_name': 'Antarctica',
471+
'french_name': 'Antarctique',
472+
'wmo_region_id': 'Antarctica',
473+
'regional_involvement': 'Antarctica',
474+
'wmo_membership': '1970-01-01',
475+
'link': 'https://www.wmo.int/pages/prog/www/Antarctica/Purpose.html'
476+
}
477+
country = Country(antarctica)
478+
registry_.save(country)
459479

460480
click.echo('Loading datasets metadata')
461481
with open(datasets) as csvfile:

‎woudc_data_registry/search.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949

5050
import click
5151
from elasticsearch import Elasticsearch
52-
from elasticsearch.exceptions import NotFoundError, RequestError
52+
from elasticsearch.exceptions import (ConnectionError, NotFoundError,
53+
RequestError)
5354
import requests
5455

5556
from woudc_data_registry import config
@@ -94,7 +95,7 @@ def create(self):
9495
try:
9596
self.connection.indices.create(index=self.index_name,
9697
body=settings)
97-
except RequestError as err:
98+
except (ConnectionError, RequestError) as err:
9899
LOGGER.error(err)
99100
raise SearchIndexError(err)
100101

0 commit comments

Comments
 (0)
Please sign in to comment.