Beispiel #1
0
    def correctradec(self, event, image):
        """ correct X and Y and return to ra dec.  """

        self.newra = self.ra
        self.newdec = self.dec

        validtimeindex = np.where((self.TIME > float(event.tstart))
                                  & (self.TIME < float(event.tstop)))

        if self.debug:
            print validtimeindex
            print self.ra[validtimeindex]

        x, y = image.imageutil.topixel(self.ra[validtimeindex],
                                       self.dec[validtimeindex])

        localtime = self.TIME[validtimeindex] - float(event.tstart)
        """ correct differences """
        dx = mlab.stineman_interp(localtime, event.rebinTIME, event.rebindX)
        dy = mlab.stineman_interp(localtime, event.rebinTIME, event.rebindY)
        newx = x + dx
        newy = y + dy
        print "x"
        print newx, x, dx
        print "y"
        print newy, y, dy
        """ after correction """
        self.newra[validtimeindex], self.newdec[
            validtimeindex] = image.imageutil.toworld(newx, newy)
        self.newdec90 = 90.0 - self.newdec
def interpolate( ts, bins ):
    yp = None
    xi = numpy.linspace( 0, bins[-1], 200 )
    yi = stineman_interp( xi, bins[:-1], ts, yp )
    # interpolate upper and lower error bars to get envelope. doesn't work with log scale.
    # COMMENTED OUT BELOW
    # upper_yi = stineman_interp( xi, bins[:-1], upper, yp )
    # lower_yi = stineman_interp( xi, bins[:-1], lower, yp )
    return xi, yi
Beispiel #3
0
def plot_interp(x,y):
    x = [float(x_step) for x_step in x]
    yp = None
    xi = linspace(x[0],x[-1],100);
    yi = stineman_interp(xi,x,y,yp);
    
    fig = figure()
    ax = fig.add_subplot(111)
    ax.plot(x,y,'r-')
    show()
        p_rf = 8*rf_drive_node.data()**2
        p_rf_time = rf_drive_node.dim_of().data()
        flow_1 = h1tree.getNode('.operations.magnetsupply.lcu.log:gas1_set').data()
        flow_2 = h1tree.getNode('.operations.magnetsupply.lcu.log:gas2_set').data()
        flow_3 = h1tree.getNode('.operations.magnetsupply.lcu.log:gas3_set').data()
        good.append(sh)

    except exception as reason:
        bad.append(sh)
        print('failed to open shot {sh}: {r} {a}'.format(sh=sh, r=reason, a=reason.args))
        continue

    w = np.where(sh == shot)[0]
    times = t_mid[w]
    for ne in 'ne_1,ne_4,ne_7'.split(',')[0:num_ne]:
        valarr = stineman_interp(times, ne_time, eval(ne))
        dd[ne][w] = valarr

    if sweeping:
        k_h = stineman_interp(times, k_h_time, k_h)
    dd['k_h'][w] = k_h

    dd['p_rf'][w] = stineman_interp(times, p_rf_time, p_rf)
    for k in 'b_0,phd_rf,flow_1,flow_2,flow_3'.split(','):
        dd[k][w] = eval(k)
    

print('{g} shots successful, {b} shots failed:'.format( g=len(good), b=len(bad)))
print(bad)
          
Beispiel #5
0
from matplotlib.pyplot import figure, show
from numpy import pi, sin, linspace
from matplotlib.mlab import stineman_interp
x = linspace(0,2*pi,20);
y = sin(x); yp = None
xi = linspace(x[0],x[-1],100);
yi = stineman_interp(xi,x,y,yp);
fig = figure()
ax = fig.add_subplot(111)
ax.plot(x,y,'ro',xi,yi,'-b.')
show()
Beispiel #6
0
from matplotlib.pyplot import figure, show
from numpy import pi, sin, linspace
from matplotlib.mlab import stineman_interp

x = linspace(0, 2 * pi, 20)
y = sin(x)
yp = None
xi = linspace(x[0], x[-1], 100)
yi = stineman_interp(xi, x, y, yp)

fig = figure()
ax = fig.add_subplot(111)
ax.plot(x, y, 'ro', xi, yi, '-b.')
show()
Beispiel #7
0
def get_basic_params(diags=None, shot=54196, times=None, delay=None, debug=0):
    """ return a list of np.arrays of normally numeric values for the 
    times given, for the given shot.
    """

    global lhd_summary

    if diags is None: diags = "<n_e19>,b_0,i_p,w_p,dw_pdt,dw_pdt2".split(',')
    
    if delay is None: delay = get_delay(shot)

    if times is None: 
        times = np.linspace(0,4,4000)

    times = np.array(times)
    vals = {}
    # create an extra time array to allow a cross check
    vals.update({'check_tm':times})
    vals.update({'check_shot':np.zeros(len(times),dtype=np.int)+shot})
    for diag in diags:
        if diag not in file_info:
            warn('diagnostic {0} not found in shot {1}'.format(diag, shot),stacklevel=2)
            vals.update({diag: np.nan + times})
        else:
            info = file_info[diag]
            varname = info['name']
            if ':' in varname: (oper,varname) = varname.split(':')
            else: oper = None

            if info['format'].find('.csv') > 0:
                try:
                    test=lhd_summary.keys()
                except:    
                    print('reloading {0}'.format(info['format']))
                    lhd_summary = read_csv_data(acq_LHD+info['format'], header=3)

                val = lhd_summary[varname][shot]    
                valarr = np.double(val)+(times*0)
            else:    
                try:
                    dg = igetfile(local_dir + info['format'], shot=shot)
                except IOError:
                    try:
                        dg = igetfile(local_dir + info['format']+'.bz2', shot=shot)
                    except IOError:
                        try:
                            dg = igetfile(local_dir + info['format']+'.gz', shot=shot)
                        except exception:
                            #debug_(1)
                            dg=None
                            #break  # give up and try next diagnostic
                if dg is None:  # messy - break doesn't do what I want?
                    valarr=None
                else:
                    nd=dg.vardict['DimNo']
                    if nd != 1:
                        raise ValueError(
                            'Expecting a 1 D array in {0}, got {1}!'
                            .format(dg.filename, nd))

                    # pre re. w = np.where(np.array(dg.vardict['ValName'])==varname)[0]
                    matches = [re.match(varname,nam) 
                               != None for nam in dg.vardict['ValName']]
                    w = np.where(np.array(matches) != False)[0]
                    if len(w) != 1:
                        raise LookupError(
                            'Need just one instance of variable {0} in {1}'.
                            format(varname, dg.filename))

                    # get the column of the array corresponding to the name
                    valarr = dg.data[:,nd+w[0]]
                    tim =  dg.data[:,0] - delay

                    if oper == 'ddt':  # derivative operator
                        valarr = np.diff(valarr)/(np.average(np.diff(tim)))
                        tim = (tim[0:-1] + tim[1:])/2.0

                    if oper == 'ddt2':  # abd(ddw)*derivative operator
                        dw = np.diff(valarr)/(np.average(np.diff(tim)))
                        ddw = np.diff(dw)/(np.average(np.diff(tim)))
                        tim = tim[2:]
                        valarr = 4e-6 * dw[1:] * np.abs(ddw)

                    valarr = (stineman_interp(times, tim, valarr))
                    w = np.where(times > max(tim))
                    valarr[w] = np.nan

            if valarr != None: vals.update({diag: valarr})
    debug_(max(pyfusion.DEBUG, debug), level=5, key='interp')
    return(vals)                
def get_basic_diagnostics(diags=None,
                          shot=54196,
                          times=None,
                          delay=None,
                          exception=False,
                          debug=0):
    """ return a list of np.arrays of normally numeric values for the 
    times given, for the given shot.
    Will access server if env('IGETFILE') points to an exe, else accesses cache
    """

    global lhd_summary
    # if no exception given and we are not debugging
    # note - exception=None is a valid entry, meaning tolerate no exceptions
    # so the "default" we use is False
    if exception == False and debug == 0: exception = Exception

    if diags is None: diags = "<n_e19>,b_0,i_p,w_p,dw_pdt,dw_pdt2".split(',')
    if len(np.shape(diags)) == 0: diags = [diags]
    if delay is None: delay = get_delay(shot)

    if times is None:
        times = np.linspace(0, 4, 4000)

    times = np.array(times)
    vals = {}
    # create an extra time array to allow a cross check
    vals.update({'check_tm': times})
    vals.update({'check_shot': np.zeros(len(times), dtype=np.int) + shot})
    for diag in diags:
        if diag not in file_info:
            warn('diagnostic {0} not found in shot {1}'.format(diag, shot),
                 stacklevel=2)
            vals.update({diag: np.nan + times})
        else:
            info = file_info[diag]
            varname = info['name']
            subfolder = info['format'].split('@')[0]
            filepath = os.path.sep.join(
                [localigetfilepath, subfolder, info['format']])
            if ':' in varname: (oper, varname) = varname.split(':')
            else: oper = None

            if info['format'].find('.csv') > 0:
                try:
                    test = lhd_summary.keys()
                except:
                    csvfilename = acq_LHD + '/' + info['format']
                    if pyfusion.DBG() > 1:
                        print('looking for lhd summary in' + csvfilename)
                    if not os.path.exists(csvfilename):
                        csvfilename += ".bz2"
                    print('reloading {0}'.format(csvfilename))
                    lhd_summary = read_csv_data(csvfilename, header=3)
                    # should make this more formal - last shots
                    # from an 'extra' file, and finally, from shot info
                if shot > 117000:  # fudge to get latest data
                    lhd_summary = np.load(acq_LHD +
                                          '/LHD_summary.npz')['LHD'].tolist()
                    print('loading newer shots from a separate file - fix-me')
                    #  val = lhd_summary[varname][shot-70000]    # not needed
                #  else:
                val = lhd_summary[varname][shot]
                valarr = np.double(val) + (times * 0)
            else:
                debug_(max(pyfusion.DBG(), debug), level=4, key='find_data')
                try:

                    dg = igetfile(filepath, shot=shot, debug=debug - 1)
                except IOError:
                    try:
                        dg = igetfile(filepath + '.bz2',
                                      shot=shot,
                                      debug=debug - 1)
                    except IOError:
                        try:
                            dg = igetfile(filepath + '.gz',
                                          shot=shot,
                                          debug=debug - 1)
                        except exception as details:
                            if debug > 0:
                                print('diag at {fp} not found'.format(
                                    fp=filepath))
                            print(details, details.args)
                            dg = None
                            #break  # give up and try next diagnostic
                if dg is None:  # messy - break doesn't do what I want?
                    valarr = None
                else:
                    nd = dg.vardict['DimNo']
                    if nd != 1:
                        raise ValueError(
                            'Expecting a 1 D array in {0}, got {1}!'.format(
                                dg.filename, nd))

                    # pre re. w = np.where(np.array(dg.vardict['ValName'])==varname)[0]
                    matches = [
                        re.match(varname, nam) != None
                        for nam in dg.vardict['ValName']
                    ]
                    w = np.where(np.array(matches) != False)[0]
                    # get the column(s) of the array corresponding to the name
                    if (oper in 'sum,average,rms,max,min'.split(',')):
                        if oper == 'sum': op = np.sum
                        elif oper == 'average': op = np.average
                        elif oper == 'min': op = np.min
                        elif oper == 'std': op = np.std
                        else:
                            raise ValueError(
                                'operator {o} in {n} not known to get_basic_diagnostics'
                                .format(o=oper, n=info['name']))
                        valarr = op(dg.data[:, nd + w], 1)
                    else:
                        if len(w) != 1:
                            raise LookupError(
                                'Need just one instance of variable {0} in {1}'
                                .format(varname, dg.filename))
                        if len(np.shape(dg.data)) != 2:
                            raise LookupError(
                                'insufficient data for {0} in {1}'.format(
                                    varname, dg.filename))

                        valarr = dg.data[:, nd + w[0]]

                    tim = dg.data[:, 0] - delay

                    if oper == 'ddt':  # derivative operator
                        valarr = np.diff(valarr) / (np.average(np.diff(tim)))
                        tim = (tim[0:-1] + tim[1:]) / 2.0

                    if oper == 'ddt2':  # abd(ddw)*derivative operator
                        dw = np.diff(valarr) / (np.average(np.diff(tim)))
                        ddw = np.diff(dw) / (np.average(np.diff(tim)))
                        tim = tim[2:]
                        valarr = 4e-6 * dw[1:] * np.abs(ddw)

                    if (len(tim) < 10) or (np.std(tim) < 0.1):
                        raise ValueError('Insufficient points or degenerate'
                                         'timebase data in {0}, {1}'.format(
                                             varname, dg.filename))

                    valarr = (stineman_interp(times, tim, valarr))
                    w = np.where(times > max(tim))
                    valarr[w] = np.nan

            if valarr != None: vals.update({diag: valarr})
    debug_(max(pyfusion.DBG(), debug), level=5, key='interp')
    return (vals)
def get_basic_diagnostics(diags=None, shot=54196, times=None, delay=None, exception=False, debug=0):
    """ return a list of np.arrays of normally numeric values for the 
    times given, for the given shot.
    """

    global lhd_summary
    # if no exception given and we are not debugging
    # note - exception=None is a valid entry, meaning tolerate no exceptions
    # so the "default" we use is False
    if exception==False and debug==0: exception=Exception

    if diags == None: diags = "<n_e19>,b_0,i_p,w_p,dw_pdt,dw_pdt2".split(',')
    if len(np.shape(diags)) == 0: diags = [diags]
    if delay == None: delay = get_delay(shot)

    if times == None: 
        times = np.linspace(0,4,4000)

    times = np.array(times)
    vals = {}
    # create an extra time array to allow a cross check
    vals.update({'check_tm':times})
    vals.update({'check_shot':np.zeros(len(times),dtype=np.int)+shot})
    for diag in diags:
        if not(file_info.has_key(diag)):
            warn('diagnostic {0} not found in shot {1}'.format(diag, shot),stacklevel=2)
            vals.update({diag: np.nan + times})
        else:
            info = file_info[diag]
            varname = info['name']
            subfolder = info['format'].split('@')[0]
            filepath = os.path.sep.join([localigetfilepath,subfolder,info['format']])
            if ':' in varname: (oper,varname) = varname.split(':')
            else: oper = None

            if info['format'].find('.csv') > 0:
                try:
                    test=lhd_summary.keys()
                except:    
                    print('reloading {0}'.format(info['format']))
                    lhd_summary = read_csv_data(acq_LHD+'/'+info['format'], header=3)

                val = lhd_summary[varname][shot]    
                valarr = np.double(val)+(times*0)
            else:    
                debug_(max(pyfusion.DEBUG, debug), level=4, key='find_data')
                try:

                    dg = igetfile(filepath, shot=shot, debug=debug-1)
                except IOError:
                    try:
                        dg = igetfile(filepath+'.bz2', shot=shot, debug=debug-1)
                    except IOError:
                        try:
                            dg = igetfile(filepath + '.gz', shot=shot, debug=debug-1)
                        except exception:
                            if debug>0: print('diag at {fp} not found'
                                              .format(fp=filepath))
                            dg=None
                            #break  # give up and try next diagnostic
                if dg==None:  # messy - break doesn't do what I want?
                    valarr=None
                else:
                    nd=dg.vardict['DimNo']
                    if nd != 1:
                        raise ValueError(
                            'Expecting a 1 D array in {0}, got {1}!'
                            .format(dg.filename, nd))

                    # pre re. w = np.where(np.array(dg.vardict['ValName'])==varname)[0]
                    matches = [re.match(varname,nam) 
                               != None for nam in dg.vardict['ValName']]
                    w = np.where(np.array(matches) != False)[0]
                    # get the column(s) of the array corresponding to the name
                    if (oper in 'sum,average,rms,max,min'.split(',')):
                        if oper=='sum': op = np.sum
                        elif oper=='average': op = np.average
                        elif oper=='min': op = np.min
                        elif oper=='std': op = np.std
                        else: raise ValueError('operator {o} in {n} not known to get_basic_diagnostics'
                                               .format(o=oper, n=info['name']))
                        valarr = op(dg.data[:,nd+w],1)
                    else:
                        if len(w) != 1:
                            raise LookupError(
                                'Need just one instance of variable {0} in {1}'
                                .format(varname, dg.filename))
                        if len(np.shape(dg.data))!=2:
                           raise LookupError(
                                'insufficient data for {0} in {1}'
                                .format(varname, dg.filename))
                             
                        valarr = dg.data[:,nd+w[0]]

                    tim =  dg.data[:,0] - delay

                    if oper == 'ddt':  # derivative operator
                        valarr = np.diff(valarr)/(np.average(np.diff(tim)))
                        tim = (tim[0:-1] + tim[1:])/2.0

                    if oper == 'ddt2':  # abd(ddw)*derivative operator
                        dw = np.diff(valarr)/(np.average(np.diff(tim)))
                        ddw = np.diff(dw)/(np.average(np.diff(tim)))
                        tim = tim[2:]
                        valarr = 4e-6 * dw[1:] * np.abs(ddw)

                    if (len(tim) < 10) or (np.std(tim)<0.1):
                        raise ValueError('Insufficient points or degenerate'
                                         'timebase data in {0}, {1}'
                                         .format(varname, dg.filename))

                    valarr = (stineman_interp(times, tim, valarr))
                    w = np.where(times > max(tim))
                    valarr[w] = np.nan

            if valarr != None: vals.update({diag: valarr})
    debug_(max(pyfusion.DEBUG, debug), level=5, key='interp')
    return(vals)                
def get_basic_diagnostics(diags=None, file_info=file_info, shot=54196, times=None, delay=None, exception=False, debug=0):
    """ return a list of np.arrays of normally numeric values for the 
    times given, for the given shot.
    Will access server if env('IGETFILE') points to an exe, else accesses cache
    This is the first version to specifically allow for access through pyfusion.cfg
    There are two types of access:
       I/  single diag on its own timebase
       II/ the original multi diag on a given timebase (i.e. that from flucstrcs)
    Stage 1 puts the file_info into .cfg file just for I/ single diag access.
    Ideally the file_info for II/ sho;d be in .cfg also.
    For stage I/, we call it with a  file_info dict constructed on the spot
    as a dictionary with one just entry (for diags[0]).
    """

    global lhd_summary
    # if no exception given and we are not debugging
    # note - exception=None is a valid entry, meaning tolerate no exceptions
    # so the "default" we use is False
    if exception==False and debug==0: exception=Exception

    if diags is None: diags = "<n_e19>,b_0,i_p,w_p,dw_pdt,dw_pdt2".split(',')
    if len(np.shape(diags)) == 0: diags = [diags]
    if delay is None: delay = get_delay(shot)

    if times is None: 
        if len(diags)>1:
            times = np.linspace(0,4,4000)  # this is a crude guess.
        # else leave it None
    else:    
        # make sure it is an array
        times = np.array(times)

    

    vals = {}

    for diag in diags:
        if not(diag in file_info):
            warn('diagnostic {0} not found in shot {1}'.format(diag, shot),stacklevel=2)
            vals.update({diag: np.nan + times})
        else:
            info = file_info[diag]
            varname = info['name']  # refers to name for igetfile - can contain ':' 
            subfolder = info['format'].split('@')[0]
            filepath = os.path.sep.join([localigetfilepath,subfolder,info['format']])
            if ':' in varname: (oper,varname) = varname.split(':')
            else: oper = None

            if info['format'].find('.csv') > 0:
                try:
                    test=list(lhd_summary.keys())
                except:    
                    csvfilename = acq_LHD+'/'+info['format']
                    if pyfusion.DBG() > 1: print('looking for lhd summary in' + csvfilename)
                    if not os.path.exists(csvfilename):
                        csvfilename += ".bz2"
                    print('reloading {0}'.format(csvfilename))
                    lhd_summary = read_csv_data(csvfilename, header=3)
                    # should make this more formal - last shots
                    # from an 'extra' file, and finally, from shot info
                if shot>117000: # fudge to get latest data
                    lhd_summary = np.load(acq_LHD+'/LHD_summary.npz')['LHD'].tolist()
                    print('loading newer shots from a separate file - fix-me')
                    #  val = lhd_summary[varname][shot-70000]    # not needed
                #  else:
                val = lhd_summary[varname][shot]    
                valarr = np.double(val)+(times*0)
            else:    
                try: # now igetfile checks for .gz etc
                    dg = igetfile(filepath, shot=shot, debug=debug-1)
                except exception as details:
                    if debug>0: print('diag at {fp} not found'
                                      .format(fp=filepath))
                    print(details,details.args)
                    dg=None
                    #break  # give up and try next diagnostic
                if dg is None:  # messy - break doesn't do what I want?
                    valarr=None
                else:
                    nd=dg.vardict['DimNo']
                    if nd != 1:
                        raise ValueError(
                            'Expecting a 1 D array in {0}, got {1}!'
                            .format(dg.filename, nd))

                    # pre re. w = np.where(np.array(dg.vardict['ValName'])==varname)[0]
                    matches = [re.match(varname,nam) 
                               != None for nam in dg.vardict['ValName']]
                    w = np.where(np.array(matches) != False)[0]
                    # get the column(s) of the array corresponding to the name
                    if (oper in 'sum,average,rms,max,min'.split(',')):
                        if oper=='sum': op = np.sum
                        elif oper=='average': op = np.average
                        elif oper=='min': op = np.min
                        elif oper=='std': op = np.std
                        else: raise ValueError('operator {o} in {n} not known to get_basic_diagnostics'
                                               .format(o=oper, n=info['name']))
                        valarr = op(dg.data[:,nd+w],1)
                    else:
                        if len(w) != 1:
                            raise LookupError(
                                'Need just one instance of variable {0} in {1}'
                                .format(varname, dg.filename))
                        if len(np.shape(dg.data))!=2:
                           raise LookupError(
                                'insufficient data for {0} in {1}'
                                .format(varname, dg.filename))
                             
                        valarr = dg.data[:,nd+w[0]]

                    tim =  dg.data[:,0] - delay

                    if oper == 'ddt':  # derivative operator
                        valarr = np.diff(valarr)/(np.average(np.diff(tim)))
                        tim = (tim[0:-1] + tim[1:])/2.0

                    if oper == 'ddt2':  # abd(ddw)*derivative operator
                        dw = np.diff(valarr)/(np.average(np.diff(tim)))
                        ddw = np.diff(dw)/(np.average(np.diff(tim)))
                        tim = tim[2:]
                        valarr = 4e-6 * dw[1:] * np.abs(ddw)

                    if (len(tim) < 10) or (np.std(tim)<0.1):
                        raise ValueError('Insufficient points or degenerate'
                                         'timebase data in {0}, {1}'
                                         .format(varname, dg.filename))

                    if times is not None:
                        debug_(max(pyfusion.DEBUG, debug), level=5, key='interp')
                        valarr = (stineman_interp(times, tim, valarr))
                        w = np.where(times > max(tim))
                        valarr[w] = np.nan
                    else:
                        times = tim
            if valarr is not None: vals.update({diag: valarr})
    # create an extra time array to allow a cross check
    vals.update({'check_tm':times})
    vals.update({'check_shot':np.zeros(len(times),dtype=np.int)+shot})

    return(vals)                
def get_basic_diagnostics(diags=None, shot=54196, times=None, delay=None, exception=False, debug=0):
    """ return a list of np.arrays of normally numeric values for the 
    times given, for the given shot.
    Will access server if env('IGETFILE') points to an exe, else accesses cache
    """

    global HJ_summary
    # if no exception given and we are not debugging
    # note - exception=None is a valid entry, meaning tolerate no exceptions
    # so the "default" we use is False
    if exception==False and debug==0: exception=Exception

    if diags is None: diags = "<n_e19>,b_0,i_p,w_p,dw_pdt,dw_pdt2".split(',')
    if len(np.shape(diags)) == 0: diags = [diags]
    # LHD only    if delay is None: delay = get_delay(shot)

    if times is None: 
        times = np.linspace(0,4,4000)

    times = np.array(times)
    vals = {}
    # create an extra time array to allow a cross check
    vals.update({'check_tm':times})
    vals.update({'check_shot':np.zeros(len(times),dtype=np.int)+shot})
    debug_(pyfusion.DEBUG,2,key='get_basic')
    for diag in diags:
        if not(diag in file_info):
            warn('diagnostic {0} not found in shot {1}'.format(diag, shot),stacklevel=2)
            vals.update({diag: np.nan + times})
            debug_(pyfusion.DEBUG,2,key='get_basic')
        else:
            info = file_info[diag]
            varname = info['name']
            infofmt = info['format']
            subfolder = infofmt.split('@')[0]
            filepath = os.path.sep.join([localigetfilepath,subfolder,infofmt])
            if ':' in varname: (oper,varname) = varname.split(':')
            else: oper = None

            if '(' in varname:  
                try:
                    left,right = varname.split('(')
                    varname,rest=right.split(')')
                except:
                    raise ValueError('in expression {v} - parens?'.format(varname))
            if infofmt.find('.npz') > 0:
                try:
                    test=HJ_summary.keys()
                except:    
                    csvfilename = acq_HJ+'/'+infofmt
                    if pyfusion.DBG() > 1: print('looking for HeliotronJ summary in' + csvfilename)
                    print('reloading {0}'.format(csvfilename))
                    HJ_summary = np.load(csvfilename)

                val = HJ_summary[varname][shot]
                valarr = np.double(val)+(times*0)
            elif 'get_static_params' in infofmt:
                pdicts = eval(infofmt.format(shot=shot))
                if len(pdicts)==0:
                    print('empty dictionary returned')

                val = pdicts[varname]
                valarr = np.double(val)+(times*0)
            else:    # read signal from data system
                debug_(max(pyfusion.DEBUG, debug), level=4, key='find_data')
                try:
                    #get HJparams
                    channel = info['name']
                    outdata=np.zeros(1024*2*256+1)
                    channel_length =(len(outdata)-1)/2
                    # outdfile only needed for opt=1 (get data via temp file)
                    # with tempfile.NamedTemporaryFile(prefix="pyfusion_") as outdfile:
                    ierror, getrets=gethjdata.gethjdata(shot,channel_length,
                                                        info['name'],
                                                        verbose=VERBOSE, opt=1,
                                                        ierror=2,
                                                        outdata=outdata, outname='')

                    if ierror != 0:
                        raise LookupError('data not found for {s}:{c}'.format(s=shot, c=channel))
                    ch = Channel(info['name'], Coords('dummy', (0,0,0)))
                    # timebase in secs (was ms in raw data)
                    dg = TimeseriesData(timebase=Timebase(1e-3 * getrets[1::2]),
                                        signal=Signal(getrets[2::2]), channels=ch)
                except exception as reason:
                    if debug>0:
                        print('exception running gethjdata {r} {a}', format(r=reason, a=reason.args))
                    dg=None
                            #break  # give up and try next diagnostic
                if dg is None:  # messy - break doesn't do what I want?
                    valarr=None
                else:
                    nd = 1   # initially only deal with single channels (HJ)
                    # get the column(s) of the array corresponding to the name
                    w = [0]
                    if (oper in 'sum,average,rms,max,min'.split(',')):
                        if oper=='sum': op = np.sum
                        elif oper=='average': op = np.average
                        elif oper=='min': op = np.min
                        elif oper=='std': op = np.std
                        else: raise ValueError('operator {o} in {n} not known to get_basic_diagnostics'
                                               .format(o=oper, n=info['name']))
                        # valarr = op(dg.data[:,nd+w],1)
                        valarr = op(dg.data[:,nd+w],1)
                    else:
                        if len(w) != 1:
                            raise LookupError(
                                'Need just one instance of variable {0} in {1}'
                                .format(varname, dg.filename))
                        dg.data = dg.signal # fudge compatibility
                        if len(np.shape(dg.data))!=1:  # 2 for igetfile
                           raise LookupError(
                                'insufficient data for {0} in {1}'
                                .format(varname, dg.filename))
                             
                        #valarr = dg.data[:,nd+w[0]]

                    #tim =  dg.data[:,0] - delay
                    valarr = dg.signal
                    tim = dg.timebase

                    # fudge until we can gete the number of points
                    valarr = valarr[:np.argmax(tim)]
                    tim = tim[:np.argmax(tim)]

                    if oper == 'ddt':  # derivative operator
                        valarr = np.diff(valarr)/(np.average(np.diff(tim)))
                        tim = (tim[0:-1] + tim[1:])/2.0

                    if oper == 'ddt2':  # abd(ddw)*derivative operator
                        dw = np.diff(valarr)/(np.average(np.diff(tim)))
                        ddw = np.diff(dw)/(np.average(np.diff(tim)))
                        tim = tim[2:]
                        valarr = 4e-6 * dw[1:] * np.abs(ddw)

                    if (len(tim) < 10) or (np.std(tim)<0.1):
                        raise ValueError('Insufficient points or degenerate'
                                         'timebase data in {0}, {1}'
                                         .format(varname, dg.filename))

                    valarr = (stineman_interp(times, tim, valarr))
                    w = np.where(times > max(tim))
                    valarr[w] = np.nan

            if valarr is not None: vals.update({diag: valarr})
    debug_(max(pyfusion.DEBUG, debug), level=5, key='interp')
    return(vals)                
Beispiel #12
0
def get_basic_params(diags=None, shot=54196, times=None, delay=None, debug=0):
    """ return a list of np.arrays of normally numeric values for the 
    times given, for the given shot.
    """

    global lhd_summary

    if diags is None: diags = "<n_e19>,b_0,i_p,w_p,dw_pdt,dw_pdt2".split(',')

    if delay is None: delay = get_delay(shot)

    if times is None:
        times = np.linspace(0, 4, 4000)

    times = np.array(times)
    vals = {}
    # create an extra time array to allow a cross check
    vals.update({'check_tm': times})
    vals.update({'check_shot': np.zeros(len(times), dtype=np.int) + shot})
    for diag in diags:
        if diag not in file_info:
            warn('diagnostic {0} not found in shot {1}'.format(diag, shot),
                 stacklevel=2)
            vals.update({diag: np.nan + times})
        else:
            info = file_info[diag]
            varname = info['name']
            if ':' in varname: (oper, varname) = varname.split(':')
            else: oper = None

            if info['format'].find('.csv') > 0:
                try:
                    test = lhd_summary.keys()
                except:
                    print('reloading {0}'.format(info['format']))
                    lhd_summary = read_csv_data(acq_LHD + info['format'],
                                                header=3)

                val = lhd_summary[varname][shot]
                valarr = np.double(val) + (times * 0)
            else:
                try:
                    dg = igetfile(local_dir + info['format'], shot=shot)
                except IOError:
                    try:
                        dg = igetfile(local_dir + info['format'] + '.bz2',
                                      shot=shot)
                    except IOError:
                        try:
                            dg = igetfile(local_dir + info['format'] + '.gz',
                                          shot=shot)
                        except exception:
                            #debug_(1)
                            dg = None
                            #break  # give up and try next diagnostic
                if dg is None:  # messy - break doesn't do what I want?
                    valarr = None
                else:
                    nd = dg.vardict['DimNo']
                    if nd != 1:
                        raise ValueError(
                            'Expecting a 1 D array in {0}, got {1}!'.format(
                                dg.filename, nd))

                    # pre re. w = np.where(np.array(dg.vardict['ValName'])==varname)[0]
                    matches = [
                        re.match(varname, nam) != None
                        for nam in dg.vardict['ValName']
                    ]
                    w = np.where(np.array(matches) != False)[0]
                    if len(w) != 1:
                        raise LookupError(
                            'Need just one instance of variable {0} in {1}'.
                            format(varname, dg.filename))

                    # get the column of the array corresponding to the name
                    valarr = dg.data[:, nd + w[0]]
                    tim = dg.data[:, 0] - delay

                    if oper == 'ddt':  # derivative operator
                        valarr = np.diff(valarr) / (np.average(np.diff(tim)))
                        tim = (tim[0:-1] + tim[1:]) / 2.0

                    if oper == 'ddt2':  # abd(ddw)*derivative operator
                        dw = np.diff(valarr) / (np.average(np.diff(tim)))
                        ddw = np.diff(dw) / (np.average(np.diff(tim)))
                        tim = tim[2:]
                        valarr = 4e-6 * dw[1:] * np.abs(ddw)

                    valarr = (stineman_interp(times, tim, valarr))
                    w = np.where(times > max(tim))
                    valarr[w] = np.nan

            if valarr != None: vals.update({diag: valarr})
    debug_(max(pyfusion.DEBUG, debug), level=5, key='interp')
    return (vals)
Beispiel #13
0
        flow_2 = h1tree.getNode(
            '.operations.magnetsupply.lcu.log:gas2_set').data()
        flow_3 = h1tree.getNode(
            '.operations.magnetsupply.lcu.log:gas3_set').data()
        good.append(sh)

    except exception as reason:
        bad.append(sh)
        print('failed to open shot {sh}: {r} {a}'.format(sh=sh,
                                                         r=reason,
                                                         a=reason.args))
        continue

    w = np.where(sh == shot)[0]
    times = t_mid[w]
    for ne in 'ne_1,ne_4,ne_7'.split(',')[0:num_ne]:
        valarr = stineman_interp(times, ne_time, eval(ne))
        dd[ne][w] = valarr

    if sweeping:
        k_h = stineman_interp(times, k_h_time, k_h)
    dd['k_h'][w] = k_h

    dd['p_rf'][w] = stineman_interp(times, p_rf_time, p_rf)
    for k in 'b_0,phd_rf,flow_1,flow_2,flow_3'.split(','):
        dd[k][w] = eval(k)

print('{g} shots successful, {b} shots failed:'.format(g=len(good),
                                                       b=len(bad)))
print(bad)
Beispiel #14
0
def get_basic_diagnostics(diags=None,
                          shot=54196,
                          times=None,
                          delay=None,
                          exception=False,
                          debug=0):
    """ return a list of np.arrays of normally numeric values for the 
    times given, for the given shot.
    Will access server if env('IGETFILE') points to an exe, else accesses cache
    """

    global HJ_summary
    # if no exception given and we are not debugging
    # note - exception=None is a valid entry, meaning tolerate no exceptions
    # so the "default" we use is False
    if exception == False and debug == 0: exception = Exception

    if diags is None: diags = "<n_e19>,b_0,i_p,w_p,dw_pdt,dw_pdt2".split(',')
    if len(np.shape(diags)) == 0: diags = [diags]
    # LHD only    if delay is None: delay = get_delay(shot)

    if times is None:
        times = np.linspace(0, 4, 4000)

    times = np.array(times)
    vals = {}
    # create an extra time array to allow a cross check
    vals.update({'check_tm': times})
    vals.update({'check_shot': np.zeros(len(times), dtype=np.int) + shot})
    debug_(pyfusion.DEBUG, 2, key='get_basic')
    for diag in diags:
        if not (diag in file_info):
            warn('diagnostic {0} not found in shot {1}'.format(diag, shot),
                 stacklevel=2)
            vals.update({diag: np.nan + times})
            debug_(pyfusion.DEBUG, 2, key='get_basic')
        else:
            info = file_info[diag]
            varname = info['name']
            infofmt = info['format']
            subfolder = infofmt.split('@')[0]
            filepath = os.path.sep.join(
                [localigetfilepath, subfolder, infofmt])
            if ':' in varname: (oper, varname) = varname.split(':')
            else: oper = None

            if '(' in varname:
                try:
                    left, right = varname.split('(')
                    varname, rest = right.split(')')
                except:
                    raise ValueError(
                        'in expression {v} - parens?'.format(varname))
            if infofmt.find('.npz') > 0:
                try:
                    test = HJ_summary.keys()
                except:
                    csvfilename = acq_HJ + '/' + infofmt
                    if pyfusion.DBG() > 1:
                        print('looking for HeliotronJ summary in' +
                              csvfilename)
                    print('reloading {0}'.format(csvfilename))
                    HJ_summary = np.load(csvfilename)

                val = HJ_summary[varname][shot]
                valarr = np.double(val) + (times * 0)
            elif 'get_static_params' in infofmt:
                pdicts = eval(infofmt.format(shot=shot))
                if len(pdicts) == 0:
                    print('empty dictionary returned')

                val = pdicts[varname]
                valarr = np.double(val) + (times * 0)
            else:  # read signal from data system
                debug_(max(pyfusion.DEBUG, debug), level=4, key='find_data')
                try:
                    #get HJparams
                    channel = info['name']
                    outdata = np.zeros(1024 * 2 * 256 + 1)
                    channel_length = (len(outdata) - 1) / 2
                    # outdfile only needed for opt=1 (get data via temp file)
                    # with tempfile.NamedTemporaryFile(prefix="pyfusion_") as outdfile:
                    ierror, getrets = gethjdata.gethjdata(shot,
                                                          channel_length,
                                                          info['name'],
                                                          verbose=VERBOSE,
                                                          opt=1,
                                                          ierror=2,
                                                          outdata=outdata,
                                                          outname='')

                    if ierror != 0:
                        raise LookupError('data not found for {s}:{c}'.format(
                            s=shot, c=channel))
                    ch = Channel(info['name'], Coords('dummy', (0, 0, 0)))
                    # timebase in secs (was ms in raw data)
                    dg = TimeseriesData(timebase=Timebase(1e-3 *
                                                          getrets[1::2]),
                                        signal=Signal(getrets[2::2]),
                                        channels=ch)
                except exception as reason:
                    if debug > 0:
                        print('exception running gethjdata {r} {a}',
                              format(r=reason, a=reason.args))
                    dg = None
                    #break  # give up and try next diagnostic
                if dg is None:  # messy - break doesn't do what I want?
                    valarr = None
                else:
                    nd = 1  # initially only deal with single channels (HJ)
                    # get the column(s) of the array corresponding to the name
                    w = [0]
                    if (oper in 'sum,average,rms,max,min'.split(',')):
                        if oper == 'sum': op = np.sum
                        elif oper == 'average': op = np.average
                        elif oper == 'min': op = np.min
                        elif oper == 'std': op = np.std
                        else:
                            raise ValueError(
                                'operator {o} in {n} not known to get_basic_diagnostics'
                                .format(o=oper, n=info['name']))
                        # valarr = op(dg.data[:,nd+w],1)
                        valarr = op(dg.data[:, nd + w], 1)
                    else:
                        if len(w) != 1:
                            raise LookupError(
                                'Need just one instance of variable {0} in {1}'
                                .format(varname, dg.filename))
                        dg.data = dg.signal  # fudge compatibility
                        if len(np.shape(dg.data)) != 1:  # 2 for igetfile
                            raise LookupError(
                                'insufficient data for {0} in {1}'.format(
                                    varname, dg.filename))

                        #valarr = dg.data[:,nd+w[0]]

                    #tim =  dg.data[:,0] - delay
                    valarr = dg.signal
                    tim = dg.timebase

                    # fudge until we can gete the number of points
                    valarr = valarr[:np.argmax(tim)]
                    tim = tim[:np.argmax(tim)]

                    if oper == 'ddt':  # derivative operator
                        valarr = np.diff(valarr) / (np.average(np.diff(tim)))
                        tim = (tim[0:-1] + tim[1:]) / 2.0

                    if oper == 'ddt2':  # abd(ddw)*derivative operator
                        dw = np.diff(valarr) / (np.average(np.diff(tim)))
                        ddw = np.diff(dw) / (np.average(np.diff(tim)))
                        tim = tim[2:]
                        valarr = 4e-6 * dw[1:] * np.abs(ddw)

                    if (len(tim) < 10) or (np.std(tim) < 0.1):
                        raise ValueError('Insufficient points or degenerate'
                                         'timebase data in {0}, {1}'.format(
                                             varname, dg.filename))

                    valarr = (stineman_interp(times, tim, valarr))
                    w = np.where(times > max(tim))
                    valarr[w] = np.nan

            if valarr is not None: vals.update({diag: valarr})
    debug_(max(pyfusion.DEBUG, debug), level=5, key='interp')
    return (vals)
def plot_hist_all( ts, nbins=50, transparent=True, norm_it=False, **kwargs ):
    """
    ts -- dictionary with values as lifespan timeseries.

    kwargs -- see pylab hist() function

    Returns interpolation functions as well as histogram triple and figure instance.
    """
    from pylab import log1p
    from matplotlib.mlab import stineman_interp

    #data = ts.values()
    data = ts
    fig = plt.figure( figsize=(12,8) )
    if transparent:
        fig.patch.set_alpha( 0.0 )
    # ax = fig.add_subplot( 121 )
    # ax2 = fig.add_subplot( 122 )
    ax2 = fig.add_subplot( 111 )
    # now plot a single cell's histogram on the first axis
    #ax.hist( data, **kwargs )

    # convert each list of lifespans to 1d arrays
    data = [ numpy.asarray( d ) for d in data ]
    xmax = max( [d.max() for d in data] )
    thebins = numpy.linspace(0, xmax, nbins )

    # add/change the kwargs 'bins' key
    kwargs['bins'] = thebins

    # holds bins counts (n) for each histogram
    all_ny = []
    for d in data:
        n, bins, patches = ax2.hist( d, **kwargs )
        all_ny.append( n )
        # all_bins.append( bins )
    # convert bins counts to a single array to find min
    arr = numpy.array( all_ny ).ravel()
    wy = arr[ numpy.where( arr != 0 )[0] ]
    min_y = min( wy )

    # for plotting average -- these are already log values if
    # log==True in kwargs
    yhist = numpy.array( all_ny, dtype=numpy.float64 )#.ravel()
    avg = yhist.mean( axis=0 )
    err = yhist.std( axis=0 )
    upper = avg + err
    lower = avg - err

    # print "yhist", yhist
    # print "avg", avg
    # print ""

    # print err
    # print ""
    # print lower
    # print upper
    # print ""

    # average value for each histogram bin
    for i, x in enumerate( avg ):
        if x == 0.0:
            avg[i] = 1.0

    # label the axes
    ax2.set_xlabel( 'Generator lifespan', fontsize=20 )
    ax2.set_ylabel( 'Number of generators (log)', fontsize=20 )
    xticks = ax2.get_xticks()
    yticks = ax2.get_yticks()
    ax2.set_xticklabels( [str(int(x)) for x in xticks], fontsize=20 )
    ax2.set_yticklabels( [str(int(y)) for y in yticks], fontsize=20 )

    # now plot the interpolated average distribution here so it is on
    # top of the other stuff
    yp = None
    xi = numpy.linspace( 0, bins[-1],200)
    yi = stineman_interp( xi, bins[:-1], avg, yp )
    # interpolate upper and lower error bars to get envelope
    # COMMENTED OUT BELOW
    upper_yi = stineman_interp( xi, bins[:-1], upper, yp )
    lower_yi = stineman_interp( xi, bins[:-1], lower, yp )
    
    # make sure lower does not go negative since this makes no sense.
    for i,v in enumerate( lower_yi ):
        if v < 1.0:
            lower_yi[i] = 1.0

    # make sure that the plot doesn't get messed up by small values
    # (esp. New cells)
    masked_yi = numpy.ma.masked_less( yi, 1 )

    # plot the interpolation of the avg and the envelope
    ax2.plot( xi, masked_yi, 'r-', lw=3 )
    # ax2.fill_between( xi, lower_yi, upper_yi, #where=masked_yi
    #                   color='r', alpha=0.5, zorder=10 )
    #fig.show()

    if norm_it:
        y_max = masked_yi.max()
        print "y_max", y_max
        masked_yi /= y_max

    return xi, masked_yi, lower_yi, upper_yi, fig, (n, bins, patches)