# The read function below reads SWAN HSR K0 txt files. This data can be found # at https://data.phys.ucalgary.ca/sort_by_project/SWAN/hsr/l0/multi_freq/. The # function takes in the path of a K0 txt file (must be downloaded prior) and # returns a Pandas dataframe. # # Changelog: # 2023-07-13: Filobateer Ghaly # - initial creation # 2023-09-05: Filobateer Ghaly # - bugfix for timestamp and indexing import pandas as pd def read(filename: str) -> dict: """ Reads k0 data files for a given site, date and file version. Args: filename: the name of the HSR K0 file to read Raises: FileNotFoundError: in case the file does not exist Returns: dict: A dictionary containing the metadata and the data frame. The metadata includes the band ids, central frequencies, passbands, and more. """ # init metadata_dict = {} # read data try: fp = open(filename, 'r') for line in fp: line = line.strip() if (len(line) == 0): continue elif (line[0] == '#' and ':' in line): key, val = line[2:].strip().split(": ") metadata_dict[key] = val else: break fp.close() except FileNotFoundError: raise FileNotFoundError(f"The file {filename} does not exist.") # create dataframe columns = ["Date", "Time"] + metadata_dict["Band Unique IDs"].split(', ') df = pd.read_csv(filename, comment='#', delim_whitespace=True, names=columns) # convert time column to datetime objects df.insert(0, "timestamp", pd.to_datetime(df["Date"] + " " + df["Time"], format="%Y-%m-%d %H:%M:%S")) df.drop(["Date", "Time"], axis=1, inplace=True) # organize data return object ret_data = { "data": df, "metadata": metadata_dict, } # return return ret_data