1
|
|
|
# Copyright (c) 2008-2015 MetPy Developers. |
2
|
|
|
# Distributed under the terms of the BSD 3-Clause License. |
3
|
|
|
# SPDX-License-Identifier: BSD-3-Clause |
4
|
|
|
|
5
|
|
|
from datetime import datetime |
6
|
|
|
import glob |
7
|
|
|
import logging |
8
|
|
|
import os.path |
9
|
|
|
|
10
|
|
|
import pytest |
11
|
|
|
|
12
|
|
|
import numpy as np |
13
|
|
|
|
14
|
|
|
from metpy.io.nexrad import Level2File, Level3File, is_precip_mode |
15
|
|
|
from metpy.cbook import get_test_data |
16
|
|
|
|
17
|
|
|
# Turn off the warnings for tests |
18
|
|
|
logging.getLogger('metpy.io.nexrad').setLevel(logging.CRITICAL) |
19
|
|
|
|
20
|
|
|
# |
21
|
|
|
# NEXRAD Level 2 Tests |
22
|
|
|
# |
23
|
|
|
|
24
|
|
|
# 1999 file tests old message 1 |
25
|
|
|
# KFTG tests bzip compression and newer format for a part of message 31 |
26
|
|
|
# KTLX 2015 has missing segments for message 18, which was causing exception |
27
|
|
|
level2_files = [('KTLX20130520_201643_V06.gz', datetime(2013, 5, 20, 20, 16, 46)), |
28
|
|
|
('KTLX19990503_235621.gz', datetime(1999, 5, 3, 23, 56, 21)), |
29
|
|
|
('Level2_KFTG_20150430_1419.ar2v', datetime(2015, 4, 30, 14, 19, 11)), |
30
|
|
|
('KTLX20150530_000802_V06.bz2', datetime(2015, 5, 30, 0, 8, 3))] |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
# ids here fixes how things are presented in pycharm |
34
|
|
|
@pytest.mark.parametrize('fname,voltime', level2_files, |
35
|
|
|
ids=[i[0].replace('.', '_') for i in level2_files]) |
36
|
|
|
def test_level2(fname, voltime): |
37
|
|
|
'Test reading NEXRAD level 2 files from the filename' |
38
|
|
|
f = Level2File(get_test_data(fname, as_file_obj=False)) |
39
|
|
|
assert f.dt == voltime |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def test_level2_fobj(): |
43
|
|
|
'Test reading NEXRAD level2 data from a file object' |
44
|
|
|
Level2File(get_test_data('Level2_KFTG_20150430_1419.ar2v')) |
45
|
|
|
|
46
|
|
|
# |
47
|
|
|
# NIDS/Level 3 Tests |
48
|
|
|
# |
49
|
|
|
|
50
|
|
|
nexrad_nids_files = glob.glob(os.path.join(get_test_data('nids', as_file_obj=False), 'K???_*')) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
@pytest.mark.parametrize('fname', nexrad_nids_files) |
54
|
|
|
def test_level3_files(fname): |
55
|
|
|
'Test opening a NEXRAD NIDS file' |
56
|
|
|
Level3File(fname) |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
tdwr_nids_files = glob.glob(os.path.join(get_test_data('nids', as_file_obj=False), |
60
|
|
|
'Level3_MCI_*')) |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
@pytest.mark.parametrize('fname', tdwr_nids_files) |
64
|
|
|
def test_tdwr_nids(fname): |
65
|
|
|
'Test opening a TDWR NIDS file' |
66
|
|
|
Level3File(fname) |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def test_basic(): |
70
|
|
|
'Basic test of reading one specific NEXRAD NIDS file based on the filename' |
71
|
|
|
f = Level3File(get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids', as_file_obj=False)) |
72
|
|
|
assert f.metadata['prod_time'].replace(second=0) == datetime(2014, 4, 7, 18, 5) |
73
|
|
|
assert f.metadata['vol_time'].replace(second=0) == datetime(2014, 4, 7, 18, 5) |
74
|
|
|
assert f.metadata['msg_time'].replace(second=0) == datetime(2014, 4, 7, 18, 6) |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def test_tdwr(): |
78
|
|
|
'Test reading a specific TDWR file' |
79
|
|
|
f = Level3File(get_test_data('nids/Level3_SLC_TV0_20160516_2359.nids')) |
80
|
|
|
assert f.prod_desc.prod_code == 182 |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
def test_dhr(): |
84
|
|
|
'Test reading a time field for DHR product' |
85
|
|
|
f = Level3File(get_test_data('nids/KOUN_SDUS54_DHRTLX_201305202016')) |
86
|
|
|
assert f.metadata['avg_time'] == datetime(2013, 5, 20, 20, 18) |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def test_nwstg(): |
90
|
|
|
'Test reading a nids file pulled from the NWSTG' |
91
|
|
|
Level3File(get_test_data('nids/sn.last', as_file_obj=False)) |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def test_fobj(): |
95
|
|
|
'Test reading a specific NEXRAD NIDS files from a file object' |
96
|
|
|
Level3File(get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids')) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def test21_precip(): |
100
|
|
|
'Test checking whether VCP 21 is precipitation mode' |
101
|
|
|
assert is_precip_mode(21), 'VCP 21 is precip' |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def test11_precip(): |
105
|
|
|
'Test checking whether VCP 11 is precipitation mode' |
106
|
|
|
assert is_precip_mode(11), 'VCP 11 is precip' |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
def test31_clear_air(): |
110
|
|
|
'Test checking whether VCP 31 is clear air mode' |
111
|
|
|
assert not is_precip_mode(31), 'VCP 31 is not precip' |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
def test_msg15(): |
115
|
|
|
'Check proper decoding of message type 15' |
116
|
|
|
f = Level2File(get_test_data('KTLX20130520_201643_V06.gz', as_file_obj=False)) |
117
|
|
|
data = f.clutter_filter_map['data'] |
118
|
|
|
assert isinstance(data[0][0], list) |
119
|
|
|
assert f.clutter_filter_map['datetime'] == datetime(2013, 5, 19, 0, 0, 0, 315000) |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def test_tracks(): |
123
|
|
|
'Check that tracks are properly decoded' |
124
|
|
|
f = Level3File(get_test_data('nids/KOUN_SDUS34_NSTTLX_201305202016')) |
125
|
|
|
for data in f.sym_block[0]: |
126
|
|
|
if 'track' in data: |
127
|
|
|
x, y = np.array(data['track']).T |
128
|
|
|
assert len(x) |
129
|
|
|
assert len(y) |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
def test_vector_packet(): |
133
|
|
|
'Check that vector packets are properly decoded' |
134
|
|
|
f = Level3File(get_test_data('nids/KOUN_SDUS64_NHITLX_201305202016')) |
135
|
|
|
for page in f.graph_pages: |
136
|
|
|
for item in page: |
137
|
|
|
if 'vectors' in item: |
138
|
|
|
x1, x2, y1, y2 = np.array(item['vectors']).T |
139
|
|
|
assert len(x1) |
140
|
|
|
assert len(x2) |
141
|
|
|
assert len(y1) |
142
|
|
|
assert len(y2) |
143
|
|
|
|