Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dast_project #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added CyberSecurity_2025_Report.pdf
Binary file not shown.

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Presentazione CyberSecurity.pdf
Binary file not shown.
Binary file not shown.
Binary file added __pycache__/plot_map_grid.cpython-313.pyc
Binary file not shown.
Binary file not shown.
116 changes: 116 additions & 0 deletions data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import rasterio
import numpy as np
import pandas as pd
from datetime import datetime
import os
import glob


def extract_dates_from_filename(filename):

try:
filename = os.path.basename(filename)

import re

dates = re.findall(r"20\d{6}", filename)

if len(dates) >= 2:
return dates[0], dates[1]

print(f"Date non trovate nel file {filename}")
return None, None
except Exception as e:
print(f"Errore nell'estrazione delle date dal file {filename}: {e}")
return None, None


def read_tif_file(file_path):

try:
print(f"Elaborazione del file: {file_path}")

start_date, end_date = extract_dates_from_filename(os.path.basename(file_path))

with rasterio.open(file_path) as src:

raster_data = src.read(1)

transform = src.transform

height, width = raster_data.shape
rows, cols = np.mgrid[0:height, 0:width]

xs, ys = rasterio.transform.xy(transform, rows, cols)

lons = np.array(xs)
lats = np.array(ys)

df = pd.DataFrame(
{
"longitude": lons.flatten(),
"latitude": lats.flatten(),
"precipitation": raster_data.flatten(),
"start_date": start_date,
"end_date": end_date,
"source_file": os.path.basename(file_path),
}
)

df = df[df["precipitation"] != src.nodata]
df = df.dropna()

return df

except Exception as e:
print(f"Errore nella lettura del file {file_path}: {str(e)}")
return None


def process_multiple_files(directory_path):

all_data = []

tif_files = glob.glob(os.path.join(directory_path, "GIOVANNI*precipitation*.tif"))

if not tif_files:
print(f"Nessun file TIF trovato in {directory_path}")
return None

print("File TIF trovati:")
for file in tif_files:
print(f"- {os.path.basename(file)}")

for file_path in tif_files:
df = read_tif_file(file_path)
if df is not None:
all_data.append(df)

if all_data:

combined_df = pd.concat(all_data, ignore_index=True)

combined_df.sort_values("start_date", inplace=True)

output_path = os.path.join(directory_path, "precipitation_timeseries.csv")
combined_df.to_csv(output_path, index=False)
print(f"\nDati salvati in: {output_path}")

print("\nStatistiche di base delle precipitazioni:")
print(combined_df.groupby("start_date")["precipitation"].describe())

return combined_df

return None


if __name__ == "__main__":

directory_path = "."
print("Inizio elaborazione dei file TIF...")
data = process_multiple_files(directory_path)

if data is not None:
print("\nElaborazione completata con successo!")
else:
print("\nErrore nell'elaborazione dei file.")
Binary file added plot/__pycache__/plot.cpython-313.pyc
Binary file not shown.
Binary file not shown.
Binary file added plot/__pycache__/plot_map_grid.cpython-313.pyc
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions plot/output_analysis/annual_statistics.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
,precipitation,precipitation,precipitation,precipitation,precipitation
,mean,std,min,max,count
year,,,,,
2018,2.822,0.38,1.983,3.96,1500
2019,2.733,0.492,1.583,4.112,1500
2020,1.766,0.42,0.958,2.973,1500
2021,2.531,0.658,1.341,4.477,1500
2022,2.018,0.442,0.97,3.128,1500
2023,1.893,0.526,0.851,3.428,1500
Loading