Exemple #1
0
def export_gpi_data_to_paraview(exp_id=None,
                                time_range=None,
                                filename=None,
                                filter_data=True,
                                flux_coordinates=False):

    if filename is None:
        filename = 'GPI_FOR_PARAVIEW_' + str(exp_id) + '_' + str(
            time_range[0]) + '_' + str(time_range[1])
#    d=flap.get_data('NSTX_GPI', exp_id=exp_id, name='', object_name='GPI')
#    d=flap.slice_data('GPI', slicing={'Time':flap.Intervals(time_range[0],time_range[1])})
#    if filter_data:
#        d=flap.filter_data('GPI',exp_id=exp_id,
#                           coordinate='Time',
#                           options={'Type':'Highpass',
#                                    'f_low':1e2,
#                                    'Design':'Chebyshev II'})
#        filename=filename+'_FILTERED'
#    time=d.coordinate('Time')[0]
#    x=d.coordinate('Device R')[0].flatten()
#    y=d.coordinate('Device z')[0].flatten()
#    t=time.flatten()
#    data=d.data.flatten()
#    np.savetxt(filename, np.asarray([[x],[y],[1000*t],[data]])[:,0,:].T, delimiter=",", header='x [m], y [m], t [ms], data [a.u.]')

    d = flap.get_data('NSTX_GPI', exp_id=exp_id, name='', object_name='GPI')
    if filter_data:
        d = flap.filter_data('GPI',
                             exp_id=exp_id,
                             coordinate='Time',
                             options={
                                 'Type': 'Highpass',
                                 'f_low': 1e2,
                                 'Design': 'Chebyshev II'
                             })
        filename += '_FILTERED'
    if flux_coordinates:
        flap.add_coordinate('GPI', 'Flux r')
        filename += '_FLUX'
    d = flap.slice_data(
        'GPI', slicing={'Time': flap.Intervals(time_range[0], time_range[1])})
    #    time=d.coordinate('Time')[0]
    #    ind=np.where(np.logical_and(time[:,0,0]>=time_range[0], time[:,0,0]<=time_range[1]))
    #    x1=d.coordinate('Device R')[0][ind,:,:].flatten()
    #    y=d.coordinate('Device z')[0][ind,:,:].flatten()
    #    t=time[ind,:,:].flatten()
    #    data=d.data[ind,:,:].flatten()

    t = d.coordinate('Time')[0].flatten()
    x1 = d.coordinate('Device R')[0].flatten()
    y = d.coordinate('Device z')[0].flatten()
    data = d.data.flatten()
    filename = filename + '.csv'
    if flux_coordinates:
        #x2=d.coordinate('Flux r')[0][ind,:,:].flatten()
        x2 = d.coordinate('Flux r')[0].flatten()
        x2 = (x2 - np.min(x2)) / (np.max(x2) - np.min(x2)) * (
            np.max(x1) - np.min(x1)) + np.min(x1)
        np.savetxt(
            filename,
            np.asarray([[x1], [x2], [y], [10000 * t], [data]])[:, 0, :].T,
            delimiter=",",
            header='R [m], PSI_rescaled [m], z [m], t [0.1ms], data [a.u.]')
    else:
        np.savetxt(filename,
                   np.asarray([[x1], [y], [10000 * t], [data]])[:, 0, :].T,
                   delimiter=",",
                   header='R [m], z [m], t [0.1ms], data [a.u.]')
Exemple #2
0
def test_filter():
    plt.close('all')
    print()
    print('>>>>>>>>>>>>>>>>>>> Test filter <<<<<<<<<<<<<<<<<<<<<<<<')
    flap.delete_data_object('*')

    print(
        "**** Generating 10 square wave signals and filtering with integrating filter, 10 microsec"
    )
    t = np.arange(1000) * 1e-6
    d = np.ndarray((len(t), 10), dtype=float)
    for i in range(10):
        d[:, i] = np.sign(np.sin(math.pi * 2 * (1e4 + i * 1e3) * t)) + 1
    c = flap.Coordinate(name='Time',
                        unit='Second',
                        mode=flap.CoordinateMode(equidistant=True),
                        start=0.0,
                        step=1e-6,
                        dimension_list=[0])
    d = flap.DataObject(data_array=d, coordinates=[c])
    flap.add_data_object(d, "Signal")

    plt.figure()
    d.plot(options={'Y sep': 3})
    di = d.filter_data(coordinate='Time',
                       intervals=flap.Intervals(np.array([1e-4, 6e-4]),
                                                np.array([2e-4, 8e-4])),
                       options={
                           'Type': 'Int',
                           'Tau': 10e-6
                       }).plot(options={'Y sep': 3})

    print("**** Filtering with differential filter, 10 microsec")
    plt.figure()
    d.plot(options={'Y sep': 3})
    flap.filter_data('Signal',
                     output_name='Signal_filt',
                     coordinate='Time',
                     intervals=flap.Intervals(np.array([1e-4, 6e-4]),
                                              np.array([2e-4, 8e-4])),
                     options={
                         'Type': 'Diff',
                         'Tau': 10e-6
                     })
    flap.plot('Signal_filt', options={'Y sep': 3})

    print(
        "**** Generating random data, 1 million points and overplotting spectra with various filters."
    )
    d = flap.get_data('TESTDATA',
                      name='TEST-1-1',
                      options={
                          'Signal': 'Random',
                          'Scaling': 'Digit',
                          'Length': 1
                      },
                      object_name='Signal')

    plt.figure()
    flap.filter_data('Signal',
                     output_name='Signal_filt',
                     coordinate='Time',
                     options={
                         'Type': 'Int',
                         'Tau': 16e-6
                     })
    flap.apsd('Signal',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\
       .plot(options={'Log x':True, 'Log y': True})
    plotid = flap.apsd('Signal_filt',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\
       .plot(options={'Log x':True, 'Log y': True})
    plotid.plt_axis_list[-1].set_title("{'Type':'Int','Tau':16e-6}")

    plt.figure()
    flap.filter_data('Signal',
                     output_name='Signal_filt',
                     coordinate='Time',
                     options={
                         'Type': 'Diff',
                         'Tau': 16e-6
                     })
    flap.apsd('Signal',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\
       .plot(options={'Log x':True, 'Log y': True})
    plotid = flap.apsd('Signal_filt',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\
       .plot(options={'Log x':True, 'Log y': True})
    plotid.plt_axis_list[-1].set_title("{'Type':'Diff','Tau':16e-6}")

    plt.figure()
    flap.filter_data('Signal',
                     output_name='Signal_filt',
                     coordinate='Time',
                     options={
                         'Type': 'Lowpass',
                         'f_high': 5e4
                     })
    flap.apsd('Signal',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\
       .plot(options={'Log x':True, 'Log y': True})
    plotid = flap.apsd('Signal_filt',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\
       .plot(options={'Log x':True, 'Log y': True})
    plotid.plt_axis_list[-1].set_title("{'Type':'Lowpass','f_high':5e4}")

    plt.figure()
    flap.filter_data('Signal',
                     output_name='Signal_filt',
                     coordinate='Time',
                     options={
                         'Type': 'Highpass',
                         'f_low': 1e4,
                         'f_high': 5e4
                     })
    flap.apsd('Signal',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\
       .plot(options={'Log x':True, 'Log y': True})
    plotid = flap.apsd('Signal_filt',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\
       .plot(options={'Log x':True, 'Log y': True})
    plotid.plt_axis_list[-1].set_title(
        "{'Type':'Highpass','f_low':1e4,'f_high':5e4}")

    plt.figure()
    flap.filter_data('Signal',
                     output_name='Signal_filt',
                     coordinate='Time',
                     options={
                         'Type': 'Bandpass',
                         'f_low': 5e3,
                         'f_high': 5e4
                     })
    flap.apsd('Signal',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\
       .plot(options={'Log x':True, 'Log y': True})
    plotid = flap.apsd('Signal_filt',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\
       .plot(options={'Log x':True, 'Log y': True})
    plotid.plt_axis_list[-1].set_title(
        "{'Type':'Bandpass','f_low':5e3,'f_high':5e4}")

    plt.figure()
    print("**** Bandpower signal [5e4-2e5] Hz, inttime 20 microsec")
    flap.filter_data('Signal',
                     output_name='Signal_filt',
                     coordinate='Time',
                     options={
                         'Type': 'Bandpass',
                         'f_low': 5e4,
                         'f_high': 2e5,
                         'Power': True,
                         'Inttime': 20e-6
                     })
    plotid = flap.plot('Signal_filt')
    plotid.plt_axis_list[-1].set_title(
        "'Type':'Bandpass','f_low':5e4,'f_high':2e5, 'Power':True, 'Inttime':20e-6}"
    )
Exemple #3
0
def test_ccf():
    plt.close('all')
    print()
    print(
        '>>>>>>>>>>>>>>>>>>> Test ccf (Cross Correlation Function) <<<<<<<<<<<<<<<<<<<<<<<<'
    )
    flap.delete_data_object('*')
    print(
        "**** Generating 10x15 random test signals, 5000 points each, 1 MHz sampling."
    )
    flap.get_data('TESTDATA',
                  name='TEST-*-*',
                  options={
                      'Length': 0.005,
                      'Signal': 'Random'
                  },
                  object_name='TESTDATA')
    print("**** Filtering with 10 microsec integrating filter.")
    flap.filter_data('TESTDATA',
                     coordinate='Time',
                     options={
                         'Type': 'Int',
                         'Tau': 1e-5
                     },
                     output_name='TESTDATA_filt')
    flap.list_data_objects()
    plt.figure()
    print("**** Plotting an original and a filtered signal.")
    flap.plot('TESTDATA', slicing={'Row': 1, 'Column': 1}, axes='Time')
    flap.plot('TESTDATA_filt', slicing={'Row': 1, 'Column': 1})
    print('**** Calculating the 10x15x10x15 CCFs, each 5000 samples.')
    print('**** CCF START')
    start = time.time()
    flap.ccf('TESTDATA_filt',
             coordinate='Time',
             options={
                 'Trend': 'Mean',
                 'Range': [-1e-4, 1e-4],
                 'Res': 1e-5,
                 'Norm': True
             },
             output_name='CCF')
    stop = time.time()
    print('**** CCF STOP')
    print("**** Calculation time: {:6.3f} ms/signal".format(
        1000 * (stop - start) / (10 * 15 * 10 * 15)))
    flap.list_data_objects()
    print(
        "**** Plotting spatiotemporal correlation function at ref row, column 3,3, column 3"
    )
    plt.figure()
    flap.plot('CCF',
              slicing={
                  'Row (Ref)': 3,
                  'Column (Ref)': 3,
                  'Column': 3
              },
              axes=['Time lag'],
              plot_type='multi xy')

    print("**** Slicing TESTDATA_filt for row: 1-3, column:1-4")
    flap.slice_data('TESTDATA_filt',
                    slicing={
                        'Row': [1, 2, 3],
                        'Column': [1, 2, 3, 4]
                    },
                    output_name='TESTDATA_filt_3x4')
    print('**** Calculating CCFs, between original and sliced TESTDATAfilt')
    print('**** CCF START')
    flap.ccf('TESTDATA_filt',
             ref='TESTDATA_filt_3x4',
             coordinate='Time',
             options={
                 'Trend': 'Mean',
                 'Range': [-1e-4, 1e-4],
                 'Res': 1e-5,
                 'Norm': True
             },
             output_name='CCF_ref')
    print('**** CCF STOP')
    flap.list_data_objects()
    print(
        "**** Plotting spatiotemporal correlation function at ref row, column 3,3, column 3"
    )
    plt.figure()
    flap.plot('CCF_ref',
              slicing={
                  'Row (Ref)': 3,
                  'Column (Ref)': 3,
                  'Column': 3
              },
              axes=['Time lag'],
              plot_type='multi xy')
Exemple #4
0
def show_nstx_gpi_slice_traces(exp_id=None,
                               time_range=None,
                               x_slices=np.linspace(0,60,14),
                               y_slices=np.linspace(0,70,16),
                               x_summing=False,
                               y_summing=False,
                               z_range=[0,512],
                               zlog=False,
                               filename=None,
                               filter_data=False,
                               save_pdf=False,
                               pdf_saving_only=False):
    
    if pdf_saving_only:
        import matplotlib
        current_backend=matplotlib.get_backend()
        matplotlib.use('agg')
    import matplotlib.pyplot as plt
    if save_pdf:
        pdf=PdfPages(filename)
    plt.cla() 
    if filename is None:
        filename='NSTX_GPI_SLICE_'+str(exp_id)+'_'+str(time_range[0])+'_'+str(time_range[1])+'.pdf'
    
    flap.get_data('NSTX_GPI', exp_id=exp_id, name='', object_name='GPI')
        
    if filter_data:
        flap.filter_data('GPI',exp_id=exp_id,
                     coordinate='Time',
                     options={'Type':'Highpass',
                              'f_low':1e2,
                              'Design':'Chebyshev II'})
                        
    if not x_summing:
        for i in range(len(x_slices)):
            plt.figure()
            flap.plot('GPI', plot_type='image', 
                      axes=['Time', 'Image y'], 
                      slicing={'Time':flap.Intervals(time_range[0],time_range[1]), 'Image x':x_slices[i]}, 
                      #plot_options={'levels':100}, 
                      options={'Z range':z_range,'Log z':zlog})
            plt.title('NSTX GPI '+str(exp_id)+' Image x = '+str(int(x_slices[i])))
            if save_pdf:
                pdf.savefig()
                plt.close()
    else:
        plt.figure()
        flap.plot('GPI', plot_type='image', 
                  axes=['Time', 'Image y'], 
                  slicing={'Time':flap.Intervals(time_range[0],time_range[1])}, 
                  summing={'Image x':'Mean'},
                  #plot_options={'levels':100}, 
                  options={'Z range':z_range,'Log z':zlog})
        plt.title('NSTX GPI '+str(exp_id)+' Mean x pixels')
        if save_pdf:
            pdf.savefig()
            plt.close()
            
    if not x_summing:    
        for j in range(len(y_slices)):
            if not y_summing:
                slicing={'Time':flap.Intervals(time_range[0],time_range[1]), 'Image y':y_slices[i]}
                y_summing_opt=None
            else:
                slicing={'Time':flap.Intervals(time_range[0],time_range[1])}
                y_summing_opt={'Image y':'Mean'}
            plt.figure()
            flap.plot('GPI', plot_type='image', 
                      axes=['Time', 'Image x'], 
                      slicing=slicing,
                      summing=y_summing_opt,
                      #plot_options={'levels':100}, 
                      options={'Z range':z_range,'Log z':zlog})
            plt.title('NSTX GPI '+str(exp_id)+' Image y = '+str(int(y_slices[j])))
            if save_pdf:
                pdf.savefig()
                plt.close()
    else:
        plt.figure()
        flap.plot('GPI', plot_type='image', 
                  axes=['Time', 'Image x'], 
                  slicing={'Time':flap.Intervals(time_range[0],time_range[1])}, 
                  summing={'Image y':'Mean'},
                  #plot_options={'levels':100}, 
                  options={'Z range':z_range,'Log z':zlog})
        plt.title('NSTX GPI '+str(exp_id)+' Mean y pixels')
        if save_pdf:
            pdf.savefig()
            plt.close()
            
    if save_pdf:
        pdf.close() 
        
    if pdf_saving_only:
        import matplotlib
        matplotlib.use(current_backend)           

#show_nstx_gpi_video(exp_id=141918, time_range=[250.,260.], plot_filtered=True, cache_data=False, plot_efit=True, flux_coordinates=False)
Exemple #5
0
def show_nstx_gpi_timetrace(exp_id=None,
                            plot_filtered=False,
                            time_range=None,
                            new_plot=False,
                            overplot=False,
                            scale=1.0,
                            save_pdf=False,
                            cache_data=True,
                            ):
    plot_options={}
    if time_range is None:
        print('time_range is None, the entire shot is plot.')
        slicing_range=None
    else:    
        if (type(time_range) is not list and len(time_range) != 2):
            raise TypeError('time_range needs to be a list with two elements.')
        plot_options['X range']=time_range
        slicing_range={'Time':flap.Intervals(time_range[0],time_range[1])}
    if exp_id is not None:
        print("\n------- Reading NSTX GPI data --------")
        if cache_data:
            try:
                d=flap.get_data_object_ref(exp_id=exp_id,object_name='GPI')
            except:
                print('Data is not cached, it needs to be read.')
                d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI')
        else:
            flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI')
    else:
        raise ValueError('The experiment ID needs to be set.')
    flap.slice_data('GPI',
                    #slicing=slicing_range,
                    slicing=slicing_range,
                    summing={'Image x':'Mean','Image y':'Mean'},
                    output_name='GPI_MEAN')
    object_name='GPI_MEAN'
    
    if plot_filtered:
        print("**** Filtering GPI")
        object_name='GPI_MEAN_FILTERED'
        flap.filter_data('GPI_MEAN',output_name='GPI_MEAN_FILTERED',coordinate='Time',
                         options={'Type':'Highpass',
                                  'f_low':1e2,
                                  'Design':'Chebyshev II'}) #Data is in milliseconds
    if scale != 1.0:
        d=flap.get_data_object_ref(object_name, exp_id)
        d.data=d.data*scale
    if new_plot and not overplot:
        plt.figure()
    elif overplot:
        plot_options['Force axes']=True
    else:
        plt.cla()
    plot_options['All points']=True
    
    flap.plot(object_name,
              axes=['Time', '__Data__'],
              exp_id=exp_id,
              options=plot_options)
    if save_pdf:
        if time_range is not None:
            filename='NSTX_'+str(exp_id)+'_GPI_'+str(time_range[0])+'_'+str(time_range[1])+'_mean.pdf'
        else:
            filename='NSTX_'+str(exp_id)+'_GPI_mean.pdf'
        plt.savefig(filename)
Exemple #6
0
def show_nstx_gpi_video(exp_id=None,                                            #Shot number
                        time_range=None,                                        #Time range to show the video in, if not set, the enire shot is shown
                        z_range=None,                                           #Range for the contour/color levels, if not set, min-max is divided
                        logz=False,                                             #Plot the image in a logarithmic coloring
                        plot_filtered=False,                                    #Plot a high pass (100Hz) filtered video
                        normalize=None,                                         #Normalize the video by dividing it with a processed GPI signal 
                                                                                #    options: 'Time dependent' (LPF filtered) (recommended)
                                                                                #             'Time averaged' (LPF filtered and averaged for the time range) 
                                                                                #             'Simple' (Averaged)
                        normalizer_time_range=None,                             #Time range for the time dependent normalization
                        subtract_background=False,                              #Subtract the background from the image (mean of the time series)
                        plot_flux=False,                                        #Plot the flux surfaces onto the video
                        plot_separatrix=False,                                  #Plot the separatrix onto the video
                        plot_limiter=False,                                     #Plot the limiter of NSTX from EFIT
                        flux_coordinates=False,                                 #Plot the signal as a function of magnetic coordinates
                        device_coordinates=False,                               #Plot the signal as a function of the device coordinates
                        new_plot=True,                                          #Plot the video into a new figure window
                        save_video=False,                                       #Save the video into an mp4 format
                        video_saving_only=False,                                #Saving only the video, not plotting it
                        prevent_saturation=False,                               #Prevent saturation of the image by restarting the colormap
                        colormap='gist_ncar',                                   #Colormap for the plotting
                        cache_data=True,                                       #Try to load the data from the FLAP storage
                        ):                
        
    if exp_id is not None:
        print("\n------- Reading NSTX GPI data --------")
        if cache_data:
            try:
                d=flap.get_data_object_ref(exp_id=exp_id,object_name='GPI')
            except:
                print('Data is not cached, it needs to be read.')
                d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI')
        else:
            d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI')
        object_name='GPI'
    else:
        raise ValueError('The experiment ID needs to be set.')
        
    if time_range is None:
        print('time_range is None, the entire shot is plotted.')
        slicing=None
    else:    
        if (type(time_range) is not list and len(time_range) != 2):
            raise TypeError('time_range needs to be a list with two elements.')
        #time_range=[time_range[0]/1000., time_range[1]/1000.] 
        slicing={'Time':flap.Intervals(time_range[0],time_range[1])}
        d=flap.slice_data(object_name, 
                          exp_id=exp_id,
                          slicing=slicing, 
                          output_name='GPI_SLICED')
        object_name='GPI_SLICED'
        
    if plot_filtered:
        print("**** Filtering GPI ****")
        
        d=flap.filter_data(object_name,
                           exp_id=exp_id,
                           output_name='GPI_FILTERED',coordinate='Time',
                           options={'Type':'Highpass',
                                    'f_low':1e2,
                                    'Design':'Chebyshev II'})
        object_name='GPI_FILTERED'
        
    if normalize is not None:
        print("**** Normalizing GPI ****")
        d=flap.get_data_object_ref(object_name)
        if normalize in ['Time averaged','Time dependent', 'Simple']:
            if normalize == 'Time averaged':
                coefficient=flap_nstx.analysis.calculate_nstx_gpi_norm_coeff(exp_id=exp_id,
                                                          time_range=normalizer_time_range,
                                                          f_high=1e2,
                                                          design='Chebyshev II',
                                                          filter_data=True,
                                                          cache_data=True,
                                                          )
            if normalize == 'Time dependent':
                coefficient=flap.filter_data('GPI',
                                             exp_id=exp_id,
                                             output_name='GPI_LPF',
                                             coordinate='Time',
                                             options={'Type':'Lowpass',
                                                      'f_high':1e2,
                                                      'Design':'Chebyshev II'})
                if slicing is not None:
                    coefficient=coefficient.slice_data(slicing=slicing)
            if normalize == 'Simple':
                coefficient=flap.slice_data(object_name,summing={'Time':'Mean'})
                
            data_obj=copy.deepcopy(d)
            data_obj.data = data_obj.data/coefficient.data
            flap.add_data_object(data_obj, 'GPI_DENORM')
            object_name='GPI_DENORM'
        else:
            raise ValueError('Normalize can either be "Time averaged","Time dependent" or "Simple".')
            
    if subtract_background: #DEPRECATED, DOESN'T DO MUCH HELP
        print('**** Subtracting background ****')
        d=flap.get_data_object_ref(object_name, exp_id=exp_id)
        background=flap.slice_data(object_name, 
                                   exp_id=exp_id,
                                   summing={'Time':'Mean'})
        
        data_obj=copy.deepcopy(d)
        data_obj.data=data_obj.data/background.data
        
        flap.add_data_object(data_obj, 'GPI_BGSUB')
        object_name='GPI_BGSUB'    
    if ((plot_flux or plot_separatrix) and not flux_coordinates):
        print('Gathering MDSPlus EFIT data.')
        oplot_options={}
        if plot_separatrix:
            flap.get_data('NSTX_MDSPlus',
                          name='\EFIT01::\RBDRY',
                          exp_id=exp_id,
                          object_name='SEP X OBJ'
                          )
    
            flap.get_data('NSTX_MDSPlus',
                          name='\EFIT01::\ZBDRY',
                          exp_id=exp_id,
                          object_name='SEP Y OBJ'
                          )
            
            oplot_options['path']={'separatrix':{'Data object X':'SEP X OBJ',
                                                 'Data object Y':'SEP Y OBJ',
                                                 'Plot':True,
                                                 'Color':'red'}}
            
        if plot_flux:
            d=flap.get_data('NSTX_MDSPlus',
                            name='\EFIT02::\PSIRZ',
                            exp_id=exp_id,
                            object_name='PSI RZ OBJ'
                            )
            oplot_options['contour']={'flux':{'Data object':'PSI RZ OBJ',
                                              'Plot':True,
                                              'Colormap':None,
                                              'nlevel':51}}
        #oplot_options['line']={'trial':{'Horizontal':[[0.200,'red'],[0.250,'blue']],
        #                                'Vertical':[[1.450,'red'],[1.500,'blue']],
        #                                'Plot':True
        #                               }}
            
    else:
        oplot_options=None
        
    if flux_coordinates:
        print("**** Adding Flux r coordinates")
        d.add_coordinate(coordinates='Flux r',exp_id=exp_id)
        x_axis='Flux r'
        y_axis='Device z'
        if plot_separatrix:
            oplot_options={}
            oplot_options['line']={'separatrix':{'Vertical':[[1.0,'red']],
                                                 'Plot':True}}
    elif device_coordinates:
        x_axis='Device R'
        y_axis='Device z'
    else:
        x_axis='Image x'
        y_axis='Image y'
    if new_plot:
        plt.figure()
        
    if save_video:
        if time_range is not None:
            video_filename='NSTX_GPI_'+str(exp_id)+'_'+str(time_range[0])+'_'+str(time_range[1])+'.mp4'
        else:
            video_filename='NSTX_GPI_'+str(exp_id)+'_FULL.mp4'
    else:
        video_filename=None
        
    if video_saving_only:
        save_video=True
        
    if z_range is None:
        d=flap.get_data_object_ref(object_name, exp_id=exp_id)
        z_range=[d.data.min(),d.data.max()]
        
    if z_range[1] < 0:
        raise ValueError('All the values are negative, Logarithmic plotting is not allowed.')
        
    if logz and z_range[0] <= 0:
        print('Z range should not start with 0 when logarithmic Z axis is set. Forcing it to be 1 for now.')
        z_range[0]=1.    
    
    if not save_video:
        flap.plot(object_name,plot_type='animation',
                  exp_id=exp_id,
                  axes=[x_axis,y_axis,'Time'],
                  options={'Z range':z_range,'Wait':0.0,'Clear':False,
                           'Overplot options':oplot_options,
                           'Colormap':colormap,
                           'Log z':logz,
                           'Equal axes':True,
                           'Prevent saturation':prevent_saturation,
                           'Plot units':{'Time':'s',
                                         'Device R':'m',
                                         'Device z':'m'}
                           })
    else:
        if video_saving_only:
            import matplotlib
            current_backend=matplotlib.get_backend()
            matplotlib.use('agg')
            waittime=0.
        else:
            waittime=1./24.
            waittime=0.
        flap.plot(object_name,plot_type='anim-image',
                  exp_id=exp_id,
                  axes=[x_axis,y_axis,'Time'],
                  options={'Z range':z_range,'Wait':0.0,'Clear':False,
                           'Overplot options':oplot_options,
                           'Colormap':colormap,
                           'Equal axes':True,
                           'Waittime':waittime,
                           'Video file':video_filename,
                           'Video format':'mp4',
                           'Prevent saturation':prevent_saturation,
                           })
        if video_saving_only:
            import matplotlib
            matplotlib.use(current_backend)
Exemple #7
0
def show_nstx_gpi_video_frames(exp_id=None, 
                               time_range=None,
                               start_time=None,
                               n_frame=20,
                               logz=False,
                               z_range=[0,512],
                               plot_filtered=False, 
                               normalize=False,
                               cache_data=False, 
                               plot_flux=False, 
                               plot_separatrix=False, 
                               flux_coordinates=False,
                               device_coordinates=False,
                               new_plot=True,
                               save_pdf=False,
                               colormap='gist_ncar',
                               save_for_paraview=False,
                               colorbar_visibility=True
                               ):
    
    if time_range is None and start_time is None:
        print('time_range is None, the entire shot is plotted.')
    if time_range is not None:    
        if (type(time_range) is not list and len(time_range) != 2):
            raise TypeError('time_range needs to be a list with two elements.')
    if start_time is not None:
        if type(start_time) is not int and type(start_time) is not float:
            raise TypeError('start_time needs to be a number.')
    if not cache_data: #This needs to be enhanced to actually cache the data no matter what
        flap.delete_data_object('*')
    if exp_id is not None:
        print("\n------- Reading NSTX GPI data --------")
        if cache_data:
            try:
                d=flap.get_data_object_ref(exp_id=exp_id,object_name='GPI')
            except:
                print('Data is not cached, it needs to be read.')
                d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI')
        else:
            d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI')
        object_name='GPI'
    else:
        raise ValueError('The experiment ID needs to be set.')
    if time_range is None:
        time_range=[start_time,start_time+n_frame*2.5e-6]
    if normalize:
        
        flap.slice_data(object_name, 
                        slicing={'Time':flap.Intervals(time_range[0]-1/1e3*10,
                                                       time_range[1]+1/1e3*10)},
                        output_name='GPI_SLICED_FOR_FILTERING')
        
        norm_obj=flap.filter_data('GPI_SLICED_FOR_FILTERING',
                                  exp_id=exp_id,
                                  coordinate='Time',
                                  options={'Type':'Lowpass',
                                           'f_high':1e3,
                                           'Design':'Elliptic'},
                                  output_name='GAS_CLOUD')
        
        norm_obj.data=np.flip(norm_obj.data,axis=0)
        norm_obj=flap.filter_data('GAS_CLOUD',
                                  exp_id=exp_id,
                                  coordinate='Time',
                                  options={'Type':'Lowpass',
                                           'f_high':1e3,
                                           'Design':'Elliptic'},
                                 output_name='GAS_CLOUD')
        
        norm_obj.data=np.flip(norm_obj.data,axis=0)                
        coefficient=flap.slice_data('GAS_CLOUD',
                                    exp_id=exp_id,
                                    slicing={'Time':flap.Intervals(time_range[0],time_range[1])},
                                    output_name='GPI_GAS_CLOUD').data
                                    
        data_obj=flap.slice_data('GPI', 
                                 exp_id=exp_id,
                                 slicing={'Time':flap.Intervals(time_range[0],time_range[1])})
        data_obj.data = data_obj.data/coefficient
        flap.add_data_object(data_obj, 'GPI_SLICED_DENORM')
        object_name='GPI_SLICED_DENORM'
                        
    if plot_filtered:
        print("**** Filtering GPI")
        object_name='GPI_FILTERED'
        try:
            flap.get_data_object_ref(object_name, exp_id=exp_id)
        except:
            flap.filter_data(object_name,
                             exp_id=exp_id,
                             coordinate='Time',
                             options={'Type':'Highpass',
                                      'f_low':1e2,
                                      'Design':'Chebyshev II'},
                             output_name='GPI_FILTERED') #Data is in milliseconds                                
    if plot_flux or plot_separatrix:
        print('Gathering MDSPlus EFIT data.')
        oplot_options={}
        if plot_separatrix:
            flap.get_data('NSTX_MDSPlus',
                          name='\EFIT01::\RBDRY',
                          exp_id=exp_id,
                          object_name='SEP X OBJ'
                          )
    
            flap.get_data('NSTX_MDSPlus',
                          name='\EFIT01::\ZBDRY',
                          exp_id=exp_id,
                          object_name='SEP Y OBJ'
                          )
        if plot_flux:
            d=flap.get_data('NSTX_MDSPlus',
                            name='\EFIT01::\PSIRZ',
                            exp_id=exp_id,
                            object_name='PSI RZ OBJ'
                            )
        x_axis='Device R'
        y_axis='Device z'
    else:
        oplot_options=None
    if flux_coordinates:
        print("**** Adding Flux r coordinates")
        d.add_coordinate(coordinates='Flux r',exp_id=exp_id)
        x_axis='Flux r'
        y_axis='Device z'
    elif device_coordinates:
        x_axis='Device R'
        y_axis='Device z'
    if (not device_coordinates and 
        not plot_separatrix and 
        not flux_coordinates):
        x_axis='Image x'
        y_axis='Image y'        
    if start_time is not None:
        start_sample_num=flap.slice_data(object_name, 
                                         slicing={'Time':start_time}).coordinate('Sample')[0][0,0]
    if n_frame == 30:
        ny=6
        nx=5
    if n_frame == 20:
        ny=5
        nx=4
    gs=GridSpec(nx,ny)
    for index_grid_x in range(nx):
        for index_grid_y in range(ny):
            
            plt.subplot(gs[index_grid_x,index_grid_y])
            
            if start_time is not None:
                slicing={'Sample':start_sample_num+index_grid_x*ny+index_grid_y}
            else:
                time=time_range[0]+(time_range[1]-time_range[0])/(n_frame-1)*(index_grid_x*ny+index_grid_y)
                slicing={'Time':time}
            d=flap.slice_data(object_name, slicing=slicing, output_name='GPI_SLICED')
            slicing={'Time':d.coordinate('Time')[0][0,0]}
            if plot_flux:
                flap.slice_data('PSI RZ OBJ',slicing=slicing,output_name='PSI RZ SLICE',options={'Interpolation':'Linear'})
                oplot_options['contour']={'flux':{'Data object':'PSI RZ SLICE',
                                                  'Plot':True,
                                                  'Colormap':None,
                                                  'nlevel':51}}
                
            if plot_separatrix:
                flap.slice_data('SEP X OBJ',slicing=slicing,output_name='SEP X SLICE',options={'Interpolation':'Linear'})
                flap.slice_data('SEP Y OBJ',slicing=slicing,output_name='SEP Y SLICE',options={'Interpolation':'Linear'})
                oplot_options['path']={'separatrix':{'Data object X':'SEP X SLICE',
                                                     'Data object Y':'SEP Y SLICE',
                                                     'Plot':True,
                                                     'Color':'red'}}
            visibility=[True,True]
            if index_grid_x != nx-1:
                visibility[0]=False
            if index_grid_y != 0:
                visibility[1]=False
            flap.plot('GPI_SLICED',
                      plot_type='contour',
                      exp_id=exp_id,
                      axes=[x_axis,y_axis,'Time'],
                      options={'Z range':z_range,
                               'Interpolation': 'Closest value',
                               'Clear':False,
                               'Equal axes':True,
                               'Plot units':{'Device R':'m',
                                             'Device z':'m'},
                               'Axes visibility':visibility,
                               'Colormap':colormap,
                               'Colorbar':colorbar_visibility,
                               'Overplot options':oplot_options,
                               },
                       plot_options={'levels':255},
                       )
            actual_time=d.coordinate('Time')[0][0,0]
            #plt.title(str(exp_id)+' @ '+f"{actual_time*1000:.4f}"+'ms')
            plt.title(f"{actual_time*1000:.3f}"+'ms')
    if save_pdf:
        if time_range is not None:
            plt.savefig('NSTX_GPI_video_frames_'+str(exp_id)+'_'+str(time_range[0])+'_'+str(time_range[1])+'_nf_'+str(n_frame)+'.pdf')
        else:
            plt.savefig('NSTX_GPI_video_frames_'+str(exp_id)+'_'+str(start_time)+'_nf_'+str(n_frame)+'.pdf')
Exemple #8
0
def calculate_nstx_gpi_crosscorrelation(exp_id=None,
                                        time_range=None,
                                        add_flux=None,
                                        reference_pixel=None,
                                        reference_flux=None,
                                        reference_position=None,
                                        reference_area=None,
                                        filter_low=None,
                                        filter_high=None,
                                        filter_design='Chebyshev II',
                                        trend=['Poly',2],
                                        frange=None,
                                        taurange=[-500e-6,500e-6],
                                        taures=2.5e-6,
                                        interval_n=11,
                                        filename=None,
                                        options=None,
                                        cache_data=False,
                                        normalize_signal=False,
                                        normalize=True,           #Calculate correlation if True (instead of covariance)
                                        plot=False,
                                        plot_acf=False,
                                        axes=['Image x', 'Image y', 'Time lag']
                                        ):
    
    if time_range is None:
        print('The time range needs to set for the calculation.')
        print('There is no point of calculating the entire time range.')
        return
    else:    
        if (type(time_range) is not list and len(time_range) != 2):
            raise TypeError('time_range needs to be a list with two elements.')
    if exp_id is not None:
        print("\n------- Reading NSTX GPI data --------")
        if cache_data:
            try:
                d=flap.get_data_object_ref(exp_id=exp_id,object_name='GPI')
            except:
                print('Data is not cached, it needs to be read.')
                d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI')
        else:
            d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI')
    else:
        raise ValueError('The experiment ID needs to be set.')
        
    if reference_flux is not None or add_flux:
        d.add_coordinate(coordinates='Flux r',exp_id=exp_id)
    
    #Normalize the data for the maximum cloud distribution
    if normalize_signal:
        normalizer=flap_nstx.analysis.calculate_nstx_gpi_norm_coeff(exp_id=exp_id,             # Experiment ID
                                                                     f_high=1e2,                # Low pass filter frequency in Hz
                                                                     design=filter_design,      # IIR filter design (from scipy)
                                                                     test=False,                # Testing input
                                                                     filter_data=True,          # IIR LPF the data
                                                                     time_range=None,           # Timer range for the averaging in ms [t1,t2]
                                                                     calc_around_max=False,     # Calculate the average around the maximum of the GPI signal
                                                                     time_window=50.,           # The time window for the calc_around_max calculation
                                                                     cache_data=True,           
                                                                     verbose=False,
                                                                     )
        d.data = d.data/normalizer.data #This should be checked to some extent, it works with smaller matrices
    
    #SLicing data to the input time range    
    flap.slice_data('GPI',exp_id=exp_id,
                    slicing={'Time':flap.Intervals(time_range[0],time_range[1])},
                    output_name='GPI_SLICED')
    
    #Filtering the signal since we are in time-space not frequency space
    if frange is not None:
        filter_low=frange[0]
        filter_high=frange[1]
    if filter_low is not None or filter_high is not None:
        if filter_low is not None and filter_high is None:
            filter_type='Highpass'
        if filter_low is None and filter_high is not None:
            filter_type='Lowpass'
        if filter_low is not None and filter_high is not None:
            filter_type='Bandpass'

        flap.filter_data('GPI_SLICED',exp_id=exp_id,
                         coordinate='Time',
                         options={'Type':filter_type,
                                  'f_low':filter_low,
                                  'f_high':filter_high, 
                                  'Design':filter_design},
                         output_name='GPI_SLICED_FILTERED')

    if reference_pixel is None and reference_position is None and reference_flux is None:
        calculate_acf=True
    else:
        calculate_acf=False
                
    if not calculate_acf:
        flap_nstx.analysis.calculate_nstx_gpi_reference('GPI_SLICED_FILTERED', exp_id=exp_id,
                                                         reference_pixel=reference_pixel,
                                                         reference_area=reference_area,
                                                         reference_position=reference_position,
                                                         reference_flux=reference_flux,
                                                         output_name='GPI_REF')
        
        flap.ccf('GPI_SLICED_FILTERED',exp_id=exp_id,
                  ref='GPI_REF',
                  coordinate='Time',
                  options={'Resolution':taures,
                           'Range':taurange,
                           'Trend':trend,
                           'Interval':interval_n,
                           'Normalize':normalize,
                           },
                   output_name='GPI_CCF')
        
    if plot:
        if not plot_acf:
            object_name='GPI_CCF'
        else:
            object_name='GPI_ACF'
            
            flap.plot(object_name, exp_id=exp_id,
                      plot_type='animation', 
                      axes=axes, 
                      options={'Plot units': {'Time lag':'us'}, 
                               'Z range':[0,1]},)