Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Update older emg3d file formats to v1.
import copy
import emg3d
def get_model(inp, grid):
"""Model needs new a grid instead of vnC."""
inp = {k: v for k, v in inp.items() if k != 'vnC'}
inp['grid'] = grid if isinstance(grid, dict) else grid.to_dict()
return emg3d.Model.from_dict(inp)
def get_field(inp, grid):
"""Field needs new a grid instead of [vnE{x;y;z}]; field->data; freq->frequency."""
inp = {k: v for k, v in inp.items() if k not in ['vnEx', 'vnEy', 'vnEz']}
inp['data'] = inp.pop('field', None)
inp['frequency'] = inp.pop('freq', None)
inp['grid'] = grid if isinstance(grid, dict) else grid.to_dict()
return emg3d.Field.from_dict(inp)
def get_survey(inp):
"""Fixed survey does not work; everything else should be oK."""
inp = copy.deepcopy(data['survey'])
if inp['fixed']:
raise NotImplementedError('Fixed Surveys are not implemented')
inp = {k: v for k, v in inp.items() if k not in ['fixed']}
for k, v in inp['sources'].items():
if v['electric']:
fct = emg3d.TxElectricDipole
fct = emg3d.TxMagneticDipole
inp['sources'][k] = fct(v['coordinates'], strength=v.pop('strength', 1.0)).to_dict()
for k, v in inp['receivers'].items():
if v['electric']:
fct = emg3d.RxElectricPoint
fct = emg3d.RxMagneticPoint
inp['receivers'][k] = fct(v['coordinates']).to_dict()
return emg3d.Survey.from_dict(inp)
def get_simulation(inp):
"""Many edge-cases might fail for Simulation."""
inp = copy.deepcopy(data['simulation'])
inp['survey'] = get_survey(inp['survey']).to_dict()
inp['model'] = get_model(inp['model'], inp['grid']).to_dict()
inp['verb'] = inp.pop('verb', 0)
inp['info'] = inp.pop('info', '')
inp['_input_s2c'] = inp.pop('_inp_nCz', None)
for k, v in inp['gridding_opts'].items():
if isinstance(v, dict) and '__type__' in v.keys():
inp['gridding_opts'][k] = {'x': v['0'], 'y': v['1'], 'z': v['2']}
for name in ['_dict_efield', '_dict_hfield']:
if name in inp.keys():
for k1, v1 in inp[name].items():
for k2, v2 in v1.items():
if v2 is not None:
if '_dict_grid' in inp.keys():
grid = inp['_dict_grid'][k1][k2]
grid = inp['grid']
inp[name][k1][k2] = get_field(v2, grid).to_dict()
_ = inp.pop('_dict_sfield', None)
_ = inp.pop('_dict_grid', None)
return emg3d.Simulation.from_dict(inp)
# # EXAMPLE # #
# To run this you need a version of emg3d with the new file format v1.0.
# OldFormats.h5 has one Grid, one Model, one Field, one Survey, one Simulation.
# This will throuw A LOT of warnings (one for each Grid/Model/Field/Survey/Simulation/Dipole).
data = emg3d.load('./OldFormats.h5')
# Grid (still works)
grid = data['grid']
# Model
model = get_model(data['model'], grid) # Needs new the corresponding grid
# Field
field = get_field(data['field'], grid) # Needs new the corresponding grid
# Survey
survey = get_survey(data['survey'])
# Simulation
simulation = get_simulation(data['simulation'])
# Store now with the new version.'./NewFormat.h5', grid=grid, model=model, field=field, survey=survey, simulation=simulation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment