From b95d91f9f0e4f42a3b6d3d61925eccd25678a22f Mon Sep 17 00:00:00 2001 From: Andrew Yan <ayan@usgs.gov> Date: Fri, 23 Feb 2018 09:05:21 -0600 Subject: [PATCH] move US states to constants.py --- config.py | 1 - data/us_state_lookup.json | 238 ------------------ manage.py | 8 - waterdata/__init__.py | 4 - .../commands/lookup_generation/__init__.py | 132 +++------- waterdata/constants.py | 147 +++++++---- waterdata/location_utils.py | 27 +- waterdata/tests/test_location_utils.py | 82 +++--- waterdata/utils.py | 2 +- waterdata/views.py | 11 +- 10 files changed, 205 insertions(+), 447 deletions(-) delete mode 100644 data/us_state_lookup.json diff --git a/config.py b/config.py index 9acb8372e..777434993 100644 --- a/config.py +++ b/config.py @@ -24,7 +24,6 @@ DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') NWIS_CODE_LOOKUP_FILENAME = 'nwis_lookup.json' COUNTRY_STATE_COUNTY_LOOKUP_FILENAME = 'nwis_country_state_lookup.json' HUC_LOOKUP_FILENAME = 'huc_lookup.json' -STATE_LOOKUP_FILENAME = 'us_state_lookup.json' GA_TRACKING_CODE = '' ENABLE_USGS_GA = False diff --git a/data/us_state_lookup.json b/data/us_state_lookup.json deleted file mode 100644 index bfb35f17b..000000000 --- a/data/us_state_lookup.json +++ /dev/null @@ -1,238 +0,0 @@ -[ - { - "name": "Alabama", - "abbreviation": "AL" - }, - { - "name": "Alaska", - "abbreviation": "AK" - }, - { - "name": "American Samoa", - "abbreviation": "AS" - }, - { - "name": "Arizona", - "abbreviation": "AZ" - }, - { - "name": "Arkansas", - "abbreviation": "AR" - }, - { - "name": "California", - "abbreviation": "CA" - }, - { - "name": "Colorado", - "abbreviation": "CO" - }, - { - "name": "Connecticut", - "abbreviation": "CT" - }, - { - "name": "Delaware", - "abbreviation": "DE" - }, - { - "name": "District Of Columbia", - "abbreviation": "DC" - }, - { - "name": "Federated States Of Micronesia", - "abbreviation": "FM" - }, - { - "name": "Florida", - "abbreviation": "FL" - }, - { - "name": "Georgia", - "abbreviation": "GA" - }, - { - "name": "Guam", - "abbreviation": "GU" - }, - { - "name": "Hawaii", - "abbreviation": "HI" - }, - { - "name": "Idaho", - "abbreviation": "ID" - }, - { - "name": "Illinois", - "abbreviation": "IL" - }, - { - "name": "Indiana", - "abbreviation": "IN" - }, - { - "name": "Iowa", - "abbreviation": "IA" - }, - { - "name": "Kansas", - "abbreviation": "KS" - }, - { - "name": "Kentucky", - "abbreviation": "KY" - }, - { - "name": "Louisiana", - "abbreviation": "LA" - }, - { - "name": "Maine", - "abbreviation": "ME" - }, - { - "name": "Marshall Islands", - "abbreviation": "MH" - }, - { - "name": "Maryland", - "abbreviation": "MD" - }, - { - "name": "Massachusetts", - "abbreviation": "MA" - }, - { - "name": "Michigan", - "abbreviation": "MI" - }, - { - "name": "Minnesota", - "abbreviation": "MN" - }, - { - "name": "Mississippi", - "abbreviation": "MS" - }, - { - "name": "Missouri", - "abbreviation": "MO" - }, - { - "name": "Montana", - "abbreviation": "MT" - }, - { - "name": "Nebraska", - "abbreviation": "NE" - }, - { - "name": "Nevada", - "abbreviation": "NV" - }, - { - "name": "New Hampshire", - "abbreviation": "NH" - }, - { - "name": "New Jersey", - "abbreviation": "NJ" - }, - { - "name": "New Mexico", - "abbreviation": "NM" - }, - { - "name": "New York", - "abbreviation": "NY" - }, - { - "name": "North Carolina", - "abbreviation": "NC" - }, - { - "name": "North Dakota", - "abbreviation": "ND" - }, - { - "name": "Northern Mariana Islands", - "abbreviation": "MP" - }, - { - "name": "Ohio", - "abbreviation": "OH" - }, - { - "name": "Oklahoma", - "abbreviation": "OK" - }, - { - "name": "Oregon", - "abbreviation": "OR" - }, - { - "name": "Palau", - "abbreviation": "PW" - }, - { - "name": "Pennsylvania", - "abbreviation": "PA" - }, - { - "name": "Puerto Rico", - "abbreviation": "PR" - }, - { - "name": "Rhode Island", - "abbreviation": "RI" - }, - { - "name": "South Carolina", - "abbreviation": "SC" - }, - { - "name": "South Dakota", - "abbreviation": "SD" - }, - { - "name": "Tennessee", - "abbreviation": "TN" - }, - { - "name": "Texas", - "abbreviation": "TX" - }, - { - "name": "Utah", - "abbreviation": "UT" - }, - { - "name": "Vermont", - "abbreviation": "VT" - }, - { - "name": "Virgin Islands", - "abbreviation": "VI" - }, - { - "name": "Virginia", - "abbreviation": "VA" - }, - { - "name": "Washington", - "abbreviation": "WA" - }, - { - "name": "West Virginia", - "abbreviation": "WV" - }, - { - "name": "Wisconsin", - "abbreviation": "WI" - }, - { - "name": "Wyoming", - "abbreviation": "WY" - } -] \ No newline at end of file diff --git a/manage.py b/manage.py index d3b35814c..3b1ea1318 100755 --- a/manage.py +++ b/manage.py @@ -25,9 +25,6 @@ cli = FlaskGroup(create_app=lambda script_info: app) help='Generate the region lookup file.') @click.option('--huc', is_flag=True, default=False, help='Generate the HUC lookup file.') -@click.option('--states', is_flag=True, default=False, - help='Generate the state lookup file') - def generate_lookups(datadir, **lookups): """ Creates lookup file(s) from NWIS web services in the specified directory. @@ -52,11 +49,6 @@ def generate_lookups(datadir, **lookups): from waterdata.commands.lookup_generation.huc_lookups import generate_hucs_file generate_hucs_file(datadir) - if lookups['states'] or lookups['gen_all']: - click.echo('Generating State lookup file...') - from waterdata.commands.lookup_generation import generate_state_abbreviation_file - generate_state_abbreviation_file(datadir) - if __name__ == '__main__': cli() diff --git a/waterdata/__init__.py b/waterdata/__init__.py index a48553158..df3f0cd74 100644 --- a/waterdata/__init__.py +++ b/waterdata/__init__.py @@ -55,10 +55,6 @@ with open(os.path.join(app.config.get('DATA_DIR'), app.config.get('HUC_LOOKUP_FILENAME')), 'r') as f: app.config['HUC_LOOKUP'] = json.loads(f.read()) -with open(os.path.join(app.config.get('DATA_DIR'), - app.config.get('STATE_LOOKUP_FILENAME')), 'r') as f: - app.config['STATE_ABBREV_LOOKUP'] = json.loads(f.read()) - if app.config.get('LOGGING_ENABLED'): # pylint: disable=C0103 diff --git a/waterdata/commands/lookup_generation/__init__.py b/waterdata/commands/lookup_generation/__init__.py index 6bb13f08e..de13e275e 100755 --- a/waterdata/commands/lookup_generation/__init__.py +++ b/waterdata/commands/lookup_generation/__init__.py @@ -55,98 +55,41 @@ COUNTRY_CODES = ['US', 'CA'] # manually created lookups # derived from a press release from July 2009 -PARAMETER_GROUPS = {'INF': {'name': 'Information'}, - 'PHY': {'name': 'Physical'}, - 'INM': {'name': 'Inorganics, Major, Metals'}, - 'INN': {'name': 'Inorganics, Major, Non-metals'}, - 'NUT': {'name': 'Nutrient'}, - 'MBI': {'name': 'Microbiological'}, - 'BIO': {'name': 'Biological'}, - 'IMM': {'name': 'Inorganics, Minor, metals'}, - 'IMN': {'name': 'Inorganics, Minor, Non-metals'}, - 'TOX': {'name': 'Toxicity'}, - 'OPE': {'name': 'Organics, pesticide'}, - 'OPC': {'name': 'Organics, PCBs'}, - 'OOT': {'name': 'Organics, other'}, - 'RAD': {'name': 'Radiochemistry'}, - 'ISO': {'name': 'Stable Isotopes'}, - 'SED': {'name': 'Sediment'}, - 'POP': {'name': 'Population/Community'}, - 'OTH': {'name': 'Other'}, - 'HAB': {'name': 'Habitat'}} - - -DATA_TYPES = {'iv': {'name': 'Instantaneous Values'}, - 'uv': {'name': 'Unit Values'}, - 'rt': {'name': 'Real-time Data'}, - 'dv': {'name': 'Daily Values'}, - 'pk': {'name': 'Peak Measurements'}, - 'sv': {'name': 'Site Visits'}, - 'gw': {'name': 'Groundwater Levels'}, - 'qw': {'name': 'Water-quality'}, - 'id': {'name': 'Historical Instantaneous Values'}, - 'aw': {'name': 'USGS Active Groundwater Level Network Site'}, - 'ad': {'name': 'USGS Annual Water Data Reports Site'}} - -US_STATES = [{'name': 'Alabama', 'abbreviation': 'AL'}, - {'name': 'Alaska', 'abbreviation': 'AK'}, - {'name': 'American Samoa', 'abbreviation': 'AS'}, - {'name': 'Arizona', 'abbreviation': 'AZ'}, - {'name': 'Arkansas', 'abbreviation': 'AR'}, - {'name': 'California', 'abbreviation': 'CA'}, - {'name': 'Colorado', 'abbreviation': 'CO'}, - {'name': 'Connecticut', 'abbreviation': 'CT'}, - {'name': 'Delaware', 'abbreviation': 'DE'}, - {'name': 'District Of Columbia', 'abbreviation': 'DC'}, - {'name': 'Federated States Of Micronesia', 'abbreviation': 'FM'}, - {'name': 'Florida', 'abbreviation': 'FL'}, - {'name': 'Georgia', 'abbreviation': 'GA'}, - {'name': 'Guam', 'abbreviation': 'GU'}, - {'name': 'Hawaii', 'abbreviation': 'HI'}, - {'name': 'Idaho', 'abbreviation': 'ID'}, - {'name': 'Illinois', 'abbreviation': 'IL'}, - {'name': 'Indiana', 'abbreviation': 'IN'}, - {'name': 'Iowa', 'abbreviation': 'IA'}, - {'name': 'Kansas', 'abbreviation': 'KS'}, - {'name': 'Kentucky', 'abbreviation': 'KY'}, - {'name': 'Louisiana', 'abbreviation': 'LA'}, - {'name': 'Maine', 'abbreviation': 'ME'}, - {'name': 'Marshall Islands', 'abbreviation': 'MH'}, - {'name': 'Maryland', 'abbreviation': 'MD'}, - {'name': 'Massachusetts', 'abbreviation': 'MA'}, - {'name': 'Michigan', 'abbreviation': 'MI'}, - {'name': 'Minnesota', 'abbreviation': 'MN'}, - {'name': 'Mississippi', 'abbreviation': 'MS'}, - {'name': 'Missouri', 'abbreviation': 'MO'}, - {'name': 'Montana', 'abbreviation': 'MT'}, - {'name': 'Nebraska', 'abbreviation': 'NE'}, - {'name': 'Nevada', 'abbreviation': 'NV'}, - {'name': 'New Hampshire', 'abbreviation': 'NH'}, - {'name': 'New Jersey', 'abbreviation': 'NJ'}, - {'name': 'New Mexico', 'abbreviation': 'NM'}, - {'name': 'New York', 'abbreviation': 'NY'}, - {'name': 'North Carolina', 'abbreviation': 'NC'}, - {'name': 'North Dakota', 'abbreviation': 'ND'}, - {'name': 'Northern Mariana Islands', 'abbreviation': 'MP'}, - {'name': 'Ohio', 'abbreviation': 'OH'}, - {'name': 'Oklahoma', 'abbreviation': 'OK'}, - {'name': 'Oregon', 'abbreviation': 'OR'}, - {'name': 'Palau', 'abbreviation': 'PW'}, - {'name': 'Pennsylvania', 'abbreviation': 'PA'}, - {'name': 'Puerto Rico', 'abbreviation': 'PR'}, - {'name': 'Rhode Island', 'abbreviation': 'RI'}, - {'name': 'South Carolina', 'abbreviation': 'SC'}, - {'name': 'South Dakota', 'abbreviation': 'SD'}, - {'name': 'Tennessee', 'abbreviation': 'TN'}, - {'name': 'Texas', 'abbreviation': 'TX'}, - {'name': 'Utah', 'abbreviation': 'UT'}, - {'name': 'Vermont', 'abbreviation': 'VT'}, - {'name': 'Virgin Islands', 'abbreviation': 'VI'}, - {'name': 'Virginia', 'abbreviation': 'VA'}, - {'name': 'Washington', 'abbreviation': 'WA'}, - {'name': 'West Virginia', 'abbreviation': 'WV'}, - {'name': 'Wisconsin', 'abbreviation': 'WI'}, - {'name': 'Wyoming', 'abbreviation': 'WY'}] +PARAMETER_GROUPS = { + 'INF': {'name': 'Information'}, + 'PHY': {'name': 'Physical'}, + 'INM': {'name': 'Inorganics, Major, Metals'}, + 'INN': {'name': 'Inorganics, Major, Non-metals'}, + 'NUT': {'name': 'Nutrient'}, + 'MBI': {'name': 'Microbiological'}, + 'BIO': {'name': 'Biological'}, + 'IMM': {'name': 'Inorganics, Minor, metals'}, + 'IMN': {'name': 'Inorganics, Minor, Non-metals'}, + 'TOX': {'name': 'Toxicity'}, + 'OPE': {'name': 'Organics, pesticide'}, + 'OPC': {'name': 'Organics, PCBs'}, + 'OOT': {'name': 'Organics, other'}, + 'RAD': {'name': 'Radiochemistry'}, + 'ISO': {'name': 'Stable Isotopes'}, + 'SED': {'name': 'Sediment'}, + 'POP': {'name': 'Population/Community'}, + 'OTH': {'name': 'Other'}, + 'HAB': {'name': 'Habitat'} +} + +DATA_TYPES = { + 'iv': {'name': 'Instantaneous Values'}, + 'uv': {'name': 'Unit Values'}, + 'rt': {'name': 'Real-time Data'}, + 'dv': {'name': 'Daily Values'}, + 'pk': {'name': 'Peak Measurements'}, + 'sv': {'name': 'Site Visits'}, + 'gw': {'name': 'Groundwater Levels'}, + 'qw': {'name': 'Water-quality'}, + 'id': {'name': 'Historical Instantaneous Values'}, + 'aw': {'name': 'USGS Active Groundwater Level Network Site'}, + 'ad': {'name': 'USGS Annual Water Data Reports Site'} +} def generate_lookup_file(datadir, filename='nwis_lookup.json'): @@ -214,8 +157,3 @@ def generate_country_state_county_file(datadir, filename='nwis_country_state_loo with open(os.path.join(datadir, filename), 'w') as f: f.write(json.dumps(lookups, indent=4)) - - -def generate_state_abbreviation_file(datadir, filename='us_state_lookup.json'): - with open(os.path.join(datadir, filename), 'w') as f: - f.write(json.dumps(US_STATES, indent=4)) diff --git a/waterdata/constants.py b/waterdata/constants.py index 05df68fc6..af014979b 100644 --- a/waterdata/constants.py +++ b/waterdata/constants.py @@ -3,46 +3,109 @@ Constants """ STATION_FIELDS_D = { - 'agency_cd' :'Agency', - 'site_no' :'Site identification number', - 'station_nm' :'Site name', - 'site_tp_cd' :'Site type', - 'lat_va' :'DMS latitude', - 'long_va' :'DMS longitude', - 'dec_lat_va' :'Decimal latitude', - 'dec_long_va' :'Decimal longitude', - 'coord_meth_cd' :'Latitude-longitude method', - 'coord_acy_cd' :'Latitude-longitude accuracy', - 'coord_datum_cd' :'Latitude-longitude datum', - 'dec_coord_datum_cd' :'Decimal Latitude-longitude datum', - 'district_cd' :'District', - 'state_cd' :'State', - 'county_cd' :'County', - 'country_cd' :'Country', - 'land_net_ds' :'Land net location description', - 'map_nm' :'Name of location map', - 'map_scale_fc' :'Scale of location map', - 'alt_va' :'Altitude of Gage/land surface', - 'alt_meth_cd' :'Method altitude determined', - 'alt_acy_va' :'Altitude accuracy', - 'alt_datum_cd' :'Altitude datum', - 'huc_cd' :'Subbasin hydrologic unit', - 'basin_cd' :'Drainage basin', - 'topo_cd' :'Topographic setting', - 'instruments_cd' :'Flags for instruments at site', - 'construction_dt' :'Date of first construction', - 'inventory_dt' :'Date site established or inventoried', - 'drain_area_va' :'Drainage area', - 'contrib_drain_area_va' :'Contributing drainage area', - 'tz_cd' :'Time Zone abbreviation', - 'local_time_fg' :'Site honors Daylight Savings Time', - 'reliability_cd' :'Data reliability', - 'gw_file_cd' :'Data-other GW files', - 'nat_aqfr_cd' :'National aquifer', - 'aqfr_cd' :'Local aquifer', - 'aqfr_type_cd' :'Local aquifer type', - 'well_depth_va' :'Well depth', - 'hole_depth_va' :'Hole depth', - 'depth_src_cd' :'Source of depth data', - 'project_no' :'Project number', + 'agency_cd': 'Agency', + 'site_no': 'Site identification number', + 'station_nm': 'Site name', + 'site_tp_cd': 'Site type', + 'lat_va': 'DMS latitude', + 'long_va': 'DMS longitude', + 'dec_lat_va': 'Decimal latitude', + 'dec_long_va': 'Decimal longitude', + 'coord_meth_cd': 'Latitude-longitude method', + 'coord_acy_cd': 'Latitude-longitude accuracy', + 'coord_datum_cd': 'Latitude-longitude datum', + 'dec_coord_datum_cd': 'Decimal Latitude-longitude datum', + 'district_cd': 'District', + 'state_cd': 'State', + 'county_cd': 'County', + 'country_cd': 'Country', + 'land_net_ds': 'Land net location description', + 'map_nm': 'Name of location map', + 'map_scale_fc': 'Scale of location map', + 'alt_va': 'Altitude of Gage/land surface', + 'alt_meth_cd': 'Method altitude determined', + 'alt_acy_va': 'Altitude accuracy', + 'alt_datum_cd': 'Altitude datum', + 'huc_cd': 'Subbasin hydrologic unit', + 'basin_cd': 'Drainage basin', + 'topo_cd': 'Topographic setting', + 'instruments_cd': 'Flags for instruments at site', + 'construction_dt': 'Date of first construction', + 'inventory_dt': 'Date site established or inventoried', + 'drain_area_va': 'Drainage area', + 'contrib_drain_area_va': 'Contributing drainage area', + 'tz_cd': 'Time Zone abbreviation', + 'local_time_fg': 'Site honors Daylight Savings Time', + 'reliability_cd': 'Data reliability', + 'gw_file_cd': 'Data-other GW files', + 'nat_aqfr_cd': 'National aquifer', + 'aqfr_cd': 'Local aquifer', + 'aqfr_type_cd': 'Local aquifer type', + 'well_depth_va': 'Well depth', + 'hole_depth_va': 'Hole depth', + 'depth_src_cd': 'Source of depth data', + 'project_no': 'Project number', } + + +US_STATES = [ + {'name': 'Alabama', 'abbreviation': 'AL'}, + {'name': 'Alaska', 'abbreviation': 'AK'}, + {'name': 'American Samoa', 'abbreviation': 'AS'}, + {'name': 'Arizona', 'abbreviation': 'AZ'}, + {'name': 'Arkansas', 'abbreviation': 'AR'}, + {'name': 'California', 'abbreviation': 'CA'}, + {'name': 'Colorado', 'abbreviation': 'CO'}, + {'name': 'Connecticut', 'abbreviation': 'CT'}, + {'name': 'Delaware', 'abbreviation': 'DE'}, + {'name': 'District Of Columbia', 'abbreviation': 'DC'}, + {'name': 'Federated States Of Micronesia', 'abbreviation': 'FM'}, + {'name': 'Florida', 'abbreviation': 'FL'}, + {'name': 'Georgia', 'abbreviation': 'GA'}, + {'name': 'Guam', 'abbreviation': 'GU'}, + {'name': 'Hawaii', 'abbreviation': 'HI'}, + {'name': 'Idaho', 'abbreviation': 'ID'}, + {'name': 'Illinois', 'abbreviation': 'IL'}, + {'name': 'Indiana', 'abbreviation': 'IN'}, + {'name': 'Iowa', 'abbreviation': 'IA'}, + {'name': 'Kansas', 'abbreviation': 'KS'}, + {'name': 'Kentucky', 'abbreviation': 'KY'}, + {'name': 'Louisiana', 'abbreviation': 'LA'}, + {'name': 'Maine', 'abbreviation': 'ME'}, + {'name': 'Marshall Islands', 'abbreviation': 'MH'}, + {'name': 'Maryland', 'abbreviation': 'MD'}, + {'name': 'Massachusetts', 'abbreviation': 'MA'}, + {'name': 'Michigan', 'abbreviation': 'MI'}, + {'name': 'Minnesota', 'abbreviation': 'MN'}, + {'name': 'Mississippi', 'abbreviation': 'MS'}, + {'name': 'Missouri', 'abbreviation': 'MO'}, + {'name': 'Montana', 'abbreviation': 'MT'}, + {'name': 'Nebraska', 'abbreviation': 'NE'}, + {'name': 'Nevada', 'abbreviation': 'NV'}, + {'name': 'New Hampshire', 'abbreviation': 'NH'}, + {'name': 'New Jersey', 'abbreviation': 'NJ'}, + {'name': 'New Mexico', 'abbreviation': 'NM'}, + {'name': 'New York', 'abbreviation': 'NY'}, + {'name': 'North Carolina', 'abbreviation': 'NC'}, + {'name': 'North Dakota', 'abbreviation': 'ND'}, + {'name': 'Northern Mariana Islands', 'abbreviation': 'MP'}, + {'name': 'Ohio', 'abbreviation': 'OH'}, + {'name': 'Oklahoma', 'abbreviation': 'OK'}, + {'name': 'Oregon', 'abbreviation': 'OR'}, + {'name': 'Palau', 'abbreviation': 'PW'}, + {'name': 'Pennsylvania', 'abbreviation': 'PA'}, + {'name': 'Puerto Rico', 'abbreviation': 'PR'}, + {'name': 'Rhode Island', 'abbreviation': 'RI'}, + {'name': 'South Carolina', 'abbreviation': 'SC'}, + {'name': 'South Dakota', 'abbreviation': 'SD'}, + {'name': 'Tennessee', 'abbreviation': 'TN'}, + {'name': 'Texas', 'abbreviation': 'TX'}, + {'name': 'Utah', 'abbreviation': 'UT'}, + {'name': 'Vermont', 'abbreviation': 'VT'}, + {'name': 'Virgin Islands', 'abbreviation': 'VI'}, + {'name': 'Virginia', 'abbreviation': 'VA'}, + {'name': 'Washington', 'abbreviation': 'WA'}, + {'name': 'West Virginia', 'abbreviation': 'WV'}, + {'name': 'Wisconsin', 'abbreviation': 'WI'}, + {'name': 'Wyoming', 'abbreviation': 'WY'} +] diff --git a/waterdata/location_utils.py b/waterdata/location_utils.py index c217a2991..ec671e918 100644 --- a/waterdata/location_utils.py +++ b/waterdata/location_utils.py @@ -8,11 +8,28 @@ import datetime from flask import url_for +from .constants import US_STATES Parameter = namedtuple('Parameter', ['parameter_cd', 'start_date', 'end_date', 'record_count']) -def get_disambiguated_values(location, code_lookups, country_state_county_lookups, huc_lookups, state_abbrev_lookups): +def get_state_abbreviation(state_full_name): + """ + Return a state's two letter abbreviation. + + :param str state_full_name: + :return: state two letter abbreviation + :rtype: str + """ + state = filter(lambda record: record['name'] == state_full_name, US_STATES) + try: + state_abbrev = next(state).get('abbreviation') + except StopIteration: + state_abbrev = None + return state_abbrev + + +def get_disambiguated_values(location, code_lookups, country_state_county_lookups, huc_lookups): """ Convert values for keys that contains codes to human readable names using the lookups :param dict location: @@ -33,14 +50,6 @@ def get_disambiguated_values(location, code_lookups, country_state_county_lookup return state_lookup.get(state_code, {}).get('name') - def get_state_abbreviation(state_full_name): - state = filter(lambda record: record['name'] == state_full_name, state_abbrev_lookups) - try: - state_abbrev = next(state).get('abbreviation') - except StopIteration: - state_abbrev = None - return state_abbrev - transformed_location = {} country_code = location.get('country_cd') diff --git a/waterdata/tests/test_location_utils.py b/waterdata/tests/test_location_utils.py index ef36b919b..25e0c8a8e 100644 --- a/waterdata/tests/test_location_utils.py +++ b/waterdata/tests/test_location_utils.py @@ -7,7 +7,7 @@ from unittest import TestCase from waterdata import app from waterdata.location_utils import Parameter, get_capabilities, get_site_parameter, build_linked_data,\ - get_disambiguated_values + get_disambiguated_values, get_state_abbreviation class GetDisambiguatedValuesTestCase(TestCase): @@ -116,15 +116,11 @@ class GetDisambiguatedValuesTestCase(TestCase): } } - self.test_state_abbrev_lookup = [ - {'name': 'Alabama', 'abbreviation': 'AL'}, - {'name': 'Alaska', 'abbreviation': 'AK'}, - ] - def test_empty_location(self): - self.assertEqual(get_disambiguated_values({}, self.test_code_lookups, - self.test_country_state_county_lookup, {}, {}), - {}) + self.assertEqual( + get_disambiguated_values({}, self.test_code_lookups, self.test_country_state_county_lookup, {}), + {} + ) def test_location_with_no_keys_in_lookups(self): test_location = { @@ -136,8 +132,7 @@ class GetDisambiguatedValuesTestCase(TestCase): 'site_no': {'name': '12345678', 'code': '12345678'} } self.assertEqual( - get_disambiguated_values(test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, {}), + get_disambiguated_values(test_location, self.test_code_lookups, self.test_country_state_county_lookup, {}), expected_location ) @@ -153,8 +148,7 @@ class GetDisambiguatedValuesTestCase(TestCase): 'nat_aqfr_cd': {'name': 'Basin and Range basin-fill aquifers', 'code': 'N100BSNRGB'} } self.assertEqual( - get_disambiguated_values(test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, {}), + get_disambiguated_values(test_location, self.test_code_lookups, self.test_country_state_county_lookup, {}), expected_location) def test_location_with_key_values_not_in_code_lookups(self): @@ -169,9 +163,9 @@ class GetDisambiguatedValuesTestCase(TestCase): 'nat_aqfr_cd': {'code': 'N100BSNRGB', 'name': 'Basin and Range basin-fill aquifers'} } self.assertEqual( - get_disambiguated_values(test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, {}), - expected_location) + get_disambiguated_values(test_location, self.test_code_lookups, self.test_country_state_county_lookup, {}), + expected_location + ) def test_state_county_in_state_county_lookup(self): test_location = { @@ -189,9 +183,9 @@ class GetDisambiguatedValuesTestCase(TestCase): 'county_cd': {'name': 'Baldwin County', 'code': '002'} } self.assertEqual( - get_disambiguated_values(test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, self.test_state_abbrev_lookup), - expected_location) + get_disambiguated_values(test_location, self.test_code_lookups, self.test_country_state_county_lookup, {}), + expected_location + ) def test_state_county_no_county_in_lookup(self): test_location = { @@ -207,8 +201,7 @@ class GetDisambiguatedValuesTestCase(TestCase): 'county_cd': {'name': '004', 'code': '004'} } self.assertEqual( - get_disambiguated_values(test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, {}), + get_disambiguated_values(test_location, self.test_code_lookups, self.test_country_state_county_lookup, {}), expected_location) def test_state_with_no_counties_in_lookup(self): @@ -225,9 +218,9 @@ class GetDisambiguatedValuesTestCase(TestCase): 'county_cd': {'name': '004', 'code': '004'} } self.assertEqual( - get_disambiguated_values(test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, {}), - expected_location) + get_disambiguated_values(test_location, self.test_code_lookups, self.test_country_state_county_lookup, {}), + expected_location + ) def test_no_state_in_lookup(self): test_location = { @@ -247,9 +240,10 @@ class GetDisambiguatedValuesTestCase(TestCase): self.assertEqual( get_disambiguated_values( test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, self.test_state_abbrev_lookup + self.test_country_state_county_lookup, {} ), - expected_location) + expected_location + ) def test_no_country_in_lookup(self): test_location = { @@ -265,9 +259,9 @@ class GetDisambiguatedValuesTestCase(TestCase): 'county_cd': {'name': '004', 'code': '004'} } self.assertEqual( - get_disambiguated_values(test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, {}), - expected_location) + get_disambiguated_values(test_location, self.test_code_lookups, self.test_country_state_county_lookup, {}), + expected_location + ) def test_missing_country(self): test_location = { @@ -281,9 +275,9 @@ class GetDisambiguatedValuesTestCase(TestCase): 'county_cd': {'name': '004', 'code': '004'} } self.assertEqual( - get_disambiguated_values(test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, {}), - expected_location) + get_disambiguated_values(test_location, self.test_code_lookups, self.test_country_state_county_lookup, {}), + expected_location + ) def test_missing_state(self): test_location = { @@ -297,9 +291,9 @@ class GetDisambiguatedValuesTestCase(TestCase): 'county_cd': {'name': '001', 'code': '001'} } self.assertEqual( - get_disambiguated_values(test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, {}), - expected_location) + get_disambiguated_values(test_location, self.test_code_lookups, self.test_country_state_county_lookup, {}), + expected_location + ) def test_missing_county(self): test_location = { @@ -313,8 +307,7 @@ class GetDisambiguatedValuesTestCase(TestCase): 'state_cd': {'name': 'Alabama', 'code': '01'}, } self.assertEqual( - get_disambiguated_values(test_location, self.test_code_lookups, - self.test_country_state_county_lookup, {}, {}), + get_disambiguated_values(test_location, self.test_code_lookups, self.test_country_state_county_lookup, {}), expected_location ) @@ -327,7 +320,7 @@ class GetDisambiguatedValuesTestCase(TestCase): 'huc_cd': {'name': 'New England Region', 'code': '01', 'url': '/hydrological-unit/01'} } self.assertEqual( - get_disambiguated_values(test_location, {}, {}, self.test_huc_lookup, {}), + get_disambiguated_values(test_location, {}, {}, self.test_huc_lookup), expected_location ) @@ -340,7 +333,7 @@ class GetDisambiguatedValuesTestCase(TestCase): 'huc_cd': {'name': 'Upper St. John', 'code': '01010001', 'url': '/hydrological-unit/01010001'} } self.assertEqual( - get_disambiguated_values(test_location, {}, {}, self.test_huc_lookup, {}), + get_disambiguated_values(test_location, {}, {}, self.test_huc_lookup), expected_location ) @@ -353,7 +346,7 @@ class GetDisambiguatedValuesTestCase(TestCase): 'huc_cd': {'name': None, 'code': '01010002', 'url': '/hydrological-unit/01010002'} } self.assertEqual( - get_disambiguated_values(test_location, {}, {}, self.test_huc_lookup, {}), + get_disambiguated_values(test_location, {}, {}, self.test_huc_lookup), expected_location ) @@ -492,3 +485,12 @@ class TestBuildLinkedData(TestCase): } } self.assertDictEqual(result, expected) + + +class TestGetStateAbbreviation(TestCase): + + def test_state_found(self): + self.assertEqual(get_state_abbreviation('California'), 'CA') + + def test_state_no_found(self): + self.assertIsNone(get_state_abbreviation('Tenochtitlan')) diff --git a/waterdata/utils.py b/waterdata/utils.py index a25296f82..d2b2492e0 100644 --- a/waterdata/utils.py +++ b/waterdata/utils.py @@ -2,7 +2,7 @@ Utility functions """ -from urllib.parse import urljoin, urlencode, urlunparse +from urllib.parse import urlencode, urljoin import requests as r diff --git a/waterdata/views.py b/waterdata/views.py index 06ea210cd..e7b484c56 100644 --- a/waterdata/views.py +++ b/waterdata/views.py @@ -71,7 +71,7 @@ def monitoring_location(site_no): param_data = [param_datum for param_datum in parse_rdb(parameter_data_resp.iter_lines(decode_unicode=True))] site_dataseries = [get_disambiguated_values(param_datum, app.config['NWIS_CODE_LOOKUP'], {}, - app.config['HUC_LOOKUP'], {}) for param_datum in param_data] + app.config['HUC_LOOKUP']) for param_datum in param_data] location_capabilities = set(param_datum['parm_cd'] for param_datum in param_data) else: site_dataseries = None @@ -89,17 +89,14 @@ def monitoring_location(site_no): station_record, app.config['NWIS_CODE_LOOKUP'], app.config['COUNTRY_STATE_COUNTY_LOOKUP'], - app.config['HUC_LOOKUP'], - app.config['STATE_ABBREV_LOOKUP'] + app.config['HUC_LOOKUP'] ) questions_link = None try: - district_abbrev = location_with_values['district_cd']['abbreviation'] + district_abbrev = location_with_values['district_cd']['abbreviation'] except KeyError: pass else: - questions_link_host = 'https://water.usgs.gov' - questions_link_path = 'contact/gsanswers' questions_link_params = { 'pemail': 'gs-w-{}_NWISWeb_Data_Inquiries'.format(district_abbrev.lower()), 'subject': 'Site Number: {}'.format(site_no), @@ -108,7 +105,7 @@ def monitoring_location(site_no): 'below that briefly summarizes your request</b></p>' ) } - questions_link = construct_url(questions_link_host, questions_link_path, questions_link_params) + questions_link = construct_url('https://water.usgs.gov', 'contact/gsanswer', questions_link_params) context = { 'status_code': status, 'stations': data_list, -- GitLab