usgs / gspy

Other
15 stars 5 forks source link

create a .gex reader #12

Open bminsley opened 8 months ago

bminsley commented 8 months ago

write a reader for skyTEM .gex files to parse information into appropriate locations in the survey metadata file

bminsley commented 8 months ago

`# Create an empty dictionary to store variables and their values variables = {}

gex file

gexfile = '20048_USA_DelawareBay_304M_SR2.gex'

Open the text file for reading

with open(gexfile, 'r') as file:

# Iterate through each line in the file
for line in file:

    # create sub-dictionary for each section in the file identified by []
    if line[0] == '[':
        section = line.strip()[1:-1]
        variables[section] = {}

    # Split the line based on the equals sign '='
    parts = line.strip().split('=')

    # Ensure there are at least two parts
    if len(parts) >= 2:
        variable_name = parts[0].strip()
        value = '='.join(parts[1:]).strip()

        # Check if the value contains whitespace, indicating multiple numbers
        if ' ' in value:
            try:
                # Split the value by whitespace and convert parts to float
                value = [float(part) for part in value.split()]
            except ValueError:
                # If it's not a valid number, keep it as a string
                pass
        else:
            # Check if the value is an integer
            if value.isdigit():
                value = int(value)
            else:
                try:
                    # Try converting the value to a float
                    value = float(value)
                except ValueError:
                    # If it's not a valid number, keep it as a string
                    pass

        # Assign the value to the variable name in the dictionary
        if variable_name in variables[section]:
            # If the variable already exists, append the value to the existing list
            if isinstance(variables[section][variable_name], list):
                variables[variable_name].append(value)
            else:
                # If the variable is not yet a list, convert it to a list and add the value
                variables[section][variable_name] = [variables[section][variable_name], value]
        else:
            variables[section][variable_name] = value

identify indexed keys and combine into 2d arrays

iterate over sections of the gex file

for section in variables.keys():

key_count = {}

# find number of indexed keys for each base key
for key in variables[section]:
    base_key = key.rstrip('0123456789')

    if base_key in key_count:
        key_count[base_key] += 1
    else:
        key_count[base_key] = 1

for base_key, count in key_count.items():
    if count > 1:
        variables[section][base_key] = [value for key, value in variables[section].items() if base_key in key]`