Skip to content

Commit 553b7c1

Browse files
authored
Merge pull request #237 from ceblanton/236.combine-statics
Two combine-static updates
2 parents 7ec9437 + aa56814 commit 553b7c1

12 files changed

Lines changed: 213 additions & 26 deletions

app/combine-statics/bin/combine-statics

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ echo " input dir: $inputDir"
1111
echo " output dir: $outputDir"
1212
echo Utilities:
1313
type cdo
14+
type ncatted
1415

1516
# Verify input directory exists and is a directory
1617
if [[ ! -d $inputDir ]]; then
@@ -28,16 +29,21 @@ cd $inputDir
2829

2930
for comp in $(ls); do
3031
pushd $comp/P0Y/P0Y || continue
31-
3232
mkdir -p $outputDir/$comp
33-
cdo -O merge *.nc $outputDir/$comp/$comp.static.nc
34-
35-
# #check
36-
# if [ -z "$( ls $outputDir/$comp)" ]; then
37-
# echo "empty"
38-
# else
39-
# echo "$(ls $outputDir/$comp)"
40-
# fi
33+
34+
# Remove the coordinates attribute (https://github.com/NOAA-GFDL/FMS/issues/1859)
35+
for file in *.nc; do
36+
ncatted -a coordinates,,d,, $file
37+
done
38+
39+
# Tiled and non-tiled output
40+
if ls *.tile1.nc; then
41+
for tile in tile1 tile2 tile3 tile4 tile5 tile6; do
42+
cdo -O merge *.$tile.nc $outputDir/$comp/$comp.static.$tile.nc
43+
done
44+
else
45+
cdo -O merge *.nc $outputDir/$comp/$comp.static.nc
46+
fi
4147

4248
popd
4349
done
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
netcdf atmos_cubedsphere_1.tile1 {
2+
dimensions:
3+
grid_xt = 2 ;
4+
grid_yt = 2 ;
5+
variables:
6+
float orog(grid_yt, grid_xt) ;
7+
orog:long_name = "surface geopotential height" ;
8+
orog:units = "m" ;
9+
orog:missing_value = 1.e+20f ;
10+
orog:_FillValue = 1.e+20f ;
11+
float grid_xt(grid_xt) ;
12+
float grid_yt(grid_yt) ;
13+
data:
14+
grid_xt = 0, 1 ;
15+
grid_yt = 0, 1 ;
16+
orog = 1, 2, 3, 4 ;
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
netcdf atmos_cubedsphere_1.tile2 {
2+
dimensions:
3+
grid_xt = 2 ;
4+
grid_yt = 2 ;
5+
variables:
6+
float orog(grid_yt, grid_xt) ;
7+
orog:long_name = "surface geopotential height" ;
8+
orog:units = "m" ;
9+
orog:missing_value = 1.e+20f ;
10+
orog:_FillValue = 1.e+20f ;
11+
float grid_xt(grid_xt) ;
12+
float grid_yt(grid_yt) ;
13+
data:
14+
grid_xt = 0, 1 ;
15+
grid_yt = 0, 1 ;
16+
orog = 5, 6, 7, 8 ;
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
netcdf atmos_cubedsphere_1.tile3 {
2+
dimensions:
3+
grid_xt = 2 ;
4+
grid_yt = 2 ;
5+
variables:
6+
float orog(grid_yt, grid_xt) ;
7+
orog:long_name = "surface geopotential height" ;
8+
orog:units = "m" ;
9+
orog:missing_value = 1.e+20f ;
10+
orog:_FillValue = 1.e+20f ;
11+
float grid_xt(grid_xt) ;
12+
float grid_yt(grid_yt) ;
13+
data:
14+
grid_xt = 0, 1 ;
15+
grid_yt = 0, 1 ;
16+
orog = 9, 10, 11, 12 ;
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
netcdf atmos_cubedsphere_1.tile4 {
2+
dimensions:
3+
grid_xt = 2 ;
4+
grid_yt = 2 ;
5+
variables:
6+
float orog(grid_yt, grid_xt) ;
7+
orog:long_name = "surface geopotential height" ;
8+
orog:units = "m" ;
9+
orog:missing_value = 1.e+20f ;
10+
orog:_FillValue = 1.e+20f ;
11+
float grid_xt(grid_xt) ;
12+
float grid_yt(grid_yt) ;
13+
data:
14+
grid_xt = 0, 1 ;
15+
grid_yt = 0, 1 ;
16+
orog = 13, 14, 15, 16 ;
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
netcdf atmos_cubedsphere_1.tile5 {
2+
dimensions:
3+
grid_xt = 2 ;
4+
grid_yt = 2 ;
5+
variables:
6+
float orog(grid_yt, grid_xt) ;
7+
orog:long_name = "surface geopotential height" ;
8+
orog:units = "m" ;
9+
orog:missing_value = 1.e+20f ;
10+
orog:_FillValue = 1.e+20f ;
11+
float grid_xt(grid_xt) ;
12+
float grid_yt(grid_yt) ;
13+
data:
14+
grid_xt = 0, 1 ;
15+
grid_yt = 0, 1 ;
16+
orog = 17, 18, 19, 20 ;
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
netcdf atmos_cubedsphere_1.tile6 {
2+
dimensions:
3+
grid_xt = 2 ;
4+
grid_yt = 2 ;
5+
variables:
6+
float orog(grid_yt, grid_xt) ;
7+
orog:long_name = "surface geopotential height" ;
8+
orog:units = "m" ;
9+
orog:missing_value = 1.e+20f ;
10+
orog:_FillValue = 1.e+20f ;
11+
float grid_xt(grid_xt) ;
12+
float grid_yt(grid_yt) ;
13+
data:
14+
grid_xt = 0, 1 ;
15+
grid_yt = 0, 1 ;
16+
orog = 21, 22, 23, 24 ;
17+
}

app/combine-statics/tests/test_combine_statics.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
"atmos_static_scalar_2.ak.nc",
2828
"atmos_static_scalar_3.ck.nc"]
2929

30+
# Tiled (cubed-sphere) component
31+
TILED_COMP_NAME = "atmos_cubedsphere"
32+
TILED_CDLFILES = [f"atmos_cubedsphere_1.tile{t}.cdl" for t in range(1, 7)]
33+
TILED_NCFILES = [f"atmos_cubedsphere_1.tile{t}.nc" for t in range(1, 7)]
34+
3035
# Input/Output directories
3136
# OutDir for generating netcdf files; used as input for combine-statics task
3237
NCGEN_OUT = f"{TEST_DIR}/test-output/ncgen-output"
@@ -196,6 +201,71 @@ def test_combine_statics_output_data(load_ncfile):
196201
assert (val == expected_data).all()
197202
#print(f"{v} has values: {val}")
198203

204+
#### TILED (CUBED-SPHERE) TEST CASE ####
205+
# NOTE: The tiled component directory is created inside test_ncgen_tiled_nc_files
206+
# rather than at module level. An empty component directory would cause
207+
# `cdo merge *.nc` to fail when the non-tiled test_combine_statics runs first.
208+
209+
def test_tiled_cdl_files_exist():
210+
"""
211+
Test for the existence of tiled CDL test files (one per cube face)
212+
"""
213+
assert all(Path(f"{DATA_DIR}/{f}").exists() for f in TILED_CDLFILES)
214+
215+
def test_ncgen_tiled_nc_files():
216+
"""
217+
Generate tiled netCDF files from CDL files; test they exist in the
218+
correct input location for combine-statics
219+
"""
220+
Path(f"{NCGEN_OUT}/{TILED_COMP_NAME}/P0Y/P0Y").mkdir(parents=True, exist_ok=True)
221+
for cdl, nc in zip(TILED_CDLFILES, TILED_NCFILES):
222+
output = f"{NCGEN_OUT}/{TILED_COMP_NAME}/P0Y/P0Y/{nc}"
223+
ex = ["ncgen", "-k", "64-bit offset",
224+
"-o", output,
225+
f"{DATA_DIR}/{cdl}"]
226+
sp = subprocess.run(ex, check=True)
227+
assert all([sp.returncode == 0,
228+
Path(output).exists()])
229+
230+
def test_combine_statics_tiled(monkeypatch):
231+
"""
232+
Test that combine-statics produces one output file per tile for a
233+
tiled (cubed-sphere) component
234+
"""
235+
monkeypatch.setenv("inputDir", NCGEN_OUT)
236+
monkeypatch.setenv("outputDir", COMBINE_STATICS_OUT)
237+
238+
script = f"{COMBINE_STATICS_DIR}/bin/combine-statics"
239+
sp = subprocess.run(script, check=True)
240+
241+
assert sp.returncode == 0
242+
for t in range(1, 7):
243+
assert Path(f"{COMBINE_STATICS_OUT}/{TILED_COMP_NAME}/{TILED_COMP_NAME}.static.tile{t}.nc").is_file()
244+
245+
@pytest.fixture(name="load_tiled_ncfile")
246+
def load_tiled_dataset():
247+
"""
248+
Load tile1 output file using xarray for content validation
249+
"""
250+
outfile = f"{TILED_COMP_NAME}.static.tile1.nc"
251+
sf = xr.open_dataset(f"{COMBINE_STATICS_OUT}/{TILED_COMP_NAME}/{outfile}")
252+
yield sf
253+
sf.close()
254+
255+
def test_combine_statics_tiled_output_variables(load_tiled_ncfile):
256+
"""
257+
Test that tiled output file contains the expected variable
258+
"""
259+
assert "orog" in load_tiled_ncfile.data_vars
260+
261+
def test_combine_statics_tiled_output_data(load_tiled_ncfile):
262+
"""
263+
Test that tile1 output contains the expected data values
264+
"""
265+
expected = np.array([[1, 2], [3, 4]], dtype=np.float32)
266+
assert (load_tiled_ncfile["orog"].values == expected).all()
267+
268+
199269
#####assert only one lat and lon exists...........
200270
# TO-DO: Having issues trying to generate an expected failure when
201271
# adding a static netcdf file that should fail at cdo merge

site/gaea.cylc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@
4343

4444
{% if DO_REGRID_STATIC or DO_NATIVE_STATIC %}
4545
[[combine-statics]]
46-
pre-script = module load cdo
46+
pre-script = module load cdo nco
4747
{% endif %}

site/gfdl-ws.cylc

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)