tsherwen / AC_tools

Module for working with global/regional Chemical Transport Model (CTM) output and observations
MIT License
13 stars 10 forks source link

Test suite raising errors #89

Open tsherwen opened 4 years ago

tsherwen commented 4 years ago

Running test suite now raises errors. This is the case since updating the module structure.

e.g. py.test

yields the following output:

========================================================== test session starts =========================================================== platform linux -- Python 3.7.2, pytest-4.2.1, py-1.7.0, pluggy-0.8.1 rootdir: /mnt/lustre/users/ts551/labbook/Python_progs/AC_tools, inifile: plugins: remotedata-0.3.1, openfiles-0.3.1, doctestplus-0.1.3, arraydiff-0.3 collected 35 items

test_AC_time.py .. [ 5%] test_GEOSChem_bpch.py .s...FF. [ 28%] test_bpch2netCDF.py sF [ 34%] test_core.py .......... [ 62%] test_funcs4pf.py . [ 65%] test_generic.py . [ 68%] test_plotting.py Essssssss [ 94%] test_variables.py .. [100%]

================================================================= ERRORS ================================================================= ____ ERROR at setup of test_map_plotdefault ____

@pytest.fixture()
def test_data():
    from ..GEOSChem_bpch import get_GC_output
  test_data = get_GC_output(wd, species='O3')

test_plotting.py:23:


wd = '../data', vars = None, species = 'O3', category = 'IJ-AVG-$', r_cubes = False, r_res = False, restore_zero_scaling = True r_list = False, trop_limit = False, dtype = <class 'numpy.float32'>, use_NetCDF = True, verbose = False, debug = False

def get_GC_output(wd, vars=None, species=None, category=None, r_cubes=False,
                  r_res=False, restore_zero_scaling=True, r_list=False, trop_limit=False,
                  dtype=np.float32, use_NetCDF=True, verbose=False, debug=False):
    """
    Return data from a directory containing NetCDF/ctm.bpch files via PyGChem (>= 0.3.0 )

    Parameters
    ----------
    vars (list): variables to extract (in NetCDF form, e.g. ['IJ_AVG_S__CO'])
     ( alterately provide a single species and category through named input variables )
    species (str): species/tracer/variable (gamap) name
    category (str): diagnostic category (gamap) name
    r_cubes (bool): To return Iris Cubes, set to True
    r_res (bool): To return resolution of model NetCDF set to True
    r_list (bool): To return data as list of arrays rather than a single array
    restore_zero_scaling(Boolean): restores scale to ctm.bpch standard (e.g. v/v not pptv)
    trop_limit(bool): limit to "chemical troposphere" (level 38 of model)
    dtype (type): type of variable to be returned
    use_NetCDF(bool): set==True to use NetCDF rather than iris cube of output
    verbose (bool): legacy debug option, replaced by python logging
    debug (bool): legacy debug option, replaced by python logging

    Returns
    -------
    (np.array) of requested output or object of data (iris cube)

    Notes
    -----
     - Core functionality of function:
    ctm.bpch files are extracted for a given directory (to a NetCDF in the directory),
    with only the specific category and species returned. This can either be as a Iris
    Cube (retaining metadata) or as a numpy array.
     - Credit for PyGChem: Ben Bovy - https://github.com/benbovy/PyGChem
     - Examples and use of pygchem is discussed on Ben Bovy's GITHib
     ( https://github.com/benbovy/PyGChem_examples/blob/master/Tutorials/Datasets.ipynb )
     - This replaces the now defunct AC_tools functions: open_ctm_bpch and get_gc_data_np
     - For simplicity use variable names (species, category) as used in
      the iris cube ( e.g. IJ_AVG_S__CO). if variable is unknown, just
      print full dataset extracted to screen to see active diagnostics.
     - Species and category variables are maintained ( and translated ) to allow for
      backwards compatibility with functions written for pygchem version 0.2.0
    """
# bjn
# This function is not completly clear to me, and could do with a re-write
# The try command would probably be useful here for large parts.
# logging would also be good for replacing debug.
    logging.info("Called get_GC_output")
    logging.debug("get_GC_output inputs:")
    logging.debug(locals())

    if not isinstance(vars, type(None)):
        logging.info('Opening >{}<, for var: >{}<'.format(wd, ','.join(vars)) +
                     '(extra) gamap variables provided: >{}< + >{}<'.format(category,
                                                                            species))

    # Also option to use gamap names ( species  + category ) to and func converts these
    # to iris cube names
    if any([(not isinstance(i, type(None))) for i in (species, category)]):
        # convert to Iris Cube name
        if (category == None) and (vars == None):
            category = "IJ-AVG-$"
        if (species == None) and (vars == None):
            species = 'O3'

        # remove scaling for 'IJ-AVG-$' - KLUDGE - needed for all ?
        if category == 'IJ-AVG-$':
          category = diagnosticname_gamap2iris(category)

E NameError: name 'diagnosticname_gamap2iris' is not defined

../GEOSChem_bpch.py:622: NameError ----------------------------------------------------------- Captured log setup ----------------------------------------------------------- GEOSChem_bpch.py 602 INFO Called get_GC_output GEOSChem_bpch.py 603 DEBUG get_GC_output inputs: GEOSChem_bpch.py 604 DEBUG {'wd': '../data', 'vars': None, 'species': 'O3', 'category': None, 'r_cubes': False, 'r_res': False, 'restore_zero_scaling': True, 'r_list': False, 'trop_limit': False, 'dtype': <class 'numpy.float32'>, 'use_NetCDF': True, 'verbose': False, 'debug': False} ================================================================ FAILURES ================================================================ ___ test_get_GC_output ___

def test_get_GC_output():
    arr = get_GC_output(wd=wd, species='O3', category='IJ_AVG_S')
    assert isinstance(arr, np.ndarray), 'GC output is not a numpy array'
  assert round(arr.sum(), 6) == round(

0.14242639, 6), "The ozone budget doesnt seem correct({bud})".format(bud=arr.sum()) E AssertionError: The ozone budget doesnt seem correct(0.14242638647556305) E assert 0.142426 == 0.142426 E + where 0.142426 = round(0.14242639, 6) E + where 0.14242639 = <built-in method sum of numpy.ndarray object at 0x7f0bc868abc0>() E + where <built-in method sum of numpy.ndarray object at 0x7f0bc868abc0> = array([[[[2.4482466e-08],\n [2.4782828e-08],\n [2.5001304e-08],\n ...,\n [1.0980792e-06],\n...877e-08],\n ...,\n [1.0854923e-06],\n [2.3876410e-07],\n [6.5473643e-08]]]], dtype=float32).sum E + and 0.142426 = round(0.14242639, 6)

test_GEOSChem_bpch.py:70: AssertionError ----------------------------------------------------------- Captured log call ------------------------------------------------------------ GEOSChem_bpch.py 602 INFO Called get_GC_output GEOSChem_bpch.py 603 DEBUG get_GC_output inputs: GEOSChem_bpch.py 604 DEBUG {'wd': '../../data', 'vars': None, 'species': 'O3', 'category': 'IJ_AVG_S', 'r_cubes': False, 'r_res': False, 'restore_zero_scaling': True, 'r_list': False, 'trop_limit': False, 'dtype': <class 'numpy.float32'>, 'use_NetCDF': True, 'verbose': False, 'debug': False} GEOSChem_bpch.py 644 DEBUG Opening netCDF file ../../data/ctm.nc GEOSChem_bpch.py 650 DEBUG opening variable IJ_AVG_SO3 variables.py 44 DEBUG Getting unit scaling for ppbv ____ test_get_HEMCO_output_for_WD __

def test_get_HEMCO_output_for_WD():
  arr = get_HEMCO_output(wd=wd, vars='ALD2_TOTAL')

test_GEOSChem_bpch.py:76:


../GEOSChem_bpch.py:512: in get_HEMCO_output hemco_to_netCDF(wd, hemco_file_list=files)


folder = '../../data', hemco_file_list = None, remake = False

def hemco_to_netCDF(folder, hemco_file_list=None, remake=False):
    """
    Conbine HEMCO diagnostic output files to a single NetCDF file.

    Parameters
    ----------
    remake (bool): overwrite existing NetCDF file

    """
    if __package__ is None:
        from .bpch2netCDF import get_folder
    else:
        from .bpch2netCDF import get_folder
    folder = get_folder(folder)
    output_file = os.path.join(folder, 'hemco.nc')

    # If the hemco netCDF file already exists then quit unless remake=True
    if not remake:
        if os.path.exists(output_file):
            logging.warning(output_file + ' already exists, not remaking')
            return

    logging.info("Combining hemco diagnostic files")

    # By default look for any files that look like hemco diagnostic files:
    # Look for all hemco netcdf files then remove the restart files.
    if hemco_file_list == None:
        hemco_files = glob.glob(folder + '/*HEMCO*.nc')
        for filename in hemco_files:
            if "restart" in filename:
                hemco_files.remove(filename)

    else:
        file_list = []
        for hemco_file in hemco_file_list:
            full_path = os.path.join(folder, hemco_file)
            if not os.path.exists(full_path):
                logging.error(full_path + " could not be found")
                raise IOError(
                    "{path} could not be found".format(path=full_path))
            file_list.append(full_path)
        hemco_files = file_list

    if len(hemco_files) == 0:
        logging.warning("No hemco diagnostic files found in {_dir}"
                        .format(_dir=folder))
    else:
        logging.debug("The following hemco files were found:")
        logging.debug(str(hemco_files))

    # Use iris cubes to combine the data into an output file
  hemco_data = iris.load(hemco_files)

E NameError: name 'iris' is not defined

../bpch2netCDF.py:126: NameError ----------------------------------------------------------- Captured log call ------------------------------------------------------------ GEOSChem_bpch.py 497 INFO Called get hemco output. bpch2netCDF.py 96 INFO Combining hemco diagnostic files bpch2netCDF.py 121 DEBUG The following hemco files were found: bpch2netCDF.py 122 DEBUG ['../../data/HEMCO_Diagnostics.nc'] ____ test_getfolder ____

def test_get_folder():
    logging.info("beginning test")
  folder = get_folder(test_file_dir)

test_bpch2netCDF.py:83:


folder = '../data'

def get_folder(folder):
    """
    Get name of folder that contains ctm.bpch data from command line
    """
    if isinstance(folder, type(None)):
       # getting the folder location from system argument
        if len(sys.argv) <= 1:
            logging.warning("No folder location specified for the data")
            folder = os.getcwd()
        else:
            folder = str(sys.argv[1])

    # Check folder exists
    if not os.path.exists(folder):
        print("Folder does not exist")
        print(folder)
      sys.exit()

E SystemExit

../bpch2netCDF.py:284: SystemExit ---------------------------------------------------------- Captured stdout call ---------------------------------------------------------- Folder does not exist ../data ----------------------------------------------------------- Captured log call ------------------------------------------------------------ test_bpch2netCDF.py 82 INFO beginning test ============================================================ warnings summary ============================================================ test_GEOSChem_bpch.py:8 /mnt/lustre/users/ts551/labbook/Python_progs/AC_tools/AC_tools/Tests/test_GEOSChem_bpch.py:8: PytestDeprecationWarning: the pytest.config global is deprecated. Please use request.config or pytest_configure (if you're a pytest plugin) instead. not pytest.config.getoption("--slow"),

test_bpch2netCDF.py:17 /mnt/lustre/users/ts551/labbook/Python_progs/AC_tools/AC_tools/Tests/test_bpch2netCDF.py:17: PytestDeprecationWarning: the pytest.config global is deprecated. Please use request.config or pytest_configure (if you're a pytest plugin) instead. not pytest.config.getoption("--slow"),

test_plotting.py:12 /mnt/lustre/users/ts551/labbook/Python_progs/AC_tools/AC_tools/Tests/test_plotting.py:12: PytestDeprecationWarning: the pytest.config global is deprecated. Please use request.config or pytest_configure (if you're a pytest plugin) instead. not pytest.config.getoption("--slow"),

-- Docs: https://docs.pytest.org/en/latest/warnings.html ================================== 3 failed, 21 passed, 10 skipped, 3 warnings, 1 error in 4.67 seconds ==================================