Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
thopri
PyNEMO
Commits
129c5043
Commit
129c5043
authored
5 years ago
by
thopri
Browse files
Options
Download
Email Patches
Plain Diff
unit test script now generating data and testing constant temp
parent
b79e2646
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
27 deletions
+48
-27
unit_tests/gen_tools.py
unit_tests/gen_tools.py
+4
-4
unit_tests/namelist_unit_test.bdy
unit_tests/namelist_unit_test.bdy
+2
-2
unit_tests/test_gen.py
unit_tests/test_gen.py
+16
-11
unit_tests/unit_test.py
unit_tests/unit_test.py
+26
-10
No files found.
unit_tests/gen_tools.py
View file @
129c5043
...
...
@@ -518,8 +518,8 @@ def write_mask(fileout,grid_h,grid_z):
nav_lat
[:,
:]
=
grid_h
[
'latt'
].
T
nav_lev
[:]
=
grid_z
[
'dept_1d'
]
threeD_mask
=
np
.
zero
s
(
np
.
shape
(
grid_z
[
'e3t'
]))
twoD_mask
=
np
.
zero
s
(
np
.
shape
(
grid_h
[
'e1t'
]))
threeD_mask
=
np
.
one
s
(
np
.
shape
(
grid_z
[
'e3t'
]))
twoD_mask
=
np
.
one
s
(
np
.
shape
(
grid_h
[
'e1t'
]))
fmask
[:,:,:]
=
threeD_mask
.
T
fmaskutil
[:,:]
=
twoD_mask
.
T
...
...
@@ -550,7 +550,7 @@ def write_parameter(fileout, grid_h,grid_z,params):
# Get input size and create appropriate dimensions
# TODO: add some sort of error handling
nt
=
2
nt
=
31
nx
,
ny
,
nz
=
np
.
shape
(
grid_z
[
'e3t'
])
dataset
.
createDimension
(
'x'
,
nx
)
dataset
.
createDimension
(
'y'
,
ny
)
...
...
@@ -576,7 +576,7 @@ def write_parameter(fileout, grid_h,grid_z,params):
longitude
[:,
:]
=
grid_h
[
'lont'
].
T
latitude
[:,
:]
=
grid_h
[
'latt'
].
T
depth
[:]
=
grid_z
[
'dept_1d'
]
time_counter
[:]
=
(
587340.00
,
588060.00
)
time_counter
[:]
=
np
.
linspace
(
587340.00
,
588060.00
,
31
)
parameter
=
dataset
.
createVariable
(
str
(
params
[
'name'
]),
np
.
float64
,
(
'time'
,
'z'
,
'y'
,
'x'
))
parameter
.
units
,
parameter
.
long_name
=
str
(
params
[
'units'
]),
str
(
params
[
'longname'
])
value_fill
=
np
.
ones
(
np
.
shape
(
grid_z
[
'e3t'
]))
...
...
This diff is collapsed.
Click to expand it.
unit_tests/namelist_unit_test.bdy
View file @
129c5043
...
...
@@ -35,7 +35,7 @@
sn_dst_hgr = '/Users/thopri/Projects/PyNEMO/unit_tests/test_data/test_dst_hgr_zps.nc'
sn_dst_zgr = '/Users/thopri/Projects/PyNEMO/unit_tests/test_data/test_dst_zgr_zps.nc'
sn_src_msk = '/Users/thopri/Projects/PyNEMO/unit_tests/test_data/mask.nc'
sn_bathy = '/Users/thopri/Projects/PyNEMO/unit_tests/test_data/dst_bathy.nc'
sn_bathy = '/Users/thopri/Projects/PyNEMO/unit_tests/test_data/
test_
dst_bathy.nc'
!------------------------------------------------------------------------------
! I/O
...
...
@@ -45,7 +45,7 @@
sn_fn = 'unit_test' ! prefix for output files
nn_fv = -1e20 ! set fill value for output files
nn_src_time_adj = 0 ! src time adjustment
sn_dst_metainfo = '
Benchmarking
Data'
sn_dst_metainfo = '
Unit Test
Data'
!------------------------------------------------------------------------------
! CMEMS Data Source Configuration
...
...
This diff is collapsed.
Click to expand it.
unit_tests/test_gen.py
View file @
129c5043
...
...
@@ -48,7 +48,10 @@ def _main():
grid_z2
=
gt
.
set_zgrid
(
grid_h2
,
jpk
,
max_dep
,
min_dep
,
z_end_dim
)
write_coord_H
=
gt
.
write_coord_H
(
h_fname
,
grid_h2
)
write_coord_Z
=
gt
.
write_coord_Z
(
z_fname
,
grid_h2
,
grid_z2
)
if
write_coord_H
+
write_coord_Z
==
0
:
# write bathy files (constant bathy)
bathy_fname
=
'unit_tests/test_data/test_dst_bathy.nc'
bathy
=
gt
.
write_bathy
(
bathy_fname
,
grid_h2
,
grid_z2
)
if
write_coord_H
+
write_coord_Z
+
bathy
==
0
:
print
(
"Success!"
)
# set rotation and origin point
...
...
@@ -66,7 +69,10 @@ def _main():
grid_rot
[
'latf'
],
grid_rot
[
'lonf'
]
=
gt
.
rotate_around_point
(
grid_h2
[
'latf'
],
grid_h2
[
'lonf'
],
theta
,
origin
)
write_coord_H
=
gt
.
write_coord_H
(
rot_h_fname
,
grid_rot
)
write_coord_Z
=
gt
.
write_coord_Z
(
rot_z_fname
,
grid_rot
,
grid_z2
)
if
write_coord_H
+
write_coord_Z
==
0
:
# write bathy files (constant bathy)
bathy_fname
=
'unit_tests/test_data/test_rot_dst_bathy.nc'
bathy
=
gt
.
write_bathy
(
bathy_fname
,
grid_rot
,
grid_z2
)
if
write_coord_H
+
write_coord_Z
+
bathy
==
0
:
print
(
"Success!"
)
# offset grid
...
...
@@ -81,25 +87,22 @@ def _main():
min_dep
=
10
z_end_dim
=
1
sf
=
10
h_fname
=
'unit_tests/test_data/test_
dst_
offset_hgr_zps.nc'
z_fname
=
'unit_tests/test_data/test_
dst_
offset_zgr_zps.nc'
h_fname
=
'unit_tests/test_data/test_offset_
dst_
hgr_zps.nc'
z_fname
=
'unit_tests/test_data/test_offset_
dst_
zgr_zps.nc'
grid_h3
=
gt
.
set_hgrid
(
dx
,
dy
,
jpi
,
jpj
,
zoffx
,
zoffy
,
sf
)
grid_z3
=
gt
.
set_zgrid
(
grid_h2
,
jpk
,
max_dep
,
min_dep
,
z_end_dim
)
write_coord_H
=
gt
.
write_coord_H
(
h_fname
,
grid_h3
)
write_coord_Z
=
gt
.
write_coord_Z
(
z_fname
,
grid_h3
,
grid_z3
)
if
write_coord_H
+
write_coord_Z
==
0
:
# write bathy files (constant bathy)
bathy_fname
=
'unit_tests/test_data/test_offset_dst_bathy.nc'
bathy
=
gt
.
write_bathy
(
bathy_fname
,
grid_h3
,
grid_z3
)
if
write_coord_H
+
write_coord_Z
+
bathy
==
0
:
print
(
"Success!"
)
# plot orginal, rotatated and source lat and lon
gt
.
plot_grids
(
grid_h2
[
'latt'
],
grid_h2
[
'lont'
],
grid_rot
[
'latt'
],
grid_rot
[
'lont'
],
grid_h3
[
'latt'
],
\
grid_h3
[
'lont'
],
grid_h1
[
'latt'
],
grid_h1
[
'lont'
])
# write bathy files (constant bathy)
bathy_fname
=
'unit_tests/test_data/dst_bathy.nc'
bathy
=
gt
.
write_bathy
(
bathy_fname
,
grid_h2
,
grid_z2
)
if
bathy
==
0
:
print
(
'Success!'
)
# write boundary files (constant parameters)
out_fname
=
'unit_tests/test_data/output_boundary_T.nc'
params
=
{
'name'
:
'thetao'
,
'const_value'
:
15.0
,
'longname'
:
'temperature'
,
'units'
:
'degreesC'
}
...
...
@@ -113,5 +116,7 @@ def _main():
if
mask
==
0
:
print
(
'Success!'
)
return
0
if
__name__
==
'__main__'
:
_main
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
unit_tests/unit_test.py
View file @
129c5043
...
...
@@ -9,11 +9,19 @@ import numpy as np
import
glob
import
os
def
test_run
():
stdout
,
stderr
=
Popen
([
'pynemo'
,
'-s'
,
'unit_tests/namelist_unit_test.bdy'
],
stdout
=
PIPE
,
stderr
=
PIPE
,
universal_newlines
=
True
).
communicate
()
assert
'Execution Time'
in
stdout
# generate test data by import test gen script and executing main function
import
unit_tests.test_gen
as
tg
gen_data
=
tg
.
_main
()
if
gen_data
!=
0
:
raise
Exception
(
'DONT PANIC: Input data generation failed'
)
# run PyNEMO with test data
stdout
,
stderr
=
Popen
([
'pynemo'
,
'-s'
,
'unit_tests/namelist_unit_test.bdy'
],
stdout
=
PIPE
,
stderr
=
PIPE
,
universal_newlines
=
True
).
communicate
()
if
'Execution Time'
not
in
stdout
:
raise
Exception
(
'DONT PANIC: Test Run Failed'
)
# perform tests
def
test_temp
():
test_files
=
glob
.
glob
(
'unit_tests/test_outputs/unit_test*'
)
for
t
in
test_files
:
...
...
@@ -36,10 +44,18 @@ def test_temp():
# assert abs(sal_[sal_ != 0.0].max() - 35) <= 0.001
# assert abs(sal_[sal_ != 0.0].min() - 35) <= 0.001
def
test_rm_output
():
files
=
glob
.
glob
(
'unit_tests/test_outputs/*'
)
for
f
in
files
:
os
.
remove
(
f
)
files
=
glob
.
glob
(
'unit_tests/test_outputs/*'
)
assert
len
(
files
)
==
0
# clean up test I/O
files
=
glob
.
glob
(
'unit_tests/test_outputs/*'
)
for
f
in
files
:
os
.
remove
(
f
)
files
=
glob
.
glob
(
'unit_tests/test_outputs/*'
)
if
len
(
files
)
!=
0
:
raise
Exception
(
'DONT PANIC: output folder not cleaned'
)
files
=
glob
.
glob
(
'unit_tests/test_data/*'
)
for
f
in
files
:
os
.
remove
(
f
)
files
=
glob
.
glob
(
'unit_tests/test_data/*'
)
if
len
(
files
)
!=
0
:
raise
Exception
(
'DONT PANIC: input folder not cleaned'
)
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
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