SSM2.0
Download Model Solutions Using Python

HYD 2017
Download multiple files using Python

Download using Python

The following Python script can be used to download selected daily Salish Sea Model 2.0 (SCHISM) outputs for YR2017.

Available model outputs include: VelX, VelY, Salinity, Temperature, and Water Surface Level (WSL). Users can select both the model variables and the range of days to download.

import urllib.request
import os
from html.parser import HTMLParser
from urllib.parse import urlparse


# ==========================================================
# User settings
# ==========================================================

YEAR = 2017

# Select variables to download.
# Available options:
# "velX", "velY", "salinity", "temperature", "out2d"

variables = [
    "velX",
    "velY",
    "salinity",
    "temperature",
    "out2d",
]


# Select the range of days to download.
# Example:
# START_DAY = 1
# END_DAY = 3
# downloads January 1 through January 3.

START_DAY = 1
END_DAY = 3


# ==========================================================
# SSM SCHISM download page
# ==========================================================

page_url = (
    "https://s3.kopah.uw.edu/"
    "ssm-schism/wordpress/"
    f"SSM_SCHISM_{YEAR}.html"
)


# ==========================================================
# Read download links from the webpage
# ==========================================================

class LinkParser(HTMLParser):

    def __init__(self):
        super().__init__()
        self.links = []

    def handle_starttag(self, tag, attrs):

        if tag == "a":

            attrs = dict(attrs)

            if "href" in attrs:
                self.links.append(attrs["href"])


parser = LinkParser()

with urllib.request.urlopen(page_url) as response:

    html = response.read().decode("utf-8")

parser.feed(html)


# ==========================================================
# Download selected model outputs
# ==========================================================

for variable in variables:

    output_dir = os.path.join(
        str(YEAR),
        variable,
        
    )

    os.makedirs(
        output_dir,
        exist_ok=True
    )


    # Select links for this variable
    variable_links = [
        link for link in parser.links
        if f"/{variable}/" in link
    ]


    # Select requested day range
    selected_links = variable_links[
        START_DAY - 1 : END_DAY
    ]


    print(
        f"\n{variable}: "
        f"Day {START_DAY} to Day {END_DAY} "
        f"({len(selected_links)} files)"
    )


    for i, url in enumerate(
        selected_links,
        start=START_DAY
    ):

        filename = os.path.basename(
            urlparse(url).path
        )

        output_file = os.path.join(
            output_dir,
            filename
        )


        # Skip files already downloaded
        if os.path.exists(output_file):

            print(
                f"Skipping {output_file} "
                "- already exists"
            )

            continue


        try:

            print(
                f"Day {i}: "
                f"Downloading {variable}/{filename} ..."
            )

            urllib.request.urlretrieve(
                url,
                output_file
            )


        except Exception as e:

            print(f"Failed: {url}")
            print(e)


print("\nDownload complete.")

Select model outputs

By default, the example downloads all five model-output groups:

variables = [
    "velX",
    "velY",
    "salinity",
    "temperature",
    "out2d",
]

For example, to download only salinity and temperature:

variables = [
    "salinity",
    "temperature",
]

Select days

Use START_DAY and END_DAY to select the desired range of daily model outputs.

For example, to download Day 1 through Day 3:

START_DAY = 1
END_DAY = 3

This downloads the first three daily files for each selected variable.

For example, to download Day 100 through Day 110:

START_DAY = 100
END_DAY = 110

To download the full YR2017 simulation:

START_DAY = 1
END_DAY = 365

Example

To download only Salinity and Temperature for Day 1 through Day 3:

variables = [
    "salinity",
    "temperature",
]

START_DAY = 1
END_DAY = 3

Output directories

Downloaded files will be organized automatically into separate directories:

2017/
+-- velX/
+-- velY/
+-- salinity/
+-- temperature/
+-- out2d/