|
1 | 1 | """ |
2 | | -Tools for retrieving data from the National Atmospheric Deposition Program (NADP) including |
3 | | -the National Trends Network (NTN), the Mercury Deposition Network (MDN). |
| 2 | +Tools for retrieving data from the National Atmospheric Deposition Program |
| 3 | +(NADP) including the National Trends Network (NTN), the Mercury Deposition |
| 4 | +Network (MDN). |
4 | 5 |
|
5 | 6 | National Trends Network |
6 | 7 | ----------------------- |
|
28 | 29 |
|
29 | 30 | """ |
30 | 31 |
|
31 | | -import requests |
32 | | -import zipfile |
33 | 32 | import io |
34 | 33 | import os |
35 | 34 | import re |
| 35 | +import zipfile |
36 | 36 | from os.path import basename |
37 | 37 |
|
| 38 | +import requests |
38 | 39 |
|
39 | 40 | NADP_URL = 'https://nadp.slh.wisc.edu' |
40 | 41 | NADP_MAP_EXT = 'filelib/maps' |
41 | 42 |
|
42 | | -NTN_CONC_PARAMS = ['pH', 'So4', 'NO3', 'NH4', 'Ca', |
43 | | - 'Mg', 'K', 'Na', 'Cl', 'Br'] |
44 | | -NTN_DEP_PARAMS = ['H', 'So4', 'NO3', 'NH4', 'Ca', 'Mg', |
45 | | - 'K', 'Na', 'Cl', 'Br', 'N', 'SPlusN'] |
| 43 | +NTN_CONC_PARAMS = ['pH', 'So4', 'NO3', 'NH4', 'Ca', 'Mg', 'K', 'Na', 'Cl', 'Br'] |
| 44 | +NTN_DEP_PARAMS = [ |
| 45 | + 'H', |
| 46 | + 'So4', |
| 47 | + 'NO3', |
| 48 | + 'NH4', |
| 49 | + 'Ca', |
| 50 | + 'Mg', |
| 51 | + 'K', |
| 52 | + 'Na', |
| 53 | + 'Cl', |
| 54 | + 'Br', |
| 55 | + 'N', |
| 56 | + 'SPlusN', |
| 57 | +] |
46 | 58 |
|
47 | 59 | NTN_MEAS_TYPE = ['conc', 'dep', 'precip'] # concentration or deposition |
48 | 60 |
|
49 | 61 |
|
50 | 62 | class NADP_ZipFile(zipfile.ZipFile): |
51 | | - """Extend zipfile.ZipFile for working on data from NADP |
52 | | - """ |
| 63 | + """Extend zipfile.ZipFile for working on data from NADP""" |
| 64 | + |
53 | 65 | def tif_name(self): |
54 | 66 | """Get the name of the tif file in the zip file.""" |
55 | 67 | filenames = self.namelist() |
56 | | - r = re.compile(".*tif$") |
| 68 | + r = re.compile('.*tif$') |
57 | 69 | tif_list = list(filter(r.match, filenames)) |
58 | 70 | return tif_list[0] |
59 | 71 |
|
@@ -93,23 +105,23 @@ def get_annual_MDN_map(measurement_type, year, path): |
93 | 105 |
|
94 | 106 | >>> # get map of mercury concentration in 2010 and extract it to a path |
95 | 107 | >>> data_path = dataretrieval.nadp.get_annual_MDN_map( |
96 | | - ... measurement_type='conc', year='2010', path='somepath') |
| 108 | + ... measurement_type='conc', year='2010', path='somepath' |
| 109 | + ... ) |
97 | 110 |
|
98 | 111 | """ |
99 | | - url = '{}/{}/MDN/grids/'.format(NADP_URL, NADP_MAP_EXT) |
| 112 | + url = f'{NADP_URL}/{NADP_MAP_EXT}/MDN/grids/' |
100 | 113 |
|
101 | | - filename = 'Hg_{}_{}.zip'.format(measurement_type, year) |
| 114 | + filename = f'Hg_{measurement_type}_{year}.zip' |
102 | 115 |
|
103 | 116 | z = get_zip(url, filename) |
104 | 117 |
|
105 | 118 | if path: |
106 | 119 | z.extractall(path) |
107 | 120 |
|
108 | | - return '{}{}{}'.format(path, os.sep, basename(filename)) |
| 121 | + return f'{path}{os.sep}{basename(filename)}' |
109 | 122 |
|
110 | 123 |
|
111 | | -def get_annual_NTN_map(measurement_type, measurement=None, year=None, |
112 | | - path="."): |
| 124 | +def get_annual_NTN_map(measurement_type, measurement=None, year=None, path='.'): |
113 | 125 | """Download a NTN map from NDAP. |
114 | 126 |
|
115 | 127 | This function looks for a zip file containing gridded information at: |
@@ -146,22 +158,23 @@ def get_annual_NTN_map(measurement_type, measurement=None, year=None, |
146 | 158 |
|
147 | 159 | >>> # get a map of precipitation in 2015 and extract it to a path |
148 | 160 | >>> data_path = dataretrieval.nadp.get_annual_NTN_map( |
149 | | - ... measurement_type='Precip', year='2015', path='somepath') |
| 161 | + ... measurement_type='Precip', year='2015', path='somepath' |
| 162 | + ... ) |
150 | 163 |
|
151 | 164 | """ |
152 | | - url = '{}/{}/NTN/grids/{}/'.format(NADP_URL, NADP_MAP_EXT, year) |
| 165 | + url = f'{NADP_URL}/{NADP_MAP_EXT}/NTN/grids/{year}/' |
153 | 166 |
|
154 | | - filename = '{}_{}.zip'.format(measurement_type, year) |
| 167 | + filename = f'{measurement_type}_{year}.zip' |
155 | 168 |
|
156 | 169 | if measurement: |
157 | | - filename = '{}_{}'.format(measurement, filename) |
| 170 | + filename = f'{measurement}_{filename}' |
158 | 171 |
|
159 | 172 | z = get_zip(url, filename) |
160 | 173 |
|
161 | 174 | if path: |
162 | 175 | z.extractall(path) |
163 | 176 |
|
164 | | - return '{}{}{}'.format(path, os.sep, basename(filename)) |
| 177 | + return f'{path}{os.sep}{basename(filename)}' |
165 | 178 |
|
166 | 179 |
|
167 | 180 | def get_zip(url, filename): |
|
0 commit comments