Skip to content

Commit

Permalink
Merge pull request #115 from Serapieum-of-alex/hm-analysis
Browse files Browse the repository at this point in the history
Hydraulic model analysis scripts
  • Loading branch information
MAfarrag authored Dec 31, 2022
2 parents 50f9075 + 396c62a commit ff66394
Show file tree
Hide file tree
Showing 41 changed files with 193,613 additions and 450,474 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: pypi-package

on:
release:
types: [created]

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.x"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERS }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
14 changes: 7 additions & 7 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ jobs:
run: |
python -c "from osgeo import gdal"
gdalinfo --version
- name: libgeo and cartopy
- name: libgeos and cartopy
run: |
sudo apt -y install libgeos-dev
pip install cartopy
- name: Install dependencies
run: |
#$CONDA/bin/mamba env update --file environment.yml --name base
pip install -r requirements.txt -r requirements-dev.txt
python setup.py install
# - name: Install dependencies
# run: |
# $CONDA/bin/mamba env update --file environment.yml --name base
# python setup.py install
# - name: Lint with flake8
# run: |
# # add CONDA/bin/ in front of everything
Expand All @@ -53,11 +51,13 @@ jobs:
# $CONDA/bin/flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# $CONDA/bin/flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

- name: Generate coverage report
# working-directory: ../
#working-directory: ../
run: |
pwd
python -m pytest -vvv --cov=hapi --cov-report=xml
ls
python -m pytest -vvv --cov=Hapi --cov-report=xml
# - name: Upload coverage to Codecov
# uses: codecov/codecov-action@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,4 @@ examples/Hydrodynamic models/test_case/results/USbnd/right_results/*
examples/Hydrodynamic models/test_case/results/q/New folder/*
examples/Hydrodynamic models/test_case/results/h/New folder/*
examples/Hydrodynamic models/test_case/results/USbnd/New folder/*
tests/mo/*
10 changes: 10 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ History
* fix pypi package names in the requirements.txt file for all internal packages
* fix python version number
* tests are all passing

1.4.0 (2022-12-27)
------------------

* remove fiona and the reading file exception using fion
* unify reading results of rainfall-runoff model in the readRRMResults, ReadLaterals, ReadUSHydrographs
* refactor code and change methods to camelcase
* add hydrodynamic model 1d config file read function
* simplify functions with too many parameters using decorator
* add automatic pypi build and publish github actions
25 changes: 14 additions & 11 deletions Hapi/hm/calibration.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Hydaulic model calibration related function module."""
import datetime as dt
from typing import Any, Union # , Optional,Tuple,
from typing import Any, Union

import fiona
import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -167,8 +166,12 @@ def readGaugesTable(self, path: str):
"""
try:
self.hm_gauges = gpd.read_file(path, driver="GeoJSON")
except fiona.errors.DriverError:
except Exception as e:
logger.warning(
f"the {path} could not be opened with geopandas and will be opened with pandas instead"
)
self.hm_gauges = pd.read_csv(path)

# sort the gauges table based on the segment
self.hm_gauges.sort_values(by="id", inplace=True, ignore_index=True)

Expand Down Expand Up @@ -514,7 +517,7 @@ def readRRM(
for i in range(len(gauges)):
station_id = gauges[i]
try:
self.q_rrm[station_id] = self.readRRMResults(
self.q_rrm[station_id] = self._readRRMResults(
self.version,
self.rrmreferenceindex,
path,
Expand All @@ -533,7 +536,7 @@ def readRRM(
for i in range(len(gauges)):
station_id = gauges[i]
try:
self.q_rrm[station_id] = self.readRRMResults(
self.q_rrm[station_id] = self._readRRMResults(
self.version,
self.rrmreferenceindex,
path,
Expand All @@ -542,7 +545,7 @@ def readRRM(
today,
date_format=fmt,
)[station_id].tolist()
self.QRRM2[station_id] = self.readRRMResults(
self.QRRM2[station_id] = self._readRRMResults(
self.version,
self.rrmreferenceindex,
path2,
Expand Down Expand Up @@ -639,7 +642,7 @@ def readHMQ(
# fill non modelled time steps with zeros
for i in range(len(gauges)):
nodeid = gauges[i]
self.q_hm[nodeid] = self.readRRMResults(
self.q_hm[nodeid] = self._readRRMResults(
self.version,
self.ReferenceIndex,
path,
Expand Down Expand Up @@ -738,7 +741,7 @@ def ReadHMWL(
self.WLHM = pd.DataFrame()
for i in range(len(gauges)):
nodeid = gauges[i]
self.WLHM[nodeid] = self.readRRMResults(
self.WLHM[nodeid] = self._readRRMResults(
self.version,
self.ReferenceIndex,
path,
Expand Down Expand Up @@ -1559,8 +1562,8 @@ def Metrics(
for i in range(len(gauges)):
start_date = Metrics.loc[gauges[i], "start"]
end_date = Metrics.loc[gauges[i], "end"]
obs = df1.loc[start_date : end_date, gauges[i]].values.tolist()
sim = df2.loc[start_date : end_date, gauges[i]].values.tolist()
obs = df1.loc[start_date:end_date, gauges[i]].values.tolist()
sim = df2.loc[start_date:end_date, gauges[i]].values.tolist()

# shift hm result
sim[shift:-shift] = sim[0 : -shift * 2]
Expand Down Expand Up @@ -1629,7 +1632,7 @@ def HMvsRRM(
right_index=True,
how="left",
sort=False,
)#.set_geometry("geometry")
)
self.MetricsHMvsRRM.index = self.MetricsHMvsRRM[self.gauge_id_col]
self.MetricsHMvsRRM.index.name = "index"
if isinstance(self.hm_gauges, GeoDataFrame):
Expand Down
3 changes: 2 additions & 1 deletion Hapi/hm/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@


class Inputs(River):
"""Inputs.
"""Hydraulic model Inputs class.
Methods
-------
1- ExtractHydrologicalInputs
2- StatisticalProperties
3- WriteHQFile
Expand Down
16 changes: 8 additions & 8 deletions Hapi/hm/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def readLateralsTable(
def _readRRMwrapper(
self,
table: DataFrame,
fromday: Union[str, int] = "",
today: Union[str, int] = "",
fromday: int = None,
today: int = None,
path: str = "",
date_format: str = "'%Y-%m-%d'",
prefix: str = "lf_xsid",
Expand Down Expand Up @@ -160,7 +160,7 @@ def _readRRMwrapper(

node_ids = table.loc[:, "xsid"].to_list()
fnames = [f"{prefix}{NodeID}" for NodeID in node_ids]
func = self.readRRMResults
func = self._readRRMResults
results = Parallel(n_jobs=cores)(
delayed(func)(
self.version,
Expand All @@ -183,7 +183,7 @@ def _readRRMwrapper(
node_id = table.loc[i, "xsid"]
fname = f"{prefix}{node_id}"

rrm_ts[node_id] = self.readRRMResults(
rrm_ts[node_id] = self._readRRMResults(
self.version,
self.ReferenceIndex,
path,
Expand All @@ -195,9 +195,9 @@ def _readRRMwrapper(
logger.info(f"Lateral file {fname} is read")

rrm_ts["total"] = rrm_ts.sum(axis=1)
if fromday == "":
if not fromday:
fromday = 1
if today == "":
if not today:
today = len(rrm_ts[rrm_ts.columns[0]])

start = self.ReferenceIndex.loc[fromday, "date"]
Expand All @@ -208,8 +208,8 @@ def _readRRMwrapper(

def readLaterals(
self,
fromday: Union[str, int] = "",
today: Union[str, int] = "",
fromday: int = None,
today: int = None,
path: str = "",
date_format: str = "'%Y-%m-%d'",
cores: Optional[Union[int, bool]] = None,
Expand Down
Loading

0 comments on commit ff66394

Please sign in to comment.