python-awips/awips/RadarCommon.py

142 lines
3.7 KiB
Python
Raw Normal View History

2018-09-06 12:11:36 -06:00
#
# Common methods for the a2gtrad and a2advrad scripts.
#
#
#
# SOFTWARE HISTORY
#
# Date Ticket# Engineer Description
# ------------ ---------- ----------- --------------------------
# 08/13/2014 3393 nabowle Initial creation to contain common
# code for a2*radStub scripts.
# 03/15/2015 mjames@ucar Edited/added to awips package as RadarCommon
#
#
2018-10-14 17:37:08 -06:00
2018-09-06 12:11:36 -06:00
def get_datetime_str(record):
"""
Get the datetime string for a record.
2018-10-11 14:18:36 -06:00
Args:
record: the record to get data for.
2018-09-06 12:11:36 -06:00
2018-10-11 14:18:36 -06:00
Returns:
datetime string.
2018-09-06 12:11:36 -06:00
"""
2018-10-16 21:33:43 -06:00
return str(record.getDataTime())[0:19].replace(" ", "_") + ".0"
2018-09-06 12:11:36 -06:00
2018-10-14 17:37:08 -06:00
2018-09-06 12:11:36 -06:00
def get_data_type(azdat):
"""
Get the radar file type (radial or raster).
2018-10-11 14:18:36 -06:00
Args:
azdat: Boolean.
2018-09-06 12:11:36 -06:00
2018-10-11 14:18:36 -06:00
Returns:
Radial or raster.
2018-09-06 12:11:36 -06:00
"""
if azdat:
return "radial"
return "raster"
2018-09-06 12:11:36 -06:00
2018-10-14 17:37:08 -06:00
2018-09-06 12:11:36 -06:00
def get_hdf5_data(idra):
rdat = []
azdat = []
depVals = []
threshVals = []
if len(idra) > 0:
for ii in range(len(idra)):
if idra[ii].getName() == "Data":
rdat = idra[ii]
elif idra[ii].getName() == "Angles":
azdat = idra[ii]
# dattyp = "radial"
elif idra[ii].getName() == "DependentValues":
depVals = idra[ii].getShortData()
elif idra[ii].getName() == "Thresholds":
threshVals = idra[ii].getShortData()
2018-09-06 12:11:36 -06:00
2018-10-16 21:33:43 -06:00
return rdat, azdat, depVals, threshVals
2018-09-06 12:11:36 -06:00
def get_header(record, headerFormat, xLen, yLen, azdat, description):
2018-09-06 12:11:36 -06:00
# Encode dimensions, time, mapping, description, tilt, and VCP
mytime = get_datetime_str(record)
dattyp = get_data_type(azdat)
if headerFormat:
2018-09-06 12:11:36 -06:00
msg = str(xLen) + " " + str(yLen) + " " + mytime + " " + \
dattyp + " " + str(record.getLatitude()) + " " + \
str(record.getLongitude()) + " " + \
str(record.getElevation()) + " " + \
str(record.getElevationNumber()) + " " + \
description + " " + str(record.getTrueElevationAngle()) + " " + \
str(record.getVolumeCoveragePattern()) + "\n"
else:
2018-09-06 12:11:36 -06:00
msg = str(xLen) + " " + str(yLen) + " " + mytime + " " + \
dattyp + " " + description + " " + \
str(record.getTrueElevationAngle()) + " " + \
str(record.getVolumeCoveragePattern()) + "\n"
return msg
def encode_thresh_vals(threshVals):
spec = [".", "TH", "ND", "RF", "BI", "GC", "IC", "GR", "WS", "DS",
"RA", "HR", "BD", "HA", "UK"]
nnn = len(threshVals)
j = 0
msg = ""
2018-10-16 21:33:43 -06:00
while j < nnn:
2018-09-06 12:11:36 -06:00
lo = threshVals[j] % 256
hi = threshVals[j] / 256
msg += " "
j += 1
if hi < 0:
if lo > 14:
2018-09-06 12:11:36 -06:00
msg += "."
else:
2018-09-06 12:11:36 -06:00
msg += spec[lo]
continue
if hi % 16 >= 8:
2018-09-06 12:11:36 -06:00
msg += ">"
elif hi % 8 >= 4:
2018-09-06 12:11:36 -06:00
msg += "<"
if hi % 4 >= 2:
2018-09-06 12:11:36 -06:00
msg += "+"
elif hi % 2 >= 1:
2018-09-06 12:11:36 -06:00
msg += "-"
if hi >= 64:
msg += "%.2f" % (lo*0.01)
elif hi % 64 >= 32:
msg += "%.2f" % (lo*0.05)
elif hi % 32 >= 16:
msg += "%.1f" % (lo*0.1)
else:
2018-09-06 12:11:36 -06:00
msg += str(lo)
msg += "\n"
return msg
def encode_dep_vals(depVals):
nnn = len(depVals)
j = 0
msg = []
while j < nnn:
2018-09-06 12:11:36 -06:00
msg.append(str(depVals[j]))
j += 1
return msg
def encode_radial(azVals):
azValsLen = len(azVals)
j = 0
msg = []
while j < azValsLen:
2018-09-06 12:11:36 -06:00
msg.append(azVals[j])
j += 1
return msg