Snippets

Nathanaël Schaeffer Read geomagnetic core-field data in SHC format

Created by Nathanaël Schaeffer
# This short script reads a .shc file into a python dictionary.
# Files of geomagnetic core-field models in this format can be found here:
# https://www.space.dtu.dk/english/research/scientific_data_and_models/magnetic_field_models

import numpy as np

def load_shc(fname):
    f=open(fname,'r')
    a=f.readlines()
    f.close()

    # Read header:
    while len(a) > 0:
        if a[0][0] == '#':
            del a[0]   # remove comments
        else:
            del a[0]   # remove header line
            times = np.fromstring(a[0],sep=' ')  # times
            del a[0]   # remove times
            break

    # Read data, one line for each (l,m), into a dictionary, with (l,m) as the key.
    glm = {}
    while len(a) > 0:
        x = np.fromstring(a[0],sep=' ')
        lm = (int(x[0]), int(x[1]))
        glm[lm] = x[2:]
        del a[0]
    # now we have 'times' and 'glm' containing the data
    return times, glm

# example using COV-OBS.x2 data file from http://www.spacecenter.dk/files/magnetic-models/COV-OBSx2/COV-OBS.x2-int.shc
# other models also available here: https://www.space.dtu.dk/english/research/scientific_data_and_models/magnetic_field_models
t,glm = load_shc("COV-OBS.x2-int.shc")
print(t.shape, glm.keys())

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.