Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
plantillajupyterbook
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hackatón entrega Grupo 25
plantillajupyterbook
Commits
be61d23f
Commit
be61d23f
authored
1 year ago
by
David Akim
Browse files
Options
Downloads
Patches
Plain Diff
Update file 1_prepare_data.ipynb
parent
9f76b07f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
1_prepare_data.ipynb
+11
-133
11 additions, 133 deletions
1_prepare_data.ipynb
with
11 additions
and
133 deletions
1_prepare_data.ipynb
+
11
−
133
View file @
be61d23f
...
...
@@ -16,8 +16,7 @@
"source": [
"DATADIR = 'era5/' # directory containing downloaded era5 data\n",
"FIREDATADIR = 'fire_danger/' # directory containing fire data\n",
"DESTDIR = 'processed_era5/' # directory to save .npy files for each time step and variable\n",
"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"
]
},
{
...
...
@@ -54,10 +53,9 @@
"metadata": {},
"outputs": [],
"source": [
"vars = ['u10','v10','t2m','lai_hv','lai_lv','tp'] #considered variables (see 0_download_data.ipynb for long names)\n",
"vars = ['u10','v10','t2m','lai_hv','lai_lv','tp'
,'fdimrk'
] #considered variables (see 0_download_data.ipynb for long names)\n",
"months = [(1,31),(2,28),(12,31)] # months + days in month in dowloaded era5 .nc files\n",
"years = np.arange(2002,2023) # downloaded years\n",
"fire_vars = ['fdimrk'] # fire data variables"
"years = np.arange(2002,2023) # downloaded years"
]
},
{
...
...
@@ -65,107 +63,25 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Procesa
r datos ERA5
"
"# Procesa
miento de datos para crear archivos .npy
"
]
},
{
"cell_type": "code",
"execution_count":
5
,
"execution_count":
null
,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "77f93c51256047b981775b4b6469c9d7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/21 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9a27a43175944c7aad21cf81d8c3b31f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/21 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5ef1a176e6204c19a63852d952442609",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/21 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "fbf30b91845a4c9bb5ca2ee543ea20ec",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/21 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6dc83680a62a42f78ba7cb7e056ac54e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/21 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b4f23446188144a389cf64ca317e40cc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/21 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"# Processing
ERA5 data
\n",
"# Processing
Data to create .npy files
\n",
"for var in vars:\n",
" if not os.path.exists(DESTDIR + f\"{var}\"):\n",
" os.makedirs(DESTDIR + f\"{var}\")\n",
" \n",
" for year in tqdm(years):\n",
" root = nc.Dataset(DATADIR + f\"{year:d}.nc\", 'r')\n",
" if var=='fdimrk':\n",
" root = nc.Dataset(FIREDATADIR + f\"{year:d}.nc\", 'r')\n",
" else:\n",
" root = nc.Dataset(DATADIR + f\"{year:d}.nc\", 'r')\n",
" v = root.variables[var][:,:-9,:-5] #crop to get to a size suitable for the considered Unet-like model, here 140x140\n",
" v = v.data\n",
" root.close()\n",
...
...
@@ -186,44 +102,6 @@
"# Procesar datos de índice de peligro de incendio"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b3a75dfe23fa44d7a96e2ff92f9ed211",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/21 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Processing Fire danger index data\n",
"for var in fire_vars:\n",
" if not os.path.exists(FIREDESTDIR + f\"{var}\"):\n",
" os.makedirs(FIREDESTDIR + f\"{var}\")\n",
" \n",
" for year in tqdm(years):\n",
" root = nc.Dataset(FIREDATADIR + f\"{year:d}.nc\", 'r')\n",
" v = root.variables[var][:,:-9,:-5] #crop to get to a size suitable for the considered Unet-like model, here 140x140\n",
" v = v.data\n",
" root.close()\n",
" t = 0 # time step within v array that I am working on\n",
" for month, days in months:\n",
" for day in range(days):\n",
" np.save(FIREDESTDIR + f\"{var}/{year}_{month:02d}_{day+1:02d}.npy\",v[t])\n",
" t += 1"
]
},
{
"attachments": {},
"cell_type": "markdown",
...
...
%% 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:
# Procesa
r datos ERA5
# Procesa
miento 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
:
02
d
}
_
{
day
+
1
:
02
d
}
.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
:
02
d
}
_
{
day
+
1
:
02
d
}
.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
)
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment