File size: 9,266 Bytes
64ab846 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | from prefect import task
from prefect.logging import get_run_logger
import torch
import os
from typing import List, Any
import pickle
import yaml
import re
import rasterio
import xarray as xr
import pandas as pd
import numpy as np
from datetime import datetime
from tools.historical_weather import get_historical_weather_data
with open('config/params.yml') as file:
config = yaml.safe_load(file)
datetime_col = config['datetime_col']
### Helper functions
@task(task_run_name="get_consortia")
def get_consortia() -> List[str]:
"""
Retrieves the list of consortia from the configuration.
Returns:
List[str]: List of consortia names.
"""
logger = get_run_logger()
consortia = config['consortia']
logger.info(f"We work with the following consortia: {consortia}")
return consortia
@task(tags=['write_to_file_{df.name}'], retries=3)
def save_to_file(df: Any, output_file: str) -> None:
"""
Saves DataFrames or GeoDataFrames to a file in the specified format.
Args:
df (Any): DataFrame or GeoDataFrame to save.
output_file (str): Path to the output file.
file_format (str, optional): Format to save ('parquet', 'geojson', 'zarr', 'pickle', 'netcdf'/'nc' and torch 'pt' are supported).
"""
logger = get_run_logger()
logger.info(f'Saving data to {output_file}...')
file_format = output_file.split('.')[-1]
logger.info(f'The output file has format {file_format}.')
folder = '//'.join(output_file.split('//')[:-1])
os.makedirs(folder, exist_ok=True)
if file_format == 'parquet':
df.to_parquet(output_file)
elif file_format == 'geojson':
df.to_file(output_file, driver="GeoJSON")
elif file_format == 'zarr':
df.to_zarr(output_file, mode='w')
elif file_format == 'pickle':
with open(output_file, 'wb') as handle:
pickle.dump(df, handle)
elif file_format == 'netcdf' or file_format == 'nc':
df.to_netcdf(output_file, engine='netcdf4', format='NETCDF4')
elif file_format == 'pt':
torch.save(df, output_file)
else:
raise Exception('Format specified is not supported.')
return
@task(task_run_name='read_satellite_data')
def read_satellite_data(consortium_name):
logger = get_run_logger()
logger.info('Collecting satellite data...')
dir = f"data//01_raw//{config['consortia_data_folders'][consortium_name]}//satellite_data"
file_paths = sorted([f for f in os.listdir(dir) if f.lower().endswith((".tif", ".tiff"))])
data = []
dates = []
# Regex pattern for datetime in filenames
pattern = r'\d{4}-\d{2}-\d{2}T\d{2}_\d{2}_\d{2}'
# Read data from each file
for file_path in file_paths:
dates.append(re.search(pattern, file_path).group())
with rasterio.open(os.path.join(dir, file_path)) as src:
transform = src.transform
bands = src.count
band_data = src.read(range(1, bands + 1))
data.append(band_data)
# Convert extracted dates
datetimes = [datetime.strptime(date, '%Y-%m-%dT%H_%M_%S') for date in dates]
############################ CHANGE 1 ###################################
# Use the last opened raster for spatial info (all files share same grid)
height, width = band_data.shape[1], band_data.shape[2]
############################ CHANGE 2 ###################################
# INSTEAD OF NESTED LOOP WE EXTRACT THE PIXEL CENTER COORDINATES based on the trasnformation
x_coords = transform[2] + (np.arange(width) + 0.5) * transform[0]
y_coords = transform[5] + (np.arange(height) + 0.5) * transform[4]
########################## CHANGE 3 #####################################
# Build the main DataArray with CENTER COORDINATES ASSOCIATED TO EACH PIXEL
satellite_data = xr.DataArray(
data,
dims=['time', 'band', 'y', 'x'],
coords={
'time': datetimes,
'x': x_coords,
'y': y_coords,
},
name='satellite_data'
)
#### this is still creating what you wanted, although i dont think it is necessary (OPTIONAL)
# it is not contributing to any potential calculation
x_edges = transform[2] + np.arange(width + 1) * transform[0]
y_edges = transform[5] + np.arange(height + 1) * transform[4]
left_edges = np.tile(x_edges[:-1], (height, 1))
right_edges = np.tile(x_edges[1:], (height, 1))
top_edges = np.tile(y_edges[:-1][:, None], (1, width))
bottom_edges = np.tile(y_edges[1:][:, None], (1, width))
# Build Dataset with all information
dataset = xr.Dataset(
{
"satellite_data": satellite_data,
"pixel_bounds_left": (("y", "x"), left_edges),
"pixel_bounds_right": (("y", "x"), right_edges),
"pixel_bounds_top": (("y", "x"), top_edges),
"pixel_bounds_bottom": (("y", "x"), bottom_edges),
}
)
return dataset
@task(task_run_name='update_historical_weather_data_{consortium_name}')
def update_historical_weather_data(consortium_name, location_ids):
"""
Update historical weather data for a consortium.
Parameters:
-----------
consortium_name : str
Name of the consortium
location_ids : pd.DataFrame
DataFrame with columns: ['datastream_name', 'datastream_id', 'x', 'y']
where x = latitude, y = longitude
Returns:
--------
pd.DataFrame with historical weather data
"""
logger = get_run_logger()
start_date = config['start_date']
end_date = config['end_date']
childs = next(os.walk('data//01_raw//'))
initial_file_exists = False
for child in childs[-1]:
if child.startswith(f'historical_weather_data_{consortium_name}_'):
initial_file_exists = True
current_dates = child.split(f'historical_weather_data_{consortium_name}_')[-1]
current_dates = current_dates.split('.parquet')[0].split('_')
current_start_date = datetime.strptime(current_dates[0], '%Y-%m-%d').date()
current_end_date = datetime.strptime(current_dates[1], '%Y-%m-%d').date()
if f'historical_weather_data_{consortium_name}_{start_date}_{end_date}.parquet' in childs[-1]:
logger.info(f'Consortium {consortium_name}: historical weather data is already fully downloaded!')
data = pd.read_parquet(
f'data//01_raw//historical_weather_data_{consortium_name}_{current_start_date}_{current_end_date}.parquet')
else:
logger.info(f'Consortium {consortium_name}: some historical weather data need to be downloaded...')
if not initial_file_exists:
data = get_historical_weather_data(location_ids, start_date=start_date, end_date=end_date)
data[datetime_col] = pd.to_datetime(data['datetime'].dt.tz_localize(None))
data = data.drop(columns=['datetime'])
else:
if current_start_date > start_date:
logger.info('Need to download some data in the past...')
new_data = get_historical_weather_data(location_ids, start_date=start_date, end_date=current_start_date - datetime.timedelta(days=1))
new_data[datetime_col] = pd.to_datetime(new_data['datetime'].dt.tz_localize(None))
new_data = new_data.drop(columns=['datetime'])
current_data = pd.read_parquet(
f'data//01_raw//historical_weather_data_{consortium_name}_{current_start_date}_{current_end_date}.parquet')
data = pd.concat(
[new_data, current_data],
axis=0
).drop_duplicates(subset=[datetime_col]).sort_values('datetime')
if current_end_date < end_date:
logger.info('Need to download some more recent data...')
new_data = get_historical_weather_data(location_ids, start_date=current_end_date, end_date=end_date)
new_data[datetime_col] = pd.to_datetime(new_data['datetime'].dt.tz_localize(None))
new_data = new_data.drop(columns=['datetime'])
current_data = pd.read_parquet(
f'data//01_raw//historical_weather_data_{consortium_name}_{current_start_date}_{current_end_date}.parquet')
data = pd.concat(
[new_data, current_data],
axis=0
).drop_duplicates(subset=[datetime_col, 'datastream_name']).sort_values(datetime_col)
logger.info('Old historical weather data are being removed...')
os.remove(
f'data//01_raw//historical_weather_data_{consortium_name}_{current_start_date}_{current_end_date}.parquet')
logger.info('New historical weather data are being saved...')
save_to_file(df=data,
output_file=f'data//01_raw//historical_weather_data_{consortium_name}_{start_date}_{end_date}.parquet')
logger.info('Done!')
return data |