diff --git a/auto_rx/autorx/geometry.py b/auto_rx/autorx/geometry.py index f2dd72f..a53daf2 100644 --- a/auto_rx/autorx/geometry.py +++ b/auto_rx/autorx/geometry.py @@ -12,7 +12,7 @@ import numpy as np from .utils import position_info -def getDensity(altitude): +def getDensity(altitude, get_pressure=False): """ Calculate the atmospheric density for a given altitude in metres. This is a direct port of the oziplotter Atmosphere class @@ -75,6 +75,9 @@ def getDensity(altitude): # Finally, work out the density... speedOfSound = math.sqrt(gamma * R * temperature) pressure = pressureRel * pressureSL + if get_pressure: + return pressure + density = densitySL * pressureRel * temperatureSL / temperature return density diff --git a/auto_rx/autorx/log_files.py b/auto_rx/autorx/log_files.py index 515e286..cfc3d64 100644 --- a/auto_rx/autorx/log_files.py +++ b/auto_rx/autorx/log_files.py @@ -16,6 +16,7 @@ import numpy as np from dateutil.parser import parse from autorx.utils import short_type_lookup, readable_timedelta, strip_sonde_serial +from autorx.geometry import GenericTrack, getDensity def log_filename_to_stats(filename): @@ -90,7 +91,81 @@ def list_log_files(): def read_log_file(filename): """ Read in a log file """ - return {} + logging.debug(f"Attempting to read file: {filename}") + + # Open the file and get the header line + _file = open(filename,'r') + _header = _file.readline() + + # Initially assume a new style log file (> ~1.4.0) + # timestamp,serial,frame,lat,lon,alt,vel_v,vel_h,heading,temp,humidity,pressure,type,freq_mhz,snr,f_error_hz,sats,batt_v,burst_timer,aux_data + fields = { + 'datetime': 'f0', + 'serial': 'f1', + 'frame': 'f2', + 'latitude': 'f3', + 'longitude': 'f4', + 'altitude': 'f5', + 'vel_v': 'f6', + 'vel_h': 'f7', + 'heading': 'f8', + 'temp': 'f9', + 'humidity': 'f10', + 'pressure': 'f11', + 'type': 'f12', + 'frequency': 'f13', + 'snr': 'f14', + 'sats': 'f16', + 'batt': 'f17', + } + + if 'other' in _header: + # Older style log file + #timestamp,serial,frame,lat,lon,alt,vel_v,vel_h,heading,temp,humidity,type,freq,other + # 2020-06-06T00:58:09.001Z,R3670268,7685,-31.21523,137.68126,33752.4,5.9,2.1,44.5,-273.0,-1.0,RS41,401.501,SNR 5.4,FERROR -187,SATS 9,BATT 2.7 + fields = { + 'datetime': 'f0', + 'serial': 'f1', + 'frame': 'f2', + 'latitude': 'f3', + 'longitude': 'f4', + 'altitude': 'f5', + 'vel_v': 'f6', + 'vel_h': 'f7', + 'heading': 'f8', + 'temp': 'f9', + 'humidity': 'f10', + 'type': 'f11', + 'frequency': 'f12', + } + # Only use a subset of the columns, as the number of colums can vary in this old format + _data = np.genfromtxt(_file, dtype=None, encoding='ascii', delimiter=',', usecols=(0,1,2,3,4,5,6,7,8,9,10,11,12)) + + else: + # Grab everything + _data = np.genfromtxt(_file, dtype=None, encoding='ascii', delimiter=',') + + _file.close() + + # Now we need to rearrange some data for easier use in the client + _output = { + 'serial': strip_sonde_serial(_data[fields['serial']][0]) + } + + # Path to display on the map + _output['path'] = np.column_stack((_data[fields['latitude']],_data[fields['longitude']],_data[fields['altitude']])).tolist() + _output['first'] = _output['path'][0] + _output['first_time'] = _data[fields['datetime']][0] + _output['last'] = _output['path'][-1] + _output['last_time'] = _data[fields['datetime']][-1] + _burst_idx = np.argmax(_data[fields['altitude']]) + _output['burst'] = _output['path'][_burst_idx] + _output['burst_time'] = _data[fields['datetime']][_burst_idx] + + + # TODO: Calculate data necessary for Skew-T plots + + return _output @@ -99,20 +174,29 @@ def read_log_by_serial(serial): # Search in the logging directory for a matching serial number - _log_mask = os.path.join(autorx.logging_path, f"*_{serial}_*_sonde.log") + _log_mask = os.path.join(autorx.logging_path, f"*_*{serial}_*_sonde.log") _matching_files = glob.glob(_log_mask) # No matching entries found if len(_matching_files) == 0: return {} + else: + try: + data = read_log_file(_matching_files[0]) + return data + except Exception as e: + logging.exception(f"Error reading file for serial: {serial}", e) + return {} - return {} - if __name__ == "__main__": import sys + logging.basicConfig( + format="%(asctime)s %(levelname)s:%(message)s", level=logging.DEBUG + ) + print(list_log_files()) if len(sys.argv) > 1: