Download YOPP data from UCAR server
The snippet can be accessed without any authentication.
Authored by
Matt Noojin
Content could not be displayed: it is larger than 1.00 MiB. Options to address this: load it anyway
or
download it
.
import re, requests
UCAR_URL = "https://www2.mmm.ucar.edu/rt/amps/YOPP-SH-WINTER/txtfiles/"
def download_ucar_files():
filenames = []
with open("ucar_source.txt") as file:
source = file.read()
source = source.split("\n")
## Loop through the HTML source
for line in source:
## Find lines with '.txt' -- these are the links
if ".txt" in line:
## Use regular expressions to extract filename
filename = re.search('href=".*txt', line).group()
filename = filename.replace('href="', '')
filename, _ = filename.split(" ")
filenames.append(filename)
## Loop through files
for ind, filename in enumerate(filenames):
try:
## Assemble file URL + download
url = UCAR_URL + filename
response = requests.get(url)
## Write file to current working directory
with open(filename, mode="wb") as file:
file.write(response.content)
print(f"{filename} saved\n{ind}/{len(filenames)} files downloaded")
except Exception as e:
print(f"Could not download {filename}:\n{e}")
continue
if __name__ == "__main__":
download_ucar_files()
Please register or sign in to comment