Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveBirkinshaw committed Nov 5, 2024
0 parents commit daf0993
Show file tree
Hide file tree
Showing 12 changed files with 522 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM continuumio/miniconda3:4.10.3

RUN mkdir src

WORKDIR src

COPY environment.yml .
RUN conda env update -f environment.yml -n base

COPY data /data

COPY script.py .

CMD [ "python", "script.py"]

27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Green-Areas
This model takes green_area polygons supplied by the user, clips the data to the domain and ensures the data is in the correct projection.
Spatial green areas can be used in the same way. If spatial green-areas are used the polygon must have a "Value" parameter with the parameters associated with this value given in infiltration.csv.

## Description
The CityCAT model can use green_area polygons to determine permeability. This model accepts green_area data in .gpkg format, clips the data to the selected area, and ensures all data is in the same projection. If the file sizes are too large, multiple .gpkgs can be added directly, or zipped.

## Input Parameters
*Permeable_areas
* Description: are the green_areas defined as being polygons (i,e using the supplied data) all permeable or all impermeable? If green_areas is supplied then polygons is used (and output as a parameter) whatever value is input here.

## Input Files (data slots)
* Green_areas
* Description: A .gpkg file of the green_areas (or spatial green_areas). If Spatial green areas are used the polygon must have a "Value" parameter with the parameters associated with this value given in infiltration.csv
* Location: /data/inputs/green_areas
* Boundary
* Description: A .gpkg of the geographical area of interest.
* Location: /data/inputs/boundary
* Parameters
* Description: location and projection
* Location: /data/inputs/parameters

## Outputs
* The model should output should have a single .gpkg file of the chosen area containing the green_areas (or spatial green_areas of interest. If Spatial green areas are used an infiltration.csv is coped from the input data
* Location: /data/outputs/green_areas
* the model should output a greenareas parameter file specfiy how green_areas are used in CityCAT
* Location: /data/outputs/parameters
Binary file added data/inputs/boundary/longbenton.gpkg
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions data/inputs/green_areas/infiltration.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HydrConductivity-cm/hr,1.09,1.09,1.09
WettingFrontSuctionHead-cm,11.01,11.01,11.01
EffectivePorosity,0.412,0.412,0.412
EffectiveSaturation,0.30,0.99,0.30

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PARAMETER,VALUE
COUNTRY,England
LOCATION,Longbenton
PROJECTION,27700
11 changes: 11 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
dependencies:
- python=3.9
- rasterio=1.2.10
- geopandas=0.12.2
- xarray=2023.6.0
- scipy=1.13.0
- pip
- pip:
- netcdf4==1.6.5
- rioxarray==0.15.0
- matplotlib-scalebar==0.7.2
5 changes: 5 additions & 0 deletions infiltration.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HydrConductivity-cm/hr,1.09,1.09,1.09
WettingFrontSuctionHead-cm,11.01,11.01,11.01
EffectivePorosity,0.412,0.412,0.412
EffectiveSaturation,0.30,0.99,0.30

72 changes: 72 additions & 0 deletions model-definition.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
kind: M
api_version: v1beta3
metadata:
display_name: Global Urban_Flooding:Extract Greenspaces
name: citycat-green-areas
publisher: Newcastle University
summary: CityCAT is a tool for modelling, analysis and visualisation of surface water flooding.
source_code: https://github.com/OpenCLIM/citycat-dafni
description: >
CityCAT (City Catchment Analysis Tool) is a unique software tool for modelling, analysis and visualisation of surface water flooding.
CityCAT enables rapid assessment of combined pluvial and fluvial flood risk and allows assessment of the effects of different flood alleviation measures.
This DAFNI model generates input data for CityCAT, runs a simulation and then converts the output data.
All input data is assumed to be projected in [OSGB 1936](https://epsg.io/27700).
The domain is generated either using a boundary polygon or a combination of centroid location and size.
A rainfall total can either be specified directly or extracted from FUTURE-DRAINAGE based on a return period, duration and time horizon.
The storm profile is generated using the FEH summer profile.
The effects of buildings, green areas and inflow boundary conditions can be included.
Results are provided in a range of formats and a metadata JSON file is created which can be used to create DAFNI datasets.
The CityCAT model can use greenspace polygons to determine permeability. This model accepts greenspace data in .gpkg format, clips the data to the
selected area, and ensures all data is in the same projection. If the file sizes are too large, multiple .gpkgs can be added directly, or zipped.
contact_point_name: steve birkinshaw
contact_point_email: s.j.birkinshaw@ncl.ac.uk

spec:
inputs:
parameters:
- name: PERMEABLE_AREAS
title: Permeable areas
type: string
description:
If `PERMEABLE_AREAS` is set to "polygons", polygons from the `green_areas` dataslot are used to define areas which are permeable.
Otherwise, all cells are treated as being "permeable" or "impermeable", depending on the selection.
required: true
default: permeable
options:
- name: polygons
title: Polygons
- name: permeable
title: Permeable
- name: impermeable
title: Impermeable
dataslots:
- name: Boundary File
description:
A .gpkg file containing the boundary of the location of interest.
path: inputs/boundary/
required: false

- name: Green Areas file
description:
This file should be gpkg format and the location of the green areas. Spatial green_areas require a "Value" in the gpkg file and an additional infiltration.csv file
path: inputs/green_areas
required: false

- name: Parameters
description:
All input parameters and their values are stored in a csv file.
path: inputs/parameters/
required: false


outputs:
datasets:
- name: outputs/green_areas/*
type: folder
description:
A gpgk of green_areas data for the city of interest. If spatial green areas are used an additional infiltration.csv file is copied over.

- name: outputs/parameters/*
type: folder
description:
All parameters and their values are stored in a csv file.
208 changes: 208 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
cmd in folder C:\Users\steve\Documents\CityCAT-dafni\global-flood-impacts-spatialGreenAreas
start docker desktop
docker build -t green-areas .
Run container in Docker Desktop. In Images find green-areas. Click on the play button. Then "optional settings" and call "Container name" something like "global-flood-impact-green-areas"
or docker run --name global-flood-impact-green-areas green-areas

docker cp global-flood-impact-green-areas:/data/outputs/green_areas/longbenton.gpkg ./longbenton.gpkg
docker cp global-flood-impact-green-areas:/data/outputs/green_areas/infiltration.csv ./infiltration.csv

docker save -o green-areas.tar green-areas
compress green-areas.tar to green-areas.tar.gz using 7Zip add to archive









superceded stuff
****************

Anaconda3 prompt
conda activate myenv2
cd C:\Users\steve\Documents\CityCAT-dafni\global-flood-impacts-frictioncoeffs-0.0.1
setenv.bat
spyder

in two places:
filename=file_path[0].split("\\")
#filename=file_path[0].split("/")


conda create -n myenv rasterio geopandasconda activate myenv
pip install citycatio
pip install pyogrio
pip install spyder


myenv citycatio files are in C:\Users\steve\anaconda3\envs\myenv\Lib\site-packages\citycatio>

Changes to:
1) run.py. a) different folder structure for parameter.csv and green_areas in DOCKER and NON DOCKER versions and b) xarrays ufunc changed to numpy c) rio.set.crs changed to rio.write.crs d) friction added to model call e) comment out figure creation near end f) read spatial rainfall if rainfall polygons exist. g) add rainfall_polygons to Model call h) read infiltration parameters if file exists and check if "value" exists in greenareas shape file if infiltration parameters exists i) add infiltration parameters to model call j) read reservoir data if it exists k) add reservoir to model call
2) utils.py modify for correct application of friction coeffs and sptial infiltration
3) friction.py allows friction coeffs (updated)
4) rainfall.py linetermination issues with different versions of the same package
5) rainfall_polygons.py change geoseries to GeoDataFrame
6) model.py a) add infiltration_parameters to init and configuration call b) a) add reservoir to init and write definitions
7) green_areas.py choice of green areas or spatial green areas
8) configuration.py a) allow infiltration parameters in init and write b) allow init_surface_water_elv in init and write
9) in inputs/_init_.py add reservoir.py
10) create reserovir.py

python run.py




cmd in folder C:\Users\steve\Documents\CityCAT-dafni\global-flood-impacts-frictioncoeffs-0.0.1
docker build -t friction-coeffs .


docker build -t Global Urban_Flooding:Friction-coeffs .
Run container in Docker Desktop. In Images find friction-coeffs. Click on the play button. Then "optional settings" and call "Container name" something like "friction-coeffs"
A new Container called "fraction-coeffs" is produced.
*** not needed as default **** Enivonment Variables DATA_PATH /data

The Dockerfile spefies that this reads script.py into the src folder and the data into the /data folder in the container. then python script.py is run.

The output is in the Docker container. to view output copy it to a local path
docker cp friction-coeffs:/data/outputs/FrictionCoeffs.txt ./FrictionCoeffs.txt


https://docs.docker.com/guides/walkthroughs/run-a-container/
https://docs.docker.com/reference/cli/docker/container/cp/


docker save -o friction-coeffs.tar friction-coeffs
compress friction-coeffs.tar to friction-coeffs.tar/gz using 7Zip add to achive



set DATA_PATH=C:\Users\steve\Documents\citycat\CityCAT-FrictionCoeffs-Docker\data
set DATA_PATH=C:\Users\steve\Documents\citycat\CityCAT-SecondModel-Docker\data
set NUMBER_TEST=5 or Enivonment Variables NUMBER_TEST 5
docker system prune -a


in run.py

#filename=file_path[0].split("\")
filename=file_path[0].split("\\")




additional environment variables. The others come from the command line in:
https://github.com/OpenCLIM/citycat-dafni/tree/master

NAME=test
OUTPUT_INTERVAL=3600

setenv.bat
set PYTHONUNBUFFERED=1
set RAINFALL_MODE=total_depth
set SIZE=0.1
set DURATION=1
set POST_EVENT_DURATION=0
set TOTAL_DEPTH=40
set RETURN_PERIOD=100
set X=258722
set Y=665028
set OPEN_BOUNDARIES=True
set PERMEABLE_AREAS=polygons
set ROOF_STORAGE=0
set TIME_HORIZON=2050
set DATA_PATH=C:\Users\steve\Documents\citycat-dafni-0.20.4\data
set NAME=test
set OUTPUT_INTERVAL=3600


C:\Users\steve\anaconda3\envs\myenv\lib\site-packages\citycatio\inputs\rainfall.py:37: FutureWarning: DataFrame.applymap has been deprecated. Use DataFrame.map instead.
self.data.applymap(float_to_str).to_csv(f, sep=' ', header=False, line_terminator='\n')


In rainfall.py
self.data.applymap(float_to_str).to_csv(f, sep=' ', header=False, line_terminator='\n')
self.data.applymap(float_to_str).to_csv(f, sep=' ', header=False, lineterminator='\n')



velocity = xr.ufuncs.sqrt(a.x_vel**2+a.y_vel**2).astype(np.float64)
Xarray’s ufuncs have been removed, now that they can be replaced by numpy’s ufuncs in all supported versions of numpy.

#velocity = xr.ufuncs.sqrt(a.x_vel**2+a.y_vel**2).astype(np.float64)
velocity = np.sqrt(a.x_vel**2+a.y_vel**2).astype(np.float64)
max_velocity = max_velocity.where(np.isfinite(max_velocity), other=output.fill_value)
#max_velocity = max_velocity.where(xr.ufuncs.isfinite(max_velocity), other=output.fill_value)
#max_velocity.rio.set_crs('EPSG:27700')
max_velocity.rio.write_crs('EPSG:27700')
max_vd_product = max_vd_product.where(np.isfinite(max_vd_product), other=output.fill_value)
#max_vd_product = max_vd_product.where(xr.ufuncs.isfinite(max_vd_product), other=output.fill_value)
#max_vd_product.rio.set_crs('EPSG:27700')
max_vd_product.rio.write_crs('EPSG:27700')


C:\Users\steve\anaconda3\envs\myenv\lib\site-packages\citycatio\utils.py

add

def geoseries_with_value_to_string(geoseries: gpd.GeoSeries, value, index=False, index_first=True):
"""GeoSeries to CityCAT string representation

Args:
geoseries: Polygons to convert
value: Fraction coefficient value
index: Whether or not to include the index
index_first: Whether or not to place the index before the number of points
Returns:
s (str): String representation readable by CityCAT

"""
assert (geoseries.geom_type == 'Polygon').all(), 'Geometries must be of type Polygon'

s = '{}\n'.format(len(geoseries))

for idx, geometry in geoseries.items():
if not index:
s += '{}'.format(len(geometry.exterior.coords))
elif index_first:
s += '{} {}'.format(idx, len(geometry.exterior.coords))
else:
s += '{} {}'.format(len(geometry.exterior.coords), value[idx])
x, y = geometry.exterior.coords.xy
for x_val in x:
s += ' {}'.format(x_val)
for y_val in y:
s += ' {}'.format(y_val)
s += '\n'

return s

C:\Users\steve\anaconda3\envs\myenv\lib\site-packages\citycatio\inputs\friction.py

from ..utils import geoseries_with_value_to_string

def write(self, path):
with open(os.path.join(path, 'FrictionCoeffs.txt'), 'w') as f:
f.write(geoseries_with_value_to_string(self.data.geometry,self.data.Value,index=True, index_first=False))


plus add frction stuff to run.py


myenv
C:\Users\steve\anaconda3\envs\myenv\Lib\site-packages\citycatio>

# If a parameter.csv is available: read the variables from the document
if len(parameter_file) == 1 :
file_path = os.path.splitext(parameter_file[0])
print('Filepath:',file_path)
#filename=file_path[0].split("\")
filename=file_path[0].split("\\")
print('Filename:',filename[-1])



Loading

0 comments on commit daf0993

Please sign in to comment.