Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Climax
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
earth_observation_public
Climax
Commits
e964a778
Commit
e964a778
authored
3 years ago
by
Frisinghelli Daniel
Browse files
Options
Downloads
Patches
Plain Diff
Aggregate ERA5 from hourly to daily average.
parent
a693fd8a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
climax/main/preprocess_ERA5.py
+86
-0
86 additions, 0 deletions
climax/main/preprocess_ERA5.py
with
86 additions
and
0 deletions
climax/main/preprocess_ERA5.py
+
86
−
0
View file @
e964a778
...
...
@@ -3,4 +3,90 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# builtins
import
re
import
sys
import
logging
from
logging.config
import
dictConfig
# externals
import
xarray
as
xr
# locals
from
climax.core.cli
import
preprocess_era5_parser
from
climax.core.constants
import
ERA5_VARIABLES
from
pysegcnn.core.logging
import
log_conf
from
pysegcnn.core.utils
import
search_files
from
pysegcnn.core.trainer
import
LogConfig
# module level logger
LOGGER
=
logging
.
getLogger
(
__name__
)
if
__name__
==
'
__main__
'
:
# initialize logging
dictConfig
(
log_conf
())
# define command line argument parser
parser
=
preprocess_era5_parser
()
# parse command line arguments
args
=
sys
.
argv
[
1
:]
if
not
args
:
parser
.
print_help
()
sys
.
exit
()
else
:
args
=
parser
.
parse_args
(
args
)
# check whether the source directory exists
if
args
.
source
.
exists
():
# check whether the target grid file exists
if
not
args
.
grid
.
exists
():
LOGGER
.
info
(
'
{} does not exist.
'
.
format
(
args
.
grid
))
sys
.
exit
()
# check whether a single variable is specified
variables
=
ERA5_VARIABLES
if
args
.
variable
is
not
None
:
variables
=
args
.
variable
# iterate over the variables to preprocess
for
var
in
variables
:
# path to files of the current variable
files
=
search_files
(
args
.
source
,
'
_
'
.
join
([
'
^ERA5
'
,
var
,
'
[0-9]{4}.nc$
'
]))
ymin
,
ymax
=
(
re
.
search
(
'
[0-9]{4}
'
,
files
[
0
].
name
)[
0
],
re
.
search
(
'
[0-9]{4}
'
,
files
[
-
1
].
name
)[
0
])
# check if aggregate file exists
filename
=
'
_
'
.
join
([
'
ERA5
'
,
var
,
ymin
,
ymax
])
filename
=
args
.
target
.
joinpath
(
var
,
filename
)
if
filename
.
exists
()
and
not
args
.
overwrite
:
LOGGER
.
info
(
'
{} already exists.
'
.
format
(
filename
))
continue
# aggregate files for different years into a single file using
# xarray and dask
ds
=
xr
.
open_mfdataset
(
files
,
parallel
=
True
).
compute
()
LogConfig
.
init_log
(
'
Aggregating ERA5 years: {}
'
.
format
(
'
-
'
.
join
([
ymin
,
ymax
])))
LOGGER
.
info
((
'
\n
'
+
(
len
(
__name__
)
+
1
)
*
'
'
).
join
(
[
'
{}
'
.
format
(
file
)
for
file
in
files
]))
# aggregate hourly data to daily data: resample in case of missing
# days
ds
=
ds
.
resample
(
time
=
'
D
'
).
mean
(
dim
=
'
time
'
)
# set NetCDF file compression for each variable
for
_
,
var
in
ds
.
data_vars
.
items
():
var
.
encoding
[
'
zlib
'
]
=
True
var
.
encoding
[
'
complevel
'
]
=
5
# save aggregated netcdf file
LOGGER
.
info
(
'
Compressing NetCDF: {}
'
.
format
(
filename
))
ds
.
to_netcdf
(
filename
,
engine
=
'
h5netcdf
'
)
else
:
LOGGER
.
info
(
'
{} does not exist.
'
.
format
(
str
(
args
.
source
)))
sys
.
exit
()
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