Skip to content

Instantly share code, notes, and snippets.

@bennyistanto
Last active March 13, 2024 08:19
Show Gist options
  • Save bennyistanto/261f538da0a2056927d5d3d74673f400 to your computer and use it in GitHub Desktop.
Save bennyistanto/261f538da0a2056927d5d3d74673f400 to your computer and use it in GitHub Desktop.
MXD13Q1 annual mean from 8-day data
# -*- coding: utf-8 -*-
"""
NAME
06_mxd13q1_8day2annual.py
MXD13Q1 annual mean from 8-day data
DESCRIPTION
Input data for this script will use MXD13Q1 8-days data generate from GEE or downloaded from NASA
This script can do MEAN calculation to derive annual data
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 06_mxd13q1_8day2annual.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
from datetime import datetime
import uuid
# To avoid overwriting outputs, change overwriteOutput option to False.
arcpy.env.overwriteOutput = True
# Raster environment settings for output consistency and optimization
arcpy.env.compression = "LZ77"
arcpy.env.pyramid = "PYRAMIDS -1 NEAREST LZ77 NO_SKIP"
# ISO3 Country Code
iso3 = "idn" # e.g., Syria: syr, Myanmar: mmr, Lebanon: lbn
# Define the range of years
start_year = 2002
end_year = 2023
# Change the data and output folder
input_folder = f"D:\\temp\\modis\\{iso3}\\gee\\04_fillnullwithstats\\evi_all_8day"
annual_output_folder = f"D:\\temp\\modis\\{iso3}\\gee\\04_fillnullwithstats\\evi_all_annual"
# Global variable to store user's choice
user_choice = None
def set_user_decision():
"""Prompt user for decision on existing files and store it globally."""
global user_choice
if user_choice is None:
decision = input("An output file already exists. Choose an action - Replace (R), Skip (S), Abort (A): ").upper()
while decision not in ['R', 'S', 'A']:
print("Invalid choice. Please choose again.")
decision = input("Choose an action - Replace (R), Skip (S), Abort (A): ").upper()
user_choice = decision
def process_files(input_folder, annual_output_folder):
global user_choice
annual_groups = {year: [] for year in range(start_year, end_year + 1)}
for file in os.listdir(input_folder):
if file.endswith(".tif") or file.endswith(".tiff"):
parts = file.split("_")
date_str = parts[-1].split(".")[0]
date = datetime.strptime(date_str, "%Y%m%d")
if start_year <= date.year <= end_year:
annual_groups[date.year].append(os.path.join(input_folder, file))
for year, files in annual_groups.items():
if files:
out_raster_name = f"{iso3}_phy_mxd13q1_evi_mean_{year}.tif"
out_raster_path = os.path.join(annual_output_folder, out_raster_name)
if arcpy.Exists(out_raster_path):
if user_choice is None:
set_user_decision()
if user_choice == 'S':
print(f"Skipping existing file: {out_raster_path}")
continue
elif user_choice == 'A':
print("Aborting process.")
exit()
elif user_choice == 'R':
print(f"Replacing existing file: {out_raster_path}")
print(f"Processing {year}...")
temp_filename = f"temp_{uuid.uuid4().hex}.tif"
temp_file_path = os.path.join(arcpy.env.scratchFolder, temp_filename)
arcpy.CheckOutExtension("Spatial")
outCellStatistics = arcpy.sa.CellStatistics(files, "MEAN", "DATA")
outCellStatistics.save(temp_file_path)
arcpy.management.CopyRaster(
in_raster=temp_file_path,
out_rasterdataset=out_raster_path,
config_keyword="",
background_value="",
nodata_value="",
onebit_to_eightbit="NONE",
colormap_to_RGB="NONE",
pixel_type="16_BIT_SIGNED",
scale_pixel_value="NONE",
RGB_to_Colormap="NONE",
format="TIFF",
transform="NONE",
process_as_multidimensional="CURRENT_SLICE",
build_multidimensional_transpose="NO_TRANSPOSE"
)
try:
arcpy.Delete_management(temp_file_path)
print(f"Temporary file {temp_file_path} deleted.")
except Exception as e:
print(f"Warning: Unable to delete temporary file {temp_file_path}")
arcpy.ClearWorkspaceCache_management()
arcpy.CheckInExtension("spatial")
print(f"{out_raster_name} completed with LZ77 compression.")
# Main function
def main():
process_files(input_folder, annual_output_folder)
if __name__ == '__main__':
main()
print("Script completed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment