Created
December 10, 2023 16:41
-
-
Save bennyistanto/1d914d17e55cfc752d414efcb188efee to your computer and use it in GitHub Desktop.
MXD13Q1 monthly statistics data, long-term average, max, min and stdev
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
NAME | |
modis_monthlystats.py | |
MXD13Q1 monthly statistics data, long-term average, max, min and stdev | |
DESCRIPTION | |
Input data for this script will use MXD13Q1 monthly data generate from modis_8day2monthly.py | |
This script can do annual statistics calculation (AVERAGE, MAXIMUM, MINIMUM and STD) | |
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 modis_monthlystats.py | |
NOTES | |
This script is designed to work with MODIS naming convention | |
If using other data, some adjustment are required: parsing filename and directory | |
CONTACT | |
Benny Istanto | |
Climate Geographer | |
GOST/DECAT/DECDG, The World Bank | |
LICENSE | |
This script is in the public domain, free from copyrights or restrictions. | |
VERSION | |
$Id$ | |
TODO | |
xx | |
""" | |
import os | |
import arcpy | |
# To avoid overwriting outputs, change overwriteOutput option to False. | |
arcpy.env.overwriteOutput = True | |
# ISO3 Country Code | |
iso3 = "tur" # Syria: syr, Myanmar: mmr, Lebanon: lbn | |
# Define the range of years | |
start_year = 2003 | |
end_year = 2022 | |
# Change the data and output folder | |
monthly_data_folder = "X:\\Temp\\modis\\{}\\gee\\02_positive\\temp".format(iso3) | |
long_term_stats_folder = "X:\\Temp\\modis\\{}\\gee\\03_statistics\\temp".format(iso3) | |
# Define statistic names | |
# 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. | |
# Define the statistic names for file naming and their corresponding arcpy names for processing | |
stat_names = { | |
"max": "MAXIMUM", | |
"min": "MINIMUM", | |
"avg": "MEAN", | |
"med": "MEDIAN", | |
"std": "STD" | |
} | |
# Monthlys | |
monthlys = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"] | |
for monthly in monthlys: | |
monthly_files = [] | |
# Gather all the monthly mean files for each monthly across all years | |
for year in range(start_year, end_year + 1): | |
monthly_mean_name = f"{iso3}_phy_mxd13q1_evi_mean_{year}{monthly}.tif" | |
monthly_mean_path = os.path.join(monthly_data_folder, monthly_mean_name) | |
if arcpy.Exists(monthly_mean_path): | |
monthly_files.append(monthly_mean_path) | |
# Calculate the long-term statistics for the monthly | |
for stat_short, stat_long in stat_names.items(): | |
out_raster_name = f"{iso3}_phy_mxd13q1_{monthly}_{start_year}_{end_year}_{stat_short}.tif" | |
out_raster_path = os.path.join(long_term_stats_folder, out_raster_name) | |
if not arcpy.Exists(out_raster_path) and monthly_files: | |
arcpy.CheckOutExtension("spatial") | |
outCellStatistics = arcpy.sa.CellStatistics(monthly_files, stat_long, "DATA") | |
outCellStatistics.save(out_raster_path) | |
arcpy.CheckInExtension("spatial") | |
print(f"{out_raster_name} completed") | |
elif monthly_files: | |
print(f"{out_raster_name} exists") | |
else: | |
print(f"No monthly mean files found for {monthly}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment