def _parse_metadata(self, metadata):
        """
        Transforms raw HADS metadata into a dictionary (station code -> props)
        """
        retval = {}

        # these are the first keys, afterwards follows a var-len list of variables/props
        # first key always blank so skip it
        field_keys = [
            "nesdis_id",
            "nwsli",
            "location_text",
            "latitude",
            "longitude",
            "hsa",
            "state",
            "owner",
            "manufacturer",
            "channel",
            "init_transmit",  # HHMM
            "trans_interval",
        ]  # min

        # repeat in blocks of 7 after field_keys
        var_keys = [
            "pe_code",
            "data_interval",  # min
            "coefficient",
            "constant",
            "time_offset",  # min
            "base_elevation",  # ft
            "gauge_correction",
        ]  # ft

        lines = metadata.splitlines()
        for line in lines:
            if len(line) == 0:
                continue

            raw_fields = line.split("|")

            fields = dict(zip(field_keys, raw_fields[1 : len(field_keys)]))

            # how many blocks of var_keys after initial fields
            var_offset = len(field_keys) + 1
            var_blocks = (len(raw_fields) - var_offset) // len(
                var_keys
            )  # how many variables
            vars_only = raw_fields[var_offset:]
            variables = {}

            for offset in range(var_blocks):
                var_dict = dict(
                    zip(
                        var_keys,
                        vars_only[
                            offset
                            * len(var_keys) : (offset + 1)
                            * len(var_keys)
                        ],
                    )
                )
                variables[var_dict["pe_code"]] = var_dict

                var_dict["base_elevation"] = float(var_dict["base_elevation"])
                var_dict["gauge_correction"] = float(
                    var_dict["gauge_correction"]
                )
                del var_dict["pe_code"]  # no need to duplicate

            line_val = {"variables": variables}
            line_val.update(fields)

            # conversions
            def dms_to_dd(dms):
                parts = dms.split(" ")
                sec = int(parts[1]) * 60 + int(parts[2])
                return float(parts[0]) + (
                    sec / 3600.0
                )  # negative already in first portion

            line_val["latitude"] = dms_to_dd(line_val["latitude"])
            line_val["longitude"] = dms_to_dd(line_val["longitude"])

            retval[line_val["nesdis_id"]] = line_val

        return retval
def _parse_metadata ( self , metadata ) : retval = { } field_keys = [ "nesdis_id" , "nwsli" , "location_text" , "latitude" , "longitude" , "hsa" , "state" , "owner" , "manufacturer" , "channel" , "init_transmit" , "trans_interval" , ] var_keys = [ "pe_code" , "data_interval" , "coefficient" , "constant" , "time_offset" , "base_elevation" , "gauge_correction" , ] lines = metadata . splitlines ( ) for line in lines : if len ( line ) == 0 : continue raw_fields = line . split ( "|" ) fields = dict ( zip ( field_keys , raw_fields [ 1 : len ( field_keys ) ] ) ) var_offset = len ( field_keys ) + 1 var_blocks = ( len ( raw_fields ) - var_offset ) // len ( var_keys ) vars_only = raw_fields [ var_offset : ] variables = { } for offset in range ( var_blocks ) : var_dict = dict ( zip ( var_keys , vars_only [ offset * len ( var_keys ) : ( offset + 1 ) * len ( var_keys ) ] , ) ) variables [ var_dict [ "pe_code" ] ] = var_dict var_dict [ "base_elevation" ] = float ( var_dict [ "base_elevation" ] ) var_dict [ "gauge_correction" ] = float ( var_dict [ "gauge_correction" ] ) del var_dict [ "pe_code" ] line_val = { "variables" : variables } line_val . update ( fields ) def dms_to_dd ( dms ) : parts = dms . split ( " " ) sec = int ( parts [ 1 ] ) * 60 + int ( parts [ 2 ] ) return float ( parts [ 0 ] ) + ( sec / 3600.0 ) line_val [ "latitude" ] = dms_to_dd ( line_val [ "latitude" ] ) line_val [ "longitude" ] = dms_to_dd ( line_val [ "longitude" ] ) retval [ line_val [ "nesdis_id" ] ] = line_val return retval
Transforms raw HADS metadata into a dictionary (station code -> props)