Completed
Pull Request — master (#505)
by Ryan
01:01
created

test_doubled_file()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
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
"""Test the `nexrad` module."""
5
6
from datetime import datetime
7
import glob
8
from io import BytesIO
9
import logging
10
import os.path
11
12
import numpy as np
13
import pytest
14
15
from metpy.cbook import get_test_data
16
from metpy.io import is_precip_mode, Level2File, Level3File
17
18
# Turn off the warnings for tests
19
logging.getLogger('metpy.io.nexrad').setLevel(logging.CRITICAL)
20
21
#
22
# NEXRAD Level 2 Tests
23
#
24
25
# 1999 file tests old message 1
26
# KFTG tests bzip compression and newer format for a part of message 31
27
# KTLX 2015 has missing segments for message 18, which was causing exception
28
level2_files = [('KTLX20130520_201643_V06.gz', datetime(2013, 5, 20, 20, 16, 46), 17),
29
                ('KTLX19990503_235621.gz', datetime(1999, 5, 3, 23, 56, 21), 16),
30
                ('Level2_KFTG_20150430_1419.ar2v', datetime(2015, 4, 30, 14, 19, 11), 12),
31
                ('KTLX20150530_000802_V06.bz2', datetime(2015, 5, 30, 0, 8, 3), 14),
32
                ('KICX_20170712_1458', datetime(2017, 7, 12, 14, 58, 5), 14)]
33
34
35
# ids here fixes how things are presented in pycharm
36
@pytest.mark.parametrize('fname, voltime, num_sweeps', level2_files,
37
                         ids=[i[0].replace('.', '_') for i in level2_files])
38
def test_level2(fname, voltime, num_sweeps):
39
    """Test reading NEXRAD level 2 files from the filename."""
40
    f = Level2File(get_test_data(fname, as_file_obj=False))
41
    assert f.dt == voltime
42
    assert len(f.sweeps) == num_sweeps
43
44
45
def test_level2_fobj():
46
    """Test reading NEXRAD level2 data from a file object."""
47
    Level2File(get_test_data('Level2_KFTG_20150430_1419.ar2v'))
48
49
50
def test_doubled_file():
51
    """Test for #489 where doubled-up files didn't parse at all"""
52
    data = get_test_data('Level2_KFTG_20150430_1419.ar2v').read()
53
    fobj = BytesIO(data + data)
54
    f = Level2File(fobj)
55
    assert len(f.sweeps) == 12
56
57
58
#
59
# NIDS/Level 3 Tests
60
#
61
nexrad_nids_files = glob.glob(os.path.join(get_test_data('nids', as_file_obj=False), 'K???_*'))
62
63
64
@pytest.mark.parametrize('fname', nexrad_nids_files)
65
def test_level3_files(fname):
66
    """Test opening a NEXRAD NIDS file."""
67
    f = Level3File(fname)
68
69
    # If we have some raster data in the symbology block, feed it into the mapper to make
70
    # sure it's working properly (Checks for #253)
71
    if hasattr(f, 'sym_block'):
72
        block = f.sym_block[0][0]
73
        if 'data' in block:
74
            f.map_data(block['data'])
75
76
77
tdwr_nids_files = glob.glob(os.path.join(get_test_data('nids', as_file_obj=False),
78
                                         'Level3_MCI_*'))
79
80
81
@pytest.mark.parametrize('fname', tdwr_nids_files)
82
def test_tdwr_nids(fname):
83
    """Test opening a TDWR NIDS file."""
84
    Level3File(fname)
85
86
87
def test_basic():
88
    """Test reading one specific NEXRAD NIDS file based on the filename."""
89
    f = Level3File(get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids', as_file_obj=False))
90
    assert f.metadata['prod_time'].replace(second=0) == datetime(2014, 4, 7, 18, 5)
91
    assert f.metadata['vol_time'].replace(second=0) == datetime(2014, 4, 7, 18, 5)
92
    assert f.metadata['msg_time'].replace(second=0) == datetime(2014, 4, 7, 18, 6)
93
94
95
def test_tdwr():
96
    """Test reading a specific TDWR file."""
97
    f = Level3File(get_test_data('nids/Level3_SLC_TV0_20160516_2359.nids'))
98
    assert f.prod_desc.prod_code == 182
99
100
101
def test_dhr():
102
    """Test reading a time field for DHR product."""
103
    f = Level3File(get_test_data('nids/KOUN_SDUS54_DHRTLX_201305202016'))
104
    assert f.metadata['avg_time'] == datetime(2013, 5, 20, 20, 18)
105
106
107
def test_nwstg():
108
    """Test reading a nids file pulled from the NWSTG."""
109
    Level3File(get_test_data('nids/sn.last', as_file_obj=False))
110
111
112
def test_fobj():
113
    """Test reading a specific NEXRAD NIDS files from a file object."""
114
    Level3File(get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids'))
115
116
117
def test21_precip():
118
    """Test checking whether VCP 21 is precipitation mode."""
119
    assert is_precip_mode(21), 'VCP 21 is precip'
120
121
122
def test11_precip():
123
    """Test checking whether VCP 11 is precipitation mode."""
124
    assert is_precip_mode(11), 'VCP 11 is precip'
125
126
127
def test31_clear_air():
128
    """Test checking whether VCP 31 is clear air mode."""
129
    assert not is_precip_mode(31), 'VCP 31 is not precip'
130
131
132
def test_msg15():
133
    """Check proper decoding of message type 15."""
134
    f = Level2File(get_test_data('KTLX20130520_201643_V06.gz', as_file_obj=False))
135
    data = f.clutter_filter_map['data']
136
    assert isinstance(data[0][0], list)
137
    assert f.clutter_filter_map['datetime'] == datetime(2013, 5, 19, 0, 0, 0, 315000)
138
139
140
def test_tracks():
141
    """Check that tracks are properly decoded."""
142
    f = Level3File(get_test_data('nids/KOUN_SDUS34_NSTTLX_201305202016'))
143
    for data in f.sym_block[0]:
144
        if 'track' in data:
145
            x, y = np.array(data['track']).T
146
            assert len(x)
147
            assert len(y)
148
149
150
def test_vector_packet():
151
    """Check that vector packets are properly decoded."""
152
    f = Level3File(get_test_data('nids/KOUN_SDUS64_NHITLX_201305202016'))
153
    for page in f.graph_pages:
154
        for item in page:
155
            if 'vectors' in item:
156
                x1, x2, y1, y2 = np.array(item['vectors']).T
157
                assert len(x1)
158
                assert len(x2)
159
                assert len(y1)
160
                assert len(y2)
161