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

Update file 1_prepare_data.ipynb

parent 510aa888
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Configurar directorios # Configurar directorios
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
DATADIR = 'era5/' # directory containing downloaded era5 data DATADIR = 'era5/' # directory containing downloaded era5 data
FIREDATADIR = 'fire_danger/' # directory containing fire data FIREDATADIR = 'fire_danger/' # directory containing fire data
DESTDIR = 'processed_data/' # directory to save .npy files for each time step and variable DESTDIR = 'processed_data/' # directory to save .npy files for each time step and variable
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Cargar bibliotecas # Cargar bibliotecas
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import numpy as np import numpy as np
import netCDF4 as nc import netCDF4 as nc
import os import os
from tqdm.notebook import tqdm from tqdm.notebook import tqdm
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Variables de configuración # Variables de configuración
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
vars = ['u10','v10','t2m','lai_hv','lai_lv','tp','fdimrk'] #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 months = [(1,31),(2,28),(12,31)] # months + days in month in dowloaded era5 .nc files
years = np.arange(2002,2023) # downloaded years years = np.arange(2002,2023) # downloaded years
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Procesamiento de datos para crear archivos .npy # Procesamiento de datos para crear archivos .npy
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Processing Data to create .npy files # Processing Data to create .npy files
for var in vars: for var in vars:
if not os.path.exists(DESTDIR + f"{var}"): if not os.path.exists(DESTDIR + f"{var}"):
os.makedirs(DESTDIR + f"{var}") os.makedirs(DESTDIR + f"{var}")
for year in tqdm(years): for year in tqdm(years):
if var=='fdimrk': if var=='fdimrk':
root = nc.Dataset(FIREDATADIR + f"{year:d}.nc", 'r') root = nc.Dataset(FIREDATADIR + f"{year:d}.nc", 'r')
else: else:
root = nc.Dataset(DATADIR + f"{year:d}.nc", 'r') 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 = root.variables[var][:,:-9,:-5] #crop to get to a size suitable for the considered Unet-like model, here 140x140
v = v.data v = v.data
root.close() root.close()
if var in ['tp']: #change unit from m to mm for precipitation if var in ['tp']: #change unit from m to mm for precipitation
v = 1000 * v v = 1000 * v
t = 0 # time step within v array that I am working on t = 0 # time step within v array that I am working on
for month, days in months: for month, days in months:
for day in range(days): for day in range(days):
np.save(DESTDIR + f"{var}/{year}_{month:02d}_{day+1:02d}.npy",v[t]) np.save(DESTDIR + f"{var}/{year}_{month:02d}_{day+1:02d}.npy",v[t])
t += 1 t += 1
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Procesar datos de índice de peligro de incendio
%% Cell type:markdown id: tags:
# Guardar información de latitud/longitud # Guardar información de latitud/longitud
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
root = nc.Dataset(DATADIR + f"2002.nc", 'r') #constant in time -> take from any year 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 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 lon = root.variables['longitude'][:-5].data
np.save(DESTDIR + 'lat.npy', lat) np.save(DESTDIR + 'lat.npy', lat)
np.save(DESTDIR + 'lon.npy', lon) 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