Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor omPriorVerify.py into tests #14

Merged
merged 23 commits into from
May 30, 2024
Merged

Conversation

crdanielbusch
Copy link
Contributor

@crdanielbusch crdanielbusch commented May 21, 2024

Description

  • Remove omPriorVerify.py call from omPrior.py
  • Refactor tests from omPriorVerify.py in test suite - see tests 005 and 006
  • Combine all tests in one module
  • Add a fixtures that downloads input files
  • Add a fixture that runs the whole process and references the input files fixture
  • Fix CI
  • Add a time out for the tests (currently at 10 min)

Checklist

Please confirm that this pull request has done the following:

  • Tests added
  • Documentation added (where applicable)

Notes

@crdanielbusch crdanielbusch changed the title Refactor omPriorVerify.py into tests Refactor omPriorVerify.py into tests May 21, 2024
@crdanielbusch crdanielbusch self-assigned this May 21, 2024
@crdanielbusch crdanielbusch requested a review from lewisjared May 29, 2024 09:05
# TODO Update this test when file structure is clear.
# This test ensures that the grid size for all input files is 10 km.
def test_006_grid_size_for_geo_files(root_dir, monkeypatch) :
expected_cell_size = 10000
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what exactly to test here. Right now the test ensure that the grid size for geo_em.d01.nc, GRIDCRO2D_1, GRIDDOT2D_1 is 10 km.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial thought was lat/lons, but then I realised that the GRIDCRO/GRIDDOT are a subset of the WRF domain.

That could be a test for after omCreateDomainInfo.py is run, but not applicable here

Copy link
Collaborator

@lewisjared lewisjared left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. There are a few suggestions for other things to test. Whether this comes in the PR or later is up to you.


def test_full_process(num_regression, root_dir, monkeypatch):
os.remove(filepath)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that filepath uses the tmpdir fixture, the files are automatically cleaned up so don't need to be removed. Therefore this line isn't needed

If you really wanted to be sure that something is cleaned up after the test you should put it in a try finally block. Otherwise the test might exit early before the remove call. Or you could have a fixture that uses yield like you did above

try:
    create_file()
    this_might_fail()
finally:
   # This is always called even if an exception is raised in `this_migh_fail()`
    if filepath.exists():
        filepath.remove()



# Fixture to download and later remove all input files
@pytest.fixture(scope="session")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixture can probably move into conftest.py as it could be used in other places. Also since it is session scoped even if you use it in multiple test modules it will still only run once

# TODO Update this test when file structure is clear.
# This test ensures that the grid size for all input files is 10 km.
def test_006_grid_size_for_geo_files(root_dir, monkeypatch) :
expected_cell_size = 10000
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial thought was lat/lons, but then I realised that the GRIDCRO/GRIDDOT are a subset of the WRF domain.

That could be a test for after omCreateDomainInfo.py is run, but not applicable here

scripts/omPrior.py Show resolved Hide resolved

@pytest.fixture(scope="session")
def output_domain_file(root_dir,
input_files
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look ruffed

filepath_in_domain = os.path.join(root_dir, "inputs/om-domain-info.nc")

# Generated by scripts/omPrior.py
filepath_out_domain = os.path.join(root_dir, "outputs/out-om-domain-info.nc")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a new test that verifies that this output file has the same lat/lons that are in the CRO/DOT files. filepath_in_domain should have the same dimensions as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that GRIDCRO2D_1 and GRIDDOT2D_1 have different dimensions?

In this test I compare COL and ROW of GRIDCRO2D_1 and GRIDDOT2D_1 with inputs/om-domain-info.nc.

The number of columns and rows in GRIDCRO2D_1 is one below the number of columns and rows in GRIDDOT2D_1:

GRIDCRO2D_1 contains the attributes ROW: 430, COL: 454

and

GRIDDOT2D_1 contains the attributes ROW: 431, COL: 455.

In om-domain-info.nc there are the attributes ROW: 430 and ROW_D: 431.
So I can compare ROW with GRIDCRO2D_1 and ROW_D with GRIDDOT2D_1?

The test that I am adding looks like this. It's passing, but I am not sure if the test makes sense.

def test_007_compare_in_domain_with_cro_dot_files(input_domain_xr, cro_xr, dot_xr):

    assert dot_xr.NCOLS == input_domain_xr.COL_D.size
    assert dot_xr.NROWS == input_domain_xr.ROW_D.size

    assert cro_xr.NCOLS == input_domain_xr.COL.size
    assert cro_xr.NROWS == input_domain_xr.ROW.size

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WRF and CMAQ use a staggered grid (https://amps-backup.ucar.edu/information/configuration/wrf_grid_structure.html) where depending on the quantity that you are measuring the grid points are slightly different. Things like temperature/pressure are defined at the center of a grid cell, where as other quantities such as wind are defined at the edges of a grid cell. If you count the number of edges in a grid along a x or y it is one larger than the number of cells in x or y.

The CRO/DOT files are generated by MCIP (?) from the WRF domain and define the domain used by CMAQ.
The CRO files define the latitude centers where as the DOT files define the corners hence why ROW_D = ROW + 1. I don't know why they are named as they are 🤷

The CMAQ domain is a little smaller than the WRF domain because the 5 grid cells around the edge of the domain are excluded. I can talk you through why that is another day.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this website, CRO is short for cross and dot.

An image of the CMAQ grid: https://www.cmascenter.org/ioapi/documentation/all_versions/html/GRIDS.jpg
and the interaction between the grid and "thickened boundary" https://www.cmascenter.org/ioapi/documentation/all_versions/html/THKBDY.jpg. The outside is the WRF domain, but the inner cells labelled "main grid" is the CMAQ grid

Copy link
Contributor Author

@crdanielbusch crdanielbusch May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks. I think I have understood it to some degree.

Is the difference between CMAQ domain and WRF domain (the thickened boundary) unrelated to the mass grid / staggered grid problem? And it wouldn't be relevant to the tests here anyway, would it?

I've added the tests 007 and 008.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. It is unrelated. If you compared the grid sizes in the raw input domain geo_em.d01.nc you would see that they don't align with the CMAQ grid

@prayner
Copy link
Contributor

prayner commented May 30, 2024 via email

@lewisjared
Copy link
Collaborator

I'm worried about hard coding a 10km test since subdomains will be
finer resolution

@prayner In this case we are testing that the example domain that is checked into the repo is consistent. These tests aren't run for every new domain (maybe they should eventually)

@crdanielbusch crdanielbusch merged commit e585743 into main May 30, 2024
1 check passed
@crdanielbusch crdanielbusch deleted the prior-verify-emissions-test branch May 30, 2024 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants