|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Compares GCHP emission diagnostic entries in HISTORY.rc |
| 4 | +and in HEMCO_Diagn.rc configuration files for consistency. |
| 5 | +""" |
| 6 | +from os.path import join, realpath |
| 7 | +from sys import argv |
| 8 | +from re import sub |
| 9 | +from gcpy.constants import ENCODING |
| 10 | +from gcpy.util import verify_variable_type |
| 11 | + |
| 12 | + |
| 13 | +def read_history(filename): |
| 14 | + """ |
| 15 | + Reads the HISTORY_Diagn.rc file and returns a list of |
| 16 | + diagnostic container names containing "Emis" or "Inv". |
| 17 | +
|
| 18 | + Args |
| 19 | + filename : str : Path to the HISTORY.rc file |
| 20 | +
|
| 21 | + Returns |
| 22 | + result : list : List of diagnostic entries |
| 23 | + """ |
| 24 | + verify_variable_type(filename, str) |
| 25 | + |
| 26 | + with open(filename, "r", encoding=ENCODING) as ifile: |
| 27 | + result = [] |
| 28 | + for line in ifile: |
| 29 | + |
| 30 | + # Only take lines with diagnostic fields |
| 31 | + if "GCHPchem" not in line: |
| 32 | + continue |
| 33 | + if "Budget" in line: |
| 34 | + continue |
| 35 | + if "Emis" not in line and "Inv" not in line: |
| 36 | + continue |
| 37 | + if "Emissions.fields:" in line: |
| 38 | + line = line.replace("Emissions.fields:", "") |
| 39 | + |
| 40 | + # The diagnostic container is the 1st word |
| 41 | + # (also strip quotes) |
| 42 | + |
| 43 | + first_word = line.strip().split()[0] |
| 44 | + first_word = first_word.replace("'", "").replace('"', "") |
| 45 | + |
| 46 | + # Replace multiple # chars with a single # char |
| 47 | + first_word = sub(r"#+", "#", first_word) |
| 48 | + |
| 49 | + # Add to list of diagnosic entries |
| 50 | + result.append(first_word) |
| 51 | + |
| 52 | + return result |
| 53 | + |
| 54 | + |
| 55 | +def read_hemco_diagn(filename): |
| 56 | + """ |
| 57 | + Reads the HEMCO_Diagn.rc file and returns a list of |
| 58 | + diagnostic container names containing "Emis" or "Inv". |
| 59 | +
|
| 60 | + Args |
| 61 | + filename : str : Path to the HEMCO_Diagn.rc file |
| 62 | +
|
| 63 | + Returns |
| 64 | + result : list : List of diagnostic entries |
| 65 | + """ |
| 66 | + verify_variable_type(filename, str) |
| 67 | + |
| 68 | + with open(filename, "r", encoding=ENCODING) as ifile: |
| 69 | + result = [] |
| 70 | + for line in ifile: |
| 71 | + |
| 72 | + # Strip newlines and split into substrings |
| 73 | + line = line.strip().split() |
| 74 | + |
| 75 | + # Skip if the line is empty |
| 76 | + if len(line) == 0: |
| 77 | + continue |
| 78 | + |
| 79 | + # Skip if the 1st word doesn't contain "Emis" or "Inv" |
| 80 | + first_word = line[0] |
| 81 | + if "Emis" not in first_word and "Inv" not in first_word: |
| 82 | + continue |
| 83 | + |
| 84 | + # Replace quotes |
| 85 | + first_word.replace("'", "").replace('"', "") |
| 86 | + |
| 87 | + # Replace multiple "#" characters with a single "#" |
| 88 | + first_word = sub(r"#+", "#", first_word) |
| 89 | + |
| 90 | + # Append to list of diagnosic entries |
| 91 | + result.append(first_word) |
| 92 | + |
| 93 | + return result |
| 94 | + |
| 95 | + |
| 96 | +def compare_containers(history_entries, hco_diagn_entries): |
| 97 | + """ |
| 98 | + Compares the list of GCHP emission entries in HISTORY.rc |
| 99 | + and HEMCO_Diagn.rc. Returns the list of entries common to |
| 100 | + both, and in one file but not the other. |
| 101 | +
|
| 102 | + Args |
| 103 | + history_containers : list : Emission entries in HISTORY.rc |
| 104 | + hco_diagn_containers : list : Emission entries in HEMCO_Diagn.rc |
| 105 | +
|
| 106 | + Returns |
| 107 | + result : dict : Results of the comparison |
| 108 | + """ |
| 109 | + verify_variable_type(history_entries, list) |
| 110 | + verify_variable_type(hco_diagn_entries, list) |
| 111 | + |
| 112 | + # Convert lists to sets |
| 113 | + set_history = set(history_entries) |
| 114 | + set_hemco = set(hco_diagn_entries) |
| 115 | + |
| 116 | + # Diagnostic entries common to History and HEMCO |
| 117 | + result = {} |
| 118 | + result["common"] = sorted(list(set_history & set_hemco)) |
| 119 | + |
| 120 | + # Diagnostic entries found in one file but not the other |
| 121 | + result["in_history_not_hemco"] = sorted(list(set_history - set_hemco)) |
| 122 | + result["in_hemco_not_history"] = sorted(list(set_hemco - set_history)) |
| 123 | + |
| 124 | + return result |
| 125 | + |
| 126 | + |
| 127 | +def print_results(result): |
| 128 | + """ |
| 129 | + Prints the results of the comparison between HISTORY.rc |
| 130 | + and HEMCO_Diagn.rc |
| 131 | +
|
| 132 | + Args |
| 133 | + result : dict : Dictionary with results of the comparison |
| 134 | + """ |
| 135 | + verify_variable_type(result, dict) |
| 136 | + |
| 137 | + print("="*79) |
| 138 | + print("Common to both HISTORY.rc and HEMCO_Diagn.rc") |
| 139 | + print("="*79) |
| 140 | + for var in result["common"]: |
| 141 | + print(f" {var}") |
| 142 | + |
| 143 | + print("") |
| 144 | + print("="*79) |
| 145 | + print("In HISTORY.rc but not in HEMCO_Diagn.rc") |
| 146 | + print("="*79) |
| 147 | + for var in result["in_history_not_hemco"]: |
| 148 | + print(f" {var}") |
| 149 | + |
| 150 | + print("") |
| 151 | + print("="*79) |
| 152 | + print("In HEMCO_Diagn.rc but not in HISTORY.rc") |
| 153 | + print("="*79) |
| 154 | + for var in result["in_hemco_not_history"]: |
| 155 | + print(f" {var}") |
| 156 | + |
| 157 | + |
| 158 | +def main(path_to_rundir, simulation): |
| 159 | + """ |
| 160 | + Main program. Calls routines to read GCHP HISTORY.rc and |
| 161 | + HEMCO_Diagn.rc files, to compare emission diagnostics, and |
| 162 | + to print the results. |
| 163 | +
|
| 164 | + Args |
| 165 | + path_to_rundir : str : Path to the run/GCHP folder |
| 166 | + simulation : str : Simulation name (e.g. fullchem) |
| 167 | + """ |
| 168 | + verify_variable_type(path_to_rundir, str) |
| 169 | + verify_variable_type(simulation, str) |
| 170 | + |
| 171 | + # Create paths to HISTORY.rc and HEMCO_Diagn.rc |
| 172 | + history_file = realpath( |
| 173 | + join( |
| 174 | + path_to_rundir, |
| 175 | + "HISTORY.rc.templates", |
| 176 | + f"HISTORY.rc.{simulation}" |
| 177 | + ) |
| 178 | + ) |
| 179 | + hco_diagn_file = realpath( |
| 180 | + join( |
| 181 | + path_to_rundir, |
| 182 | + "HEMCO_Diagn.rc.templates", |
| 183 | + f"HEMCO_Diagn.rc.{simulation}" |
| 184 | + ) |
| 185 | + ) |
| 186 | + |
| 187 | + # Read emission entries |
| 188 | + history_entries = read_history(history_file) |
| 189 | + hco_diag_entries = read_hemco_diagn(hco_diagn_file) |
| 190 | + |
| 191 | + # Compare and print results |
| 192 | + result = compare_containers(history_entries, hco_diag_entries) |
| 193 | + print_results(result) |
| 194 | + |
| 195 | + |
| 196 | +if __name__ == '__main__': |
| 197 | + |
| 198 | + if len(argv) != 3: |
| 199 | + MSG = "Usage: python -m gcpy.examples.diagnostics.check_gchp_emission_diags /path/to/run/GCHP SIMULATION-NAME" |
| 200 | + raise ValueError(MSG) |
| 201 | + |
| 202 | + main(argv[1], argv[2]) |
0 commit comments