|
1 | 1 | import pytest |
2 | 2 |
|
3 | 3 | import os |
4 | | -import re |
5 | 4 |
|
6 | 5 | from metplotpy.plots.skew_t import skew_t as skew_t |
7 | 6 |
|
8 | 7 | def test_skew_t(module_setup_env): |
| 8 | + expected_times = { |
| 9 | + '2023010100': range(0, 61, 6), |
| 10 | + '2023010106': range(0, 49, 6), |
| 11 | + } |
| 12 | + expected_files = [] |
| 13 | + for init, leads in expected_times.items(): |
| 14 | + for lead in leads: |
| 15 | + expected_files.append(f'ssh052023_avno_doper_{init}_diag_{lead}_hr.png') |
| 16 | + |
9 | 17 | custom_config_filename = os.path.join(os.environ['TEST_DIR'], "test_skew_t.yaml") |
10 | 18 | skew_t.main(custom_config_filename) |
11 | 19 |
|
12 | | - # Verify that files for the ssh052023 data exists for the 0,6, 12,18,24, 30, 42, |
13 | | - # 48, 54, and 60 hour data. |
14 | | - output_dir = os.environ['TEST_OUTPUT'] |
| 20 | + # Verify that files for the ssh052023 data exists for |
| 21 | + # the 0, 6, 12, 18, 24, 30, 42, 48, 54, and 60 hour data. |
| 22 | + # Some of these data files have incomplete data so check for the expected hour plots. |
15 | 23 |
|
16 | | - # Some of these data files have incomplete data so check for the expected hour |
17 | | - # plots. |
18 | | - |
19 | | - print(f"Output dir: {output_dir}") |
20 | 24 | file_ext = '.png' |
21 | 25 | files_of_interest = [] |
22 | | - for root, _, files in os.walk(output_dir): |
| 26 | + for root, _, files in os.walk(os.environ['TEST_OUTPUT']): |
23 | 27 | for item in files: |
24 | 28 | if item.endswith(file_ext): |
25 | | - # print(f"Item of interest: {item}") |
26 | 29 | full_file = os.path.join(root, item) |
27 | 30 | base_file = os.path.basename(full_file) |
28 | 31 | files_of_interest.append(base_file) |
29 | 32 |
|
30 | | - _check_files_exist(files_of_interest) |
31 | | - _check_files_not_created(files_of_interest) |
32 | | - _check_empty_input(files_of_interest) |
33 | | - |
34 | | - |
35 | | -def _check_files_exist(files_of_interest): |
36 | | - ''' |
37 | | - Checking that only the expected plot files are getting created and |
38 | | - input files with only fill/missing data are not created. |
39 | | - ''' |
40 | | - # List of files for the sh052023 data (which is missing data for hours 66-240). |
41 | | - # Config file is requesting all the available sounding hours |
42 | | - data_some_missing_data = { |
43 | | - '2023010100': range(0, 61, 6), |
44 | | - '2023010106': range(0, 49, 6), |
45 | | - } |
46 | | - |
47 | | - # Create a list of expected base file names with their expected hours. |
48 | | - expected_base_filenames = [] |
49 | | - # Expected base for expected plot output name of format: |
50 | | - # ssh_052023_avno_doper_202301010[0|6]_diag_[0-9]{1,2}_hr |
51 | | - for filetime, expected_hours in data_some_missing_data.items(): |
52 | | - for cur_hr in expected_hours: |
53 | | - base_hr = f'ssh052023_avno_doper_{filetime}_diag_{cur_hr}_hr' |
54 | | - expected_base_filenames.append(base_hr) |
55 | | - |
56 | | - # Subset only the files that correspond to the sh052023 data |
57 | | - subset_files_of_interest = [] |
58 | | - for cur_file in files_of_interest: |
59 | | - match_found = re.match(r'(ssh052023_.*).png', cur_file) |
60 | | - if match_found: |
61 | | - subset_files_of_interest.append(match_found.group(1)) |
62 | | - |
63 | | - # Verify that the expected plots were generated. |
64 | | - num_found = 0 |
65 | | - for expected in expected_base_filenames: |
66 | | - if expected in subset_files_of_interest: |
67 | | - num_found += 1 |
68 | | - |
69 | | - assert len(expected_base_filenames) == num_found |
70 | | - |
71 | | - |
72 | | -def _check_files_not_created(files_of_interest): |
73 | | - ''' |
74 | | - Checking that input files with only fill/missing data are not created. |
75 | | - ''' |
76 | | - # List of files with no sounding data (9999 for all fields and times) |
77 | | - no_sounding_data = ['ssh162023_avno_doper_2023022712_diag', |
78 | | - 'ssh162023_avno_doper_2023022800_diag', |
79 | | - 'ssh162023_avno_doper_2023022806_diag', |
80 | | - 'ssh162023_avno_doper_2023030706_diag'] |
81 | | - |
82 | | - # Subset the files of interest to just sh162023 output. |
83 | | - subsetted_files_of_interest = [] |
84 | | - for cur in files_of_interest: |
85 | | - match = re.match(r'^ssh162023', cur) |
86 | | - if match: |
87 | | - subsetted_files_of_interest.append(cur) |
88 | | - |
89 | | - # Verify that there aren't any plots created for the files with missing sounding |
90 | | - # data. First, create a list of the base names of the plots that were created and |
91 | | - # that correspond to the input data of interest (i.e. the sh162023_*.dat data). |
92 | | - subsetted_basenames = [] |
93 | | - for cur_plot in subsetted_files_of_interest: |
94 | | - match = re.match(r'(ssh162023_avno_doper_20230[0-9]{5}_diag)_*._hr.png', |
95 | | - cur_plot) |
96 | | - if match: |
97 | | - subsetted_basenames.append(match.group(1)) |
98 | | - |
99 | | - # Count how often we find a basename of a plot that we didn't expect to create with |
100 | | - # the list of base names of plots that were created. |
101 | | - fail_counter = 0 |
102 | | - for cur in no_sounding_data: |
103 | | - if cur in subsetted_basenames: |
104 | | - fail_counter += 1 |
105 | | - |
106 | | - assert fail_counter == 0 |
107 | | - |
108 | | - |
109 | | -def _check_empty_input(files_of_interest): |
110 | | - ''' |
111 | | - Checking that empty input file is not creating any plots. |
112 | | - ''' |
113 | | - # List of empty files |
114 | | - no_data_empty_file = ['sal092022_avno_doper_2022092800_diag'] |
115 | | - |
116 | | - # Verify that there aren't any plots created for the file with missing sounding |
117 | | - # data. |
118 | | - |
119 | | - # First, subset the files of interest to just sal0920223 output. |
120 | | - subsetted_files_of_interest = [] |
121 | | - for cur in files_of_interest: |
122 | | - match = re.match(r'^sal092022', cur) |
123 | | - if match: |
124 | | - subsetted_files_of_interest.append(cur) |
125 | | - |
126 | | - match_found = re.match(r'^sal092022_avno_doper_2022092800_diag', |
127 | | - no_data_empty_file[0]) |
128 | | - # The output file was created when it shouldn't have been, fail. |
129 | | - assert match_found not in subsetted_files_of_interest |
| 33 | + assert len(expected_files) == len(files_of_interest) |
| 34 | + for expected_file in expected_files: |
| 35 | + assert expected_file in files_of_interest |
0 commit comments