Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create a .gex reader #12

Open
bminsley opened this issue Nov 1, 2023 · 1 comment
Open

create a .gex reader #12

bminsley opened this issue Nov 1, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@bminsley
Copy link
Collaborator

bminsley commented Nov 1, 2023

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

@bminsley bminsley added the enhancement New feature or request label Nov 1, 2023
@bminsley bminsley reopened this Nov 14, 2023
@bminsley
Copy link
Collaborator Author

`# 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]`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant