Skip to content
Snippets Groups Projects
Commit be61d23f authored by David Akim's avatar David Akim
Browse files

Update file 1_prepare_data.ipynb

parent 9f76b07f
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Configurar directorios
%% Cell type:code id: tags:
``` python
DATADIR = 'era5/' # directory containing downloaded era5 data
FIREDATADIR = 'fire_danger/' # directory containing fire data
DESTDIR = 'processed_era5/' # directory to save .npy files for each time step and variable
FIREDESTDIR = 'processed_fire_data/' # directory to save .npy files for each time step and variable for fire data
DESTDIR = 'processed_data/' # directory to save .npy files for each time step and variable
```
%% Cell type:markdown id: tags:
# Cargar bibliotecas
%% Cell type:code id: tags:
``` python
import numpy as np
import netCDF4 as nc
import os
from tqdm.notebook import tqdm
```
%% Cell type:markdown id: tags:
# Variables de configuración
%% Cell type:code id: tags:
``` python
vars = ['u10','v10','t2m','lai_hv','lai_lv','tp'] #considered variables (see 0_download_data.ipynb for long names)
vars = ['u10','v10','t2m','lai_hv','lai_lv','tp','fdimrk'] #considered variables (see 0_download_data.ipynb for long names)
months = [(1,31),(2,28),(12,31)] # months + days in month in dowloaded era5 .nc files
years = np.arange(2002,2023) # downloaded years
fire_vars = ['fdimrk'] # fire data variables
```
%% Cell type:markdown id: tags:
# Procesar datos ERA5
# Procesamiento de datos para crear archivos .npy
%% Cell type:code id: tags:
``` python
# Processing ERA5 data
# Processing Data to create .npy files
for var in vars:
if not os.path.exists(DESTDIR + f"{var}"):
os.makedirs(DESTDIR + f"{var}")
for year in tqdm(years):
root = nc.Dataset(DATADIR + f"{year:d}.nc", 'r')
if var=='fdimrk':
root = nc.Dataset(FIREDATADIR + f"{year:d}.nc", 'r')
else:
root = nc.Dataset(DATADIR + f"{year:d}.nc", 'r')
v = root.variables[var][:,:-9,:-5] #crop to get to a size suitable for the considered Unet-like model, here 140x140
v = v.data
root.close()
if var in ['tp']: #change unit from m to mm for precipitation
v = 1000 * v
t = 0 # time step within v array that I am working on
for month, days in months:
for day in range(days):
np.save(DESTDIR + f"{var}/{year}_{month:02d}_{day+1:02d}.npy",v[t])
t += 1
```
%% Output
%% Cell type:markdown id: tags:
# Procesar datos de índice de peligro de incendio
%% Cell type:code id: tags:
``` python
# Processing Fire danger index data
for var in fire_vars:
if not os.path.exists(FIREDESTDIR + f"{var}"):
os.makedirs(FIREDESTDIR + f"{var}")
for year in tqdm(years):
root = nc.Dataset(FIREDATADIR + f"{year:d}.nc", 'r')
v = root.variables[var][:,:-9,:-5] #crop to get to a size suitable for the considered Unet-like model, here 140x140
v = v.data
root.close()
t = 0 # time step within v array that I am working on
for month, days in months:
for day in range(days):
np.save(FIREDESTDIR + f"{var}/{year}_{month:02d}_{day+1:02d}.npy",v[t])
t += 1
```
%% Output
%% Cell type:markdown id: tags:
# Guardar información de latitud/longitud
%% Cell type:code id: tags:
``` python
root = nc.Dataset(DATADIR + f"2002.nc", 'r') #constant in time -> take from any year
lat = root.variables['latitude'][:-9].data #crop to get to a size suitable for the considered Unet-like model
lon = root.variables['longitude'][:-5].data
np.save(DESTDIR + 'lat.npy', lat)
np.save(DESTDIR + 'lon.npy', lon)
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment