diff --git a/tofu/data/_class8_plot_coverage_broadband.py b/tofu/data/_class8_plot_coverage_broadband.py index 763ae6bbc..ef0bda427 100644 --- a/tofu/data/_class8_plot_coverage_broadband.py +++ b/tofu/data/_class8_plot_coverage_broadband.py @@ -338,7 +338,7 @@ def _plot_cross( # prepare figure # ---------------- - if dax.get('ndet_cross') is None: + if dax is None or len(dax) == 0: if fs is None: fs = (16, 7) @@ -430,7 +430,7 @@ def _plot_cross( # check / format dax dax = ds._generic_check._check_dax(dax) - fig = dax['ndet_cross']['handle'].figure + fig = dax[list(dax.keys())[0]]['handle'].figure # --------------- # plot spans diff --git a/tofu/imas2tofu/_comp.py b/tofu/imas2tofu/_comp.py index 4258ca0c9..0622edc31 100644 --- a/tofu/imas2tofu/_comp.py +++ b/tofu/imas2tofu/_comp.py @@ -44,7 +44,10 @@ _DSHORT = _defimas2tofu._dshort _DCOMP = _defimas2tofu._dcomp -_DDUNITS = imas.dd_units.DataDictionaryUnits() +try: + _DDUNITS = imas.dd_units.DataDictionaryUnits() +except Exception: + _DDUNITS = None _ISCLOSE = True _POS = False @@ -294,6 +297,19 @@ def fsig(obj, indt=None, indch=None, stack=None, dcond=dcond): raise Exception(msg) sig[jj] = sig[jj][ind[0]] + # convert from IMAS classes to numpy / float etc + for ii in range(len(sig)): + if isinstance(sig[ii], imas.ids_primitive.IDSNumericArray): + sig[ii] = np.array(sig[ii]) + elif isinstance(sig[ii], imas.ids_primitive.IDSFloat0D): + sig[ii] = float(sig[ii]) + elif isinstance(sig[ii], imas.ids_primitive.IDSInt0D): + sig[ii] = int(sig[ii]) + elif isinstance(sig[ii], imas.ids_primitive.IDSString0D): + sig[ii] = str(sig[ii]) + elif isinstance(sig[ii], imas.ids_primitive.IDSString1D): + sig[ii] = str(sig[ii]) + # Conditions for stacking / sqeezing sig lc = [ ( @@ -660,12 +676,16 @@ def _check_data(data, pos=None, nan=None, isclose=None, empty=None): isempty = [None for ii in range(len(data))] if empty is True: for ii in range(len(data)): - isempty[ii] = (len(data[ii]) == 0 - or (isinstance(data[ii], np.ndarray) - and (data[ii].size == 0 - or 0 in data[ii].shape))) + isempty[ii] = ( + len(data[ii]) == 0 + or ( + isinstance(data[ii], np.ndarray) + and (data[ii].size == 0 or 0 in data[ii].shape) + ) + ) if isinstance(data[ii], np.ndarray) and data[ii].dtype.kind != 'U': isempty[ii] &= bool(np.all(np.isnan(data[ii]))) + return data, isempty @@ -767,17 +787,22 @@ def _get_data_units(ids=None, sig=None, occ=None, # Check data isempty = None if errdata is None and data is True: - out, isempty = _check_data(out, - pos=pos, nan=nan, - isclose=isclose, empty=empty) + out, isempty = _check_data( + out, + pos=pos, nan=nan, + isclose=isclose, empty=empty, + ) if np.all(isempty): msg = ("empty data in {}.{}".format(ids, sig)) errdata = Exception(msg) elif nocc == 1 and flatocc is True: out = out[0] isempty = isempty[0] - return {'data': out, 'units': unit, - 'isempty': isempty, 'errdata': errdata, 'errunits': errunits} + + return { + 'data': out, 'units': unit, + 'isempty': isempty, 'errdata': errdata, 'errunits': errunits, + } def get_data_units(dsig=None, occ=None, diff --git a/tofu/imas2tofu/_comp_toobjects.py b/tofu/imas2tofu/_comp_toobjects.py index 5cf0be323..838050f53 100644 --- a/tofu/imas2tofu/_comp_toobjects.py +++ b/tofu/imas2tofu/_comp_toobjects.py @@ -633,7 +633,7 @@ def get_plasma( R = out_['2dmeshR']['data'] Z = out_['2dmeshZ']['data'] if R.ndim == 2: - if np.allclose(R[0, :], R[0,0]): + if np.allclose(R[0, :], R[0, 0]): R = R[:, 0] Z = Z[0, :] else: @@ -658,13 +658,32 @@ def get_plasma( # profiles2d on mesh lprof2d = set(out_.keys()).difference(lsigmesh) + + # Check for non-arrays + derr = { + ss: type(out_[ss]['data']) for ss in lprof2d + if not isinstance(out_[ss]['data'], np.ndarray) + } + + if len(derr) > 0: + lstr = [f"\t- {kk}: {vv}" for kk, vv in derr.items()] + msg = ( + "The following keys in profiles2d are not np.ndarrays:\n" + + "\n".join(lstr) + ) + raise Exception(msg) + + # loop for ss in lprof2d: # identify proper 2d mesh lm = [ km for km, vm in dmesh.items() if ( - (km == 'tri' and vm['n1'] in out_[ss]['data'].shape) + ( + km == 'tri' + and vm['n1'] in out_[ss]['data'].shape + ) or ( km == 'rect' and vm['n1'] in out_[ss]['data'].shape diff --git a/tofu/imas2tofu/_core.py b/tofu/imas2tofu/_core.py index ef339fe61..8616fe7f4 100644 --- a/tofu/imas2tofu/_core.py +++ b/tofu/imas2tofu/_core.py @@ -57,9 +57,13 @@ # imas try: import imas - from imas import imasdef + from imas.ids_defs import HDF5_BACKEND + try: + from imas import imasdef + except Exception: + imasdef = None except Exception as err: - raise Exception('imas not available') + raise Exception(f'imas not available: {str(err)}') __all__ = [ @@ -957,15 +961,11 @@ def _checkformat_idd( user=user, database=database, version=version, backend=backend, ) - for kk,vv in defidd.items(): + for kk, vv in defidd.items(): if params[kk] is None: params[kk] = vv - # convert backend str => pointer - params['backend'] = getattr( - imasdef, - f"{params['backend']}_BACKEND".upper(), - ) + params['backend'] = HDF5_BACKEND # create entry idd = imas.DBEntry( @@ -1189,13 +1189,13 @@ def _checkformat_ids( ids = [ids] # check ids is allowed - for ids_ in ids: - if not ids_ in self._lidsnames: - msg = ( - "ids {ids_} matched no known imas ids !" - f" => Available ids are:\n{repr(self._lidsnames)}" - ) - raise Exception(msg) + # for ids_ in ids: + # if not ids_ in self._lidsnames: + # msg = ( + # "ids {ids_} matched no known imas ids !" + # f" => Available ids are:\n{repr(self._lidsnames)}" + # ) + # raise Exception(msg) # initialise dict for k in ids: @@ -2012,14 +2012,14 @@ class with which the output shall be returned # ---------------------- # lids determines order in which ids are read # may be important in case 2d mesh only exists in one ids! - + lids = sorted(dsig.keys()) if 'equilibrium' in lids: lids = ['equilibrium'] + [ids for ids in lids if ids != 'equilibrium'] # ------------------------- # data source consistency - + _, _, shot, Exp = _comp_toobjects.get_lidsidd_shotExp( lids, upper=True, errshot=False, errExp=False, dids=self._dids, didd=self._didd, @@ -3797,4 +3797,4 @@ def _save_to_imas_DataCam1D( # ------------------ _put_ids(idd, ids, shotfile, occ=occ, cls_name='%s_%s'%(obj.Id.Cls,obj.Id.Name), - err=err0, dryrun=dryrun, verb=verb) \ No newline at end of file + err=err0, dryrun=dryrun, verb=verb)