Skip to content

Instantly share code, notes, and snippets.

View bennyistanto's full-sized avatar

Benny Istanto bennyistanto

View GitHub Profile
@bennyistanto
bennyistanto / Clip_NetCDF_with_SHP.py
Last active February 27, 2024 12:36
Mask NetCDF time series data from a shapefile in Python
#!/usr/bin/python
# Source: https://gis.stackexchange.com/a/354798/97103
import geopandas
import rioxarray
import xarray
from shapely.geometry import mapping
CHIRPS_daily = xarray.open_dataarray('Z:\Temp\CHIRPS\NC\chirps-v2.0.1981.days_p05.nc')
@bennyistanto
bennyistanto / FocalRegression.R
Last active March 16, 2023 00:18
Linear regression between every 5×5 pixels group of cells of two rasters
# Linear regression between every 5×5 pixels group of cells of two rasters
library(raster)
library(gstat)
library(sp)
library(orcutt)
library(lmtest)
library(zoo)
# Set working directory
@bennyistanto
bennyistanto / SPI_based_on_CHIRPS_netCDF.md
Last active February 22, 2021 10:08
Calculate SPI using CHIRPS data

Calculate SPI using dekad rainfall data in netCDF format

Below is another way to acquired different data (CHIRPS) using different tool (CDO - Climate Data Operator) and NCO - netCDF Operator.

Why CHIRPS? Because I want to get higher resolution, more frequent monitoring (updated every dekad ~ 10days), and long-term historical data from 1981 – now.

Lets try!

This step-by-step guide was tested using Macbook Pro, 2.9 GHz 6-Core Intel Core i9, 32 GB 2400 MHz DDR4, running on macOS Catalina 10.15.7

@bennyistanto
bennyistanto / edit_nc.py
Last active February 17, 2024 22:58
Rename variable, dimension and unit in a netcdf file for climate-indices calculation
#!/usr/bin/python
# Rename variable, dimension and unit in a netcdf file for climate-indices calculation
# Source: https://github.com/monocongo/climate_indices/issues/404#issuecomment-735474320
# https://stackoverflow.com/a/55686749
import pandas as pd
import xarray as xr
ds = xr.open_dataset("/Users/bennyistanto/Temp/CHIRTS/nc/rbb_cli_chirts_monthly_tmax_1983_2016_d1.nc",
decode_times=False)
@bennyistanto
bennyistanto / tiff2nc.py
Last active August 18, 2022 14:40
Convert clipped Java boundary's CHIRPS GeoTIFFs in a folder to single NetCDF file with time dimension enabled and CF-Compliant
#!/usr/bin/env python
"""
-------------------------------------------------------------------------------------------------------------
Convert CHIRPS GeoTIFF in a folder to single NetCDF file with time dimension enabled that is CF-Compliant
http://cfconventions.org/cf-conventions/v1.6.0/cf-conventions.html
Based on Rich Signell's answer on StackExchange: https://gis.stackexchange.com/a/70487
This script was tested using CHIRPS dekad data. Adjustment is needed if using other timesteps data for CHIRPS
NCO (http://nco.sourceforge.net) must be installed before using this script
@bennyistanto
bennyistanto / simpul-jaringan-indonesia.MD
Last active February 23, 2021 04:23
Daftar simpul jaringan Infrastruktur Data Spasial Indonesia.

Simpul Jaringan IDSN Indonesia

Update: 23 Feb 2021

Daftar simpul jaringan Infrastruktur Data Spasial Indonesia.

Daftar ini memuat alamat (server maupun geoportal) dari simpul jaringan Infastruktur Data Spasial yang ada di Indonesia. Referensi awal berasal dari https://github.com/ppids-ugm/simpul-jaringan-indonesia tetapi banyak alamat dari beberapa Kementerian sudah tidak aktif lagi. List berikut diupdate berdasarkan informasi dari Ina-Geoportal https://tanahair.indonesia.go.id/portal-web

Index

@bennyistanto
bennyistanto / SPI_based_on_CHIRPS_GeoTIFF.md
Last active November 22, 2024 21:50
Calculate SPI using CHIRPS data

Calculate SPI using monthly rainfall data in GeoTIFF format

These last few months, I have tried a lot of difference formulation to calculate Standardized Precipitation Index (SPI) based on rainfall data in netCDF format, check below files as a background:

The reason why I use rainfall in netCDF format in above files because the software to calculate SPI: climate-indices python package will only accept single netCDF as input, and the SPI script will read the netCDF input file based on time dimension.

Converting raster files into netCDF is easy using GDAL or other GIS software, but to make the time dimension enabled n

@bennyistanto
bennyistanto / gfs_ee_parse.py
Created June 16, 2021 09:46
Check GFS data availability from EarthEngine
import json
import csv
import sys
import datetime as dt
with open('result.json') as f:
content = json.load(f)
w = csv.writer(sys.stdout)
# Haversine formula using Py3
import math
def haversine(lat1, lon1, lat2, lon2):
# distance between latitudes and longitudes
dLat = (lat2 - lat1) * math.pi / 180.0
dLon = (lon2 - lon1) * math.pi / 180.0
# convert to radians
## Reorder dimension for SPI and SPEI calculation
ncpdq -a lat,lon,time /Users/benny/Temp/TERRACLIMATE/CDO/Tile11/Tile11_TerraClimate_pet_1958_2020.nc /Users/benny/Temp/TERRACLIMATE/CDO/Tile11/llt_Tile11_TerraClimate_pet_1958_2020.nc
ncpdq -a lat,lon,time /Users/benny/Temp/TERRACLIMATE/CDO/Tile11/Tile11_TerraClimate_ppt_1958_2020.nc /Users/benny/Temp/TERRACLIMATE/CDO/Tile11/llt_Tile11_TerraClimate_ppt_1958_2020.nc
ncks --fix_rec_dmn lat /Users/benny/Temp/TERRACLIMATE/CDO/Tile11/llt_Tile11_TerraClimate_pet_1958_2020.nc -o outfixed.nc4 ; mv outfixed.nc4 /Users/benny/Temp/TERRACLIMATE/CDO/Tile11/llt_Tile11_TerraClimate_pet_1958_2020.nc
ncks --fix_rec_dmn lat /Users/benny/Temp/TERRACLIMATE/CDO/Tile11/llt_Tile11_TerraClimate_ppt_1958_2020.nc -o outfixed.nc4 ; mv outfixed.nc4 /Users/benny/Temp/TERRACLIMATE/CDO/Tile11/llt_Tile11_TerraClimate_ppt_1958_2020.nc
ncks --mk_rec_dmn time /Users/benny/Temp/TERRACLIMATE/CDO/Tile11/llt_Tile11_TerraClimate_pet_1958_2020.nc -o outunlim.nc4 ; mv outunlim.nc4 /Users/benny/Temp/T