Skip to content

Commit

Permalink
Updates based on bug fixes and feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaxmonsky committed Apr 3, 2024
1 parent 19e1586 commit 680d5a8
Show file tree
Hide file tree
Showing 7 changed files with 819 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Externals_CAM.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ required = True
local_path = src/physics/ncar_ccpp
protocol = git
repo_url = https://github.com/mwaxmonsky/atmospheric_physics
tag = 9705c32c2c2e9b47655a3a057d25b5acbed20210
tag = 8e04841df78634fd38b6826af893485e0ecdbf8f
required = True

[externals_description]
Expand Down
684 changes: 684 additions & 0 deletions src/data/CCPP Standard Names - Sheet1.csv

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions src/data/generate_input_to_stdnames_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import argparse
from collections import defaultdict
from pathlib import Path
from bs4 import BeautifulSoup
import csv
import re


def parse_csv(csv_filepath):
datamap = defaultdict(set)
pattern = re.compile("\w+")
print(f"Opening {csv_filepath}")
with open(csv_filepath) as csvfile:
csvdata = csv.reader(csvfile)
for row in csvdata:
inputname = row[0].split(" ")[0]
standardnameMatch = pattern.fullmatch(row[5].split(" ")[0])
if csvdata.line_num < 432 and standardnameMatch and inputname and "Skipping" not in row[5] and "CCPP" not in row[5]:
print(f"Adding {inputname} under {standardnameMatch.string}")
# if standardnameMatch.string in datamap:
# raise Exception(f"Found duplicate standard name {standardnameMatch.string} on line {csvdata.line_num}")
datamap[standardnameMatch.string].add(inputname)
return datamap



def generate_stdname_xml(current_dict, output_filename):
xmltree = BeautifulSoup(features="xml")

entries = xmltree.new_tag("entries")
for k, v in current_dict.items():
entry = xmltree.new_tag("entry")
entry["stdname"] = k
names = xmltree.new_tag("ic_file_input_names")
for name in v:
namenode = xmltree.new_tag("ic_file_input_name")
namenode.string = name
names.append(namenode)
entry.append(names)
entries.append(entry)
xmltree.append(entries)
with open(output_filename, "w") as xmlout:
print(f"Creating new xml file : {output_filename}")
xmlout.write(xmltree.prettify())


def main():
parser = argparse.ArgumentParser(description='')
parser.add_argument('--csv-file', type=str, default='CCPP Standard Names - Sheet1.csv', help='')
parser.add_argument('--current-map', type=str, default='stdnames_to_inputnames_dictionary.xml', help='')
parser.add_argument('--output-map', type=str, default='stdnames_to_inputnames_dictionary_new.xml', help='')

args = parser.parse_args()

current_csv_entries = parse_csv(args.csv_file)
generate_stdname_xml(current_csv_entries, args.output_map)


if __name__=="__main__":
main()
15 changes: 10 additions & 5 deletions src/data/inputnames_to_stdnames.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def write_new_ncdata_file(input_filename, output_filename, inputname_dict):
base_cmd += f' {input_filename}'
os.system(base_cmd)

def parse_stdname_file(file_to_parse):
def parse_stdname_file(file_to_parse, tphys):
"""Parse XML standard name dictionary"""
with open(file_to_parse, encoding='utf-8') as fh1:
try:
Expand All @@ -35,15 +35,17 @@ def parse_stdname_file(file_to_parse):
for sub_element in entry:
if sub_element.tag == "ic_file_input_names":
for input_name in sub_element:
inputname_dict[input_name.text.strip()] = stdname
if not input_name.text.startswith(tphys):
inputname_dict[input_name.text.strip()] = stdname
# end if startswith
# end for input_name
# end if sub_element.tag
# end if for sub_element in entry
# end if for entry in root
return inputname_dict


def main(input_file, output_filename, stdname_file):
def main(input_file, output_filename, stdname_file, tphys):
"""Parse standard name dictionary and then replace input name variables with stdnames"""
if not os.path.isfile(input_file):
print(f"Input file {input_file} does not exist")
Expand Down Expand Up @@ -74,7 +76,7 @@ def main(input_file, output_filename, stdname_file):
#end if os.path.isdir(output_dir)
#end if len(output_dir.strip())) == 0
# Parse the standard name dictionary
inputname_dict = parse_stdname_file(stdname_file)
inputname_dict = parse_stdname_file(stdname_file, tphys)
if not inputname_dict:
print(f"Standard name dictionary {stdname_file} empty or not parse-able")
return 6
Expand All @@ -97,9 +99,12 @@ def parse_command_line(arguments, description):
parser.add_argument("--stdnames", type=str, required=True,
metavar='stdname file',
help="Full path to the standard names dictionary (e.g. stdnames_to_inputnames_dictionary.xml)")
parser.add_argument('--tphys', type=str, required=True,
metavar='tphysac or tphybs group - REQUIRED',
help='Group to convert to stdandard names')
pargs = parser.parse_args(arguments)
return pargs

if __name__ == "__main__":
ARGS = parse_command_line(sys.argv[1:], __doc__)
sys.exit(main(ARGS.input, ARGS.output, ARGS.stdnames))
sys.exit(main(ARGS.input, ARGS.output, ARGS.stdnames, ARGS.tphys))
8 changes: 0 additions & 8 deletions src/data/registry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,5 @@
<dimensions>horizontal_dimension vertical_layer_dimension</dimensions>
<initial_value>zvir</initial_value>
</variable>
<variable local_name="heating_rate"
standard_name="tendency_of_dry_air_enthalpy_at_constant_pressure"
units="J kg-1 s-1" type="real" kind="kind_phys"
allocatable="allocatable">
<long_name>ff</long_name>
<dimensions>horizontal_dimension vertical_layer_dimension</dimensions>
<ic_file_input_names>tendency_of_air_enthalpy</ic_file_input_names>
</variable>
</file>
</registry>
6 changes: 5 additions & 1 deletion src/data/stdnames_to_inputnames_dictionary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,9 @@
<ic_file_input_name>n2ovmr</ic_file_input_name>
</ic_file_input_names>
</entry>

<entry stdname="standard_gravitational_acceleration">
<ic_file_input_names>
<ic_file_input_name>gravit</ic_file_input_name>
</ic_file_input_names>
</entry>
</entries>
68 changes: 59 additions & 9 deletions src/dynamics/utils/hycoef.F90
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module hycoef

!> \section arg_table_hycoef Argument Table
!! \htmlinclude hycoef.html
real(kind_phys), public, :: etamid(pver) ! hybrid coordinate - midpoints
real(kind_phys), allocatable, public :: etamid(:) ! hybrid coordinate - midpoints

!=======================================================================
contains
Expand All @@ -59,6 +59,7 @@ subroutine hycoef_init(file, psdry)
! use cam_history_support, only: add_hist_coord, add_vert_coord, formula_terms_t
use physconst, only: pref
use string_utils, only: to_str
use phys_vars_init_check, only: mark_as_initialized

!-----------------------------------------------------------------------
!
Expand Down Expand Up @@ -126,10 +127,10 @@ subroutine hycoef_init(file, psdry)
call endrun(subname//': allocate hybm(pver) failed with stat: '//to_str(iret))
end if

!allocate(etamid(pver), stat=iret)
!if (iret /= 0) then
! call endrun(subname//': allocate etamid(pver) failed with stat: '//to_str(iret))
!end if
allocate(etamid(pver), stat=iret)
if (iret /= 0) then
call endrun(subname//': allocate etamid(pver) failed with stat: '//to_str(iret))
end if

allocate(hybd(pver), stat=iret)
if (iret /= 0) then
Expand Down Expand Up @@ -329,6 +330,11 @@ subroutine hycoef_init(file, psdry)
write(iulog,9830) pverp, hypi(pverp)
end if

! Mark etamid (input name) as initialized (by standard name sum_of_sigma_...)
call mark_as_initialized( &
'sum_of_sigma_pressure_hybrid_coordinate_a_coefficient_and_sigma_pressure_hybrid_coordinate_b_coefficient')


9800 format( 1x, i3, 3p, 3(f10.4,10x) )
9810 format( 1x, 3x, 3p, 3(10x,f10.4) )
9820 format(1x,'reference pressures (Pa)')
Expand Down Expand Up @@ -397,26 +403,68 @@ subroutine hycoef_read(File)
character(len=*), parameter :: routine = 'hycoef_read'
!----------------------------------------------------------------------------

! Set PIO to return error codes.
call pio_seterrorhandling(file, PIO_BCAST_ERROR, pio_errtype)

! PIO traps errors internally, no need to check ierr

ierr = PIO_Inq_DimID(File, 'lev', lev_dimid)
if (ierr /= PIO_NOERR) then
ierr = PIO_Inq_DimID(File, 'reference_pressure_in_atmosphere_layer', lev_dimid)
if (ierr /= PIO_NOERR) then
call endrun(routine//': reading lev')
end if
end if
ierr = PIO_Inq_dimlen(File, lev_dimid, flev)
if (pver /= flev) then
write(iulog,*) routine//': ERROR: file lev does not match model. lev (file, model):',flev, pver
call endrun(routine//': ERROR: file lev does not match model.')
end if

ierr = PIO_Inq_DimID(File, 'ilev', lev_dimid)
if (ierr /= PIO_NOERR) then
ierr = PIO_Inq_DimID(File, 'reference_pressure_in_atmosphere_layer_at_interface', lev_dimid)
if (ierr /= PIO_NOERR) then
call endrun(routine//': reading ilev')
end if
end if
ierr = PIO_Inq_dimlen(File, lev_dimid, filev)
if (pverp /= filev) then
write(iulog,*) routine//':ERROR: file ilev does not match model ilev (file, model):',filev, pverp
call endrun(routine//':ERROR: file ilev does not match model.')
end if

ierr = pio_inq_varid(File, 'hyai', hyai_desc)
if (ierr /= PIO_NOERR) then
ierr = pio_inq_varid(File, 'sigma_pressure_hybrid_coordinate_a_coefficient_at_interface', hyai_desc)
if (ierr /= PIO_NOERR) then
call endrun(routine//': reading hyai')
end if
end if

ierr = pio_inq_varid(File, 'hyam', hyam_desc)
if (ierr /= PIO_NOERR) then
ierr = pio_inq_varid(File, 'sigma_pressure_hybrid_coordinate_a_coefficient', hyam_desc)
if (ierr /= PIO_NOERR) then
call endrun(routine//': reading hyam')
end if
end if

ierr = pio_inq_varid(File, 'hybi', hybi_desc)
if (ierr /= PIO_NOERR) then
ierr = pio_inq_varid(File, 'sigma_pressure_hybrid_coordinate_b_coefficient_at_interface', hybi_desc)
if (ierr /= PIO_NOERR) then
call endrun(routine//': reading hybi')
end if
end if

ierr = pio_inq_varid(File, 'hybm', hybm_desc)
if (ierr /= PIO_NOERR) then
ierr = pio_inq_varid(File, 'sigma_pressure_hybrid_coordinate_b_coefficient', hybm_desc)
if (ierr /= PIO_NOERR) then
call endrun(routine//': reading hybm')
end if
end if

ierr = pio_get_var(File, hyai_desc, hyai)
ierr = pio_get_var(File, hybi_desc, hybi)
Expand All @@ -428,11 +476,13 @@ subroutine hycoef_read(File)
end if

! Check whether file contains value for P0. If it does then use it

! Set PIO to return error codes.
call pio_seterrorhandling(file, PIO_BCAST_ERROR, pio_errtype)

ierr = pio_inq_varid(file, 'P0', p0_desc)
if (ierr /= PIO_NOERR) then
ierr = pio_inq_varid(File, 'reference_pressure', p0_desc)
!if (ierr /= PIO_NOERR) then
! call endrun(routine//': reading P0')
!end if
end if
if (ierr == PIO_NOERR) then
ierr = pio_get_var(file, p0_desc, ps0)
if (ierr /= PIO_NOERR) then
Expand Down

0 comments on commit 680d5a8

Please sign in to comment.