NCAR / ccpp-framework

Common Community Physics Package (CCPP)
http://www.dtcenter.org/community-code/common-community-physics-package-ccpp/
Other
26 stars 63 forks source link

Unit converter does not handle a variable with units = 1 #458

Closed gold2718 closed 1 year ago

gold2718 commented 1 year ago

Description

If the units converter is invoked because a variable is listed in metadata with the same standard name but a different unit and one of the units is "1", an obscure error is generated as the units converter does not know how to handle units = 1.

Steps to Reproduce

195ee0736a05b9bbe26bfcd45390800098be36d7, no modifications

Please provide detailed steps for reproducing the issue.

  1. Create a metadata file with a scheme variable whose units are not 1
  2. Create another metadata file with a scheme variable that has the same Standard Name but with units = 1.
  3. Create a CCPP suite that includes these two schemes.
  4. Run the framework on the suite created in the previous step.

Additional Context

The problem is that the unit converter attempts to convert each unit into a python identifier and 1 does not cooperate.

mkavulich commented 1 year ago

Fix implemented in #462

Old (bad) output:

INFO: Comparing metadata for requested and provided variables ...
Traceback (most recent call last):
  File "ccpp/framework/scripts/ccpp_prebuild.py", line 813, in <module>
    main()
  File "ccpp/framework/scripts/ccpp_prebuild.py", line 767, in main
    (success, modules, metadata) = compare_metadata(metadata_define, metadata_request)
  File "ccpp/framework/scripts/ccpp_prebuild.py", line 473, in compare_metadata
    var.convert_to(metadata_define[var_name][0].units)
  File "/glade/work/kavulich/CCPP/workdir/issue_458/ccpp-scm/ccpp/framework/scripts/mkcap.py", line 191, in convert_to
    function_name = '{0}__to__{1}'.format(string_to_python_identifier(self.units), string_to_python_identifier(units))
  File "/glade/work/kavulich/CCPP/workdir/issue_458/ccpp-scm/ccpp/framework/scripts/common.py", line 197, in string_to_python_identifier
    raise Exception("Resulting string '{0}' is not a valid Python identifier".format(string))
Exception: Resulting string '1' is not a valid Python identifier

After fix:

INFO: Comparing metadata for requested and provided variables ...
Traceback (most recent call last):
  File "/glade/work/kavulich/CCPP/workdir/issue_458/ccpp-scm/ccpp/framework/scripts/mkcap.py", line 193, in convert_to
    function = getattr(unit_conversion, function_name)
AttributeError: module 'conversion_tools.unit_conversion' has no attribute 'one__to__none'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ccpp/framework/scripts/ccpp_prebuild.py", line 813, in <module>
    main()
  File "ccpp/framework/scripts/ccpp_prebuild.py", line 767, in main
    (success, modules, metadata) = compare_metadata(metadata_define, metadata_request)
  File "ccpp/framework/scripts/ccpp_prebuild.py", line 473, in compare_metadata
    var.convert_to(metadata_define[var_name][0].units)
  File "/glade/work/kavulich/CCPP/workdir/issue_458/ccpp-scm/ccpp/framework/scripts/mkcap.py", line 196, in convert_to
    raise Exception('Error, automatic unit conversion from {0} to {1} for {2} in {3} not implemented'.format(self.units, units, self.standard_name, self.container))
Exception: Error, automatic unit conversion from 1 to none for ccpp_error_message in MODULE_cu_gf_driver SCHEME_cu_gf_driver SUBROUTINE_cu_gf_driver_init not implemented