Created
December 10, 2023 16:40
-
-
Save bennyistanto/dd6c1161c43f0d69fef2f906a715a04a to your computer and use it in GitHub Desktop.
MXD13Q1 monthly mean from 8-day data
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_8day2monthly.py | |
MXD13Q1 monthly 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 monthly 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 modis_8day2monthly.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 | |
# 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 = 2002 | |
end_year = 2023 | |
# Change the data and output folder | |
input_folder = "X:\\Temp\\modis\\{}\\gee\\04_fillnullwithstats\\cropland".format(iso3) | |
monthly_output_folder = "X:\\Temp\\modis\\{}\\gee\\02_positive\\temp".format(iso3) | |
# Define the monthlys | |
monthlys = { | |
"01": [(1, 1), (1, 27)], | |
"02": [(2, 1), (2, 19)], | |
"03": [(2, 25), (3, 23)], | |
"04": [(3, 28), (4, 24)], | |
"05": [(4, 29), (5, 26)], | |
"06": [(5, 31), (6, 27)], | |
"07": [(7, 2), (7, 21)], | |
"08": [(7, 26), (8, 22)], | |
"09": [(8, 27), (9, 23)], | |
"10": [(9, 28), (10, 25)], | |
"11": [(10, 30), (11, 26)], | |
"12": [(12, 1), (12, 28)] | |
} | |
def date_in_range(date, start, end): | |
return start <= date <= end | |
# Initialize the data structure for holding file paths | |
monthly_groups = {year: {monthly: [] for monthly in monthlys} 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 not (start_year <= date.year <= end_year): | |
continue | |
for monthly, (start_month_day, end_month_day) in monthlys.items(): | |
start_date = datetime(date.year, *start_month_day) | |
end_date = datetime(date.year, *end_month_day) | |
if date_in_range(date, start_date, end_date): | |
monthly_groups[date.year][monthly].append(os.path.join(input_folder, file)) | |
break | |
for year, monthlys_files in monthly_groups.items(): | |
for monthly, files in monthlys_files.items(): | |
if files: | |
out_raster_name = f"{iso3}_phy_mxd13q1_evi_mean_{year}{monthly}.tif" | |
out_raster_path = os.path.join(monthly_output_folder, out_raster_name) | |
if not arcpy.Exists(out_raster_path): | |
arcpy.CheckOutExtension("spatial") | |
outCellStatistics = arcpy.sa.CellStatistics(files, "MEAN", "DATA") | |
outCellStatistics.save(out_raster_path) | |
arcpy.CheckInExtension("spatial") | |
print(f"{out_raster_name} completed") | |
else: | |
print(f"{out_raster_name} exists") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment