Source code for pycaps.util.wsr88d
import numpy as np
import os
[docs]class RadarMeta(object):
"""
Class for keeping track of metadata for US radars.
"""
_tdwr = staticmethod(lambda rad: rad[0] == 'T')
_casa = staticmethod(lambda rad: rad in ['KSAO', 'KRSP', 'KLWE', 'KCYR'])
_legacy = staticmethod(
lambda rad: rad in ['KTAW', 'KBTV', 'KBYX', 'KCRI', 'KOU3', 'KWSD', 'KINS', 'KWRB', 'KMXF', 'KOKC', 'KTOP',
'KVBG', 'KYUX', 'PGGR', 'PWRC', 'PJMG', 'PBCS', 'PRCW', 'PDVS', 'PSGL', 'PJAE', 'PTEJ']
)
_research = staticmethod(lambda rad: rad in ['KOUN'])
_wsr88d = staticmethod(
lambda rad: not (RadarMeta._tdwr(rad) or RadarMeta._casa(rad) or
RadarMeta._legacy(rad) or RadarMeta._research(rad))
)
def __init__(self, filename="data/radarinfo.dat"):
"""
Constructor for the RadarMeta class
Args:
filename (str): Optional argument specifying where to find the radar info file.
"""
self._networks = {'tdwr': RadarMeta._tdwr, 'casa': RadarMeta._casa, 'legacy': RadarMeta._legacy,
'research': RadarMeta._research, 'wsr88d': RadarMeta._wsr88d}
filename = os.path.join(os.path.dirname(__file__), '..', filename)
self._load_radar_locations(filename)
return
def _load_radar_locations(self, filename):
dtype_raw = [('id', '|S4'), ('name', '|S10'), ('lat_deg', int), ('lat_min', int), ('lat_sec', int),
('lon_deg', int), ('lon_min', int), ('lon_sec', int)]
dtype = [('id', '|S4'), ('name', '|S10'), ('latitude', np.float), ('longitude', np.float32)]
wsr88d_raw = np.loadtxt(filename, skiprows=1, dtype=dtype_raw)
self._radar = np.empty(wsr88d_raw.shape, dtype=dtype)
self._radar['id'] = wsr88d_raw['id']
self._radar['name'] = wsr88d_raw['name']
self._radar['latitude'] = wsr88d_raw['lat_deg'] + wsr88d_raw['lat_min'] / 60. + wsr88d_raw['lat_sec'] / 3600.
self._radar['longitude'] = -wsr88d_raw['lon_deg'] - wsr88d_raw['lon_min'] / 60. - wsr88d_raw['lon_sec'] / 3600.
return
[docs] def __getitem__(self, key):
"""
Overloads the container access operator (e.g. obj[...])
Args:
key (str): A 4-character radar ID
Returns:
A dictionary containing the metadata for the radar with the given 4-character ID
"""
return RadarMeta._tuple_to_dict(self._radar[np.where(self._radar['id'] == key)][0])
[docs] def __iter__(self):
"""
Overloads the iterator (e.g. for elem in obj)
Returns:
An iterator over all the radars
"""
for idx in range(len(self)):
yield RadarMeta._tuple_to_dict(self._radar[idx])
[docs] def __len__(self):
"""
Overrides the length operator (e.g. len(obj))
Returns:
The total number of radars.
"""
return self._radar.shape[0]
[docs] def __call__(self, network):
"""
Overrides the function call syntax (e.g. obj(...))
Args:
network (str): Name of the network from which to return the radar ids. The expected values are 'tdwr' for
TDWRs, 'casa' for the CASA radar network, 'legacy' for legacy radars, 'research' for research radars,
and 'wsr88d' for the WSR-88D network. The default value is None, which returns all radar ids.
Returns:
An iterator over radar metadata for the network.
"""
for radar in self:
if self._networks[network](radar['id']):
yield self[radar['id']]
@staticmethod
def _tuple_to_dict(entry):
return dict(zip(['id', 'name', 'latitude', 'longitude'], entry))
_radars = None
def _load_radars():
if _radars is None:
global _radars
_radars = RadarMeta()
[docs]def get_radar_ids(network=None):
"""
Get a list of radar ids from a given network
Args:
network (str): Name of the network from which to return the radar ids. The expected values are 'tdwr' for TDWRs,
'casa' for the CASA radar network, 'legacy' for legacy radars, 'research' for research radars, and 'wsr88d'
for the WSR-88D network. The default value is None, which returns all radar ids.
Returns:
A list of 4-character radar ids
"""
_load_radars()
if network is None:
return list(_radars)
else:
return list(_radars(network))
[docs]def get_radar_meta(radar_id):
"""
Gets the metadata for the given 4-character radar id.
Args:
radar_id (str): The 4-character id for a radar
Returns:
A dictionary containing the latitude, longitude, name, and ID of the radar.
"""
_load_radars()
return _radars[radar_id]
if __name__ == "__main__":
rm = RadarMeta()
for r in rm:
print r