def __init__(self, file:Any): from metpy.io.nexrad import Level3File f = Level3File(file) self.dtype = self._det_product_type(f.prod_desc.prod_code) self.radial_flag = self._is_radial(f.prod_desc.prod_code) data_block = f.sym_block[0][0] data = np.ma.array(data_block['data']) data[data == 0] = np.ma.masked self.data = np.ma.masked_invalid(f.map_data(data)) self.max_range = f.max_range if self.radial_flag: self.az = np.array(data_block['start_az'] + [data_block['end_az'][-1]]) * deg2rad self.rng = np.linspace(0, f.max_range, data.shape[-1] + 1) else: # TODO: Support grid type data xdim, ydim = data.shape x = np.linspace(xdim * f.ij_to_km * -1, xdim * f.ij_to_km, xdim) / 111 + f.lon y = np.linspace(ydim * f.ij_to_km, ydim * f.ij_to_km * -1, ydim) / 111 + f.lat self.lon, self.lat = np.meshgrid(x, y) self.reso = f.ij_to_km self.stationlat = f.lat self.stationlon = f.lon self.el = f.metadata['el_angle'] self.scantime = f.metadata['vol_time'] o = open(file, 'rb') o.seek(12) code = np.frombuffer(o.read(2), '>i2')[0] if code in range(0, 100): cds = '0{}'.format(code) else: cds = str(code) self.code = 'Z9' + cds o.close() self._update_radar_info()
def __init__(self, file: Any): from metpy.io.nexrad import Level3File f = Level3File(file) # Because metpy interface doesn't provide station codes, # it's necessary to reopen it and read the code. with open(file, "rb") as buf: buf.seek(12) code = np.frombuffer(buf.read(2), ">i2")[0] cds = str(code).zfill(3) self.code = "Z9" + cds self._update_radar_info() product_code = f.prod_desc.prod_code self.dtype = self._det_product_type(product_code) self.radial_flag = self._is_radial(product_code) data_block = f.sym_block[0][0] data = np.array(data_block["data"], dtype=int) if self.dtype == "VEL": mapped_data = np.ma.masked_invalid(velocity_tbl[data]) rf = np.ma.masked_not_equal(mapped_data, 30) data = np.ma.masked_equal(mapped_data, 30) self.data = (data, rf) else: data[data == 0] = np.ma.masked self.data = np.ma.masked_invalid(f.map_data(data)) if self.dtype == "ET": # convert kft to km self.data *= 0.30478 elif self.dtype == "OHP": # convert in to mm self.data *= 25.4 station_info = _get_radar_info(self.code) self.radar_type = station_info[3] self.max_range = int(f.max_range) # Hard coding to adjust max range for different types of radar if f.max_range >= 230: if self.radar_type in ["SC", "CC"]: self.max_range = 150 elif self.radar_type in ["CA", "CB"]: self.max_range = 200 elif self.radar_type == "CD": self.max_range = 125 if self.radial_flag: start_az = data_block["start_az"][0] az = np.linspace(0, 360, data.shape[0]) az += start_az az[az > 360] -= 360 self.az = az * deg2rad self.reso = self.max_range / data.shape[1] self.rng = np.arange(self.reso, self.max_range + self.reso, self.reso) else: xdim, ydim = data.shape x = np.linspace(self.max_range * -1, self.max_range, xdim) / 111 + f.lon y = np.linspace(self.max_range, self.max_range * -1, ydim) / 111 + f.lat self.lon, self.lat = np.meshgrid(x, y) self.reso = self.max_range / data.shape[0] * 2 self.stationlat = f.lat self.stationlon = f.lon self.el = np.round_(f.metadata.get("el_angle", 0), 1) self.scantime = f.metadata["vol_time"]
def test_tracks(self): f = Level3File(get_test_data('nids/KOUN_SDUS34_NSTTLX_201305202016')) for data in f.sym_block[0]: if 'track' in data: x, y = np.array(data['track']).T assert len(x) assert len(y)
def test_tracks(): 'Check that tracks are properly decoded' f = Level3File(get_test_data('nids/KOUN_SDUS34_NSTTLX_201305202016')) for data in f.sym_block[0]: if 'track' in data: x, y = np.array(data['track']).T assert len(x) assert len(y)
def test_vector_packet(self): f = Level3File(get_test_data('nids/KOUN_SDUS64_NHITLX_201305202016')) for page in f.graph_pages: for item in page: if 'vectors' in item: x1, x2, y1, y2 = np.array(item['vectors']).T assert len(x1) assert len(x2) assert len(y1) assert len(y2)
def test_level3_files(fname): """Test opening a NEXRAD NIDS file.""" f = Level3File(fname) # If we have some raster data in the symbology block, feed it into the mapper to make # sure it's working properly (Checks for #253) if hasattr(f, 'sym_block'): block = f.sym_block[0][0] if 'data' in block: f.map_data(block['data'])
def test_basic(): """Test reading one specific NEXRAD NIDS file based on the filename.""" f = Level3File( get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids', as_file_obj=False)) assert f.metadata['prod_time'].replace(second=0) == datetime( 2014, 4, 7, 18, 5) assert f.metadata['vol_time'].replace(second=0) == datetime( 2014, 4, 7, 18, 5) assert f.metadata['msg_time'].replace(second=0) == datetime( 2014, 4, 7, 18, 6)
def test_vector_packet(): """Check that vector packets are properly decoded.""" f = Level3File(get_test_data('nids/KOUN_SDUS64_NHITLX_201305202016')) for page in f.graph_pages: for item in page: if 'vectors' in item: x1, x2, y1, y2 = np.array(item['vectors']).T assert len(x1) assert len(x2) assert len(y1) assert len(y2)
def process(bio): """Process our data, please""" l3 = Level3File(bio) del bio ctx = {} ctx["nexrad"] = l3.siteID ctx["ts"] = l3.metadata["vol_time"].replace(tzinfo=pytz.UTC) ctx["lines"] = [] if not hasattr(l3, "graph_pages"): log.msg("%s %s has no graph_pages" % (ctx["nexrad"], ctx["ts"])) return for page in l3.graph_pages: for line in page: if "text" in line: ctx["lines"].append(line["text"]) df = PGCONN.runInteraction(really_process, ctx) df.addErrback(common.email_error, ctx)
def __init__(self, file: Any): from metpy.io.nexrad import Level3File f = Level3File(file) self.dtype = self._det_product_type(f.prod_desc.prod_code) self.radial_flag = self._is_radial(f.prod_desc.prod_code) data_block = f.sym_block[0][0] data = np.ma.array(data_block["data"]) data[data == 0] = np.ma.masked self.data = np.ma.masked_invalid(f.map_data(data)) self.max_range = f.max_range if self.radial_flag: self.az = ( np.array(data_block["start_az"] + [data_block["end_az"][-1]]) * deg2rad) self.rng = np.linspace(0, f.max_range, data.shape[-1] + 1) self.reso = self.max_range / data.shape[1] else: # TODO: Support grid type data raise NotImplementedError("Grid-type data is not supported") xdim, ydim = data.shape x = (np.linspace(xdim * f.ij_to_km * -1, xdim * f.ij_to_km, xdim) / 111 + f.lon) y = (np.linspace(ydim * f.ij_to_km, ydim * f.ij_to_km * -1, ydim) / 111 + f.lat) self.lon, self.lat = np.meshgrid(x, y) self.reso = f.ij_to_km self.stationlat = f.lat self.stationlon = f.lon self.el = f.metadata["el_angle"] self.scantime = f.metadata["vol_time"] # Because metpy interface doesn't provide station codes, # it's necessary to reopen it and read the code. o = open(file, "rb") o.seek(12) code = np.frombuffer(o.read(2), ">i2")[0] if code in range(0, 100): cds = "0{}".format(code) else: cds = str(code) self.code = "Z9" + cds o.close() self._update_radar_info()
def __init__(self, file): from metpy.io.nexrad import Level3File f = Level3File(file) data_block = f.sym_block[0][0] data = np.ma.array(data_block['data'][1:]) # First element in data is mysteriously empty data[data == 0] = np.ma.masked self.az = np.array(data_block['start_az'][:-1]) * deg2rad self.rng = np.linspace(1, f.max_range, data.shape[-1]) self.data = f.map_data(data) self.stationlat = f.lat self.stationlon = f.lon self.el = f.metadata['el_angle'] self.scantime = f.metadata['vol_time'] o = open(file, 'rb') spec = np.frombuffer(o.read(2), '>i2')[0] self.dtype = self._det_product_type(spec) o.seek(12) self.code = 'Z9{}'.format(np.frombuffer(o.read(2), '>i2')[0]) o.close() self._update_radar_info()
def read_level3_file(fname): Level3File(fname)
def test_fobj(): 'Test reading a specific NEXRAD NIDS files from a file object' Level3File(get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids'))
def test_nwstg(): 'Test reading a nids file pulled from the NWSTG' Level3File(get_test_data('nids/sn.last', as_file_obj=False))
def test_tdwr(): 'Test reading a specific TDWR file' f = Level3File(get_test_data('nids/Level3_SLC_TV0_20160516_2359.nids')) assert f.prod_desc.prod_code == 182
def test_basic(): 'Basic test of reading one specific NEXRAD NIDS file based on the filename' Level3File( get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids', as_file_obj=False))
def test_basic(self): Level3File( os.path.join(datadir, 'nids/Level3_FFC_N0Q_20140407_1805.nids'))
def test_fobj(self): Level3File(get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids'))
def test_dhr(): """Test reading a time field for DHR product.""" f = Level3File(get_test_data('nids/KOUN_SDUS54_DHRTLX_201305202016')) assert f.metadata['avg_time'] == datetime(2013, 5, 20, 20, 18)
cdict = [ (0 / 255, 172 / 255, 164 / 255), (192 / 255, 192 / 255, 255 / 255), (122 / 255, 114 / 255, 238 / 255), (30 / 255, 38 / 255, 208 / 255), (166 / 255, 252 / 255, 168 / 255), (0 / 255, 234 / 255, 0 / 255), (16 / 255, 146 / 255, 26 / 255), (252 / 255, 254 / 255, 100 / 255), (200 / 255, 200 / 255, 2 / 255), (140 / 255, 140 / 255, 0 / 255), (254 / 255, 172 / 255, 172 / 255), (255 / 255, 100 / 255, 92 / 255), (238 / 255, 2 / 255, 48 / 255), (212 / 255, 142 / 255, 255 / 255), (170 / 255, 36 / 255, 250 / 255) ] r_cmap = colors.ListedColormap(cdict, 'indexed') return r_cmap, cmx.Normalize(-5, 70) # Open the file f = Level3File('C:\\pyproj\\cinrad\\pupdata\\R\\19\\20170330.084822.02.19.778') datadict = f.sym_block[0][0] data = np.ma.array(datadict['data']) data[data == 0] = np.ma.masked data = np.ma.masked_invalid(f.map_data(data)) az = np.array(datadict['start_az'] + [datadict['end_az'][-1]]) rng = np.linspace(0, f.max_range, data.shape[-1] + 1) xlocs = rng * np.sin(np.deg2rad(az[:, np.newaxis])) #第一维是方位角,第二维是极径 ylocs = rng * np.cos(np.deg2rad(az[:, np.newaxis])) sta_lon = f.lon #站点经纬度 sta_lat = f.lat a = 6.371e3
file = 'C:\\Users\\Matt\\Desktop\\KumjianCode\\2015_Concatenated_FieldData.xlsx' df = pd.read_excel(file) groups = df.groupby('ID') latGR = groups['LAT'].first() lonGR = groups['LON'].first() dataArray = [] for l in range(len(latGR)): print(l, latGR[l], lonGR[l]) for filename in os.listdir(dataPath): print(filename) # Grab every file in the folder a = get_test_data(dataPath + filename) #Extract Level 3 data and place in an array format b = Level3File(a) datadict = b.sym_block[0][0] data = b.map_data(datadict['data']) time = b.metadata.get('vol_time') # Turn into an array, then mask data[np.isnan(data)] = ma.masked # Grab azimuths and calculate a range based on number of gates az = np.array(datadict['start_az'] + [datadict['end_az'][-1]]) az = np.delete(az, 0, 0) #Clear duplicate azimuth rng = np.linspace(0, b.max_range, data.shape[-1]) theta = b.metadata['el_angle'] beam = np.transpose(np.repeat(rng[:, np.newaxis], 360, 1)) center_lat = np.ones([len(az), len(rng)]) * b.lat
(0, 176 / 255, 176 / 255), (0 / 255, 255 / 255, 0 / 255), (0 / 255, 196 / 255, 0 / 255), (0 / 255, 128 / 255, 0 / 255), (255 / 255, 255 / 255, 255 / 255), (255 / 255, 255 / 255, 255 / 255), (255 / 255, 0 / 255, 0 / 255), (255 / 255, 88 / 255, 88 / 255), (255 / 255, 176 / 255, 176 / 255), (255 / 255, 124 / 255, 0 / 255), (255 / 255, 210 / 255, 0 / 255), (255 / 255, 255 / 255, 0 / 255), (124 / 255, 0 / 255, 124 / 255) ] # v_cmap = colors.ListedColormap(cdict, 'indexed') return v_cmap, mpl.colors.BoundaryNorm( [-60, -27, -20, -15, -10, -5, -1, 0, 1, 5, 10, 15, 20, 27, 40, 45], v_cmap.N) # Open the file f = Level3File('C:\\pyproj\\cinrad\\pupdata\\V\\26\\20170330.093021.02.26.778') knots2ms = True datadict = f.sym_block[0][0] data1 = np.ma.array(datadict['data']) data1[data1 == 0] = np.ma.masked if knots2ms: data2 = f.map_data(data1) * 0.53 else: data2 = f.map_data(data1) data = np.ma.masked_invalid(data2) data_rf = data1 + 30 az = np.array(datadict['start_az'] + [datadict['end_az'][-1]]) rng = np.linspace(0, f.max_range, data.shape[-1] + 1)
def test_nwstg(self): Level3File(os.path.join(datadir, 'nids/sn.last'))
def test_basic(self): Level3File( get_test_data('nids/Level3_FFC_N0Q_20140407_1805.nids', as_file_obj=False))
def test_nwstg(self): Level3File(get_test_data('nids/sn.last', as_file_obj=False))
def test_level3_files(fname): 'Test opening a NEXRAD NIDS file' Level3File(fname)
def __init__(self, filepath: str): from metpy.io.nexrad import Level3File self.handler = Level3File(filepath) self.info = self.get_all_hi() self.storm_list = self.get_all_id()
def test_tdwr_nids(fname): 'Test opening a TDWR NIDS file' Level3File(fname)
# -*- coding: utf-8 -*- # Author: Du puyuan import numpy as np from metpy.io.nexrad import Level3File from tkinter import filedialog from metpy.plots import colortables from cinrad.datastruct import Radial from cinrad.projection import get_coordinate from cinrad.visualize import ppi name = filedialog.askopenfilename() f = Level3File(name) # Pull the data out of the file object datadict = f.sym_block[0][0] # Turn into an array, then mask data = np.array(datadict['data']).astype(float) data[data == 0] = np.nan rf = np.ma.array(data, mask=(data != 1)) # Grab azimuths and calculate a range based on number of gates az = np.deg2rad(datadict['start_az'] + [datadict['end_az'][-1]]) rng = np.linspace(0, f.max_range, data.shape[-1] + 1) elev = f.metadata['el_angle'] slon, slat = f.lon, f.lat scantime = f.metadata['msg_time'] lon, lat = get_coordinate(rng, az, elev, slon, slat) threshold = f.thresholds data = data * threshold[1] / 10 + threshold[0] / 10 v_obj = Radial([data, rf], int(f.max_range), elev, f.ij_to_km, f.siteID, f.siteID, scantime.strftime('%Y%m%d%H%M%S'), 'VEL', slon, slat)
def __init__(self, filepath:str): self.handler = Level3File(filepath) self.info = self.get_all_sti() self.storm_list = self.get_all_id()