Skip to content

Instantly share code, notes, and snippets.

View bennyistanto's full-sized avatar

Benny Istanto bennyistanto

View GitHub Profile
@bennyistanto
bennyistanto / moving_window.md
Created July 2, 2024 07:18
The moving window approach is used to balance the advantages of capturing both spatial and temporal variations in precipitation patterns

Moving Window illustration

You can paste the below code into an online Python compiler like https://python-fiddle.com/ and grab the result instantly.

This script will generate a plot that illustrates:

  1. This highlighting the spatial moving window on the top layer. The selected pixel at (4,5) will be red, and the 24 surrounding pixels within the 5x5 window will be pink.
  2. This illustrate how the temporal moving window considers data from multiple layers (time steps) when smoothing the value for a single pixel.

The moving window approach is used to balance the advantages of capturing both spatial and temporal variations in precipitation patterns. By considering both spatial and temporal windows, the bias correction method can address local variations and seasonal changes more effectively.

LSEQM Bias Correction

Bias correction of satellite-based precipitation estimates is crucial in climate studies to enhance their accuracy and reliability. This exercise explores a method that combines Linear Scaling (LS) and Empirical Quantile Mapping (EQM) with tail adjustment using the Generalized Pareto Distribution (GPD). This hybrid approach aims to correct both the mean bias and the distributional discrepancies between satellite estimates and ground-based observations.

lseqm_xkcd_style_b

The following sections will describe the methodology, the role of different functions, their strengths, and weaknesses in achieving accurate bias correction.

1 Linear Scaling Approach

@bennyistanto
bennyistanto / flood_probability_model.md
Created June 25, 2024 05:58
Visual representation of flood probability model, illustrating the different scenarios

Visual representation of flood probability model

You can paste the below code into an online Python compiler like https://python-fiddle.com/ and grab the result instantly.

This script will generate a plot that illustrates:

  1. The original model (blue line).
  2. A scenario with a steeper slope (red line), showing a more rapid increase in flood risk with rainfall.
  3. A scenario with a gentler slope (green line), showing a more gradual increase in risk.
  4. A scenario with a less negative intercept (purple line), resulting in higher probabilities at lower rainfall amounts.
@bennyistanto
bennyistanto / gev_threshold.py
Created June 23, 2024 04:17
Simple Generalized Extreme Value (GEV) from set of data
# Copy and paste the code below into https://python-fiddle.com/
# You will get instant result: GEV 25-year threshold: 96.12570045491432
import numpy as np
from scipy.stats import genextreme
# This data is the maximum daily precipitation in a month (let say May) for the period
# 2001-2023
data = [
35.809994, 55.120003, 97.575005, 17.01, 43.260006, 50.285004, 51.489998, 59.05999,
58.339996, 82.835, 42.875, 55.574997, 105.149994, 61.155, 83.68001, 73.56,
@bennyistanto
bennyistanto / precip_pettitt.md
Last active June 12, 2024 13:46
Simulate how Pettitt test detects inhomogeneous or homogeneous on precipitation data

xkcd-style Pettitt's illustration on Homogeneity testing

The script below try to simulate how Pettitt test detects inhomogeneous or homogeneous on precipitation data. Here's the updated approach:

  1. Daily Precipitation Data:

    • We have daily precipitation.
  2. Generate Monthly Maximum Precipitation Values:

  • Create synthetic monthly maxima with distinct differences to ensure inhomogeneity.
@bennyistanto
bennyistanto / imerg_thresholdbymonth.py
Created June 8, 2024 07:04
Calculates the thresholds for extreme rainfall events using both percentile-based and Generalized Extreme Value (GEV) methods
import xarray as xr
import numpy as np
import os
import glob
import pandas as pd
from tqdm import tqdm
from scipy.stats import genextreme
# Define directories and filenames for threshold calculations
threshold_calculations = [
@bennyistanto
bennyistanto / imerg_dailymaxbydekad.py
Last active June 8, 2024 12:48
Computes the maximum precipitation values for each dekad from the daily and multi-day accumulated datasets
import xarray as xr
import numpy as np
import os
import glob
import pandas as pd
from tqdm import tqdm
# Define the input and output directories
max_calculations = [
{"days": 1, "input_dir": "/mnt/d/temp/imerg/data/fdd_1day", "output_dir": "/mnt/d/temp/imerg/data/fddmaxd_1day"},
@bennyistanto
bennyistanto / imerg_dailymaxbymonth.py
Created June 8, 2024 07:01
Computes the maximum precipitation values for each month from the daily and multi-day accumulated datasets
import xarray as xr
import os
import glob
from tqdm import tqdm
import pandas as pd
# Define directories and filenames for max value calculations
max_calculations = [
{"days": 1, "input_dir": "/mnt/d/temp/imerg/data/fdd_1day", "output_dir": "/mnt/d/temp/imerg/data/fddmaxm_1day"},
{"days": 2, "input_dir": "/mnt/d/temp/imerg/data/fdd_2day", "output_dir": "/mnt/d/temp/imerg/data/fddmaxm_2day"},
@bennyistanto
bennyistanto / imerg_rollingdays.py
Created June 8, 2024 06:59
Generating IMERG 2-day, 3-day, 4-day, and 5-day rolling accumulations
import xarray as xr
import numpy as np
import os
import glob
from tqdm import tqdm
import pandas as pd
# Define the rolling day output directories
rolling_days = [2, 3, 4, 5]
input_dir = "/mnt/d/temp/imerg/data/fdd_1day"
@bennyistanto
bennyistanto / imerg_precipextract.py
Created June 8, 2024 06:57
Extract IMERG precipitation variable
import xarray as xr
import os
import glob
from tqdm import tqdm
# Define the input and output directories
input_dir = "/mnt/d/temp/imerg/data/nc4_final"
output_dir = "/mnt/d/temp/imerg/data/fdd_1day"
# Check if input folder exists