Last active
October 24, 2023 04:07
-
-
Save bennyistanto/b7bf48022b3725de6f5c1ed965819d0f to your computer and use it in GitHub Desktop.
Aggregates MODIS's tabular 8-days data into monthly
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "ea9ecc52", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Processing columns: 100%|████████████████████████████████████████████████| 3/3 [00:01<00:00, 2.34it/s]" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Completed\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"import pandas as pd\n", | |
"from tqdm import tqdm\n", | |
"\n", | |
"def aggregate_monthly(data):\n", | |
" # Define the monthly date ranges\n", | |
" monthly_periods = {\n", | |
" \"Jan\": (\"01\", \"01\", \"01\", \"26\"),\n", | |
" \"Feb\": (\"02\", \"01\", \"02\", \"19\"),\n", | |
" \"Mar\": (\"02\", \"25\", \"03\", \"23\"),\n", | |
" \"Apr\": (\"03\", \"28\", \"04\", \"24\"),\n", | |
" \"May\": (\"04\", \"29\", \"05\", \"26\"),\n", | |
" \"Jun\": (\"05\", \"31\", \"06\", \"27\"),\n", | |
" \"Jul\": (\"07\", \"01\", \"07\", \"21\"),\n", | |
" \"Aug\": (\"07\", \"25\", \"08\", \"22\"),\n", | |
" \"Sep\": (\"08\", \"26\", \"09\", \"23\"),\n", | |
" \"Oct\": (\"09\", \"27\", \"10\", \"25\"),\n", | |
" \"Nov\": (\"10\", \"29\", \"11\", \"26\"),\n", | |
" \"Dec\": (\"12\", \"01\", \"12\", \"28\")\n", | |
" }\n", | |
" \n", | |
" # Create new dataframe for aggregated data\n", | |
" aggregated_data = pd.DataFrame()\n", | |
"\n", | |
" # Copy the first two columns\n", | |
" aggregated_data[\"No\"] = data[\"No\"]\n", | |
" aggregated_data[\"ADM3_PCODE\"] = data[\"ADM3_PCODE\"]\n", | |
"\n", | |
" new_columns = [] # Store the new columns here\n", | |
"\n", | |
" # For each year\n", | |
" for year in range(2002, 2024):\n", | |
" # For each month\n", | |
" for month, days in monthly_periods.items():\n", | |
" # Create the start and end dates\n", | |
" start_date = f\"{year}{days[0]}{days[1]}\"\n", | |
" end_date = f\"{year}{days[-2]}{days[-1]}\"\n", | |
" \n", | |
" # Extract columns between start and end dates\n", | |
" relevant_columns = [col for col in data.columns if start_date <= col <= end_date]\n", | |
" \n", | |
" # Calculate the mean for each row and store in new_columns list\n", | |
" new_column = data[relevant_columns].mean(axis=1)\n", | |
" new_column.name = f\"{month}-{str(year)[2:]}\"\n", | |
" new_columns.append(new_column)\n", | |
"\n", | |
" # Concatenate all the new columns to the aggregated_data DataFrame\n", | |
" aggregated_data = pd.concat([aggregated_data] + new_columns, axis=1)\n", | |
"\n", | |
" # Return the aggregated data\n", | |
" return aggregated_data\n", | |
"\n", | |
"# Define iso3 and column_names\n", | |
"iso3 = 'syr'\n", | |
"column_names = ['MIN', 'MAX', 'MEAN']\n", | |
"\n", | |
"for column_name in tqdm(column_names, desc=\"Processing columns\"):\n", | |
" # Construct the input file path\n", | |
" input_file = f'/mnt/x/Temp/modis/{iso3}/gee/13_tables/zonal/csv/{iso3}_phy_evi_da_all_2002_2022_{column_name}_mxd13q1_adm3.csv'\n", | |
" \n", | |
" # Load the data\n", | |
" data = pd.read_csv(input_file, na_values='')\n", | |
" \n", | |
" # Aggregate the data\n", | |
" aggregated_data = aggregate_monthly(data)\n", | |
" \n", | |
" # Construct the output file path and save the aggregated data to a new CSV file\n", | |
" output_file = f'/mnt/x/Temp/modis/{iso3}/gee/13_tables/zonal/csv/{iso3}_phy_evi_da_all_2002_2022_{column_name}_mxd13q1_adm3_monthly.csv'\n", | |
" aggregated_data.to_csv(output_file, index=False)\n", | |
"\n", | |
"print('Completed')\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3fe4fca2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment