Skip to content

Commit

Permalink
Staging (#330)
Browse files Browse the repository at this point in the history
* [WPS-69]: use 512px tile sizes for mapbox

* [WPSI-62]: implementing google action
  • Loading branch information
carloshdelreal authored Nov 9, 2024
1 parent a8c1e8e commit 985c221
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 7 deletions.
76 changes: 76 additions & 0 deletions .github/workflows/cron_update_SPI24.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Update 24-month SPI Anomaly (Long-term)

on:
workflow_dispatch:
# Allows manual triggering of the action
schedule:
- cron: '0 12 * * *' # Runs daily at 12:00 UTC, adjust as needed

jobs:
spi-24-month:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4
- uses: actions/setup-python@v3
with:
python-version: '3.13'

- name: Upgrade pip & Install dependencies
run: |
python -m pip install --upgrade pip
echo "Installing Python dependencies"
if [ -f ./python/requirements.txt ]; then pip install -r ./python/requirements.txt; fi
pip install rasterio
pip install earthengine-api
pip install earthengine-api --upgrade
# Get file source name and put it on env
# - name: Run Python Script on File
# run: |
# python python/getFilename24month.py

# # Copy from Google storage to local
# - name: Copy from Google storage to local
# uses: actions-hub/gcloud@master
# env:
# PROJECT_ID: ${{secrets.GCP_PROJECT_ID}}
# APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
# with:
# args: cp $WPS_24_MONTH_GS $WPS_24_MONTH_FILENAME
# cli: gsutil

# # Get file source name and put it on env
# - name: Project downloaded file
# run: |
# python python/project.py

# # Upload to Google storage
# - name: Upload to Google storage
# uses: actions-hub/gcloud@master
# env:
# PROJECT_ID: ${{secrets.GCP_PROJECT_ID}}
# APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
# with:
# args: cp $WPS_24_MONTH_FILENAME $WPS_24_MONTH_GS_PROJ
# cli: gsutil


- name: 'auth'
uses: 'google-github-actions/auth@v2'
with:
token_format: 'access_token'
project_id: ${{ secrets.GCP_PROJECT_ID }}
workload_identity_provider: 'projects/297470958436/locations/global/workloadIdentityPools/wps-github-action-automations'
service_account: '[email protected]'

- name: 'Use gcloud CLI'
run: 'gcloud info'

# upload to GEE
- name: Upload to GEE
run: |
python python/earthengine.py

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ coverage
.env.local
.env

.secrets

# vscode and vendor cache
.vscode/
untracked
Expand Down
27 changes: 20 additions & 7 deletions app/javascript/components/constants.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
const BASEMAPS = {
dark: {
id: 'dark',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjhqgjq1908ar2smep2wd7wf7/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjhqgjq1908ar2smep2wd7wf7/tiles/512/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
label: 'Dark',
options: {
zoomOffset: -1, tileSize: 512,
attribution: '<a href="https://www.mapbox.com/about/maps/" target="_blank">© Mapbox</a> <a href="http://www.openstreetmap.org/about/" target="_blank">© OpenStreetMap</a>'
}
},
light: {
id: 'light',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjhqgk77j0r7h2sqw220p7imy/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjhqgk77j0r7h2sqw220p7imy/tiles/512/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
label: 'Light',
options: {
zoomOffset: -1, tileSize: 512,
attribution: '<a href="https://www.mapbox.com/about/maps/" target="_blank">© Mapbox</a> <a href="http://www.openstreetmap.org/about/" target="_blank">© OpenStreetMap</a>'
}
},
satellite: {
id: 'satellite',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjhqiecof53wv2rl9gw4cehmy/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjhqiecof53wv2rl9gw4cehmy/tiles/512/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
label: 'Satellite',
options: {
zoomOffset: -1, tileSize: 512,
attribution: '<a href="https://www.mapbox.com/about/maps/" target="_blank">© Mapbox</a> <a href="http://www.openstreetmap.org/about/" target="_blank">© OpenStreetMap</a>'
}
},
terrain: {
id: 'terrain',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjhqi456h02pg2rp6w2mwp61c/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjhqi456h02pg2rp6w2mwp61c/tiles/512/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
label: 'Terrain',
options: {
zoomOffset: -1, tileSize: 512,
attribution: '<a href="https://www.mapbox.com/about/maps/" target="_blank">© Mapbox</a> <a href="http://www.openstreetmap.org/about/" target="_blank">© OpenStreetMap</a>'
}
}
Expand All @@ -42,20 +46,29 @@ const LABELS = {
light: {
id: 'light',
label: 'Labels light',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjgcf9rs05qnu2rrpp4qzucox/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ'
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjgcf9rs05qnu2rrpp4qzucox/tiles/512/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
options: {
zoomOffset: -1, tileSize: 512
}
},
dark: {
id: 'dark',
label: 'Labels dark',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjgcf9gqk9tmm2spd9zr0tml3/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ'
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjgcf9gqk9tmm2spd9zr0tml3/tiles/512/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
options: {
zoomOffset: -1, tileSize: 512
}
}
};

const BOUNDARIES = {
dark: {
id: 'dark',
label: 'Boundaries',
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjgcf8qdaai1x2rn6w3j4q805/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ'
value: 'https://api.mapbox.com/styles/v1/resourcewatch/cjgcf8qdaai1x2rn6w3j4q805/tiles/512/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmVzb3VyY2V3YXRjaCIsImEiOiJjbHVpdTh5N2swMm9sMndwYzZuNnFtY3NkIn0.26SGTMZGMtgNJaonQfiKjQ',
options: {
zoomOffset: -1, tileSize: 512
}
}
};

Expand Down
1 change: 1 addition & 0 deletions python/.run-act.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
act -P ubuntu-latest=ubuntu-builder --pull=false --container-architecture linux/amd64 --secret-file .secrets workflow_dispatch --workflows '.github/workflows/cron_update_SPI24.yml'
1 change: 1 addition & 0 deletions python/automation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("***************HELLOO******************")
3 changes: 3 additions & 0 deletions python/currMonth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from datetime import datetime

print(f"{datetime.now().year}{datetime.now().month}")
10 changes: 10 additions & 0 deletions python/docker/Act.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM ubuntu:20.04

RUN apt-get update \
&& apt-get upgrade -y \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
build-essential \
curl \
nodejs \
npm \
&& rm -rf /var/lib/apt/lists/*
7 changes: 7 additions & 0 deletions python/earthengine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ee

ee.Authenticate()

ee.Initialize(project="wpsi-208318")

print(ee.String('Hello from the Earth Engine servers!').getInfo())
24 changes: 24 additions & 0 deletions python/getFilename24month.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from datetime import datetime
import os

# 24 month

def getPrevMonth(month, delta):
if month+delta <= 0:
return month+delta+12
else:
return (month+delta)%12

current_year_month = f"{datetime.now().year}{getPrevMonth(datetime.now().month, -1):02}"
prev_year_month = f"{datetime.now().year}{getPrevMonth(datetime.now().month, -2):02}"

file = 'ERA5_SPI24_{year_month}'

env_file = os.getenv('GITHUB_ENV') # Get the path of the runner file

# write to the file
with open(env_file, "a") as env_file:
env_file.write(f"WPS_24_MONTH_GS=gs://wps_pillar1a/data_sources/Deltares/Data{current_year_month}/{file.format(year_month=prev_year_month)}.tif\n")
env_file.write(f"WPS_24_MONTH_GS_PROJ=gs://wps_pillar1a/data_sources/Deltares/Data{current_year_month}/proj_{file.format(year_month=prev_year_month)}.tif\n")
env_file.write(f"WPS_24_MONTH_FILENAME={file.format(year_month=prev_year_month)}.tif")

14 changes: 14 additions & 0 deletions python/project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import rasterio
from rasterio.crs import CRS
import os

FILE = os.environ["WPS_24_MONTH_FILENAME"]


if FILE:
print('The filename evn WPS_24_MONTH_FILENAME={FILE} successfully loaded!')
else:
print('Please check the env WPS_24_MONTH_FILENAME was not loaded')

with rasterio.open(FILE, "r+") as rds:
rds.crs = CRS.from_epsg(4326)
13 changes: 13 additions & 0 deletions python/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Using act to locally run github actions

you have to create a local image with the tag ubuntu-builder by running



```bash
docker build -f python/docker/Act.Dockerfile -t ubuntu-builder .
```

```bash
act -P ubuntu-latest=ubuntu-builder --pull=false --container-architecture linux/amd64 --secret-file .secrets workflow_dispatch --workflows '.github/workflows/cron_update_SPI24.yml'
```
4 changes: 4 additions & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
LMIPy==0.6.2
pandas==2.2.3
python-dotenv==1.0.1
Requests==2.32.3

0 comments on commit 985c221

Please sign in to comment.