Skip to content

Commit

Permalink
copy entire water module
Browse files Browse the repository at this point in the history
  • Loading branch information
jmunroe committed Nov 21, 2024
1 parent 819ebfa commit 2f08606
Show file tree
Hide file tree
Showing 66 changed files with 100,108 additions and 0 deletions.
973 changes: 973 additions & 0 deletions 2i2c/m101-wsim-gldas_2i2c.qmd

Large diffs are not rendered by default.

575 changes: 575 additions & 0 deletions 2i2c/m102-lance-modis-nrt-global-flood_2i2c.qmd

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM quay.io/isciences/tops-school:latest

USER root
RUN pip install nbgitpuller
USER rstudio
31 changes: 31 additions & 0 deletions Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Get the rocker image we want
FROM rocker/geospatial:latest

# copy the whole repo to the image
ADD . /home/rstudio/TOPSTSCHOOL-MODULE-1-WATER

## give the rstudio user permissions on these files
RUN chown -R rstudio /home/rstudio/TOPSTSCHOOL-MODULE-1-WATER

# Add packages not part of rocker/geospatial
RUN Rscript -e "install.packages('cubelyr')"
RUN Rscript -e "install.packages('exactextractr')"
RUN Rscript -e "install.packages('basemaps')"
RUN Rscript -e "install.packages('shiny')"

# Python Environment
## python and pip
RUN apt-get update
RUN apt-get install -y python3
RUN apt-get install -y python3-pip
## python geospatial
RUN pip install geopandas pyproj shapely xarray rioxarray
RUN pip install rasterio netcdf4 h5netcdf dask bottleneck
RUN pip install numpy pandas nc-time-axis requests
## exactextract
### dependencies
RUN pip install CMake pybind11 GEOS
### latest on git
RUN pip install git+https://github.com/isciences/exactextract.git

CMD ["/init"]
27 changes: 27 additions & 0 deletions Dockerfile-ws
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Get the rocker image we want
FROM rocker/binder:latest

# Add packages not part of rocker/geospatial
RUN Rscript -e "install.packages('cubelyr')"
RUN Rscript -e "install.packages('exactextractr')"
RUN Rscript -e "install.packages('basemaps')"
RUN Rscript -e "install.packages('shiny')"
RUN Rscript -e "install.packages('data.table')"
RUN Rscript -e "install.packages('kableExtra')"
RUN Rscript -e "install.packages('aws.s3')"

# copy the lesson contents into the image
## WSIM lessons
# ADD m101a-wsim-gldas-acquisition.qmd /home/rstudio/
# ADD m101b-wsim-gldas-vis.qmd /home/rstudio/
# ADD m101b-wsim-vis-python.qmd /home/rstudio
## MODIS NRT LESSON
# ADD m102-lance-modis-nrt-global-flood.qmd /home/rstudio

## Copy the shiny app
# ADD /m103-nyc-lead home/rstudio

## give the rstudio user permissions on these files
# RUN chown -R rstudio /home/rstudio/

CMD ["/init"]
47 changes: 47 additions & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
project:
title: "SCHOOL Water Module"
type: website
output-dir: docs
format:
html:
embed-resources: true
theme: sandstone
css: "https://raw.githubusercontent.com/ciesin-geospatial/TOPSTSCHOOL/main/custom.scss"
toc: true
toc-title: Contents
toc-location: left
code-overflow: wrap
lightbox: true
execute:
warning: false

website:
navbar:
background: primary
search: true
left:
- text: "Home"
url: https://ciesin-geospatial.github.io/TOPSTSCHOOL-water/index.html
- text: "Lessons"
menu:
- text: "Module Prerequisites and GLossary"
url: https://ciesin-geospatial.github.io/TOPSTSCHOOL-water/m0-prereq-glossary.html
- text: "WSIM-GLDAS: Acquisition, Exploration, and Integration"
url: https://ciesin-geospatial.github.io/TOPSTSCHOOL-water/m101-wsim-gldas.html
- text: "MODIS NRT Flood Data Acquisition Exploration"
url: https://ciesin-geospatial.github.io/TOPSTSCHOOL-water/m102-lance-modis-nrt-global-flood.html
- text: "Interactive NYC School Lead Data Explorer"
url: https://topstschool.shinyapps.io/nyc-lead/
tools:
- icon: github
menu:
- text: Source Code
url: https://github.com/ciesin-geospatial/TOPSTSCHOOL
- text: Report a Bug
url: https://github.com/ciesin-geospatial/TOPSTSCHOOL/issues
- icon: globe
href: https://ciesin-geospatial.github.io/TOPSTSCHOOL/
text: "TOPSTSCHOOL GitHub Home Page"
- icon: youtube
href: https://www.youtube.com/channel/UCOIrczFd7_ht2bNUQ3qnM8w
text: "Youtube Channel"
1,388 changes: 1,388 additions & 0 deletions amerigeo-inundaciones-vulnerabilidad.ipynb

Large diffs are not rendered by default.

1,364 changes: 1,364 additions & 0 deletions amerigeo-inundaciones-vulnerabilidad.qmd

Large diffs are not rendered by default.

234 changes: 234 additions & 0 deletions deprecated/m101b-wsim-gldas-vis-python.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
---
title: "Untitled"
format: html
jupyter: python3
---
## Read in WSIM-GLDAS

```{python}
import xarray as xr
file_path = "composite_12mo.nc"
wsim_gldas = xr.open_dataset(file_path, engine = 'h5netcdf')
```

## Temporal/Variable Subsetting
```{python}
import pandas as pd
wsim_gldas
keeps = pd.date_range(start="2000-12-01", end="2014-12-01", freq = "YS-DEC")
keeps
wsim_gldas = wsim_gldas.sel(time= keeps)
wsim_gldas = wsim_gldas[["deficit", "crs"]]
wsim_gldas
```

```{python}
p = wsim_gldas.deficit.plot(x="lon", y="lat", col="time", col_wrap = 3, cmap = "Reds_r", aspect = 2)
```

## Read in geoBoundaries

```{python}
import requests
import geopandas as gp
usa = requests.get("https://www.geoboundaries.org/api/current/gbOpen/USA/ADM1/")
usa = usa.json()
usa['gjDownloadURL']
usa = gp.read_file(usa['gjDownloadURL'])
drops = ["Alaska", "Hawaii", "American Samoa", "Puerto Rico", "Commonwealth of the Northern Mariana Islands", "Guam", "United States Virgin Islands"]
usa = usa[~usa.shapeName.isin(drops)]
usa.boundary.plot()
```

```{python}
import rioxarray as rio
wsim_gldas = wsim_gldas.rio.write_crs("epsg: 4326")
wsim_gldas = wsim_gldas.rio.clip(usa.geometry.values)
```

```{python fig.width="100%"}
wsim_gldas.deficit.plot(x="lon", y="lat", col="time", col_wrap = 3, cmap = "Reds_r", aspect = 1.75, size =1.25)
```

## Extract the California Counties
```{python}
usa_counties = requests.get("https://www.geoboundaries.org/api/current/gbOpen/USA/ADM2/")
usa_counties = usa_counties.json()
usa_counties['gjDownloadURL']
usa_counties = gp.read_file(usa_counties['gjDownloadURL'])
california = usa[usa["shapeName"].str.contains("California")]
california_counties = usa_counties.overlay(california, how='intersection')
california_counties.plot()
```

## WSIM-GLDAS 1-Month Integration
```{python}
file_path = "composite_1mo.nc"
wsim_gldas_1mo = xr.open_dataset(file_path, engine = 'h5netcdf')
```

## Temporal/Variable Subsetting
```{python}
keeps = pd.date_range(start="2014-01-01", end="2014-12-01", freq = "MS")
wsim_gldas_1mo = wsim_gldas_1mo.sel(time= keeps)
wsim_gldas_1mo = wsim_gldas_1mo[["deficit", "crs"]]
wsim_gldas_1mo
```

```{python}
wsim_gldas_1mo = wsim_gldas_1mo.rio.write_crs("epsg: 4326")
wsim_gldas_1mo = wsim_gldas_1mo.rio.clip(california_counties.geometry.values)
```

```{python}
wsim_gldas_1mo.deficit.plot(x="lon", y="lat", col="time", col_wrap = 3, cmap = "Reds_r", aspect = 1, size =2.5)
```

## Choropleths
```{python}
from exactextract import exact_extract
import calendar
cc_summaries = exact_extract(wsim_gldas_1mo.deficit, california_counties, "mean", output = 'pandas', include_cols = "shapeName_1", include_geom = True)
col_names = [["county"], calendar.month_name[1:], ["geometry"]]
col_names = sum(col_names, [])
cc_summaries.columns = col_names
cc_summaries.plot("January", cmap = "Reds_r")
```


## Re-Classify the WSIM-GLDAS Raster

```{python}
import numpy
wsim_bins = [numpy.inf, 0, -3, -5, -10, -20, -40, -60, numpy.NINF]
wsim_class = xr.apply_ufunc(
numpy.digitize,
wsim_gldas_1mo,
wsim_bins)
# wsim_class.where(wsim_class['deficit'] == -9223372036854775808) = 7
```

# GPW

```{python}
file_path = "gpw_v4_population_count_rev11_2015_15_min.tif"
# Open with rioxarray
gpw = rio.open_rasterio(file_path)
```

```{python}
cal_wsim_gpw = exact_extract(
wsim_class.deficit,
california_counties,
['coverage', 'values', 'weights'],
output = 'pandas',
include_geom = False,
weights = gpw)
```

```{python}
explode_cols = list(cal_wsim_gpw.columns)
cal_wsim_gpw = cal_wsim_gpw.explode(explode_cols)
```

```{python}
cal_wsim_cov = cal_wsim_gpw.filter(like = 'coverage', axis = 1)
cal_wsim_cov.columns = calendar.month_name[1:]
cal_wsim_cov= pd.melt(cal_wsim_cov, var_name='month', value_name= 'coverage')
```

```{python}
cal_wsim_val = cal_wsim_gpw.filter(like = 'values', axis = 1)
cal_wsim_val.columns = calendar.month_name[1:]
cal_wsim_val= pd.melt(cal_wsim_val, var_name='month', value_name = 'wsim_class')
```

```{python}
cal_wsim_weight = cal_wsim_gpw.filter(like = 'weight', axis = 1)
cal_wsim_weight.columns = calendar.month_name[1:]
cal_wsim_weight= pd.melt(cal_wsim_weight, var_name='month', value_name = 'cell_pop_count')
```

```{python}
test = pd.concat(
[cal_wsim_cov,
cal_wsim_val["wsim_class"],
cal_wsim_weight["cell_pop_count"]],
axis=1)
```

```{python}
test['wsim_class'].replace(-9223372036854775808, 7)
```

```{python}
test["wsim_class_pop"] = test["coverage"]*test["cell_pop_count"]
test.wsim_class_pop = test['wsim_class_pop'].astype('float').round(0)
test = test.groupby(['month', 'wsim_class'])['wsim_class_pop'].sum().reset_index()
test['month_pop'] = test.groupby(['month'])['wsim_class_pop'].transform('sum')
test['wsim_class_frac'] = test['wsim_class_pop'] / test['month_pop']
test
```

```{python}
test['wsim_class'] = test['wsim_class'].astype('category')
test['wsim_class'] = test['wsim_class'].cat.rename_categories(
{0: "0", 1: "-3", 2: "-5", 3: "-10", 4: "-20", 5: "-40", 6: "-50", 7: "-60"})
```
```{python}
test["month"] = pd.Categorical(test["month"],
categories=["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"],
ordered=True)
leg_colors=['#9B0039',
# -50 to -40
'#D44135',
# -40 to -20
'#FF8D43',
# -20 to -10
'#FFC754',
# -10 to -5
'#FFEDA3',
# -5 to -3
'#fffdc7',
# -3 to 0
'#FFF4C7',
# 0-3
"#FFFFFF"]
```

```{python}
from plotnine import *
(ggplot(test, aes('month', 'wsim_class_frac', fill = 'wsim_class', group='wsim_class'))+
scale_fill_manual(values = leg_colors[::-1])+
geom_bar(stat='identity', position='stack')+
labs(title = "Monthly Fraction of Population Under Water Deficits in California During 2014",
subtitle = "Categorized by Intensity of Deficit Return Period",
x = "",
y = "Fraction of Population*",
caption = "*Population derived from Gridded Population of the World (2015)",
fill = "Deficit Class")+
theme_minimal()+
theme(axis_text_x=element_text(rotation=25, hjust=1))
)
```
1 change: 1 addition & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
search.json
Empty file added docs/.nojekyll
Empty file.
Loading

0 comments on commit 2f08606

Please sign in to comment.