Skip to content

Instantly share code, notes, and snippets.

View bennyistanto's full-sized avatar

Benny Istanto bennyistanto

View GitHub Profile
@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 / 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 / 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 / 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 / GeoTIFF_to_NetCDF.py
Last active January 4, 2024 00:52
Convert CHIRPS GeoTIFF in a folder to single NetCDF file with time dimension enabled that is CF-Compliant
#!/usr/bin/env python
"""
-------------------------------------------------------------------------------------------------------------
Convert raster 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 TerraClimate monthly data. Adjustment is needed if using other timesteps data
NCO (http://nco.sourceforge.net), GDAL, NetCDF4, numpy must be installed before using this script
@bennyistanto
bennyistanto / imerg.sh
Created July 5, 2020 15:11
Arrange IMERG data based on number of days within month
#!/bin/bash
# Arrange IMERG data based on number of days within month
# Source: https://github.com/andyprasetya
# -- Start script: change directory ke target directory, misal: 'imerg'
cd imerg
# -- Set shell option ke 'nullglob' - optional, bisa di-comment/disabled
shopt -s nullglob
# Mulai loop ke seluruh file dalam target directory.
# Biasanya menggunakan 'i', tapi ini pakai 'file' untuk memperjelas penulisan variable
@bennyistanto
bennyistanto / Tiff_to_NetCDF.py
Created July 1, 2020 07:47
Convert multiple monthly CHIRPS GeoTIFF in a folder to NetCDF
#!/usr/bin/python
# -*- coding: utf8 -*-
import arcpy
import os
#Overwrite the output if exist
arcpy.env.overwriteOutput = True
# Set local variables
ws_in = r"Z:\Temp\CHIRPS\SPI\Month1D1"
#This script required daily CHIRPS data and previous wetspell data, if previous data not available
#then it will start with 0.
import os
import arcpy
from arcpy.sa import *
from datetime import date, timedelta
# to execute first DSLD data
def execute_first_DSLD(_tiffolder, _DSLDFolder, threshold):
sr = arcpy.SpatialReference(4326)
#This script required daily CHIRPS data and previous drspell data, if previous data not available
#then it will start with 0.
import os
import arcpy
from arcpy.sa import *
from datetime import date, timedelta
# to execute first DSLR data
def execute_first_DSLR(_tiffolder, _DSLRFolder, threshold):
sr = arcpy.SpatialReference(4326)
@bennyistanto
bennyistanto / CHIRPS_sum5days.py
Created June 19, 2020 07:41
Generate 5days rolling accumulation of rainfall, and calculate maximum value from the result using arcpy
import arcpy
import os
from datetime import date
from datetime import timedelta
import multiprocessing
#Overwrite the output if exist
arcpy.env.overwriteOutput = True
#Working directory