Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mlee03 authored and mlee03 committed Nov 6, 2023
1 parent b6227d5 commit b571feb
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 46 deletions.
2 changes: 1 addition & 1 deletion fv3core/pace/fv3core/initialization/baroclinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


"""
Expand Down
1 change: 0 additions & 1 deletion tests/main/grid/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

42 changes: 21 additions & 21 deletions tests/main/grid/test_eta.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 1 addition & 6 deletions util/pace/util/grid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 20 additions & 13 deletions util/pace/util/grid/eta.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -38,33 +40,38 @@ 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]
else:
raise ValueError("bk must contain at least one 0.")

pressure_data = HybridPressureCoefficients(ks, ptop, ak, bk)

return pressure_data
4 changes: 1 addition & 3 deletions util/pace/util/grid/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


0 comments on commit b571feb

Please sign in to comment.