Skip to content

Instantly share code, notes, and snippets.

@bennyistanto
Last active September 8, 2023 13:36
Show Gist options
  • Save bennyistanto/8f4b0f21082ed277d66a8e405c81cb8f to your computer and use it in GitHub Desktop.
Save bennyistanto/8f4b0f21082ed277d66a8e405c81cb8f to your computer and use it in GitHub Desktop.
Global IMERG daily to monthly
# -*- coding: utf-8 -*-
"""
NAME
imerg_daily2monthly.py
Global IMERG daily to monthly
DESCRIPTION
Input data for this script will use IMERG daily data generated by imerg_nc2tif.py or other
IMERG-based daily products. Example: daily wetdays to monthly sum, daily cdd to monthly max
REQUIREMENT
ArcGIS must installed before using this script, as it required arcpy module.
EXAMPLES
C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\python imerg_daily2monthly.py
NOTES
This script is designed to work with global IMERG data (Final or Late Run)
If using other data, some adjustment are required: parsing filename, directory, threshold
All IMERG data and products are available at s3://wbgdecinternal-ntl/climate/
CONTACT
Benny Istanto
Climate Geographer
GOST, The World Bank
LICENSE
This script is in the public domain, free from copyrights or restrictions.
VERSION
$Id$
TODO
xx
"""
import os
import arcpy
from collections import defaultdict
# To avoid overwriting outputs, change overwriteOutput option to False.
arcpy.env.overwriteOutput = True
# Change the data and output folder
input_folder = "X:\\Temp\\imerg\\products\\cwd\\cwd_1mm"
output_folder = "X:\\Temp\\imerg\\products\\cwd\\monthly_1mm"
# Create file collection based on MM information
groups = defaultdict(list)
for file_monthly in os.listdir(input_folder):
if file_monthly.endswith(".tif") or file_monthly.endswith(".tiff"):
# Parsing the filename to get YYYYMM information
i_imerg = file_monthly.index('imerg_')
# 6 is length of 'imerg_', and 6+6 is length of 'imerg_' and yyyymm
groupkey = file_monthly[i_imerg + 6:i_imerg+6+6]
fpath = os.path.join(input_folder, file_monthly)
groups[groupkey].append(fpath)
for groupkey, files in groups.items():
print(files)
ext = ".tif"
# Output filename
newfilename_monthly_stat1 = 'wld_cli_cwd_1mm_monthly_max_imerg_{0}{1}'.format(groupkey, ext)
print(newfilename_monthly_stat1)
# Statistics type.
# MEAN — The mean (average) of the inputs will be calculated.
# MAJORITY — The majority (value that occurs most often) of the inputs will be determined.
# MAXIMUM — The maximum (largest value) of the inputs will be determined.
# MEDIAN — The median of the inputs will be calculated. Note: The input must in integers
# MINIMUM — The minimum (smallest value) of the inputs will be determined.
# MINORITY — The minority (value that occurs least often) of the inputs will be determined.
# RANGE — The range (difference between largest and smallest value) of the inputs will be calculated.
# STD — The standard deviation of the inputs will be calculated.
# SUM — The sum (total of all values) of the inputs will be calculated.
# VARIETY — The variety (number of unique values) of the inputs will be calculated.
# To get another stats, you can duplicate 7 lines below and adjust the statistics type.
# Don't forget to add additional output file name, you can copy from line 59.
if arcpy.Exists(os.path.join(output_folder, newfilename_monthly_stat1)):
print(newfilename_monthly_stat1 + " exists")
else:
arcpy.CheckOutExtension("spatial")
outCellStatistics_stat1 = arcpy.sa.CellStatistics(files, "MAXIMUM", "DATA")
outCellStatistics_stat1.save(os.path.join(output_folder, newfilename_monthly_stat1))
arcpy.CheckInExtension("spatial")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment