| import json
|
| import os
|
| import glob
|
|
|
|
|
| base_directory = 'weather'
|
| output_filename = 'merged_general_weather_forecast.json'
|
|
|
|
|
| report_type_folder = 'general_report'
|
| file_prefix = 'general_weather_forecast'
|
| file_pattern = os.path.join(base_directory, '*', 'weather_report', report_type_folder, f'{file_prefix}_*.json')
|
|
|
|
|
| all_files = glob.glob(file_pattern)
|
|
|
| if not all_files:
|
| print(f"No files found for pattern '{file_pattern}'. Please check the path and script location.")
|
| else:
|
| print(f"Found {len(all_files)} files to merge. Starting process...")
|
|
|
|
|
|
|
| master_data = {}
|
|
|
| for file_path in all_files:
|
| try:
|
|
|
|
|
| path_parts = os.path.normpath(file_path).split(os.sep)
|
| region = path_parts[-3]
|
|
|
|
|
| with open(file_path, 'r', encoding='utf-8') as f:
|
| region_data = json.load(f)
|
|
|
|
|
| for timestamp, report_details in region_data.items():
|
| timestamp_entry = master_data.setdefault(timestamp, {})
|
|
|
| timestamp_entry[region] = report_details
|
|
|
| print(f" - Processed and integrated: {region}")
|
|
|
| except Exception as e:
|
| print(f"An error occurred while processing file {file_path}: {e}")
|
|
|
|
|
| if master_data:
|
| print("\nWriting all data to the final file...")
|
|
|
|
|
| sorted_master_data = dict(sorted(master_data.items()))
|
|
|
| with open(output_filename, 'w', encoding='utf-8') as f:
|
| json.dump(sorted_master_data, f, indent=4, ensure_ascii=False)
|
|
|
| print(f"Merge complete!")
|
| print(f"All data has been aggregated by timestamp into: {output_filename}")
|
| print(f"The final file contains {len(master_data)} unique timestamps.")
|
| else:
|
| print("No files were processed successfully. The output file was not generated.") |