Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
synergy
o3skim
Commits
db1495cb
Commit
db1495cb
authored
Aug 24, 2020
by
BorjaEst
Browse files
Move sources.py utils functions to utils.py
parent
35ad9caf
Changes
4
Show whitespace changes
Inline
Side-by-side
o3skim/sources.py
View file @
db1495cb
"""This module creates the sources objects"""
import
glob
import
yaml
import
netCDF4
import
xarray
as
xr
import
os.path
def
load
(
yaml_file
):
"""Loads the .yaml file with the sources configurations"""
with
open
(
yaml_file
,
"r"
)
as
ymlfile
:
return
yaml
.
load
(
ymlfile
)
from
.
import
utils
class
Source
:
...
...
@@ -32,16 +25,16 @@ class Model:
"""Standarised model with standarised variables"""
def
__init__
(
self
,
variables
):
self
.
get_tco3_zm
(
**
variables
)
self
.
get_vrm_zm
(
**
variables
)
self
.
__
get_tco3_zm
(
**
variables
)
self
.
__
get_vrm_zm
(
**
variables
)
def
skim
(
self
,
path
):
if
hasattr
(
self
,
'_tco3_zm'
):
to_netcdf
(
path
,
"tco3_zm"
,
self
.
_tco3_zm
)
utils
.
to_netcdf
(
path
,
"tco3_zm"
,
self
.
_tco3_zm
)
if
hasattr
(
self
,
'_vrm_zm'
):
to_netcdf
(
path
,
"vrm_zm"
,
self
.
_vrm_zm
)
utils
.
to_netcdf
(
path
,
"vrm_zm"
,
self
.
_vrm_zm
)
def
get_tco3_zm
(
self
,
tco3_zm
=
None
,
**
kwarg
):
def
__
get_tco3_zm
(
self
,
tco3_zm
=
None
,
**
kwarg
):
"""Gets and standarises the tco3_zm data"""
if
tco3_zm
:
fnames
=
glob
.
glob
(
tco3_zm
[
'dir'
]
+
"/*.nc"
)
...
...
@@ -53,7 +46,7 @@ class Model:
tco3_zm
[
'coordinades'
][
'lon'
]:
'lon'
})[
'tco3_zm'
].
to_dataset
()
def
get_vrm_zm
(
self
,
vrm_zm
=
None
,
**
kwarg
):
def
__
get_vrm_zm
(
self
,
vrm_zm
=
None
,
**
kwarg
):
"""Gets and standarises the vrm_zm data"""
if
vrm_zm
:
fnames
=
glob
.
glob
(
vrm_zm
[
'dir'
]
+
"/*.nc"
)
...
...
@@ -66,16 +59,3 @@ class Model:
})[
'tco3_zm'
].
to_dataset
()
def
create_empty_netCDF
(
fname
):
"""Creates a new empty netCDF file"""
root_grp
=
netCDF4
.
Dataset
(
fname
,
'w'
,
format
=
'NETCDF4'
)
root_grp
.
description
=
'Example simulation data'
root_grp
.
close
()
def
to_netcdf
(
path
,
name
,
dataset
):
"""Creates or appends data to named netcdf files"""
years
,
dsx
=
zip
(
*
dataset
.
groupby
(
"time.year"
))
fnames
=
[
path
+
"/"
+
name
+
"_%s.nc"
%
y
for
y
in
years
]
[
create_empty_netCDF
(
fn
)
for
fn
in
fnames
if
not
os
.
path
.
isfile
(
fn
)]
xr
.
save_mfdataset
(
dsx
,
fnames
,
mode
=
'a'
)
o3skim/utils.py
View file @
db1495cb
"""This module offers some utils for code management"""
from
contextlib
import
contextmanager
import
os
import
yaml
import
netCDF4
import
xarray
as
xr
@
contextmanager
...
...
@@ -12,3 +15,24 @@ def cd(newdir):
yield
finally
:
os
.
chdir
(
prevdir
)
def
load
(
yaml_file
):
"""Loads the .yaml file with the sources configurations"""
with
open
(
yaml_file
,
"r"
)
as
ymlfile
:
return
yaml
.
load
(
ymlfile
)
def
create_empty_netCDF
(
fname
):
"""Creates a new empty netCDF file"""
root_grp
=
netCDF4
.
Dataset
(
fname
,
'w'
,
format
=
'NETCDF4'
)
root_grp
.
description
=
'Example simulation data'
root_grp
.
close
()
def
to_netcdf
(
path
,
name
,
dataset
):
"""Creates or appends data to named netcdf files"""
years
,
dsx
=
zip
(
*
dataset
.
groupby
(
"time.year"
))
fnames
=
[
path
+
"/"
+
name
+
"_%s.nc"
%
y
for
y
in
years
]
[
create_empty_netCDF
(
fn
)
for
fn
in
fnames
if
not
os
.
path
.
isfile
(
fn
)]
xr
.
save_mfdataset
(
dsx
,
fnames
,
mode
=
'a'
)
tests/mockup_data.py
View file @
db1495cb
...
...
@@ -5,7 +5,7 @@ import numpy as np
import
netCDF4
import
os.path
import
datetime
from
o3skim
import
source
s
from
o3skim
import
util
s
base
=
datetime
.
datetime
(
2000
,
1
,
1
)
...
...
@@ -40,4 +40,4 @@ def dataset(name, coordinades):
def
netcdf
(
path
,
name
,
coordinades
,
**
kwarg
):
"""Creates or appends data to a mock netcdf file"""
ds
=
dataset
(
name
,
coordinades
)
source
s
.
to_netcdf
(
path
,
name
,
ds
)
util
s
.
to_netcdf
(
path
,
name
,
ds
)
tests/test_o3skim.py
View file @
db1495cb
...
...
@@ -16,7 +16,7 @@ class TestO3SKIM_sources(unittest.TestCase):
def
setUp
(
self
):
"""Loads and creates the test folders and files from test_sources.yaml"""
self
.
config
=
source
s
.
load
(
"tests/test_sources.yaml"
)
self
.
config
=
util
s
.
load
(
"tests/test_sources.yaml"
)
self
.
assertTrue
(
type
(
self
.
config
)
is
dict
)
self
.
create_mock_datasets
()
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment