tofu.plugins.ITER.Ves._core — tofu v1.1

# Source code for tofu.plugins.ITER.Ves._core

    # -*- coding: utf-8 -*-
    """
    Created on Wed Mar  5 10:42:24 2014

    @author: didiervezinet
    """

    import os
    import numpy as np
    import datetime as dtm
    import warnings

    # ToFu-specific
    import tofu.defaults as tfd
    import tofu.pathfile as tfpf
    import tofu.geom as tfg
    from .. import _path as _tfiter_path

    __author__ = "Didier Vezinet"
    __all__ = ["create","load"]

    _Exp = 'ITER'

    DefName, DefFile, Defskiprows = 'ITER_D_2N9J75 v1.7-1', ['PoloidalCurvesCoordinates','ITER','2N9J75','csv'], 4

    ############################################################################
    ############################################################################
    ############################################################################
    # --------- Miscellaneous -------
    ############################################################################

    def _get_defaultsSavePathsdtime(SavePathObj=None, SavePathInp=None, Type='Object'):  # Deprecated ?
        """ Get default path for loading input and saving output """
        assert SavePathObj is None or type(SavePathObj) is str, "Arg SavePathObj must be a str !"
        assert SavePathInp is None or type(SavePathInp) is str, "Arg SavePathInp must be a str !"
        RP = TFPF.Find_Rootpath(Path=cwd,substr='/ToFu_ITER')
        if SavePathInp is None:
            SavePathInp = RP+'/Inputs_'+Exp+'/'
        if SavePathObj is None:
            SavePathObj = RP+'/Objects_'+Exp+'/' if Type=='Object' else RP+'/Outputs_'+Exp+'/'
        return SavePathObj, SavePathInp

    ############################################################################
    ############################################################################
    ############################################################################
    #                   Ves creation
    ############################################################################

    def create(Name=DefName, Poly=DefFile, shot=0,
               SavePathInp=None, SavePathObj=None, Root=_tfiter_path._Root, save=True,
               skiprows=Defskiprows, comments='#', units='mm',
               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
        Poly :           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/ITER/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)
        Test :          bool
            Flag indicating whether the inpurts should be checked for conformity

        Returns
        -------
        Ves :           :class:`tofu.geom.Ves`
            The created tfg.Ves instance

        """

        if Test:
            assert type(Name) is str, "Arg Name must be a str !"
            assert Poly is None or type(Poly) in [tfg.Ves,str,list,np.ndarray], "Arg Poly 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 !"

        # Get default path for loading inputs and saving object
        #SavePathObj, SavePathInp = _get_defaultsSavePathsdtime(SavePathObj=SavePathObj, SavePathInp=SavePathInp, Type='Object')
        if SavePathInp is None:
            SavePathInp = Root + '/tofu/plugins/'+_Exp+'/Ves/Inputs/'
        if SavePathObj is None:
            SavePathObj = Root + '/tofu/plugins/'+_Exp+'/Ves/Objects/'

        # Get polygon and info
        Poly, addInfo = tfpf.get_PolyFromPolyFileObj(Poly, SavePathInp, units=units, comments=comments, skiprows=skiprows)

        # Create Ves object
        Ves = tfg.Ves(Name, Poly, 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

    ############################################################################
    ############################################################################
    ############################################################################
    #                   Ves loading
    ############################################################################

    def load(Name=None, SavePathObj=None, Root=_tfiter_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 (no or several matches)
        SavePathObj :   None / 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 Name is None or 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/'+_Exp+'/Ves/Objects/'

        if Name is None:
            Name = ['2N9J75','v1.7-1']

        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