Skip to content

Instantly share code, notes, and snippets.

View bennyistanto's full-sized avatar

Benny Istanto bennyistanto

View GitHub Profile
@bennyistanto
bennyistanto / data_ppt.sh
Created October 10, 2022 14:21
TerraClimate's precipitation
wget -c http://thredds.northwestknowledge.net:8080/thredds/fileServer/TERRACLIMATE_ALL/data/TerraClimate_ppt_1958.nc
wget -c http://thredds.northwestknowledge.net:8080/thredds/fileServer/TERRACLIMATE_ALL/data/TerraClimate_ppt_1959.nc
wget -c http://thredds.northwestknowledge.net:8080/thredds/fileServer/TERRACLIMATE_ALL/data/TerraClimate_ppt_1960.nc
wget -c http://thredds.northwestknowledge.net:8080/thredds/fileServer/TERRACLIMATE_ALL/data/TerraClimate_ppt_1961.nc
wget -c http://thredds.northwestknowledge.net:8080/thredds/fileServer/TERRACLIMATE_ALL/data/TerraClimate_ppt_1962.nc
wget -c http://thredds.northwestknowledge.net:8080/thredds/fileServer/TERRACLIMATE_ALL/data/TerraClimate_ppt_1963.nc
wget -c http://thredds.northwestknowledge.net:8080/thredds/fileServer/TERRACLIMATE_ALL/data/TerraClimate_ppt_1964.nc
wget -c http://thredds.northwestknowledge.net:8080/thredds/fileServer/TERRACLIMATE_ALL/data/TerraClimate_ppt_1965.nc
wget -c http://thredds.northwestknowledge.net:8080/thredds/fileServer/TERRACLIMATE_ALL/d
@bennyistanto
bennyistanto / focal_regression.py
Created December 24, 2022 13:24
Focal regression using python
"""
This code uses the rasterio library to open the two input rasters and the scipy library
to perform the focal regression using the generic_filter function. The size parameter
specifies the size of the neighborhood around each pixel to use for the regression,
in this case a 3x3 window. The extra_arguments parameter is used to pass the second raster
to the np.polyfit function, which fits a polynomial of degree 1 (a linear regression) to
the two rasters. The result is a 3D array with the slope and intercept values for each pixel.
The code then extracts the slope and intercept arrays from the result and saves them to file
using rasterio.
@bennyistanto
bennyistanto / clip_netcdf_with_shp.py
Created January 5, 2023 18:01
Batch clip NetCDF in a folder with a shapefile
# -*- coding: utf-8 -*-
"""
NAME
clip_netcdf_with_shp.py
Batch clip NetCDF in a folder with a shapefile
DESCRIPTION
Input data for this script will be NetCDF files and a shapefile in a folder
This script can do batch clipping of NetCDF files, and save it in seperate folder
REQUIREMENT
It required glob, xarray, rasterio and shapely module. So it will work on any machine environment
@bennyistanto
bennyistanto / taylor_diagram_outmetrics.py
Last active January 20, 2023 14:05
Generate Taylor Diagram from the output metrics of corrected data to the source
# -*- coding: utf-8 -*-
"""
NAME
taylor_diagram_outmetrics.py
Generate Taylor Diagram from all metrics output
DESCRIPTION
Input data for this script will use metric.csv generated by imerg_cpc_biascorrection.py.
REQUIREMENT
It required numpy, pandas, metpy, matplotlib and xarray module. So it will work on any machine environment
EXAMPLES
@bennyistanto
bennyistanto / wide-to-long.py
Created January 6, 2023 15:52
Reshape wide column structure to long following Stata-style CSV
import pandas as pd
# Load the data
min_df = pd.read_csv("min.csv")
mean_df = pd.read_csv("mean.csv")
max_df = pd.read_csv("max.csv")
# Melt the data frames to long format
min_long = pd.melt(min_df, id_vars=["lon", "lat", "orig_id"], var_name="date", value_name="min")
mean_long = pd.melt(mean_df, id_vars=["lon", "lat", "orig_id"], var_name="date", value_name="mean")
@bennyistanto
bennyistanto / long-to-wide.py
Created January 6, 2023 15:58
Reshape long Stata-style structure to wide column structure
import pandas as pd
# Load the data from the output CSV file
df = pd.read_csv("output.csv")
# Pivot the data frame to wide format for the min variable
min_wide = df.pivot_table(index=["lon", "lat", "orig_id", "year", "month"], columns="variable", values="value")
# Reset the index to obtain a flat data frame
min_wide = min_wide.reset_index()
@bennyistanto
bennyistanto / imerg_cpc_biascorrection.py
Last active January 21, 2023 13:15
Bias correction using various methods, the input come from IMERG and CPC Gauge
# -*- coding: utf-8 -*-
"""
NAME
imerg_cpc_biascorrection.py
DESCRIPTION
Bias correction using various methods:
i. Scale,
ii. Distribution,
iii. Delta,
iv. the Least Squares Composite differencing (LSC)
@bennyistanto
bennyistanto / ensemble_corrected_data.py
Last active February 23, 2023 03:30
Ensemble bias correction output using various methods
# -*- coding: utf-8 -*-
"""
NAME
ensemble_corrected_data.py
DESCRIPTION
Ensemble bias correction output using various methods:
i. Decision Tree
ii. Bayesian
iii. Random Forest
iv. Neural Network
@bennyistanto
bennyistanto / 02_mxd13q1_stats_8day.py
Last active March 13, 2024 08:16
MXD13Q1 8-days statistics data, long-term average, max, min and stdev
# -*- coding: utf-8 -*-
"""
NAME
02_mxd13q1_stats_8day.py
MXD13Q1 8-days statistics data, long-term average, max, min and stdev
DESCRIPTION
Input data for this script will use MXD13Q1 8-days data generate from GEE or downloaded from NASA
This script can do 8-days statistics calculation (AVERAGE, MAXIMUM, MINIMUM and STD)
REQUIREMENT
ArcGIS must installed before using this script, as it required arcpy module.
@bennyistanto
bennyistanto / spei_monthly_blend.py
Last active March 30, 2023 23:43
Global TerraClimate's SPEI-based drought indicators blend
# -*- coding: utf-8 -*-
"""
NAME
spei_monthly_blend.py
Global TerraClimate's SPEI-based dry/wet indicators blend.
DESCRIPTION
Input data for this script will use TerraClimate SPEI monthly data in GeoTIFF format
This experimental SPEI blends integrate several SPEI scales into a single product.
The combines 3-, 6-, 9-, 12-, and 24-month SPEI to estimate the overall dry/wet conditions.
METHOD