diff --git a/fv3core/pace/fv3core/initialization/baroclinic.py b/fv3core/pace/fv3core/initialization/baroclinic.py index ba4e994d..3efe722d 100644 --- a/fv3core/pace/fv3core/initialization/baroclinic.py +++ b/fv3core/pace/fv3core/initialization/baroclinic.py @@ -10,7 +10,7 @@ import pace.util.constants as constants from pace.dsl.typing import Float from pace.fv3core.initialization.dycore_state import DycoreState -from pace.util.grid import GridData, lon_lat_midpoint, compute_eta +from pace.util.grid import GridData, compute_eta, lon_lat_midpoint nhalo = fv3util.N_HALO_DEFAULT diff --git a/fv3core/pace/fv3core/initialization/baroclinic_jablonowski_williamson.py b/fv3core/pace/fv3core/initialization/baroclinic_jablonowski_williamson.py index bcf7bd91..dd7dccc3 100644 --- a/fv3core/pace/fv3core/initialization/baroclinic_jablonowski_williamson.py +++ b/fv3core/pace/fv3core/initialization/baroclinic_jablonowski_williamson.py @@ -3,7 +3,7 @@ import numpy as np import pace.util.constants as constants -from pace.util.grid import great_circle_distance_lon_lat, vertical_coordinate, compute_eta +from pace.util.grid import great_circle_distance_lon_lat, vertical_coordinate """ diff --git a/tests/main/grid/__init__.py b/tests/main/grid/__init__.py index 8b137891..e69de29b 100644 --- a/tests/main/grid/__init__.py +++ b/tests/main/grid/__init__.py @@ -1 +0,0 @@ - diff --git a/tests/main/grid/test_eta.py b/tests/main/grid/test_eta.py index 3555670d..2939218f 100755 --- a/tests/main/grid/test_eta.py +++ b/tests/main/grid/test_eta.py @@ -1,58 +1,58 @@ #!/usr/bin/env python3 -import os -import pytest import numpy as np +import pytest + from pace.util.grid.eta import set_hybrid_pressure_coefficients + km = 79 -eta_file='./input/eta79.txt' +eta_file = "./input/eta79.txt" ak_79, bk_79 = np.loadtxt(eta_file, unpack=True) + @pytest.mark.xfail -def test_set_hybrid_pressure_coefficients_nofile( ) : +def test_set_hybrid_pressure_coefficients_nofile(): """ File does not exist. Test should fail """ - pressure_data = set_hybrid_pressure_coefficients( km=90 ) - + pressure_data = set_hybrid_pressure_coefficients(km=90) -def test_set_hybrid_pressure_coefficients_correct( ) : + +def test_set_hybrid_pressure_coefficients_correct(): """ Good values of ak, bk. Test should pass """ ks = 18 ptop = 300.0 - pressure_data = set_hybrid_pressure_coefficients( km ) - - if( not np.array_equal(ak_79,pressure_data.ak) ) : + pressure_data = set_hybrid_pressure_coefficients(km) + + if not np.array_equal(ak_79, pressure_data.ak): raise ValueError("Unexpected values in ak array") - if( not np.array_equal(bk_79,pressure_data.bk) ) : + if not np.array_equal(bk_79, pressure_data.bk): raise ValueError("Unexpected values in bk array") - if( ks != pressure_data.ks ) : + if ks != pressure_data.ks: raise ValueError("Unexpected ks value") - if( ptop != pressure_data.ptop) : + if ptop != pressure_data.ptop: raise ValueError("Unexpected ptopt value") - + @pytest.mark.xfail -def test_set_hybrid_pressure_coefficients_nonincreasing( ) : +def test_set_hybrid_pressure_coefficients_nonincreasing(): """ Array bk is not monotonically increasing. Test is expected to fail """ - ak_cp = ak_79k[:] + ak_cp = ak_79[:] bk_cp = bk_79[:] - bk_cp[10]=0.1 #random number - ak_cp[13]=1.4 #random number - - pressure_data = set_hybrid_pressure_coefficients( km ) + bk_cp[10] = 0.1 # random number + ak_cp[13] = 1.4 # random number - + pressure_data = set_hybrid_pressure_coefficients(km) diff --git a/util/pace/util/grid/__init__.py b/util/pace/util/grid/__init__.py index db25ef97..2a755d0c 100644 --- a/util/pace/util/grid/__init__.py +++ b/util/pace/util/grid/__init__.py @@ -21,9 +21,4 @@ VerticalGridData, ) from .stretch_transformation import direct_transform - -from .utils import ( - vertical_coordinate, - compute_eta, -) - +from .utils import compute_eta, vertical_coordinate diff --git a/util/pace/util/grid/eta.py b/util/pace/util/grid/eta.py index 13aaed9f..4589e943 100644 --- a/util/pace/util/grid/eta.py +++ b/util/pace/util/grid/eta.py @@ -1,8 +1,10 @@ +import os from dataclasses import dataclass -from .utils import compute_eta import numpy as np -import os + +from .utils import compute_eta + @dataclass class HybridPressureCoefficients: @@ -38,27 +40,32 @@ def set_hybrid_pressure_coefficients(km: int) -> HybridPressureCoefficients: """ # set path where the eta file lives - GRID_DIR=os.path.join( os.path.abspath('./'), "input/") + GRID_DIR = os.path.join(os.path.abspath("./"), "input/") # set filename, e.g, eta79.txt for km=79 - eta_file = GRID_DIR + 'eta' + str(km) + '.txt' - if( not os.path.isfile(eta_file) ) : raise IOError("file "+eta_file+" does not exist") + eta_file = GRID_DIR + "eta" + str(km) + ".txt" + if not os.path.isfile(eta_file): + raise IOError("file " + eta_file + " does not exist") # read file into ak, bk arrays ak, bk = np.loadtxt(eta_file, unpack=True) # check size of ak and bk array is km+1 - if ak.size-1 != km : raise ValueError("size of ak array is not equal to km="+str(km)) - if bk.size-1 != km : raise ValueError("size of bk array is not equal to km="+str(km)) + if ak.size - 1 != km: + raise ValueError("size of ak array is not equal to km=" + str(km)) + if bk.size - 1 != km: + raise ValueError("size of bk array is not equal to km=" + str(km)) # check that the eta values computed from ak and bk are monotonically increasing eta, etav = compute_eta(ak, bk) - ( eta_sorted, etav_sorted ) = ( np.sort(eta), np.sort(etav) ) - for i in range( eta.size ) : - if eta_sorted[i] != eta[i] : raise ValueError("ETA values are not monotonically increasing") - if etav_sorted[i] != etav[i] : raise ValueError("ETAV values are not monotonically increasing") - + (eta_sorted, etav_sorted) = (np.sort(eta), np.sort(etav)) + for i in range(eta.size): + if eta_sorted[i] != eta[i]: + raise ValueError("ETA values are not monotonically increasing") + if etav_sorted[i] != etav[i]: + raise ValueError("ETAV values are not monotonically increasing") + if 0.0 in bk: ks = 0 if km == 91 else np.where(bk == 0)[0][-1] ptop = ak[0] @@ -66,5 +73,5 @@ def set_hybrid_pressure_coefficients(km: int) -> HybridPressureCoefficients: raise ValueError("bk must contain at least one 0.") pressure_data = HybridPressureCoefficients(ks, ptop, ak, bk) - + return pressure_data diff --git a/util/pace/util/grid/utils.py b/util/pace/util/grid/utils.py index ad4837b4..17a15568 100644 --- a/util/pace/util/grid/utils.py +++ b/util/pace/util/grid/utils.py @@ -1,10 +1,10 @@ import math -import numpy as np eta_0 = 0.252 surface_pressure = 1.0e5 # units of (Pa), from Table VI of DCMIP2016 + def vertical_coordinate(eta_value): """ Equation (1) JRMS2006 @@ -21,5 +21,3 @@ def compute_eta(ak, bk): eta = 0.5 * ((ak[:-1] + ak[1:]) / surface_pressure + bk[:-1] + bk[1:]) eta_v = vertical_coordinate(eta) return eta, eta_v - -