|
16 | 16 | from requests.models import PreparedRequest |
17 | 17 | from typing import List, Optional, Tuple, Union |
18 | 18 | from io import StringIO |
| 19 | +import json |
19 | 20 | from typing import TYPE_CHECKING |
20 | 21 |
|
21 | 22 | import pandas as pd |
@@ -69,6 +70,137 @@ def _check_profiles( |
69 | 70 | f"{services_dict[service]}." |
70 | 71 | ) |
71 | 72 |
|
| 73 | +def _get_codeservice(input_name): |
| 74 | + """Grab dataframe from Samples code service. |
| 75 | + |
| 76 | + Parameters |
| 77 | + ---------- |
| 78 | + input_name : string |
| 79 | + One of the following options: "states", "counties", |
| 80 | + "sitetype", "samplemedia", "characteristicgroup", |
| 81 | + "characteristics", or "observedproperty" |
| 82 | + """ |
| 83 | + |
| 84 | + url = "https://api.waterdata.usgs.gov/samples-data/codeservice/" + input_name + "?mimeType=application%2Fjson" |
| 85 | + |
| 86 | + response = requests.get(url) |
| 87 | + |
| 88 | + response.raise_for_status |
| 89 | + |
| 90 | + # Extract json |
| 91 | + data_dict = json.loads(response.text) |
| 92 | + |
| 93 | + # Convert to list |
| 94 | + data_list = data_dict['data'] |
| 95 | + |
| 96 | + # Create lookup dataFrame |
| 97 | + df = pd.DataFrame(data_list) |
| 98 | + |
| 99 | + return df |
| 100 | + |
| 101 | +def stateFips_lookup(): |
| 102 | + """Code service that returns a dataframe of all possible stateFips codes to be used |
| 103 | + in `get_USGS_samples()` |
| 104 | +
|
| 105 | + Parameters |
| 106 | + ---------- |
| 107 | + None, returns a standard pandas dataframe of all FIPS codes. |
| 108 | +
|
| 109 | + """ |
| 110 | + df = _get_codeservice(input_name="states") |
| 111 | + |
| 112 | + df['stateFips'] = "US:" + df['fipsCode'] |
| 113 | + |
| 114 | + df = df[['stateName', 'stateFips']] |
| 115 | + |
| 116 | + return df |
| 117 | + |
| 118 | +def countyFips_lookup(): |
| 119 | + """Code service that returns a dataframe of all possible countyFips codes to be used |
| 120 | + in `get_USGS_samples()` |
| 121 | +
|
| 122 | + Parameters |
| 123 | + ---------- |
| 124 | + None, returns a standard pandas dataframe of all FIPS codes. |
| 125 | + |
| 126 | + """ |
| 127 | + counties = _get_codeservice(input_name="counties") |
| 128 | + states = _get_codeservice(input_name="states") |
| 129 | + |
| 130 | + county_states = pd.merge(counties[['countyCode', 'countyName', 'stateAbbrev']], states[['stateAbbrev', 'fipsCode', 'stateName']], on="stateAbbrev", how="left") |
| 131 | + |
| 132 | + county_states['countyFips'] = "US:" + county_states['fipsCode'] + ":" + county_states['countyCode'] |
| 133 | + |
| 134 | + df = county_states[['stateName', 'countyName', 'countyFips']] |
| 135 | + |
| 136 | + return df |
| 137 | + |
| 138 | +def siteType_lookup(): |
| 139 | + """Code service that returns a dataframe of all possible siteType values and |
| 140 | + siteTypeName values to be used in `get_USGS_samples()` |
| 141 | +
|
| 142 | + Parameters |
| 143 | + ---------- |
| 144 | + None, returns a standard pandas dataframe of all FIPS codes. |
| 145 | +
|
| 146 | + """ |
| 147 | + df = _get_codeservice(input_name="sitetype") |
| 148 | + |
| 149 | + df.rename(columns={'typeCode': 'siteTypeCode', |
| 150 | + 'typeName': 'siteTypeName'}, |
| 151 | + inplace=True) |
| 152 | + |
| 153 | + df = df[['siteTypeCode', 'siteTypeName', 'typeDescription']] |
| 154 | + |
| 155 | + return df |
| 156 | + |
| 157 | +def activityMediaName_lookup(): |
| 158 | + """Code service that returns a dataframe of all possible activityMediaName values |
| 159 | + to be used in `get_USGS_samples()` |
| 160 | +
|
| 161 | + Parameters |
| 162 | + ---------- |
| 163 | + None, returns a standard pandas dataframe of all activityMediaName values. |
| 164 | +
|
| 165 | + """ |
| 166 | + df = _get_codeservice(input_name="samplemedia") |
| 167 | + |
| 168 | + df.rename(columns={'activityMedia': 'activityMediaName'}, |
| 169 | + inplace=True) |
| 170 | + |
| 171 | + df = df[['activityMediaName']] |
| 172 | + |
| 173 | + return df |
| 174 | + |
| 175 | +def characteristicGroup_lookup(): |
| 176 | + """Code service that returns a dataframe of all possible characteristicGroup values |
| 177 | + to be used in `get_USGS_samples()` |
| 178 | +
|
| 179 | + Parameters |
| 180 | + ---------- |
| 181 | + None, returns a standard pandas dataframe of all characteristicGroup values. |
| 182 | +
|
| 183 | + """ |
| 184 | + df = _get_codeservice(input_name="characteristicgroup") |
| 185 | + |
| 186 | + return df |
| 187 | + |
| 188 | +def characteristic_lookup(): |
| 189 | + """Code service that returns a dataframe of all possible characteristic values, |
| 190 | + USGS pcodes, and their associated characteristicGroup to be used in `get_USGS_samples()` |
| 191 | +
|
| 192 | + Parameters |
| 193 | + ---------- |
| 194 | + None, returns a standard pandas dataframe of all characteristic values. |
| 195 | +
|
| 196 | + """ |
| 197 | + df = _get_codeservice(input_name="characteristics") |
| 198 | + |
| 199 | + df.rename(columns={'parameterCode': 'usgsPCode'}, |
| 200 | + inplace=True) |
| 201 | + |
| 202 | + return df |
| 203 | + |
72 | 204 | def get_USGS_samples( |
73 | 205 | ssl_check=True, |
74 | 206 | service="results", |
|
0 commit comments