Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CloudStorage #34

Merged
merged 15 commits into from
Nov 9, 2023
2 changes: 1 addition & 1 deletion .typos.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[files]
extend-exclude = ["*.csv", "*.json", "*.ipynb"]
extend-exclude = ["*.csv", "*.json", "*.ipynb", "cloud.py"]
67 changes: 24 additions & 43 deletions docs/thegoodcloud.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,41 @@
# Connecting The Good Cloud

## Modules
Just `os`, `pathlib`, `requests` are required to get started
## OS environment variables
We recommend to set the following os environment variables:
- `RIBASIM_NL_CLOUD_PASS`: password for the cloud, to be requested at Deltares
- `RIBASIM_DATA_DIR`: directory with your local copy of data in de RIBASM_nl cloud

## Init the cloud
Import the cloud and initialize it
```
import os
from pathlib import Path
import requests
from ribasim_nl import Cloud
```

## global variables
We define a few global variables, `RIBASIM_NL_CLOUD_PASS` is to be supplied as an OOS environment variable.
You need to get one from Deltares first.

If you have set os environment variables:
```
RIBASIM_NL_CLOUD_PASS = os.getenv("RIBASIM_NL_CLOUD_PASS")
RIBASIM_NL_CLOUD_USER = "nhi_api"
WEBDAV_URL = "https://deltares.thegood.cloud/remote.php/dav"
BASE_URL = f"{WEBDAV_URL}/files/{RIBASIM_NL_CLOUD_USER}/D-HYDRO modeldata"

try:
assert RIBASIM_NL_CLOUD_PASS is not None
except AssertionError:
raise ValueError(
"Put RIBASIM_NL_CLOUD_PASS in your OS environment variables first."
)
cloud = Cloud()
```

## Local path and remote URL
An example-file, `my_file.ext` to upload. Please be aware to use a file_name without spaces (`" "`)
And else
```
path = Path("my_file.ext")
url = f"{BASE_URL}/test_files/{path.name}"
cloud = Cloud(password=password, data_dir=my_data_dir)
```

## Uploading a file


## Find water authorities
To find available water authorities:
```
cloud.water_authorities
```
def upload_file(url, path):
with open(path, "rb") as f:
r = requests.put(
url, data=f, auth=(RIBASIM_NL_CLOUD_USER, RIBASIM_NL_CLOUD_PASS)
)
r.raise_for_status()

upload_file(url, path)
## Download water authority data-sets
```
authority = "Rijkswaterstaat"

## Downloading a file
# to download external data (aangeleverd) only
cloud.download_aangeleverd(authority)

```
def download_file(url, path):
r = requests.get(url, auth=(RIBASIM_NL_CLOUD_USER, RIBASIM_NL_CLOUD_PASS))
r.raise_for_status()
with open(path, "wb") as f:
f.write(r.content)
# to download manipulated data (verwerkt) only
cloud.download_verwerkt(authority)

download_file(url, path)
```
# to download all
cloud.download_all(authority)
3 changes: 2 additions & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ lint = { depends_on = [
# Test
test-hydamo = "pytest --numprocesses=auto --basetemp=src/hydamo/tests/temp src/hydamo/tests"
test-ribasim_nl = "pytest --numprocesses=auto --basetemp=src/ribasim_nl/tests/temp src/ribasim_nl/tests"
test-hydamo-cov = "pytest --numprocesses=auto --cov=hydamo --cov-report=xml src/hydamo/tests"
test-hydamo-cov = "pytest --numprocesses=auto --cov=hydamo --cov-report=xml --cov-report=term-missing src/hydamo/tests"
test-ribasim_nl-cov = "pytest --numprocesses=auto --cov=ribasim_nl --cov-report=xml --cov-report=term-missing src/ribasim_nl/tests"
tests = { depends_on = [
"lint",
"test-hydamo",
Expand Down
60 changes: 2 additions & 58 deletions src/hydamo/tests/test_datamodel.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
import os
from pathlib import Path

import hydamo
import requests
import shapely

# basic auth for https://deltares.thegood.cloud/
RIBASIM_NL_CLOUD_USER = "nhi_api"
RIBASIM_NL_CLOUD_PASS = os.getenv("RIBASIM_NL_CLOUD_PASS")
assert RIBASIM_NL_CLOUD_PASS is not None
WEBDAV_URL = "https://deltares.thegood.cloud/remote.php/dav"
BASE_URL = f"{WEBDAV_URL}/files/{RIBASIM_NL_CLOUD_USER}/D-HYDRO modeldata"


def download_file(url, path):
r = requests.get(url, auth=(RIBASIM_NL_CLOUD_USER, RIBASIM_NL_CLOUD_PASS))
r.raise_for_status()
with open(path, "wb") as f:
f.write(r.content)


def upload_file(url, path):
with open(path, "rb") as f:
r = requests.put(
url, data=f, auth=(RIBASIM_NL_CLOUD_USER, RIBASIM_NL_CLOUD_PASS)
)
r.raise_for_status()


def test_initialize():
damo = hydamo.HyDAMO()
assert damo.version == "2.2"
damo = hydamo.HyDAMO(version="2.2.1")
assert damo.version == "2.2.1"
assert "stuw" in damo.layers
assert isinstance(damo.stuw, hydamo.ExtendedGeoDataFrame)
assert "kruinbreedte" in damo.stuw.columns
Expand All @@ -40,34 +15,3 @@ def test_initialize():
path = Path("hydamo.gpkg")
damo.to_geopackage(path)
assert not path.is_file()


def test_download(tmp_path):
"""Download a HyDAMO GeoPackage, check contents and upload the export"""
# download file
filename = "uitlaten_inlaten.gpkg"
url = f"{BASE_URL}/HyDAMO_geconstrueerd/{filename}"
path = tmp_path / filename
download_file(url, path)
assert path.is_file()

# load the data
damo = hydamo.HyDAMO.from_geopackage(path)
assert damo.data_layers == ["gemaal", "stuw"]
# check the content
rozema = damo.gemaal.loc[damo.gemaal.naam == "Gemaal Rozema"].squeeze()
assert rozema.code == "KGM-O-11700"
rozema.geometry
assert isinstance(rozema.geometry, shapely.Point)

assert not damo.stuw.empty
assert "CASPARGOUW STUW" in damo.stuw.naam.to_numpy()

# export file
to_path = path.with_suffix(".to.gpkg")
damo.to_geopackage(to_path)
assert to_path.is_file()

# upload file
to_url = url = f"{BASE_URL}/HyDAMO_geconstrueerd/{to_path.name}"
upload_file(to_url, to_path)
2 changes: 2 additions & 0 deletions src/ribasim_nl/ribasim_nl/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
__version__ = "0.0.1"

from ribasim_nl.cloud import Cloud # noqa
Loading