In [2]:
import requests
import json
import os
import credentials
In [3]:
baseurl = "https://www.ncdc.noaa.gov/cdo-web/api/v2/"
endpoint_stations = "stations"
dataset = "GSOM"
custom_headers = {
"token": credentials.token
}
all_stations = ["USW00026411",
"USW00025308",
"USW00025309",
"USW00025339",
"USW00025501",
"USW00025503",
"USW00025507",
"USW00025624",
"USW00026451",
"USW00025713",
"USW00026415",
"USW00026425",
"USW00026442",
"USW00026510",
"USW00026528",
"USW00026533",
"USW00026615",
"USW00026616",
"USW00026617",
"USW00027502",]
Get information for all stations in list and write out to JSON file
In [2]:
OUTFN = "AK_NCDC_FirstOrderStations.json"
In [15]:
SAVEDATA = False
stationdata = []
for station in all_stations:
path = os.path.join(endpoint_stations, "GHCND:{}".format(station))
fullbase = requests.compat.urljoin(baseurl, path)
r = requests.get(
fullbase,
headers=custom_headers,
)
stationdata.append(json.loads(r.text))
if SAVEDATA:
with open(OUTFN, "w") as fh:
fh.write(json.dumps(stationdata, indent=2))
In [16]:
with open(OUTFN, "w") as fh:
fh.write(json.dumps(stationdata, indent=2))
Optional: Add short names and then save again
In [17]:
[item["name"] for item in stationdata]
Out[17]:
In [19]:
shortnamedic = {
'FAIRBANKS INTERNATIONAL AIRPORT, AK US': "Fairbanks",
'ANNETTE WEATHER SERVICE OFFICE AIRPORT, AK US': "Annette",
'JUNEAU AIRPORT, AK US': "Juneau",
'YAKUTAT AIRPORT, AK US': "Yakutat",
'KODIAK AIRPORT, AK US': "Kodiak",
'KING SALMON AIRPORT, AK US': "King Salmon",
'HOMER AIRPORT, AK US': "Homer",
'COLD BAY AIRPORT, AK US': "Cold Bay",
'ANCHORAGE TED STEVENS INTERNATIONAL AIRPORT, AK US': "Anchorage",
'ST PAUL ISLAND AIRPORT, AK US': "St. Paul",
'BIG DELTA AIRPORT, AK US': "Big Delta",
'GULKANA AIRPORT, AK US': "Gulkana",
'VALDEZ WEATHER SERVICE OFFICE, AK US': "Valdez",
'MCGRATH AIRPORT, AK US': "McGrath",
'TALKEETNA AIRPORT, AK US': "Talkeetna",
'BETTLES AIRPORT, AK US': "Bettles",
'BETHEL AIRPORT, AK US': "Bethel",
'KOTZEBUE RALPH WEIN MEMORIAL AIRPORT, AK ': "Kotzebue",
'NOME MUNICIPAL AIRPORT, AK US': "Nome",
'BARROW W POST W ROGERS AIRPORT, AK US': "Barrow (Utqiaġvik)"}
In [21]:
for item in stationdata:
item['shortname'] = shortnamedic[item['name']]
In [22]:
with open(OUTFN, "w") as fh:
fh.write(json.dumps(stationdata, indent=2))