Skip to content

Commit

Permalink
tests for CRO/DOT file dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Busch committed May 30, 2024
1 parent 17e6e35 commit 7d25a2d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 42 deletions.
1 change: 1 addition & 0 deletions scripts/omPrior.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@
omWetlandEmis.processEmissions(args.startDate, args.endDate)

omOutputs.sumLayers()
omPriorVerify.verifyEmis()
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

@pytest.fixture(scope="session")
def root_dir():
return Path(__file__).parent.parent
return Path(__file__).parent.parent
108 changes: 67 additions & 41 deletions tests/test_om_prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,27 @@

root_path = Path(__file__).parent.parent
sys.path.insert(1, os.path.join(root_path, "scripts"))
from omDownloadInputs import download_input_files, downloads, remote, sectoralEmissionsPath
from omDownloadInputs import downloads, remote, sectoralEmissionsPath, download_input_files


@pytest.fixture(scope="session")
def output_domain_file(root_dir,
input_files
) :
subprocess.run(["python", os.path.join(root_dir, "scripts/omCreateDomainInfo.py")], check=True)

subprocess.run(["python", os.path.join(root_dir, "scripts/omPrior.py"), "2022-07-01", "2022-07-02"], check=True)

# Generated by scripts/omCreateDomainInfo.py
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")

yield xr.load_dataset(filepath_out_domain)
@pytest.fixture(scope="session")
def cro_xr(root_dir) :
cmaqExamplePath = getenv("CMAQ_EXAMPLE")
croFilePath = os.path.join(root_dir, cmaqExamplePath, getenv("CROFILE"))
return xr.open_dataset(croFilePath)

os.remove(filepath_out_domain)
@pytest.fixture(scope="session")
def dot_xr(root_dir) :
cmaqExamplePath = getenv("CMAQ_EXAMPLE")
dotFilePath = os.path.join(root_dir, cmaqExamplePath, getenv("DOTFILE"))
return xr.open_dataset(dotFilePath)

os.remove(filepath_in_domain)
@pytest.fixture(scope="session")
def geom_xr(root_dir) :
cmaqExamplePath = getenv("CMAQ_EXAMPLE")
geomFilePath = os.path.join(root_dir, cmaqExamplePath, getenv("GEO_EM"))
return xr.open_dataset(geomFilePath)


# Fixture to download and later remove all input files
Expand All @@ -56,6 +55,28 @@ def input_files(root_dir) :
filepath = os.path.join(input_folder, file)
os.remove(filepath)

@pytest.fixture(scope="session")
def input_domain_xr(root_dir, input_files) :
subprocess.run(["python", os.path.join(root_dir, "scripts/omCreateDomainInfo.py")], check=True)

# Generated by scripts/omCreateDomainInfo.py
filepath_in_domain = os.path.join(root_dir, "inputs/om-domain-info.nc")

yield xr.load_dataset(filepath_in_domain)

os.remove(filepath_in_domain)


@pytest.fixture(scope="session")
def output_domain_xr(root_dir, input_domain_xr) :
subprocess.run(["python", os.path.join(root_dir, "scripts/omPrior.py"), "2022-07-01", "2022-07-02"], check=True)

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

yield xr.load_dataset(filepath_out_domain)

os.remove(filepath_out_domain)

def test_001_response_for_download_links() :
for filename, filepath in downloads :
Expand All @@ -75,8 +96,6 @@ def test_002_cdsapi_connection(root_dir, tmpdir) :

assert os.path.exists(filepath)

os.remove(filepath)


def test_003_inputs_folder_is_empty(root_dir) :
input_folder = os.path.join(root_dir, "inputs")
Expand Down Expand Up @@ -121,38 +140,45 @@ def test_005_agriculture_emissions(root_dir, input_files) :

# 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) :
def test_006_grid_size_for_geo_files(cro_xr, geom_xr, dot_xr) :
expected_cell_size = 10000

monkeypatch.chdir(root_dir)
assert cro_xr.XCELL == expected_cell_size
assert cro_xr.YCELL == expected_cell_size

cmaqExamplePath = getenv("CMAQ_EXAMPLE")
assert geom_xr.DX == expected_cell_size
assert geom_xr.DY == expected_cell_size

assert dot_xr.XCELL == expected_cell_size
assert dot_xr.YCELL == expected_cell_size


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

croFilePath = os.path.join(cmaqExamplePath, getenv("CROFILE"))
dotFilePath = os.path.join(cmaqExamplePath, getenv("DOTFILE"))
geomFilePath = os.path.join(cmaqExamplePath, getenv("GEO_EM"))
def test_008_compare_out_domain_with_cro_dot_files(input_domain_xr, output_domain_xr, cro_xr, dot_xr):

with xr.open_dataset(geomFilePath) as geomXr :
assert geomXr.DX == expected_cell_size
assert geomXr.DY == expected_cell_size
assert dot_xr.NCOLS == output_domain_xr.COL_D.size
assert dot_xr.NROWS == output_domain_xr.ROW_D.size

with xr.open_dataset(dotFilePath) as dotXr :
assert dotXr.XCELL == expected_cell_size
assert dotXr.YCELL == expected_cell_size
assert cro_xr.NCOLS == output_domain_xr.COL.size
assert cro_xr.NROWS == output_domain_xr.ROW.size

with xr.open_dataset(croFilePath) as croXr :
assert croXr.XCELL == expected_cell_size
assert croXr.YCELL == expected_cell_size


def test_007_output_domain_file(output_domain_file, num_regression) :
mean_values = {key : output_domain_file[key].mean().item() for key in output_domain_file.keys()}
def test_009_output_domain_xr(output_domain_xr, num_regression) :
mean_values = {key : output_domain_xr[key].mean().item() for key in output_domain_xr.keys()}

num_regression.check(mean_values)


def test_008_emission_discrepancy(root_dir, output_domain_file, input_files) :
modelAreaM2 = output_domain_file.DX * output_domain_file.DY
def test_010_emission_discrepancy(root_dir, output_domain_xr, input_files) :
modelAreaM2 = output_domain_xr.DX * output_domain_xr.DY

filepath_sector = os.path.join(root_dir, sectoralEmissionsPath)
sector_data = pd.read_csv(filepath_sector).to_dict(orient="records")[0]
Expand All @@ -163,11 +189,11 @@ def test_008_emission_discrepancy(root_dir, output_domain_file, input_files) :
sectorVal = float(sector_data[sector]) * 1e9

# Check each layer in the output sums up to the input
if layerName in output_domain_file :
layerVal = np.sum(output_domain_file[layerName][0].values * modelAreaM2 * secsPerYear)
if layerName in output_domain_xr :
layerVal = np.sum(output_domain_xr[layerName][0].values * modelAreaM2 * secsPerYear)

if sector == "agriculture" :
layerVal += np.sum(output_domain_file["OCH4_LIVESTOCK"][0].values * modelAreaM2 * secsPerYear)
layerVal += np.sum(output_domain_xr["OCH4_LIVESTOCK"][0].values * modelAreaM2 * secsPerYear)

diff = round(layerVal - sectorVal)
perectenageDifference = diff / sectorVal * 100
Expand Down
2 changes: 2 additions & 0 deletions tests/test_om_prior/test_009_output_domain_xr.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
,LAT,LON,LANDMASK,LATD,LOND,OCH4_AGRICULTURE,OCH4_LULUCF,OCH4_WASTE,OCH4_LIVESTOCK,OCH4_INDUSTRIAL,OCH4_STATIONARY,OCH4_TRANSPORT,OCH4_ELECTRICITY,OCH4_FUGITIVE,OCH4_TERMITE,OCH4_FIRE,OCH4_WETLANDS,OCH4_TOTAL
0,-26.983160018920898,133.302001953125,0.39128163456916809,-26.980266571044922,133.302001953125,3.8221794892533283e-13,8.2839841777071689e-13,7.6806688034203811e-13,3.3252710482543991e-12,4.6406982245152562e-15,8.5852921845127254e-14,1.8562792898061025e-14,2.3204437472569127e-14,1.9068246493083643e-12,8.1061502996138124e-13,2.6130391795643748e-13,1.8596938045956645e-11,2.7011896889060713e-11

0 comments on commit 7d25a2d

Please sign in to comment.