# -*- coding: utf-8 -*-
#! /usr/bin/python
"""
Provide all necessary modules for dealing with AUG-specific data (geometry, diag and signal names...)
"""
import os
import datetime as dtm
# ToFu specific
import tofu.defaults as tfd
import tofu.pathfile as tfpf
import tofu.geom as tfg
from .. import _path as _tfaug_path
__author__ = "Didier Vezinet"
__all__ = ["_create","load"]
############################################################################
############################################################################
# Pre-set default SavePathIn, SavePathObj and SavePathOut
############################################################################
############################################################################
############################################################################
############################################################################
# Ves creation and loading
############################################################################
def _create(Name='V1', Ves=None, shot=0, SavePathInp=None, SavePathObj=None, Root=_tfaug_path._Root, save=True,
skiprows=0, comments='#', units="m",
dtime=None, dtFormat=tfd.dtmFormat, dtimeIn=False, Test=True):
""" Create and save a Ves object from givn input file or Ves object
A Ves object can be created from an input file, a np.ndarray or another :class:`~tofu.geom.Ves` object
Parameters
----------
Name : str
The name to be given to the created Ves instance
Ves : None / str / :class:`tofu.geom.Ves` / np.ndarray
The source where the polygon is to be found, either:
- str: the name of a file containing the coordinates of a polygon to be loaded with :meth:`numpy.loadtxt()`
- A :class:`tofu.geom.Ves` object: to re-use its Poly attribute and build one with different name
- np.ndarray: an 2-dimensional array containing the 2D cartesian coordinates of a polygon
shot : int
A shot number, to be used as a reference point in time, marking from when the provided geometry is valid
SavePathInp : None / str
If provided, forces the routine to search for the input file at SavePathInp, if not provided SavePathInp is automatically set to default (i.e. tofu/plugin/Ves/Inputs/)
SavePathObj : None / str
If provided, forces the routine to save the created instance at SavePathObj, if not provided SavePathObj is automatically set to default (i.e. tofu/plugin/Ves/Objects/)
Root : str
If SavePathObj=None, a default value is created by appending '/tofu/plugins/AUG/Ves/Objects/' to Root
save : bool
Flag indicating whether the created Ves instance shall be saved automatically (in SavePathObj)
skiprows : int
Parameter fed to np.loadtxt() for reading the polygon from a txt file
comments : str
Parameter fed to np.loadtxt() for reading the polygon from a txt file
units : str
Flag indicating in which units the input polygon is provided (in ['m','cm','mm'])
dtime : None / dtm.datetime
A datetime instance used for labelling the created instance (mostly used for debugging)
dtFormat : str
The format of the labelling (mostly used for debugging)
dtimeIn : bool
Flag indicating whether to include the label in the file name (mostly used for debugging)
Returns
-------
Ves : tfg.Ves
The created tfg.Ves instance
"""
if Test:
assert type(Name) is str, "Arg Name must be a str !"
assert Ves is None or type(Ves) in [tfg.Ves,str,np.ndarray], "Arg Ves must be a :class:`~tofu.geom.Ves` instance, a file name or a np.ndarray !"
assert type(shot) is int, "Arg shot must be a int !"
assert all([ss is None or type(ss) is str for ss in [SavePathInp,SavePathObj]]), "Args [SavePathInp,SavePathObj] must be str !"
assert type(save) is bool, "Arg save must be a bool !"
assert type(units) is str and units in ['m','cm','mm'], "Arg units must be in ['m','cm','mm'] !"
assert dtime is None or type(dtime) is dtm.datetime, "Arg dtime must be a dtm.datetime !"
assert type(dtFormat) is str, "Arg dtFormat must be a str !"
assert type(dtimeIn) is bool, "Arg dtimeIn must be a bool !"
if SavePathInp is None:
SavePathInp = Root + '/tofu/plugins/AUG/Ves/Inputs/'
if SavePathObj is None:
SavePathObj = Root + '/tofu/plugins/AUG/Ves/Objects/'
# Get default paths and dtime
dtime, dtFormat = tfpf.get_Default_dtimeFmt(dtime=dtime, dtFormat=dtFormat)
# Get default Ves
Ves = SavePathInp+'AUG_Ves.txt' if Ves is None else Ves
# Create ID object for Ves
Ves, addInfo = tfpf.get_PolyFromPolyFileObj(Ves, SavePathInp, units=units, comments=comments, skiprows=skiprows)
Ves = tfg.Ves(Name, Ves, Type='Tor', Sino_RefPt=None, Sino_NP=tfd.TorNP, Clock=False, arrayorder='C', Exp=_Exp, shot=shot, dtime=dtime, dtimeIn=dtimeIn, SavePath=SavePathObj)
# Add info about input to Id
for dd in addInfo.keys():
Ves.Id._USRdict[dd] = addInfo[dd]
if save:
Ves.save()
return Ves
[docs]def load(Name='V1', SavePathObj=None, Root=_tfaug_path._Root, Test=True):
""" Load and return the selected Ves object (selected by name or file name)
Several Ves object might exist for the same experiment depending changes to the experiment in time for example
This function loads the one specified by its name.
Parameters
----------
Name : str / list
Name of the file to be loaded, or a subset of this name or a list of subsets, the file with a name matching all the subsets will be loaded. An error is issued in case of ambiguity
SavePathObj : str
Absolute path where the objects can be found, if None sets to default
Root : str
If SavePathObj=None, a default value is created by appending '/tofu/plugins/AUG/Ves/Objects/' to Root
Test : bool
Flag indicating whether the inputs should be tested for conformity
Returns
-------
Ves : :class:`tofu.geom.Ves`
The loaded Ves object
"""
if Test:
assert type(Name) is str or (type(Name) is list and all([type(ss) is str for ss in Name])), "Arg Name must be a str or a list of str !"
assert SavePathObj is None or type(SavePathObj) is str, "Arg SavePathObj must be a str !"
if SavePathObj is None:
SavePathObj = Root + '/tofu/plugins/AUG/Ves/Objects/'
Name = [Name] if type(Name) is str else Name
lobj = os.listdir(SavePathObj)
lobj = [ff for ff in lobj if all([ss in ff for ss in Name])]
assert len(lobj)==1, "Several possible matching files for "+str(Name)+" in "+SavePathObj
Ves = tfpf.Open(SavePathObj+lobj[0])
return Ves