diff --git a/tofu/data/_physics.py b/tofu/_physics.py similarity index 100% rename from tofu/data/_physics.py rename to tofu/_physics.py diff --git a/tofu/data/_core.py b/tofu/data/_core.py index a4f45c47e..0529c28ec 100644 --- a/tofu/data/_core.py +++ b/tofu/data/_core.py @@ -24,13 +24,13 @@ import tofu.data._comp as _comp import tofu.data._plot as _plot import tofu.data._def as _def - import tofu.data._physics as _physics + import tofu._physics as _physics import tofu.data._spectrafit2d as _spectrafit2d except Exception: from . import _comp as _comp from . import _plot as _plot from . import _def as _def - from . import _physics as _physics + from .. import _physics as _physics from . import _spectrafit2d as _spectrafit2d __all__ = ['DataCam1D','DataCam2D', diff --git a/tofu/data/_core_new.py b/tofu/data/_core_new.py index 6c9b97937..0b2b49d33 100644 --- a/tofu/data/_core_new.py +++ b/tofu/data/_core_new.py @@ -24,12 +24,12 @@ import tofu.data._comp as _comp import tofu.data._plot as _plot import tofu.data._def as _def - import tofu.data._physics as _physics + import tofu._physics as _physics except Exception: from . import _comp as _comp from . import _plot as _plot from . import _def as _def - from . import _physics as _physics + from .. import _physics as _physics __all__ = ['DataHolder'] # , 'Plasma0D'] diff --git a/tofu/imas2tofu/__init__.py b/tofu/imas2tofu/__init__.py index 842ecaef4..5df50ae23 100644 --- a/tofu/imas2tofu/__init__.py +++ b/tofu/imas2tofu/__init__.py @@ -11,8 +11,10 @@ try: try: from tofu.imas2tofu._core import * + from tofu.imas2tofu._mat2ids2calc import * except Exception: from ._core import * + from ._mat2ids2calc import * except Exception as err: if str(err) == 'imas not available': msg = "" diff --git a/tofu/imas2tofu/_core.py b/tofu/imas2tofu/_core.py index 6ec5f4939..6f298dcc8 100644 --- a/tofu/imas2tofu/_core.py +++ b/tofu/imas2tofu/_core.py @@ -23,6 +23,12 @@ import matplotlib as mpl import datetime as dtm +# tofu +try: + import tofu.imas2tofu._def as _defimas2tofu +except Exception as err: + from . import _def as _defimas2tofu + # imas try: import imas @@ -90,402 +96,8 @@ class MultiIDSLoader(object): 'shot', 'run', 'refshot', 'refrun'] # Known short version of signal str - - _dshort = { - 'wall': - {'wallR':{'str':'description_2d[0].limiter.unit[0].outline.r'}, - 'wallZ':{'str':'description_2d[0].limiter.unit[0].outline.z'}}, - - 'pulse_schedule': - {'events_times':{'str':'event[].time_stamp'}, - 'events_names':{'str':'event[].identifier'}}, - - 'equilibrium': - {'t':{'str':'time'}, - 'ip':{'str':'time_slice[time].global_quantities.ip', - 'dim':'current', 'quant':'Ip', 'units':'A'}, - 'q0':{'str':'time_slice[time].global_quantities.q_axis'}, - 'qmin':{'str':'time_slice[time].global_quantities.q_min.value'}, - 'q95':{'str':'time_slice[time].global_quantities.q_95'}, - 'volume':{'str':'time_slice[time].global_quantities.volume', - 'dim':'volume', 'quant':'pvol', 'units':'m3'}, - 'psiaxis':{'str':'time_slice[time].global_quantities.psi_axis', - 'dim':'B flux', 'quant':'psi', 'units':'Wb'}, - 'psisep':{'str':'time_slice[time].global_quantities.psi_boundary', - 'dim':'B flux', 'quant':'psi', 'units':'Wb'}, - 'BT0':{'str':'time_slice[time].global_quantities.magnetic_axis.b_field_tor', - 'dim':'B', 'quant':'BT', 'units':'T'}, - 'axR':{'str':'time_slice[time].global_quantities.magnetic_axis.r', - 'dim':'distance', 'quant':'R', 'units':'m'}, - 'axZ':{'str':'time_slice[time].global_quantities.magnetic_axis.z', - 'dim':'distance', 'quant':'Z', 'units':'m'}, - 'x0R':{'str':'time_slice[time].boundary.x_point[0].r'}, - 'x0Z':{'str':'time_slice[time].boundary.x_point[0].z'}, - 'x1R':{'str':'time_slice[time].boundary.x_point[1].r'}, - 'x1Z':{'str':'time_slice[time].boundary.x_point[1].z'}, - 'strike0R':{'str':'time_slice[time].boundary.strike_point[0].r'}, - 'strike0Z':{'str':'time_slice[time].boundary.strike_point[0].z'}, - 'strike1R':{'str':'time_slice[time].boundary.strike_point[1].r'}, - 'strike1Z':{'str':'time_slice[time].boundary.strike_point[1].z'}, - 'sepR':{'str':'time_slice[time].boundary_separatrix.outline.r'}, - 'sepZ':{'str':'time_slice[time].boundary_separatrix.outline.z'}, - - '1drhotn':{'str':'time_slice[time].profiles_1d.rho_tor_norm', - 'dim':'rho', 'quant':'rhotn', 'units':'adim.'}, - '1dphi':{'str':'time_slice[time].profiles_1d.phi', - 'dim':'B flux', 'quant':'phi', 'units':'Wb'}, - '1dpsi':{'str':'time_slice[time].profiles_1d.psi', - 'dim':'B flux', 'quant':'psi', 'units':'Wb'}, - '1dq':{'str':'time_slice[time].profiles_1d.q', - 'dim':'safety factor', 'quant':'q', 'units':'adim.'}, - '1dpe':{'str':'time_slice[time].profiles_1d.pressure', - 'dim':'pressure', 'quant':'pe', 'units':'Pa'}, - '1djT':{'str':'time_slice[time].profiles_1d.j_tor', - 'dim':'vol. current dens.', 'quant':'jT', 'units':'A/m^2'}, - - '2dphi':{'str':'time_slice[time].ggd[0].phi[0].values', - 'dim':'B flux', 'quant':'phi', 'units':'Wb'}, - '2dpsi':{'str':'time_slice[time].ggd[0].psi[0].values', - 'dim':'B flux', 'quant':'psi', 'units':'Wb'}, - '2djT':{'str':'time_slice[time].ggd[0].j_tor[0].values', - 'dim':'vol. current dens.', 'quant':'jT', 'units':'A/m^2'}, - '2dBR':{'str':'time_slice[time].ggd[0].b_field_r[0].values', - 'dim':'B', 'quant':'BR', 'units':'T'}, - '2dBT':{'str':'time_slice[time].ggd[0].b_field_tor[0].values', - 'dim':'B', 'quant':'BT', 'units':'T'}, - '2dBZ':{'str':'time_slice[time].ggd[0].b_field_z[0].values', - 'dim':'B', 'quant':'BZ', 'units':'T'}, - '2dmeshNodes': {'str': ('grids_ggd[0].grid[0].space[0]' - + '.objects_per_dimension[0]' - + '.object[].geometry')}, - '2dmeshFaces': {'str': ('grids_ggd[0].grid[0].space[0]' - + '.objects_per_dimension[2]' - + '.object[].nodes')}, - '2dmeshR': {'str': 'time_slice[0].profiles_2d[0].r'}, - '2dmeshZ': {'str': 'time_slice[0].profiles_2d[0].z'}}, - - 'core_profiles': - {'t':{'str':'time'}, - 'ip':{'str':'global_quantities.ip', - 'dim':'current', 'quant':'Ip', 'units':'A'}, - 'vloop':{'str':'global_quantities.v_loop', - 'dim':'voltage', 'quant':'Vloop', 'units':'V/m'}, - - '1dTe':{'str':'profiles_1d[time].electrons.temperature', - 'dim':'temperature', 'quant':'Te', 'units':'eV'}, - '1dne':{'str':'profiles_1d[time].electrons.density', - 'dim':'density', 'quant':'ne', 'units':'/m^3'}, - '1dzeff':{'str':'profiles_1d[time].zeff', - 'dim':'charge', 'quant':'zeff', 'units':'adim.'}, - '1dphi':{'str':'profiles_1d[time].grid.phi', - 'dim':'B flux', 'quant':'phi', 'units':'Wb'}, - '1dpsi':{'str':'profiles_1d[time].grid.psi', - 'dim':'B flux', 'quant':'psi', 'units':'Wb'}, - '1drhotn':{'str':'profiles_1d[time].grid.rho_tor_norm', - 'dim':'rho', 'quant':'rhotn', 'units':'adim.'}, - '1drhopn':{'str':'profiles_1d[time].grid.rho_pol_norm', - 'dim':'rho', 'quant':'rhopn', 'units':'adim.'}, - '1dnW':{'str':'profiles_1d[time].ions[identifier.label=W].density', - 'dim':'density', 'quant':'nI', 'units':'/m^3'}}, - - 'edge_profiles': - {'t':{'str':'time'}}, - - 'core_sources': - {'t':{'str':'time'}, - '1dpsi':{'str':'source[identifier.name=lineradiation].profiles_1d[time].grid.psi', - 'dim':'B flux', 'quant':'psi', 'units':'Wb'}, - '1drhotn':{'str':'source[identifier.name=lineradiation].profiles_1d[time].grid.rho_tor_norm', - 'dim':'rho', 'quant':'rhotn', 'units':'Wb'}, - '1dbrem':{'str':"source[identifier.name=bremsstrahlung].profiles_1d[time].electrons.energy", - 'dim':'vol.emis.', 'quant':'brem.', 'units':'W/m^3'}, - '1dline':{'str':"source[identifier.name=lineradiation].profiles_1d[time].electrons.energy", - 'dim':'vol. emis.', 'quant':'lines', 'units':'W/m^3'}}, - - 'edge_sources': - {'t':{'str':'time'}, - '2dmeshNodes':{'str':'grid_ggd[0].space[0].objects_per_dimension[0].object[].geometry'}, - '2dmeshFaces':{'str':'grid_ggd[0].space[0].objects_per_dimension[2].object[].nodes'}, - '2dradiation':{'str':'source[13].ggd[0].electrons.energy[0].values', - 'dim':'vol. emis.', 'quant':'vol.emis.', - 'name':'tot. vol. emis.','units':'W/m^3'}}, - - 'lh_antennas': - {'t':{'str':'antenna[chan].power_launched.time'}, - 'power0':{'str':'antenna[0].power_launched.data', - 'dim':'power', 'quant':'lh power', 'units':'W', - 'pos':True}, - 'power1':{'str':'antenna[1].power_launched.data', - 'dim':'power', 'quant':'lh power', 'units':'W', - 'pos':True}, - 'power':{'str':'antenna[chan].power_launched.data', - 'dim':'power', 'quant':'lh power', 'units':'W', - 'pos':True}, - 'R':{'str':'antenna[chan].position.r.data', - 'dim':'distance', 'quant':'R', 'units':'m'}}, - - 'ic_antennas': - {'t':{'str':'antenna[chan].module[0].power_forward.time'}, - 'power0mod_fwd':{'str':'antenna[0].module[].power_forward.data', - 'dim':'power', 'quant':'ic power', 'units':'W'}, - 'power0mod_reflect':{'str':'antenna[0].module[].power_reflected.data', - 'dim':'power', 'quant':'ic power', 'units':'W'}, - 'power1mod_fwd':{'str':'antenna[1].module[].power_forward.data', - 'dim':'power', 'quant':'ic power', 'units':'W'}, - 'power1mod_reflect':{'str':'antenna[1].module[].power_reflected.data', - 'dim':'power', 'quant':'ic power', 'units':'W'}, - 'power2mod_fwd':{'str':'antenna[2].module[].power_forward.data', - 'dim':'power', 'quant':'ic power', 'units':'W'}, - 'power2mod_reflect':{'str':'antenna[2].module[].power_reflected.data', - 'dim':'power', 'quant':'ic power', 'units':'W'}, - }, - - 'magnetics': - {'t':{'str':'time'}, - 'ip':{'str':'method[0].ip.data'}, - 'diamagflux':{'str':'method[0].diamagnetic_flux.data'}, - 'bpol_B':{'str':'bpol_probe[chan].field.data', - 'dim':'B', 'quant':'Bpol', 'units':'T'}, - 'bpol_name':{'str':'bpol_probe[chan].name'}, - 'bpol_R':{'str':'bpol_probe[chan].position.r', - 'dim':'distance', 'quant':'R', 'units':'m'}, - 'bpol_Z':{'str':'bpol_probe[chan].position.z', - 'dim':'distance', 'quant':'Z', 'units':'m'}, - 'bpol_angpol':{'str':'bpol_probe[chan].poloidal_angle', - 'dim':'angle', 'quant':'angle_pol', 'units':'rad'}, - 'bpol_angtor':{'str':'bpol_probe[chan].toroidal_angle', - 'dim':'angle', 'quant':'angle_tor', 'units':'rad'}, - 'floop_flux':{'str':'flux_loop[chan].flux.data', - 'dim':'B flux', 'quant':'B flux', 'units':'Wb'}, - 'floop_name':{'str':'flux_loop[chan].name'}, - 'floop_R': {'str': 'flux_loop[chan].position.r', - 'dim': 'distance', 'quant': 'R', 'units': 'm'}, - 'floop_Z': {'str': 'flux_loop[chan].position.z', - 'dim': 'distance', 'quant': 'Z', 'units': 'm'}}, - - 'barometry': - {'t': {'str': 'gauge[chan].pressure.time'}, - 'names': {'str': 'gauge[chan].name'}, - 'p': {'str': 'gauge[chan].pressure.data', - 'dim': 'pressure', 'quant': 'p', 'units': 'Pa?'}}, - - 'calorimetry': - {'t': {'str': 'group[chan].component[0].power.time'}, - 'names': {'str': 'group[chan].name'}, - 'power': {'str': 'group[chan].component[0].power.data', - 'dim': 'power', 'quant': 'extracted power', - 'units': 'W'}}, - - 'neutron_diagnostic': - {'t':{'str':'time', 'units':'s'}, - 'flux_total':{'str':'synthetic_signals.total_neutron_flux', - 'dim':'particle flux', 'quant':'particle flux', 'units':'Hz'}}, - - 'ece': - {'t':{'str':'time', - 'quant':'t', 'units':'s'}, - 'freq':{'str':'channel[chan].frequency.data', - 'dim':'freq', 'quant':'freq', 'units':'Hz'}, - 'Te': {'str':'channel[chan].t_e.data', - 'dim':'temperature', 'quant':'Te', 'units':'eV'}, - 'R': {'str':'channel[chan].position.r.data', - 'dim':'distance', 'quant':'R', 'units':'m'}, - 'rhotn':{'str':'channel[chan].position.rho_tor_norm.data', - 'dim':'rho', 'quant':'rhotn', 'units':'adim.'}, - 'theta':{'str':'channel[chan].position.theta.data', - 'dim':'angle', 'quant':'theta', 'units':'rad.'}, - 'tau1keV':{'str':'channel[chan].optical_depth.data', - 'dim':'optical_depth', 'quant':'tau', 'units':'adim.'}, - 'validity_timed': {'str':'channel[chan].t_e.validity_timed'}, - 'names': {'str': 'channel[chan].name'}, - 'Te0': {'str':'t_e_central.data', - 'dim':'temperature', 'quant':'Te', 'units':'eV'}}, - - 'reflectometer_profile': - {'t': {'str': 'time'}, - 'ne': {'str': 'channel[chan].n_e.data', - 'dim': 'density', 'quant': 'ne', 'units': '/m^3'}, - 'R': {'str': 'channel[chan].position.r', - 'dim': 'distance', 'quant': 'R', 'units': 'm'}, - 'Z': {'str': 'channel[chan].position.z', - 'dim': 'distance', 'quant': 'Z', 'units': 'm'}, - 'phi': {'str': 'channel[chan].position.phi', - 'dim': 'angle', 'quant': 'phi', 'units': 'rad'}, - 'names': {'str': 'channel[chan].name'}, - 'mode': {'str': 'mode'}, - 'sweep': {'str': 'sweep_time'}}, - - 'interferometer': - {'t': {'str': 'time', - 'quant': 't', 'units': 's'}, - 'names': {'str': 'channel[chan].name'}, - 'ne_integ': {'str': 'channel[chan].n_e_line.data', - 'dim': 'ne_integ', 'quant': 'ne_integ', - 'units': '/m2', 'Brightness': True}}, - - 'polarimeter': - {'t': {'str': 'time', - 'quant': 't', 'units': 's'}, - 'lamb': {'str': 'channel[chan].wavelength', - 'dim': 'distance', 'quant': 'wavelength', - 'units': 'm'}, - 'fangle': {'str': 'channel[chan].faraday_angle.data', - 'dim': 'angle', 'quant': 'faraday angle', - 'units': 'rad', 'Brightness': True}, - 'names': {'str': 'channel[chan].name'}}, - - 'bolometer': - {'t': {'str': 'channel[chan].power.time', - 'quant': 't', 'units': 's'}, - 'power': {'str': 'channel[chan].power.data', - 'dim': 'power', 'quant': 'power radiative', - 'units': 'W', 'Brightness': False}, - 'etendue': {'str': 'channel[chan].etendue', - 'dim': 'etendue', 'quant': 'etendue', - 'units': 'm2.sr'}, - 'names': {'str': 'channel[chan].name'}, - 'tpower': {'str': 'time', 'quant': 't', 'units': 's'}, - 'prad': {'str': 'power_radiated_total', - 'dim': 'power', 'quant': 'power radiative', - 'units': 'W'}, - 'pradbulk': {'str': 'power_radiated_inside_lcfs', - 'dim': 'power', 'quant': 'power radiative', - 'units': 'W'}}, - - 'soft_x_rays': - {'t': {'str': 'time', - 'quant': 't', 'units': 's'}, - 'power': {'str': 'channel[chan].power.data', - 'dim': 'power', 'quant': 'power radiative', - 'units': 'W', 'Brightness': False}, - 'brightness': {'str': 'channel[chan].brightness.data', - 'dim': 'brightness', 'quant': 'brightness', - 'units': 'W/(m2.sr)', 'Brightness': True}, - 'names': {'str': 'channel[chan].name'}, - 'etendue': {'str': 'channel[chan].etendue', - 'dim': 'etendue', 'quant': 'etendue', - 'units': 'm2.sr'}}, - - 'spectrometer_visible': - {'t':{'str':'channel[chan].grating_spectrometer.radiance_spectral.time', - 'quant':'t', 'units':'s'}, - 'spectra': {'str': ('channel[chan].grating_spectrometer' - + '.radiance_spectral.data'), - 'dim': 'radiance_spectral', - 'quant': 'radiance_spectral', - 'units': 'ph/s/(m2.sr)/m', 'Brightness': True}, - 'names': {'str': 'channel[chan].name'}, - 'lamb':{'str':'channel[chan].grating_spectrometer.wavelengths', - 'dim':'wavelength', 'quant':'wavelength', 'units':'m'}}, - - 'bremsstrahlung_visible': - {'t': {'str': 'time', - 'quant': 't', 'units': 's'}, - 'radiance': {'str': 'channel[chan].radiance_spectral.data', - 'dim': 'radiance_spectral', - 'quant': 'radiance_spectral', - 'units': 'ph/s/(m2.sr)/m', - 'Brightness': True}, - 'names': {'str': 'channel[chan].name'}, - 'lamb_up': {'str':'channel[chan].filter.wavelength_upper'}, - 'lamb_lo': {'str':'channel[chan].filter.wavelength_lower'}}, - } - - _didsdiag = {'lh_antennas': {'datacls': 'DataCam1D', - 'geomcls': False, - 'sig': {'data': 'power', - 't': 't'}}, - 'ic_antennas': {'datacls': 'DataCam1D', - 'geomcls': False, - 'sig': {'data': 'power', - 't': 't'}}, - 'magnetics': {'datacls': 'DataCam1D', - 'geomcls': False, - 'sig': {'data': 'bpol_B', - 't': 't'}}, - 'barometry': {'datacls': 'DataCam1D', - 'geomcls': False, - 'sig': {'data': 'p', - 't': 't'}}, - 'calorimetry': {'datacls': 'DataCam1D', - 'geomcls': False, - 'sig': {'data': 'power', - 't': 't'}}, - 'ece': {'datacls': 'DataCam1D', - 'geomcls': False, - 'sig': {'t': 't', - 'X': 'rhotn_sign', - 'data': 'Te'}}, - 'neutron_diagnostic': {'datacls': 'DataCam1D', - 'geomcls': False, - 'sig': {'t': 't', - 'data': 'flux_total'}}, - 'reflectometer_profile': {'datacls': 'DataCam1D', - 'geomcls': False, - 'sig': {'t': 't', - 'X': 'R', - 'data': 'ne'}}, - 'interferometer': {'datacls': 'DataCam1D', - 'geomcls': 'CamLOS1D', - 'sig': {'t': 't', - 'data': 'ne_integ'}, - 'synth': {'dsynth': { - 'quant': 'core_profiles.1dne', - 'ref1d': 'core_profiles.1drhotn', - 'ref2d': 'equilibrium.2drhotn'}, - 'dsig': {'core_profiles': ['t'], - 'equilibrium': ['t']}, - 'Brightness': True}}, - 'polarimeter': {'datacls': 'DataCam1D', - 'geomcls': 'CamLOS1D', - 'sig': {'t': 't', - 'data': 'fangle'}, - 'synth': {'dsynth': { - 'fargs': ['core_profiles.1dne', - 'equilibrium.2dBR', - 'equilibrium.2dBT', - 'equilibrium.2dBZ', - 'core_profiles.1drhotn', - 'equilibrium.2drhotn']}, - 'dsig': {'core_profiles': ['t'], - 'equilibrium': ['t']}, - 'Brightness': True}}, - 'bolometer': {'datacls': 'DataCam1D', - 'geomcls': 'CamLOS1D', - 'sig': {'t': 't', - 'data': 'power'}, - 'synth': {'dsynth': { - 'quant': 'core_sources.1dprad', - 'ref1d': 'core_sources.1drhotn', - 'ref2d': 'equilibrium.2drhotn'}, - 'dsig': {'core_sources': ['t'], - 'equilibrium': ['t']}, - 'Brightness': True}}, - 'soft_x_rays': {'datacls': 'DataCam1D', - 'geomcls': 'CamLOS1D', - 'sig': {'t': 't', - 'data': 'power'}}, - 'spectrometer_visible': {'datacls': 'DataCam1DSpectral', - 'geomcls': 'CamLOS1D', - 'sig': {'data': 'spectra', - 't': 't', - 'lamb': 'lamb'}}, - 'bremsstrahlung_visible': { - 'datacls': 'DataCam1D', - 'geomcls': 'CamLOS1D', - 'sig': {'t': 't', - 'data': 'radiance'}, - 'synth': { - 'dsynth': { - 'quant': ['core_profiles.1dTe', - 'core_profiles.1dne', - 'core_profiles.1dzeff'], - 'ref1d': 'core_profiles.1drhotn', - 'ref2d': 'equilibrium.2drhotn'}, - 'dsig': {'core_profiles': ['t'], - 'equilibrium': ['t']}, - 'Brightness': True}}} - + _dshort = _defimas2tofu._dshort + _didsdiag = _defimas2tofu._didsdiag _lidsplasma = ['equilibrium', 'core_profiles', 'core_sources', 'edge_profiles', 'edge_sources'] @@ -581,6 +193,8 @@ def _rhosign(rho, theta): rho[ind] = -rho[ind] return rho + def _lamb(lamb_up, lamb_lo): + return 0.5*(lamb_up + lamb_lo) _dcomp = { 'pulse_schedule': @@ -640,8 +254,10 @@ def _rhosign(rho, theta): 'units':'adim.'}}, 'bremsstrahlung_visible': - {'lamb':{'lstr':['lamb_up','lamb_lo'], 'func':np.mean, - 'dim':'distance', 'quantity':'wavelength', 'units':'m'}} + {'lamb': {'lstr': ['lamb_up', 'lamb_lo'], 'func': _lamb, + 'dim': 'distance', + 'quantity': 'wavelength', + 'units': 'm'}} } _lstr = ['los_pt1R', 'los_pt1Z', 'los_pt1Phi', @@ -2547,21 +2163,22 @@ def to_Config(self, Name=None, occ=None, mobile = True elif nmob == 0 and nlim > 0: mobile = False - elif nmob == nlim: + elif nmob > nlim: msgw = 'wall.description_2[{}]'.format(description_2d) - msg = ("\nids wall has same number of limiter / mobile units\n" + msg = ("\nids wall has less limiter than mobile units\n" + "\t- len({}.limiter.unit) = {}\n".format(msgw, nlim) + "\t- len({}.mobile.unit) = {}\n".format(msgw, nmob) - + " => Choosing limiter by default") + + " => Choosing mobile by default") warnings.warn(msg) - mobile = False - else: + mobile = True + elif nmob <= nlim: msgw = 'wall.description_2[{}]'.format(description_2d) - msg = ("Can't decide automatically whether to choose" - + " limiter or mobile!\n" + msg = ("\nids wall has more limiter than mobile units\n" + "\t- len({}.limiter.unit) = {}\n".format(msgw, nlim) - + "\t- len({}.mobile.unit) = {}".format(msgw, nmob)) - raise Exception(msg) + + "\t- len({}.mobile.unit) = {}\n".format(msgw, nmob) + + " => Choosing limiter by default") + warnings.warn(msg) + mobile = False assert isinstance(mobile, bool) # Get PFC @@ -3376,7 +2993,9 @@ def _checkformat_Cam_geom(self, ids=None, geomcls=None, indch=None): ids = next(iter(idsok)) if ids not in self._dids.keys(): - msg = "Provided ids should be available as a self.dids.keys() !" + msg = ("Provided ids should be available as a self.dids.keys()!\n" + + "\t- provided: {}\n".format(str(ids)) + + "\t- available: {}".format(sorted(self._dids.keys()))) raise Exception(msg) if ids not in self._lidsdiag: @@ -3657,7 +3276,7 @@ def to_Cam(self, ids=None, indch=None, indch_auto=False, """ # Check ids - idsok = set(self._lidsdiag).intersection(self._dids.keys()) + idsok = set(self._lidslos).intersection(self._dids.keys()) if ids is None and len(idsok) == 1: ids = next(iter(idsok)) diff --git a/tofu/imas2tofu/_def.py b/tofu/imas2tofu/_def.py new file mode 100644 index 000000000..306fa7b77 --- /dev/null +++ b/tofu/imas2tofu/_def.py @@ -0,0 +1,409 @@ +_dshort = { + 'wall': { + 'wallR': {'str': 'description_2d[0].limiter.unit[0].outline.r'}, + 'wallZ': {'str': 'description_2d[0].limiter.unit[0].outline.z'}}, + + 'pulse_schedule': { + 'events_times': {'str': 'event[].time_stamp'}, + 'events_names': {'str': 'event[].identifier'}}, + + 'equilibrium': { + 't': {'str': 'time'}, + 'ip': {'str': 'time_slice[time].global_quantities.ip', + 'dim': 'current', 'quant': 'Ip', 'units': 'A'}, + 'q0': {'str': 'time_slice[time].global_quantities.q_axis'}, + 'qmin': {'str': 'time_slice[time].global_quantities.q_min.value'}, + 'q95': {'str': 'time_slice[time].global_quantities.q_95'}, + 'volume': {'str': 'time_slice[time].global_quantities.volume', + 'dim': 'volume', 'quant': 'pvol', 'units': 'm3'}, + 'psiaxis': {'str': 'time_slice[time].global_quantities.psi_axis', + 'dim': 'B flux', 'quant': 'psi', 'units': 'Wb'}, + 'psisep': {'str': 'time_slice[time].global_quantities.psi_boundary', + 'dim': 'B flux', 'quant': 'psi', 'units': 'Wb'}, + 'BT0': {'str': ('time_slice[time].global_quantities' + + '.magnetic_axis.b_field_tor'), + 'dim': 'B', 'quant': 'BT', 'units': 'T'}, + 'axR': {'str': 'time_slice[time].global_quantities.magnetic_axis.r', + 'dim': 'distance', 'quant': 'R', 'units': 'm'}, + 'axZ': {'str': 'time_slice[time].global_quantities.magnetic_axis.z', + 'dim': 'distance', 'quant': 'Z', 'units': 'm'}, + 'x0R': {'str': 'time_slice[time].boundary.x_point[0].r'}, + 'x0Z': {'str': 'time_slice[time].boundary.x_point[0].z'}, + 'x1R': {'str': 'time_slice[time].boundary.x_point[1].r'}, + 'x1Z': {'str': 'time_slice[time].boundary.x_point[1].z'}, + 'strike0R': {'str': 'time_slice[time].boundary.strike_point[0].r'}, + 'strike0Z': {'str': 'time_slice[time].boundary.strike_point[0].z'}, + 'strike1R': {'str': 'time_slice[time].boundary.strike_point[1].r'}, + 'strike1Z': {'str': 'time_slice[time].boundary.strike_point[1].z'}, + 'sepR': {'str': 'time_slice[time].boundary_separatrix.outline.r'}, + 'sepZ': {'str': 'time_slice[time].boundary_separatrix.outline.z'}, + + '1drhotn': {'str': 'time_slice[time].profiles_1d.rho_tor_norm', + 'dim': 'rho', 'quant': 'rhotn', 'units': 'adim.'}, + '1dphi': {'str': 'time_slice[time].profiles_1d.phi', + 'dim': 'B flux', 'quant': 'phi', 'units': 'Wb'}, + '1dpsi': {'str': 'time_slice[time].profiles_1d.psi', + 'dim': 'B flux', 'quant': 'psi', 'units': 'Wb'}, + '1dq': {'str': 'time_slice[time].profiles_1d.q', + 'dim': 'safety factor', 'quant': 'q', 'units': 'adim.'}, + '1dpe': {'str': 'time_slice[time].profiles_1d.pressure', + 'dim': 'pressure', 'quant': 'pe', 'units': 'Pa'}, + '1djT': {'str': 'time_slice[time].profiles_1d.j_tor', + 'dim': 'vol. current dens.', 'quant': 'jT', 'units': 'A/m^2'}, + + '2dphi': {'str': 'time_slice[time].ggd[0].phi[0].values', + 'dim': 'B flux', 'quant': 'phi', 'units': 'Wb'}, + '2dpsi': {'str': 'time_slice[time].ggd[0].psi[0].values', + 'dim': 'B flux', 'quant': 'psi', 'units': 'Wb'}, + '2djT': {'str': 'time_slice[time].ggd[0].j_tor[0].values', + 'dim': 'vol. current dens.', 'quant': 'jT', 'units': 'A/m^2'}, + '2dBR': {'str': 'time_slice[time].ggd[0].b_field_r[0].values', + 'dim': 'B', 'quant': 'BR', 'units': 'T'}, + '2dBT': {'str': 'time_slice[time].ggd[0].b_field_tor[0].values', + 'dim': 'B', 'quant': 'BT', 'units': 'T'}, + '2dBZ': {'str': 'time_slice[time].ggd[0].b_field_z[0].values', + 'dim': 'B', 'quant': 'BZ', 'units': 'T'}, + '2dmeshNodes': {'str': ('grids_ggd[0].grid[0].space[0]' + + '.objects_per_dimension[0]' + + '.object[].geometry')}, + '2dmeshFaces': {'str': ('grids_ggd[0].grid[0].space[0]' + + '.objects_per_dimension[2]' + + '.object[].nodes')}, + '2dmeshR': {'str': 'time_slice[0].profiles_2d[0].r'}, + '2dmeshZ': {'str': 'time_slice[0].profiles_2d[0].z'}}, + + 'core_profiles': { + 't': {'str': 'time'}, + 'ip': {'str': 'global_quantities.ip', + 'dim': 'current', 'quant': 'Ip', 'units': 'A'}, + 'vloop': {'str': 'global_quantities.v_loop', + 'dim': 'voltage', 'quant': 'Vloop', 'units': 'V/m'}, + + '1dTe': {'str': 'profiles_1d[time].electrons.temperature', + 'dim': 'temperature', 'quant': 'Te', 'units': 'eV'}, + '1dne': {'str': 'profiles_1d[time].electrons.density', + 'dim': 'density', 'quant': 'ne', 'units': '/m^3'}, + '1dzeff': {'str': 'profiles_1d[time].zeff', + 'dim': 'charge', 'quant': 'zeff', 'units': 'adim.'}, + '1dphi': {'str': 'profiles_1d[time].grid.phi', + 'dim': 'B flux', 'quant': 'phi', 'units': 'Wb'}, + '1dpsi': {'str': 'profiles_1d[time].grid.psi', + 'dim': 'B flux', 'quant': 'psi', 'units': 'Wb'}, + '1drhotn': {'str': 'profiles_1d[time].grid.rho_tor_norm', + 'dim': 'rho', 'quant': 'rhotn', 'units': 'adim.'}, + '1drhopn': {'str': 'profiles_1d[time].grid.rho_pol_norm', + 'dim': 'rho', 'quant': 'rhopn', 'units': 'adim.'}, + '1dnW': {'str': 'profiles_1d[time].ions[identifier.label=W].density', + 'dim': 'density', 'quant': 'nI', 'units': '/m^3'}}, + + 'edge_profiles': { + 't': {'str': 'time'}}, + + 'core_sources': { + 't': {'str': 'time'}, + '1dpsi': {'str': ('source[identifier.name=lineradiation]' + + '.profiles_1d[time].grid.psi'), + 'dim': 'B flux', 'quant': 'psi', 'units': 'Wb'}, + '1drhotn': {'str': ('source[identifier.name=lineradiation]' + + '.profiles_1d[time].grid.rho_tor_norm'), + 'dim': 'rho', 'quant': 'rhotn', 'units': 'Wb'}, + '1dbrem': {'str': ('source[identifier.name=bremsstrahlung]' + + '.profiles_1d[time].electrons.energy'), + 'dim': 'vol.emis.', 'quant': 'brem.', 'units': 'W/m^3'}, + '1dline': {'str': ('source[identifier.name=lineradiation]' + + '.profiles_1d[time].electrons.energy'), + 'dim': 'vol. emis.', 'quant': 'lines', 'units': 'W/m^3'}}, + + 'edge_sources': { + 't': {'str': 'time'}, + '2dmeshNodes': {'str': ('grid_ggd[0].space[0].objects_per_dimension[0]' + + '.object[].geometry')}, + '2dmeshFaces': {'str': ('grid_ggd[0].space[0].objects_per_dimension[2]' + + '.object[].nodes')}, + '2dradiation': {'str': 'source[13].ggd[0].electrons.energy[0].values', + 'dim': 'vol. emis.', 'quant': 'vol.emis.', + 'name': 'tot. vol. emis.', 'units': 'W/m^3'}}, + + 'lh_antennas': { + 't': {'str': 'antenna[chan].power_launched.time'}, + 'power0': {'str': 'antenna[0].power_launched.data', + 'dim': 'power', 'quant': 'lh power', 'units': 'W', + 'pos': True}, + 'power1': {'str': 'antenna[1].power_launched.data', + 'dim': 'power', 'quant': 'lh power', 'units': 'W', + 'pos': True}, + 'power': {'str': 'antenna[chan].power_launched.data', + 'dim': 'power', 'quant': 'lh power', 'units': 'W', + 'pos': True}, + 'R': {'str': 'antenna[chan].position.r.data', + 'dim': 'distance', 'quant': 'R', 'units': 'm'}}, + + 'ic_antennas': { + 't': {'str': 'antenna[chan].module[0].power_forward.time'}, + 'power0mod_fwd': {'str': 'antenna[0].module[].power_forward.data', + 'dim': 'power', 'quant': 'ic power', 'units': 'W'}, + 'power0mod_reflect': {'str': ('antenna[0].module[]' + + '.power_reflected.data'), + 'dim': 'power', 'quant': 'ic power', + 'units': 'W'}, + 'power1mod_fwd': {'str': 'antenna[1].module[].power_forward.data', + 'dim': 'power', 'quant': 'ic power', 'units': 'W'}, + 'power1mod_reflect': {'str': ('antenna[1].module[]' + + '.power_reflected.data'), + 'dim': 'power', 'quant': 'ic power', + 'units': 'W'}, + 'power2mod_fwd': {'str': 'antenna[2].module[].power_forward.data', + 'dim': 'power', 'quant': 'ic power', 'units': 'W'}, + 'power2mod_reflect': {'str': ('antenna[2].module[]' + + '.power_reflected.data'), + 'dim': 'power', 'quant': 'ic power', + 'units': 'W'}}, + + 'magnetics': { + 't': {'str': 'time'}, + 'ip': {'str': 'method[0].ip.data'}, + 'diamagflux': {'str': 'method[0].diamagnetic_flux.data'}, + 'bpol_B': {'str': 'bpol_probe[chan].field.data', + 'dim': 'B', 'quant': 'Bpol', 'units': 'T'}, + 'bpol_name': {'str': 'bpol_probe[chan].name'}, + 'bpol_R': {'str': 'bpol_probe[chan].position.r', + 'dim': 'distance', 'quant': 'R', 'units': 'm'}, + 'bpol_Z': {'str': 'bpol_probe[chan].position.z', + 'dim': 'distance', 'quant': 'Z', 'units': 'm'}, + 'bpol_angpol': {'str': 'bpol_probe[chan].poloidal_angle', + 'dim': 'angle', 'quant': 'angle_pol', 'units': 'rad'}, + 'bpol_angtor': {'str': 'bpol_probe[chan].toroidal_angle', + 'dim': 'angle', 'quant': 'angle_tor', 'units': 'rad'}, + 'floop_flux': {'str': 'flux_loop[chan].flux.data', + 'dim': 'B flux', 'quant': 'B flux', 'units': 'Wb'}, + 'floop_name': {'str': 'flux_loop[chan].name'}, + 'floop_R': {'str': 'flux_loop[chan].position.r', + 'dim': 'distance', 'quant': 'R', 'units': 'm'}, + 'floop_Z': {'str': 'flux_loop[chan].position.z', + 'dim': 'distance', 'quant': 'Z', 'units': 'm'}}, + + 'barometry': { + 't': {'str': 'gauge[chan].pressure.time'}, + 'names': {'str': 'gauge[chan].name'}, + 'p': {'str': 'gauge[chan].pressure.data', + 'dim': 'pressure', 'quant': 'p', 'units': 'Pa?'}}, + + 'calorimetry': { + 't': {'str': 'group[chan].component[0].power.time'}, + 'names': {'str': 'group[chan].name'}, + 'power': {'str': 'group[chan].component[0].power.data', + 'dim': 'power', 'quant': 'extracted power', + 'units': 'W'}}, + + 'neutron_diagnostic': { + 't': {'str': 'time', 'units': 's'}, + 'flux_total': {'str': 'synthetic_signals.total_neutron_flux', + 'dim': 'particle flux', 'quant': 'particle flux', + 'units': 'Hz'}}, + + 'ece': { + 't': {'str': 'time', + 'quant': 't', 'units': 's'}, + 'freq': {'str': 'channel[chan].frequency.data', + 'dim': 'freq', 'quant': 'freq', 'units': 'Hz'}, + 'Te': {'str': 'channel[chan].t_e.data', + 'dim': 'temperature', 'quant': 'Te', 'units': 'eV'}, + 'R': {'str': 'channel[chan].position.r', + 'dim': 'distance', 'quant': 'R', 'units': 'm'}, + 'rhotn': {'str': 'channel[chan].position.rho_tor_norm', + 'dim': 'rho', 'quant': 'rhotn', 'units': 'adim.'}, + 'theta': {'str': 'channel[chan].position.theta', + 'dim': 'angle', 'quant': 'theta', 'units': 'rad.'}, + 'tau1keV': {'str': 'channel[chan].optical_depth.data', + 'dim': 'optical_depth', 'quant': 'tau', 'units': 'adim.'}, + 'validity_timed': {'str': 'channel[chan].t_e.validity_timed'}, + 'names': {'str': 'channel[chan].name'}, + 'Te0': {'str': 't_e_central.data', + 'dim': 'temperature', 'quant': 'Te', 'units': 'eV'}}, + + 'reflectometer_profile': { + 't': {'str': 'time'}, + 'ne': {'str': 'channel[chan].n_e.data', + 'dim': 'density', 'quant': 'ne', 'units': '/m^3'}, + 'R': {'str': 'channel[chan].position.r', + 'dim': 'distance', 'quant': 'R', 'units': 'm'}, + 'Z': {'str': 'channel[chan].position.z', + 'dim': 'distance', 'quant': 'Z', 'units': 'm'}, + 'phi': {'str': 'channel[chan].position.phi', + 'dim': 'angle', 'quant': 'phi', 'units': 'rad'}, + 'names': {'str': 'channel[chan].name'}, + 'mode': {'str': 'mode'}, + 'sweep': {'str': 'sweep_time'}}, + + 'interferometer': { + 't': {'str': 'time', + 'quant': 't', 'units': 's'}, + 'names': {'str': 'channel[chan].name'}, + 'ne_integ': {'str': 'channel[chan].n_e_line.data', + 'dim': 'ne_integ', 'quant': 'ne_integ', + 'units': '/m2', 'Brightness': True}}, + + 'polarimeter': { + 't': {'str': 'time', + 'quant': 't', 'units': 's'}, + 'lamb': {'str': 'channel[chan].wavelength', + 'dim': 'distance', 'quant': 'wavelength', + 'units': 'm'}, + 'fangle': {'str': 'channel[chan].faraday_angle.data', + 'dim': 'angle', 'quant': 'faraday angle', + 'units': 'rad', 'Brightness': True}, + 'names': {'str': 'channel[chan].name'}}, + + 'bolometer': { + 't': {'str': 'channel[chan].power.time', + 'quant': 't', 'units': 's'}, + 'power': {'str': 'channel[chan].power.data', + 'dim': 'power', 'quant': 'power radiative', + 'units': 'W', 'Brightness': False}, + 'etendue': {'str': 'channel[chan].etendue', + 'dim': 'etendue', 'quant': 'etendue', + 'units': 'm2.sr'}, + 'names': {'str': 'channel[chan].name'}, + 'tpower': {'str': 'time', 'quant': 't', 'units': 's'}, + 'prad': {'str': 'power_radiated_total', + 'dim': 'power', 'quant': 'power radiative', + 'units': 'W'}, + 'pradbulk': {'str': 'power_radiated_inside_lcfs', + 'dim': 'power', 'quant': 'power radiative', + 'units': 'W'}}, + + 'soft_x_rays': { + 't': {'str': 'time', + 'quant': 't', 'units': 's'}, + 'power': {'str': 'channel[chan].power.data', + 'dim': 'power', 'quant': 'power radiative', + 'units': 'W', 'Brightness': False}, + 'brightness': {'str': 'channel[chan].brightness.data', + 'dim': 'brightness', 'quant': 'brightness', + 'units': 'W/(m2.sr)', 'Brightness': True}, + 'names': {'str': 'channel[chan].name'}, + 'etendue': {'str': 'channel[chan].etendue', + 'dim': 'etendue', 'quant': 'etendue', + 'units': 'm2.sr'}}, + + 'spectrometer_visible': { + 't': {'str': ('channel[chan].grating_spectrometer' + + '.radiance_spectral.time'), + 'quant': 't', 'units': 's'}, + 'spectra': {'str': ('channel[chan].grating_spectrometer' + + '.radiance_spectral.data'), + 'dim': 'radiance_spectral', + 'quant': 'radiance_spectral', + 'units': 'ph/s/(m2.sr)/m', 'Brightness': True}, + 'names': {'str': 'channel[chan].name'}, + 'lamb': {'str': 'channel[chan].grating_spectrometer.wavelengths', + 'dim': 'wavelength', 'quant': 'wavelength', 'units': 'm'}}, + + 'bremsstrahlung_visible': { + 't': {'str': 'time', + 'quant': 't', 'units': 's'}, + 'radiance': {'str': 'channel[chan].radiance_spectral.data', + 'dim': 'radiance_spectral', + 'quant': 'radiance_spectral', + 'units': 'ph/s/(m2.sr)/m', + 'Brightness': True}, + 'names': {'str': 'channel[chan].name'}, + 'lamb_up': {'str': 'channel[chan].filter.wavelength_upper'}, + 'lamb_lo': {'str': 'channel[chan].filter.wavelength_lower'}} + } + +_didsdiag = { + 'lh_antennas': {'datacls': 'DataCam1D', + 'geomcls': False, + 'sig': {'data': 'power', + 't': 't'}}, + 'ic_antennas': {'datacls': 'DataCam1D', + 'geomcls': False, + 'sig': {'data': 'power', + 't': 't'}}, + 'magnetics': {'datacls': 'DataCam1D', + 'geomcls': False, + 'sig': {'data': 'bpol_B', + 't': 't'}}, + 'barometry': {'datacls': 'DataCam1D', + 'geomcls': False, + 'sig': {'data': 'p', + 't': 't'}}, + 'calorimetry': {'datacls': 'DataCam1D', + 'geomcls': False, + 'sig': {'data': 'power', + 't': 't'}}, + 'ece': {'datacls': 'DataCam1D', + 'geomcls': False, + 'sig': {'t': 't', + 'X': 'rhotn_sign', + 'data': 'Te'}}, + 'neutron_diagnostic': {'datacls': 'DataCam1D', + 'geomcls': False, + 'sig': {'t': 't', + 'data': 'flux_total'}}, + 'reflectometer_profile': {'datacls': 'DataCam1D', + 'geomcls': False, + 'sig': {'t': 't', + 'X': 'R', + 'data': 'ne'}}, + 'interferometer': {'datacls': 'DataCam1D', + 'geomcls': 'CamLOS1D', + 'sig': {'t': 't', + 'data': 'ne_integ'}, + 'synth': {'dsynth': { + 'quant': 'core_profiles.1dne', + 'ref1d': 'core_profiles.1drhotn', + 'ref2d': 'equilibrium.2drhotn'}, + 'dsig': {'core_profiles': ['t'], + 'equilibrium': ['t']}, + 'Brightness': True}}, + 'polarimeter': {'datacls': 'DataCam1D', + 'geomcls': 'CamLOS1D', + 'sig': {'t': 't', + 'data': 'fangle'}, + 'synth': {'dsynth': { + 'fargs': ['core_profiles.1dne', + 'equilibrium.2dBR', + 'equilibrium.2dBT', + 'equilibrium.2dBZ', + 'core_profiles.1drhotn', + 'equilibrium.2drhotn']}, + 'dsig': {'core_profiles': ['t'], + 'equilibrium': ['t']}, + 'Brightness': True}}, + 'bolometer': {'datacls': 'DataCam1D', + 'geomcls': 'CamLOS1D', + 'sig': {'t': 't', + 'data': 'power'}, + 'synth': {'dsynth': { + 'quant': 'core_sources.1dprad', + 'ref1d': 'core_sources.1drhotn', + 'ref2d': 'equilibrium.2drhotn'}, + 'dsig': {'core_sources': ['t'], + 'equilibrium': ['t']}, + 'Brightness': True}}, + 'soft_x_rays': {'datacls': 'DataCam1D', + 'geomcls': 'CamLOS1D', + 'sig': {'t': 't', + 'data': 'power'}}, + 'spectrometer_visible': {'datacls': 'DataCam1DSpectral', + 'geomcls': 'CamLOS1D', + 'sig': {'data': 'spectra', + 't': 't', + 'lamb': 'lamb'}}, + 'bremsstrahlung_visible': { + 'datacls': 'DataCam1D', + 'geomcls': 'CamLOS1D', + 'sig': {'t': 't', + 'data': 'radiance'}, + 'synth': { + 'dsynth': { + 'quant': ['core_profiles.1dTe', + 'core_profiles.1dne', + 'core_profiles.1dzeff'], + 'ref1d': 'core_profiles.1drhotn', + 'ref2d': 'equilibrium.2drhotn'}, + 'dsig': {'core_profiles': ['t'], + 'equilibrium': ['t']}, + 'Brightness': True}}} diff --git a/tofu/imas2tofu/_mat2ids2calc.py b/tofu/imas2tofu/_mat2ids2calc.py new file mode 100644 index 000000000..1f490b813 --- /dev/null +++ b/tofu/imas2tofu/_mat2ids2calc.py @@ -0,0 +1,205 @@ + +# Built-in +import os + +# Common +import scipy.io as scpio +import numpy as np + +# tofu-specific +from .. import _physics + + +__all__ = ['get_data_from_matids'] +_LIDSOK = ['core_profiles'] +_DRETURN = {'core_profiles': ['rhotn', 'ne', 'Te', 'zeff', 't', 'brem']} +_MSG0 = ("The input file structure is not as expected !\n" + + " => Maybe file structure changed ?\n" + + " => Maybe corrupted data ?\n") + + +# #################################################### +# Utility +# #################################################### + +def _get_indtlim(t, tlim=None, shot=None, out=bool): + """ """ + c0 = tlim is None + c1 = type(tlim) in [list, tuple, np.ndarray] + assert c0 or c1 + assert type(t) is np.ndarray + + if c0: + tlim = [-np.inf, np.inf] + else: + assert len(tlim) == 2 + ls = [int, float, np.int64, np.float64] # , str + assert all([tt is None or type(tt) in ls for tt in tlim]) + tlim = list(tlim) + for (ii, sgn) in [(0, -1.), (1, 1.)]: + if tlim[ii] is None: + tlim[ii] = sgn*np.inf + # elif type(tlim[ii]) is str and 'ign' in tlim[ii].lower(): + # tlim[ii] = get_t0(shot) + + assert tlim[0] < tlim[1] + indt = (t >= tlim[0]) & (t <= tlim[1]) + if out is int: + indt = indt.nonzero()[0] + return indt + + +# #################################################### +# Main function +# #################################################### + +def get_data_from_matids(input_pfe=None, tlim=None, + return_fields=None, lamb=None): + """ Extract tofu-compatible from an ids saved as a mat file + + Assumes that the mat file contains the ids data + Only the following ids are handled: + {} + + """.format(_LIDSOK) + + # --------------- + # Check + if not os.path.isfile(input_pfe): + msg = ("Provided file does not seem to exist:\n" + + "\t- {}".format(input_pfe)) + raise Exception(msg) + lc = [return_fields is None, + isinstance(return_fields, str), + isinstance(return_fields, list) + and all([isinstance(ss, str) for ss in return_fields])] + if not any(lc): + msg = "return_fields must be a str or a list of str " + raise Exception(msg) + if lc[1]: + return_fields = [return_fields] + + # --------------- + # Load and check / extract ids + mat = scpio.loadmat(input_pfe) + ids = [k0 for k0 in mat.keys() if '__' not in k0] + if len(ids) != 1 or ids[0] not in _LIDSOK: + msg = ("The file does not seem to contain a known ids:\n" + + "\t- file: {}\n".format(input_pfe) + + "\t- keys: {}\n".format(sorted(mat.keys())) + + "\t- known ids: {}".format(_LIDSOK)) + raise Exception(msg) + ids = ids[0] + data = mat[ids] + + if return_fields is None: + return_fields = _DRETURN[ids] + notok = [ss for ss in return_fields if ss not in _DRETURN[ids]] + if len(notok) > 0: + msg = ("Some requested fields are not available:\n" + + "\t- requested: {}\n".format(notok) + + "\t- available: {}".format(_DRETURN[ids])) + raise Exception(msg) + + # --------------- + # Get inside ids and extract data + if ids == 'core_profiles': + # --------------- + # Check expected structure + if not (data.shape == (1, 1) and data[0, 0].size == 1): + msg = ("\t{}.shape = {}\n".format(ids, data.shape) + + "\t{}.size = {}".format(ids, data[0, 0].size)) + raise Exception(_MSG0 + msg) + data = data[0, 0].tolist() + if not (isinstance(data, tuple) and len(data) == 6): + msg = ("\ttype({}[0, 0].tolist()) = {}\n".format(ids, type(data)) + + "\tlen({}[0, 0].tolist()) = {}".format(ids, len(data))) + raise Exception(_MSG0 + msg) + + ls = [pp.shape for pp in data] + c0 = [len(ss) == 2 for ss in ls] + c1 = np.sum([ss == (1, 1) for ss in ls]) == 4 + c2 = np.sum([(ss[1] == 1 and ss[0] > ss[1]) for ss in ls]) == 1 + c3 = np.sum([(ss[0] == 1 and ss[1] > ss[0]) for ss in ls]) == 1 + if c0 and c1 and c2 and c3: + indt = [ii for ii in range(len(ls)) if ls[ii][0] > ls[ii][1]] + indp = [ii for ii in range(len(ls)) if ls[ii][0] < ls[ii][1]] + else: + if np.sum([ss == (1, 1) for ss in ls]) == 6: + warnings.warn("There seems to be only one time step...") + indt = [ii for ii in range(len(ls)) if data[ii].dtype == ' imas may not be installed?") raise Exception(msg) lok = ['Data'] c0 = out is None or out in lok if not c0: - msg = "Arg out must be in %s"%str(lok) + msg = "Arg out must be in {}".format(lok) raise Exception(msg) + if plot is None: + if output_file is not None: + plot = False + else: + plot = True + if extra is None: + if input_file is not None: + extra = False + else: + extra = True + # ------------------- # Prepare ids - assert ids is None or type(ids) in [list,str] + assert ids is None or type(ids) in [list, str] if type(ids) is str: ids = [ids] if type(ids) is list: @@ -1083,12 +1095,24 @@ def calc_from_imas(shot=None, run=None, user=None, tokamak=None, version=None, if nids > 1: assert not any([ids_ in ids for ids_ in lidscustom]) + # Check if input_file + if input_file is not None: + lids_input_file = ['bremsstrahlung_visible'] + if nids != 1 or ids[0] not in lids_input_file: + msg = ("input_file is only available for a single ids in:\n" + + "\t- " + "\n\t- ".join(lids_input_file)) + raise Exception(msg) # ------------------- # Prepare shot shot = np.r_[shot].astype(int) nshot = shot.size + # Check if input_file + if input_file is not None: + if nshot != 1: + msg = "input_file not available for multiple shots!" + raise Exception(msg) # ------------------- # Prepare out @@ -1107,8 +1131,8 @@ def calc_from_imas(shot=None, run=None, user=None, tokamak=None, version=None, if nids > 1: if not all([ids_ in imas2tofu.MultiIDSLoader._lidsdiag for ids_ in ids]): - msg = "tf.load_from_imas() only handles multipe ids\n" - msg += "if all are diagnostics ids !" + msg = ("tf.load_from_imas() only handles multipe ids " + + "if all are diagnostics ids!") raise Exception(msg) # ------------------- @@ -1215,29 +1239,84 @@ def calc_from_imas(shot=None, run=None, user=None, tokamak=None, version=None, # ------------------- # load - for ss in shot: - multi = imas2tofu.MultiIDSLoader(shot=ss, run=run, user=user, - tokamak=tokamak, version=version, - ids=lids, synthdiag=True) - - # export to instances - for ii in range(0,nids): - if out[ii] == "Data": - multi.calc_signal(ids=lids[ii], - tlim=tlim, dsig=dsig, - config=config, t=t, - res=res, indch=indch, - Brightness=Brightness, - interp_t=interp_t, - indch_auto=indch_auto, - t0=t0, dextra=dextra, - plot=True, - plot_compare=plot_compare) - - - + if input_file is None: + for ss in shot: + multi = imas2tofu.MultiIDSLoader(shot=ss, run=run, user=user, + tokamak=tokamak, version=version, + ids=lids, synthdiag=True) + + # export to instances + for ii in range(0, nids): + if out[ii] == "Data": + multi.calc_signal(ids=lids[ii], + tlim=tlim, dsig=dsig, + config=config, t=t, + res=res, indch=indch, + Brightness=Brightness, + interp_t=interp_t, + indch_auto=indch_auto, + t0=t0, dextra=dextra, + plot=True, + plot_compare=plot_compare) + else: + multi = imas2tofu.MultiIDSLoader(shot=shot[0], run=run, user=user, + tokamak=tokamak, version=version, + ids=lids, synthdiag=False, get=False) + if 'bremsstrahlung_visible' in lids: + multi.add_ids('equilibrium', get=True) + plasma = multi.to_Plasma2D() + lf = ['t', 'rhotn', 'brem'] + lamb = multi.get_data('bremsstrahlung_visible', sig='lamb')['lamb'] + dout = imas2tofu.get_data_from_matids(input_file, + return_fields=lf, + lamb=lamb[0]) + plasma.add_ref(key='core_profiles.t', data=dout['t'], group='time', + origin='input_file') + nrad = dout['rhotn'].shape[1] + plasma.add_ref(key='core_profiles.radius', data=np.arange(0, nrad), + group='radius', origin='input_file') + plasma.add_quantity(key='core_profiles.1drhotn', + data=dout['rhotn'], + depend=('core_profiles.t', + 'core_profiles.radius'), + origin='input_file', + quant='rhotn', dim='rho', units='adim.') + plasma.add_quantity(key='core_profiles.1dbrem', data=dout['brem'], + depend=('core_profiles.t', + 'core_profiles.radius'), + origin='input_file') + cam = multi.to_Cam(plot=False) + sig = cam.calc_signal_from_Plasma2D(plasma, + quant='core_profiles.1dbrem', + ref1d='core_profiles.1drhotn', + ref2d='equilibrium.2drhotn', + Brightness=True, plot=plot)[0] + if output_file is not None: + try: + # Format output dictionnary to be saved + dout = {'shot': shot[0], + 't': sig.t, + 'data': sig.data, + 'units_t': 's', + 'units_data': 'ph / (s.m2.sr.m)', + 'channels': sig.dchans('names'), + 'tofu_version': __version__} + + # Save to specified path + filename + extension + if output_file[-4:] != '.mat': + assert len(output_file.split('.')) == 1 + output_file += '.mat' + scpio.savemat(output_file, dout) + msg = ("Successfully saved in:\n" + + "\t{}".format(output_file)) + print(msg) + except Exception as err: + msg = str(err) + msg += "\nCould not save computed synthetic signal to:\n" + msg += "scpio.savemat({0}, dout)".format(output_file) + warnings.warn(msg) ############################################# diff --git a/tofu/version.py b/tofu/version.py index f3fbe0706..f15d3876b 100644 --- a/tofu/version.py +++ b/tofu/version.py @@ -1,2 +1,2 @@ # Do not edit, pipeline versioning governed by git tags! -__version__ = '1.4.2b4-39-g2b1aa022' +__version__ = '1.4.2b4-49-gee6a0f47'