Exemple #1
0
def test_resample():
    plt.close('all')
    print()
    print(">>>>>>>>>>>>> Test signal resampling (interpolation) <<<<<<<<<<<")
    flap.delete_data_object('*')
    print(
        "**** Generating two test signals with different sampling frequency.")
    flap.get_data('TESTDATA',
                  name='TEST-1-1',
                  options={
                      'Scaling': 'Volt',
                      'Frequency': 1e3,
                      'Samplerate': 1e6
                  },
                  object_name='TEST-1MHz',
                  coordinates={'Time': [0, 0.001]})
    flap.get_data('TESTDATA',
                  name='TEST-1-1',
                  options={
                      'Scaling': 'Volt',
                      'Frequency': 1.5e3,
                      'Samplerate': 3e6
                  },
                  object_name='TEST-3MHz',
                  coordinates={'Time': [0, 0.001]})
    print("\n***** Resampling from lower to higher frequency.")
    plt.figure()
    flap.plot('TEST-1MHz', axes='Time', plot_options={'marker': 'o'})
    flap.plot('TEST-3MHz', plot_options={'marker': 'o'})
    flap.slice_data('TEST-1MHz',
                    slicing={'Time': flap.get_data_object('TEST-3MHz')},
                    options={'Interpol': 'Linear'},
                    output_name='TEST-1MHz_resample')
    flap.plot('TEST-1MHz_resample', plot_options={'marker': 'x'})

    print("\n***** Resampling from higher to lower frequency.")
    plt.figure()
    flap.plot('TEST-1MHz', axes='Time', plot_options={'marker': 'o'})
    flap.plot('TEST-3MHz', plot_options={'marker': 'o'})
    flap.slice_data('TEST-3MHz',
                    slicing={'Time': flap.get_data_object('TEST-1MHz')},
                    options={'Interpol': 'Linear'},
                    output_name='TEST-3MHz_resample')
    flap.plot('TEST-3MHz_resample', plot_options={'marker': 'x'})

    print("\n***** Cutting parts.")
    plt.figure()
    flap.slice_data(
        'TEST-1MHz',
        slicing={'Time': flap.Intervals([1e-4, 5e-4], [2e-4, 7e-4])},
        options={'Slice': 'Simple'},
        output_name='TEST-1MHz_parts')
    flap.plot('TEST-1MHz_parts', axes='Time', plot_options={'marker': 'o'})
    flap.list_data_objects()
Exemple #2
0
def plot_results_for_paper():
    pearson=False
    wd=flap.config.get_all_section('Module NSTX_GPI')['Working directory']
    #Figure 1
    '''NO CODE IS NEEDED'''
    #Figure 2
    '''NO CODE IS NEEDED'''
    
    #Figure 3
    from flap_nstx.analysis import show_nstx_gpi_video_frames
    #fig, ax = plt.subplots(figsize=(6.5,5))
    if plot[3]:
        gs=GridSpec(5,2)
        ax,fig=plt.subplots(figsize=(8.5/2.54,6))
        pdf=PdfPages(wd+'/plots/figure_3_139901_basic_plots.pdf')
        
        plt.subplot(gs[0,0])
        flap.get_data('NSTX_MDSPlus',
                      name='\WF::\DALPHA',
                      exp_id=139901,
                      object_name='DALPHA').plot(options={'Axes visibility':[False,True]})
        plt.xlim([0,1.2])
        plt.subplot(gs[1,0])
        flap.get_data('NSTX_GPI',
                      name='',
                      exp_id=139901,
                      object_name='GPI').slice_data(summing={'Image x':'Mean', 'Image y':'Mean'}).plot(options={'Axes visibility':[False,True]})
        plt.xlim([0,1.2])
        
        plt.xlim([0,1.2])
        plt.subplot(gs[2,0])
        flap.get_data('NSTX_MDSPlus',
                      name='IP',
                      exp_id=139901,
                      object_name='IP').plot(options={'Axes visibility':[False,True]})
        plt.xlim([0,1.2])
        plt.subplot(gs[3,0])
        d=flap_nstx_thomson_data(exp_id=139901, density=True, output_name='DENSITY')
        dR = d.coordinate('Device R')[0][:,:]-np.insert(d.coordinate('Device R')[0][0:-1,:],0,0,axis=0)
        LID=np.sum(d.data*dR,axis=0)
        plt.plot(d.coordinate('Time')[0][0,:],LID)
        plt.title('Line integrated density')
        plt.xlabel('Time [s]')
        plt.ylabel('n_e [m^-2]')
        plt.xlim([0,1.2])
        ax=plt.gca()
        ax.get_xaxis().set_visible(False)
        
        plt.subplot(gs[4,0])
        magnetics=flap.get_data('NSTX_MDSPlus',
                                name='\OPS_PC::\\BDOT_L1DMIVVHF5_RAW',
                                exp_id=139901,
                                object_name='MIRNOV')
        
        magnetics.coordinates.append(copy.deepcopy(flap.Coordinate(name='Time equi',
                                           unit='s',
                                           mode=flap.CoordinateMode(equidistant=True),
                                           shape = [],
                                           start=magnetics.coordinate('Time')[0][0],
                                           step=magnetics.coordinate('Time')[0][1]-magnetics.coordinate('Time')[0][0],
                                           dimension_list=[0])))
        
        magnetics.filter_data(coordinate='Time equi',
                              options={'Type':'Bandpass',
                                       'f_low':100e3,
                                       'f_high':500e3,
                                       'Design':'Elliptic'}).plot()
        plt.xlim([0,1.2])
        
        plt.subplot(gs[0,1])
        flap.get_data('NSTX_MDSPlus',
                      name='\WF::\DALPHA',
                      exp_id=139901,
                      object_name='DALPHA').plot(options={'Axes visibility':[False,False]})
        plt.xlim([0.25,0.4])
        plt.subplot(gs[1,1])
        flap.get_data('NSTX_GPI',
                      name='',
                      exp_id=139901,
                      object_name='GPI').slice_data(summing={'Image x':'Mean', 'Image y':'Mean'}).plot(options={'Axes visibility':[False,False]})
        plt.xlim([0.25,0.4])
        
        plt.subplot(gs[2,1])
        flap.get_data('NSTX_MDSPlus',
                      name='IP',
                      exp_id=139901,
                      object_name='IP').plot(options={'Axes visibility':[False,False]})
        plt.xlim([0.25,0.4])
        
        plt.subplot(gs[3,1])
        d=flap_nstx_thomson_data(exp_id=139901, density=True, output_name='DENSITY')
        dR = d.coordinate('Device R')[0][:,:]-np.insert(d.coordinate('Device R')[0][0:-1,:],0,0,axis=0)
        LID=np.sum(d.data*dR,axis=0)
        plt.plot(d.coordinate('Time')[0][0,:],LID)
        plt.title('Line integrated density')
        plt.xlabel('Time [s]')
        plt.ylabel('n_e [m^-2]')
        plt.xlim([0.25,0.4])
        ax=plt.gca()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
        
        plt.subplot(gs[4,1])
        magnetics=flap.get_data('NSTX_MDSPlus',
                                name='\OPS_PC::\\BDOT_L1DMIVVHF5_RAW',
                                exp_id=139901,
                                object_name='MIRNOV')
        
        magnetics.coordinates.append(copy.deepcopy(flap.Coordinate(name='Time equi',
                                           unit='s',
                                           mode=flap.CoordinateMode(equidistant=True),
                                           shape = [],
                                           start=magnetics.coordinate('Time')[0][0],
                                           step=magnetics.coordinate('Time')[0][1]-magnetics.coordinate('Time')[0][0],
                                           dimension_list=[0])))
        
        magnetics.filter_data(coordinate='Time equi',
                              options={'Type':'Bandpass',
                                       'f_low':100e3,
                                       'f_high':500e3,
                                       'Design':'Elliptic'}).plot(slicing={'Time':flap.Intervals(0.25,0.4)})
    
        plt.xlim([0.25,0.4])
        ax=plt.gca()
        ax.get_yaxis().set_visible(False)
        pdf.savefig()
        pdf.close()
        
    if plot[4]:
        plt.figure()
        ax,fig=plt.subplots(figsize=(3.35*2,5.5))
        pdf=PdfPages(wd+'/plots/figure_5_139901_0.3249158_30_frame.pdf')
        show_nstx_gpi_video_frames(exp_id=139901, 
                                   start_time=0.3249158,
                                   n_frame=30,
                                   logz=False,
                                   z_range=[0,3900],
                                   plot_filtered=False, 
                                   normalize=False,
                                   cache_data=False, 
                                   plot_flux=False, 
                                   plot_separatrix=True, 
                                   flux_coordinates=False,
                                   device_coordinates=True,
                                   new_plot=False,
                                   save_pdf=True,
                                   colormap='gist_ncar',
                                   save_for_paraview=False,
                                   colorbar_visibility=True
                                   )
        pdf.savefig()
        pdf.close()
    #Figure 5
    if plot[5] or plot[6] or plot[7]:
        try:
            d1,d2,d3,d4=pickle.load(open(wd+'/processed_data/fig_6_8_flap_object.pickle','rb'))
            flap.add_data_object(d1, 'GPI_SLICED_FULL')
            flap.add_data_object(d2, 'GPI_GAS_CLOUD')
            flap.add_data_object(d3, 'GPI_SLICED_DENORM_CCF_VEL')
            flap.add_data_object(d4, 'GPI_CCF_F_BY_F')
        except:
            calculate_nstx_gpi_avg_frame_velocity(exp_id=139901, 
                                                  time_range=[0.325-1e-3,0.325+1e-3], 
                                                  plot=False,
                                                  subtraction_order_for_velocity=1,
                                                  skip_structure_calculation=False,
                                                  correlation_threshold=0.,
                                                  pdf=False, 
                                                  nlevel=51, 
                                                  nocalc=False, 
                                                  filter_level=3, 
                                                  normalize_for_size=True,
                                                  normalize_for_velocity=True,
                                                  threshold_coeff=1.,
                                                  normalize_f_high=1e3, 
                                                  
                                                  normalize='roundtrip', 
                                                  velocity_base='cog', 
                                                  return_results=False, 
                                                  plot_gas=True)
            pickle.dump((flap.get_data_object('GPI_SLICED_FULL'),
                         flap.get_data_object('GPI_GAS_CLOUD'),
                         flap.get_data_object('GPI_SLICED_DENORM_CCF_VEL'),
                         flap.get_data_object('GPI_CCF_F_BY_F')), open(wd+'/processed_data/fig_6_8_flap_object.pickle','wb'))
    if plot[5]:
        pdf=PdfPages(wd+'/plots/figure_6_normalization.pdf')
        times=[0.3245,0.3249560,0.3255]
        signals=['GPI_SLICED_FULL',
                 'GPI_GAS_CLOUD',
                 'GPI_SLICED_DENORM_CCF_VEL']
        gs=GridSpec(3,3)
        plt.figure()                
        ax,fig=plt.subplots(figsize=(3.35,4))
        titles=['Raw frame', 'Gas cloud', 'Normalized']
        for index_grid_x in range(3):
            for index_grid_y in range(3):
                plt.subplot(gs[index_grid_x,index_grid_y])
                visibility=[True,True]
                if index_grid_x != 3-1:
                    visibility[0]=False
                if index_grid_y != 0:
                    visibility[1]=False
#                if index_grid_x == 0:
#                    z_range=[0,4096]
#                elif index_grid_x == 1:
#                    z_range=[0,400]
#                elif index_grid_x == 2:
#                    z_range=[0,40]
                z_range=None
                flap.plot(signals[index_grid_x], 
                          plot_type='contour', 
                          slicing={'Time':times[index_grid_y]}, 
                          axes=['Image x', 'Image y'],
                          options={'Z range':z_range,
                                   'Interpolation': 'Closest value',
                                   'Clear':False,
                                   'Equal axes':True,
                                   'Axes visibility':visibility,
                                   #'Colormap':'gist_ncar',
                                   'Colorbar':True,
                                   #'Overplot options':oplot_options,
                                   },
                           plot_options={'levels':51},
                           )
                if index_grid_x == 0:
                    #ax=plt.gca()
                    plt.title(f"{times[index_grid_y]*1e3:.3f}"+' '+titles[index_grid_x])
                else:
                    plt.title(titles[index_grid_x])
    
        pdf.savefig()
        pdf.close()
    
    #Figure 6
    if plot[6]:
        flap.get_data('NSTX_GPI',exp_id=139901,
                      name='',
                      object_name='GPI')
        flap.slice_data('GPI', slicing={'Time':flap.Intervals(0.3245,0.3255)}, output_name='GPI_SLICED_FULL')
        data_object_name='GPI_SLICED_DENORM_CCF_VEL'
        detrended=flap_nstx.analysis.detrend_multidim(data_object_name,
                                                      exp_id=139901,
                                                      order=4, 
                                                      coordinates=['Image x', 'Image y'], 
                                                      output_name='GPI_DETREND_VEL')
        
        d=copy.deepcopy(flap.get_data_object(data_object_name))
        
        d.data=d.data-detrended.data
        flap.add_data_object(d,'GPI_TREND')
        
        signals=[data_object_name,
                 'GPI_TREND',
                 'GPI_DETREND_VEL']
        
        pdf=PdfPages(wd+'/plots/figure_7_trend_subtraction.pdf')

        gs=GridSpec(1,3)
        plt.figure()                
        ax,fig=plt.subplots(figsize=(8.5/2.54,2))
        for index_grid_x in range(3):
            plt.subplot(gs[index_grid_x])
            visibility=[True,True]
            if index_grid_x != 0:
                visibility[1]=False
            z_range=[0,10]
            colorbar=False
            flap.plot(signals[index_grid_x], 
                      plot_type='contour', 
                      slicing={'Time':0.3249560},
                      #slicing={'Sample':29808},
                      axes=['Image x', 'Image y'],
                      options={'Interpolation': 'Closest value',
                               'Clear':False,
                               'Equal axes':True,
                               'Axes visibility':visibility,
                               #'Colormap':colormap,
                               'Colorbar':True,
                               #'Overplot options':oplot_options,
                               },
                       plot_options={'levels':51},
                           )
        #fig.tight_layout()
        pdf.savefig()
        pdf.close()
        
    #Figure 7
    if plot[7]:
        pdf=PdfPages(wd+'/plots/figure_8_CCF_frame_by_frame.pdf')
        gs=GridSpec(1,3)
        plt.figure()                
        ax,fig=plt.subplots(figsize=(8.5/2.54,2))
        plt.subplot(gs[0])
        flap.plot('GPI_SLICED_FULL', 
                  plot_type='contour', 
                  slicing={'Sample':29806}, 
                  axes=['Image x', 'Image y'],
                  options={
                          'Z range':[0,4096],
                           'Interpolation': 'Closest value',
                           'Clear':False,
                           'Equal axes':True,
                           'Axes visibility':[True,True],
                           'Colormap':'gist_ncar',
                           'Colorbar':False,
                           #'Overplot options':oplot_options,
                           },
                   plot_options={'levels':51},
                       )
        plt.title("324.959ms")
        plt.subplot(gs[1])
        flap.plot('GPI_SLICED_FULL', 
                   plot_type='contour', 
                   slicing={'Sample':29807}, 
                   axes=['Image x', 'Image y'],
                   options={'Z range':[0,4096],
                            'Interpolation': 'Closest value',
                            'Clear':False,
                            'Equal axes':True,
                            'Axes visibility':[True,False],
                            'Colorbar':False,
                            'Colormap':'gist_ncar',
                            },
                    plot_options={'levels':51},
                    )
        plt.title("324.961ms")
        plt.subplot(gs[2])
        flap.plot('GPI_CCF_F_BY_F', 
                  plot_type='contour', 
                  slicing={'Sample':29807, 'Image x':flap.Intervals(-10,10),'Image y':flap.Intervals(-10,10)}, 
                  
                  axes=['Image x', 'Image y'],
                  options={
                          #'Z range':[0,2048],
                           'Interpolation': 'Closest value',
                           'Clear':False,
                           'Equal axes':True,
                           'Axes visibility':[True,True],
                           #'Colormap':colormap,
                           'Colorbar':True,
                           #'Overplot options':oplot_options,
                           },
                   plot_options={'levels':51},
                       )
        plt.title("CCF")
        pdf.savefig()
        pdf.close()
        
    #Figure 8
    if plot[8]:
    #2x2 frames with the found structures during an ELM burst
        calculate_nstx_gpi_avg_frame_velocity(exp_id=139901, 
                                              time_range=[0.32495,0.325], 
                                              plot=False,
                                              subtraction_order_for_velocity=4,
                                              skip_structure_calculation=False,
                                              correlation_threshold=0.5,
                                              pdf=True, 
                                              nlevel=51, 
                                              nocalc=False, 
                                              filter_level=5, 
                                              normalize_for_size=True,
                                              normalize_for_velocity=True,
                                              threshold_coeff=1.,
                                              normalize_f_high=1e3, 
                                              normalize='roundtrip', 
                                              velocity_base='cog', 
                                              return_results=False, 
                                              plot_gas=True,
                                              structure_pixel_calc=True,
                                              structure_pdf_save=True,
                                              test_structures=True
                                              )
        #Post processing done with illustrator
        
    #Figure 9
    if plot[9]:
    #2x3
    #Synthetic GPI signal
    #Postprocessing done with illustrator
        nstx_gpi_generate_synthetic_data(exp_id=1, 
                                         time=0.0001, 
                                         amplitude=1.0, 
                                         output_name='test', 
                                         poloidal_velocity=3e3, 
                                         radial_velocity=0., 
                                         poloidal_size=0.10,
                                         radial_size=0.05, 
                                         waveform_divider=1, 
                                         sinusoidal=True)
        d=flap.get_data_object('test', exp_id=1)
        d.data=d.data-np.mean(d.data,axis=0)
        calculate_nstx_gpi_avg_frame_velocity(data_object='test',
                                              exp_id=1,
                                              time_range=[0.000000,0.00005],
                                              plot=False,
                                              subtraction_order_for_velocity=1,
                                              skip_structure_calculation=False,
                                              correlation_threshold=0.5,
                                              pdf=True, 
                                              nlevel=51, 
                                              nocalc=False, 
                                              filter_level=5, 
                                              normalize_for_size=False,
                                              normalize_for_velocity=False,
                                              threshold_coeff=1.,
                                              normalize_f_high=1e3, 
                                              normalize=None, 
                                              velocity_base='cog', 
                                              return_results=False, 
                                              plot_gas=False,
                                              structure_pixel_calc=True,
                                              structure_pdf_save=True,
                                              test_structures=True
                                              )
    #Figure 10
    if plot[10]:
    #Single shot results
        calculate_nstx_gpi_avg_frame_velocity(exp_id=139901, 
                                              time_range=[0.325-2e-3,0.325+2e-3], 
                                              plot_time_range=[0.325-0.5e-3,0.325+0.5e-3],
                                              plot=True,
                                              subtraction_order_for_velocity=4,
                                              skip_structure_calculation=False,
                                              correlation_threshold=0.6,
                                              pdf=True, 
                                              nlevel=51, 
                                              nocalc=True, 
                                              gpi_plane_calculation=True,
                                              filter_level=5, 
                                              normalize_for_size=True,
                                              normalize_for_velocity=True,
                                              threshold_coeff=1.,
                                              normalize_f_high=1e3, 
                                              normalize='roundtrip', 
                                              velocity_base='cog', 
                                              return_results=False, 
                                              plot_gas=True,
                                              plot_for_publication=True,
                                              plot_scatter=False,
                                              overplot_average=False,
                                              overplot_str_vel=False)
    #2x3
    #Done with Illustrator
    #Figure 12
    if plot[11]:
    #Conditional averaged results
        calculate_avg_velocity_results(pdf=True, 
                                       plot=True, 
                                       plot_max_only=True,
                                       plot_for_publication=True,
                                       normalized_velocity=True, 
                                       subtraction_order=4, 
                                       normalized_structure=True, 
                                       opacity=0.5, 
                                       correlation_threshold=0.6,
                                       gpi_plane_calculation=True,
                                       plot_scatter=False)
    #Post processing done with Illustrator
    
    #Figure 11
    if plot[12]:
        if pearson:
            pdf=PdfPages(wd+'/plots/figure_13_pearson_matrix.pdf')
            pearson=calculate_nstx_gpi_correlation_matrix(calculate_average=False,
                                                          gpi_plane_calculation=True,
                                                          window_average=0.050e-3,
                                                          elm_burst_window=True)
            data=pearson[:,:,0]
            variance=pearson[:,:,1]
            data[10,10]=-1
            plt.figure()
            plt.subplots(figsize=(8.5/2.54,8.5/2.54/1.618))
            plt.matshow(data, cmap='seismic')
            plt.xticks(ticks=np.arange(11), labels=['Velocity ccf R',       #0,1
                                                    'Velocity ccf z',       #0,1
                                                    'Velocity str max R',   #2,3
                                                    'Velocity str max z',   #2,3
                                                    'Size max R',           #4,5
                                                    'Size max z',           #4,5
                                                    'Position max R',       #6,7
                                                    'Position max z',       #6,7
                                                    'Area max',           #8
                                                    'Elongation max',     #9
                                                    'Angle max'], rotation='vertical')
            plt.yticks(ticks=np.arange(11), labels=['Velocity ccf R',       #0,1
                                                    'Velocity ccf z',       #0,1
                                                    'Velocity str max R',   #2,3
                                                    'Velocity str max z',   #2,3
                                                    'Size max R',           #4,5
                                                    'Size max z',           #4,5
                                                    'Position max R',       #6,7
                                                    'Position max z',       #6,7
                                                    'Area max',           #8
                                                    'Elongation max',     #9
                                                    'Angle max'])
            plt.colorbar()
            plt.show()
            pdf.savefig()
            plt.figure()
            plt.subplots(figsize=(8.5/2.54,8.5/2.54/1.618))
            variance[10,10]=-1
            variance[9,9]=1
            plt.matshow(variance, cmap='seismic')
            #plt.matshow(data, cmap='gist_ncar')
            plt.xticks(ticks=np.arange(11), labels=['Velocity ccf R',       #0,1
                                                    'Velocity ccf z',       #0,1
                                                    'Velocity str max R',   #2,3
                                                    'Velocity str max z',   #2,3
                                                    'Size max R',           #4,5
                                                    'Size max z',           #4,5
                                                    'Position max R',       #6,7
                                                    'Position max z',       #6,7
                                                    'Area max',           #8
                                                    'Elongation max',     #9
                                                    'Angle max'], rotation='vertical')
            plt.yticks(ticks=np.arange(11), labels=['Velocity ccf R',       #0,1
                                                    'Velocity ccf z',       #0,1
                                                    'Velocity str max R',   #2,3
                                                    'Velocity str max z',   #2,3
                                                    'Size max R',           #4,5
                                                    'Size max z',           #4,5
                                                    'Position max R',       #6,7
                                                    'Position max z',       #6,7
                                                    'Area max',           #8
                                                    'Elongation max',     #9
                                                    'Angle max'])
            plt.colorbar()
            plt.show()
            pdf.savefig()
            pdf.close()
            
        else:
            pdf=PdfPages(wd+'/plots/figure_13_dependence.pdf')
            plt.figure()
            plt.subplots(figsize=(17/2.54,17/2.54/1.618))
            plot_all_parameters_vs_all_other_average(window_average=0.2e-3, symbol_size=0.3, plot_error=True)
            pdf.savefig()
            pdf.close()
Exemple #3
0
                      slicing={'Time': 0.324 + 2.5e-6},
                      output_name='GPI_RAW_SLICED_2')
trend1 = flap_nstx.analysis.detrend_multidim(
    data_object='GPI_RAW_SLICED',
    coordinates=['Image x', 'Image y'],
    order=order,
    output_name='GPI_RAW_SLICED_DETREND',
    return_trend=True)
trend2 = flap_nstx.analysis.detrend_multidim(
    data_object='GPI_RAW_SLICED_2',
    coordinates=['Image x', 'Image y'],
    order=order,
    output_name='GPI_RAW_SLICED_2_DETREND',
    return_trend=True)

d = flap.get_data_object('GPI_RAW_SLICED')
d1 = flap.get_data_object_ref('GPI_RAW_SLICED_DETREND')
d2 = flap.get_data_object_ref('GPI_RAW_SLICED_2_DETREND')

plt.figure()
flap.plot('GPI_RAW_SLICED',
          plot_type='contour',
          axes=['Image x', 'Image y'],
          plot_options={'levels': 51})
plt.title('139901 GPI @ 0.324s')
plt.savefig('139901_0.324s_orig_frame.pdf')

plt.figure()
flap.plot('GPI_RAW_SLICED_DETREND',
          plot_type='contour',
          axes=['Image x', 'Image y'],
Exemple #4
0
def test_image():
    plt.close('all')
    print()
    print('>>>>>>>>>>>>>>>>>>> Test image <<<<<<<<<<<<<<<<<<<<<<<<')
    flap.delete_data_object('*')
    print("**** Generating a sequence of test images")
    flap.get_data('TESTDATA',
                  name='VIDEO',
                  object_name='TEST_VIDEO',
                  options={
                      'Length': 0.1,
                      'Samplerate': 1e3,
                      'Frequency': 10,
                      'Spotsize': 100
                  })
    flap.list_data_objects()
    print("***** Showing one image")
    plt.figure()
    flap.plot('TEST_VIDEO',
              slicing={'Time': 30e-3 / 4},
              plot_type='image',
              axes=['Image x', 'Image y'],
              options={'Clear': True})
    plt.figure()
    print("**** Showing a sequence of images and saving to test_video.avi")
    flap.plot('TEST_VIDEO',
              plot_type='anim-image',
              axes=['Image x', 'Image y', 'Time'],
              options={
                  'Z range': [0, 4095],
                  'Wait': 0.01,
                  'Clear': True,
                  'Video file': 'test_video.avi',
                  'Colorbar': True,
                  'Aspect ratio': 'equal'
              })
    plt.figure()
    print(
        "*** Showing the same images as contour plots and saving to test_video_contour.avi"
    )
    flap.plot('TEST_VIDEO',
              plot_type='anim-contour',
              axes=['Image x', 'Image y', 'Time'],
              options={
                  'Z range': [0, 4095],
                  'Wait': 0.01,
                  'Clear': True,
                  'Video file': 'test_video_contour.avi',
                  'Colorbar': False
              })
    print("*** Converting data object x, y coordinates to non-equidistant.")
    d = flap.get_data_object('TEST_VIDEO')
    coord_x = d.get_coordinate_object('Image x')
    index = [0] * 3
    index[coord_x.dimension_list[0]] = ...
    x = np.squeeze(d.coordinate('Image x', index=index)[0])
    coord_x.mode.equidistant = False
    coord_x.values = x
    coord_x.shape = x.shape
    coord_y = d.get_coordinate_object('Image y')
    index = [0] * 3
    index[coord_y.dimension_list[0]] = ...
    y = np.squeeze(d.coordinate('Image y', index=index)[0])
    coord_y.mode.equidistant = False
    coord_y.values = y
    coord_y.shape = y.shape
    flap.add_data_object(d, "TEST_VIDEO_noneq")
    flap.list_data_objects()
    plt.figure()
    print("**** Showing this video and saving to  test_video_noneq.avi")
    flap.plot('TEST_VIDEO_noneq',
              plot_type='anim-image',
              axes=['Image x', 'Image y', 'Time'],
              options={
                  'Z range': [0, 4095],
                  'Wait': 0.01,
                  'Clear': True,
                  'Video file': 'test_video_noneq.avi',
                  'Colorbar': True,
                  'Aspect ratio': 'equal'
              })
Exemple #5
0
def nstx_gpi_velocity_analysis_spatio_temporal_displacement(exp_id=None,                          #Shot number
                                                            time_range=None,                      #The time range for the calculation
                                                            data_object=None,                     #Input data object if available from outside (e.g. generated sythetic signal)
                                                            x_range=[0,63],                       #X range for the calculation
                                                            y_range=[0,79],                       #Y range for the calculation
                                                            x_search=10,
                                                            y_search=10,
                                                            
                                                            fbin=10,
                                                            plot=True,                            #Plot the results
                                                            pdf=False,                            #Print the results into a PDF
                                                            plot_error=False,                     #Plot the errorbars of the velocity calculation based on the line fitting and its RMS error
                                                          
                                                            #File input/output options
                                                            filename=None,                        #Filename for restoring data
                                                            nocalc=True,                          #Restore the results from the .pickle file from filename+.pickle
                                                            return_results=False,
                                                            ):
        
    #Constants for the calculation
    #Using the spatial calibration to find the actual velocities.
    coeff_r=np.asarray([3.7183594,-0.77821046,1402.8097])/1000. #The coordinates are in meters, the coefficients are in mm
    coeff_z=np.asarray([0.18090118,3.0657776,70.544312])/1000.  #The coordinates are in meters, the coefficients are in mm
    
    #Input error handling
    if exp_id is None and data_object is None:
        raise ValueError('Either exp_id or data_object needs to be set for the calculation.')
        
    if data_object is None:
        if time_range is None and filename is None:
            raise ValueError('It takes too much time to calculate the entire shot, please set a time_range.')
        else:    
            if type(time_range) is not list and filename is None:
                raise TypeError('time_range is not a list.')
            if filename is None and len(time_range) != 2:
                raise ValueError('time_range should be a list of two elements.')
    
    if data_object is not None and type(data_object) == str:
        if exp_id is None:
            exp_id='*'
        d=flap.get_data_object(data_object,exp_id=exp_id)
        time_range=[d.coordinate('Time')[0][0,0,0],
                    d.coordinate('Time')[0][-1,0,0]]
        exp_id=d.exp_id
        flap.add_data_object(d, 'GPI_SLICED_FULL')
               
    if filename is None:
        wd=flap.config.get_all_section('Module NSTX_GPI')['Working directory']
        comment=''
        filename=flap_nstx.analysis.filename(exp_id=exp_id,
                                             working_directory=wd+'/processed_data',
                                             time_range=time_range,
                                             purpose='sz velocity',
                                             comment=comment)
        
    pickle_filename=filename+'.pickle'
    if not os.path.exists(pickle_filename) and nocalc:
        print('The pickle file cannot be loaded. Recalculating the results.')
        nocalc=False

    if nocalc is False:
        slicing={'Time':flap.Intervals(time_range[0],time_range[1])}
        #Read data
        if data_object is None:
            print("\n------- Reading NSTX GPI data --------")
            d=flap.get_data('NSTX_GPI',exp_id=exp_id,
                            name='',
                            object_name='GPI')
          
            d=flap.slice_data('GPI',exp_id=exp_id, 
                              slicing=slicing,
                              output_name='GPI_SLICED_FULL')
            d.data=np.asarray(d.data, dtype='float32')
            
        count=d.data.shape[0]
        vpol_p     = np.zeros([x_range[1]-x_range[0]+1,y_range[1]-y_range[0]+1,count])    #  poloidal velocity in km/sec vs. pixel
        vrad_p     = np.zeros([x_range[1]-x_range[0]+1,y_range[1]-y_range[0]+1,count])    #  radial velocity vs. pixel
        vpol_n     = np.zeros([x_range[1]-x_range[0]+1,y_range[1]-y_range[0]+1,count])    #  poloidal velocity in km/sec vs. pixel
        vrad_n     = np.zeros([x_range[1]-x_range[0]+1,y_range[1]-y_range[0]+1,count])    #  radial velocity vs. pixel  
        
        vpol = np.zeros([x_range[1]-x_range[0]+1,y_range[1]-y_range[0]+1,count])
        vrad = np.zeros([x_range[1]-x_range[0]+1,y_range[1]-y_range[0]+1,count])
        cmax_n = np.zeros([x_range[1]-x_range[0]+1,y_range[1]-y_range[0]+1,count])
        cmax_p = np.zeros([x_range[1]-x_range[0]+1,y_range[1]-y_range[0]+1,count])
        
        sample_time=d.coordinate('Time')[0][1,0,0]-d.coordinate('Time')[0][0,0,0]
        ccorr_n=np.zeros([x_range[1]-x_range[0]+1,
                          y_range[1]-y_range[0]+1,
                          x_range[1]-x_range[0]+2*x_search+1,
                          y_range[1]-y_range[0]+2*y_search+1])
            
        ccorr_p=np.zeros([x_range[1]-x_range[0]+1,
                          y_range[1]-y_range[0]+1,
                          x_range[1]-x_range[0]+2*x_search+1,
                          y_range[1]-y_range[0]+2*y_search+1])
        
        for t0 in range(fbin+1,count-fbin-1):
            #Zero lag Autocorrelation calculation for the reference, +sample_time, -sample_time data
            n_data=d.data[t0-fbin-1:t0+fbin-1,
                          x_range[0]-x_search:x_range[1]+x_search+1,
                          y_range[0]-y_search:y_range[1]+y_search+1]
            acorr_pix_n=np.sqrt(np.sum((n_data-np.mean(n_data, axis=0))**2,axis=0))
            
            p_data=d.data[t0-fbin+1:t0+fbin+1,
                          x_range[0]-x_search:x_range[1]+x_search+1,
                          y_range[0]-y_search:y_range[1]+y_search+1]
            acorr_pix_p=np.sqrt(np.sum((p_data-np.mean(p_data, axis=0))**2,axis=0))
            
            ref_data=d.data[t0-fbin:t0+fbin,
                            x_range[0]:x_range[1]+1,
                            y_range[0]:y_range[1]+1]
            acorr_pix_ref=np.sqrt(np.sum((ref_data-np.mean(ref_data, axis=0))**2,axis=0))
            
            print((t0-fbin-1)/(count-2*(fbin-1))*100.)
            #Zero lag Crosscovariance calculation for the positive and negative sample time signal
            for i0 in range(x_range[1]-x_range[0]+1):
                for j0 in range(y_range[1]-y_range[0]+1):
                    
                    frame_ref=d.data[t0-fbin:t0+fbin,i0+x_range[0],j0+y_range[0]]
                    frame_ref=frame_ref-np.mean(frame_ref)
                    
                    for i1 in range(2*x_search+1):
                        for j1 in range(2*y_search+1):
                            frame_n=d.data[t0-fbin-1:t0+fbin-1,
                                           i1+i0+x_range[0]-x_search,
                                           j1+j0+y_range[0]-y_search]
                            frame_n=frame_n-np.mean(frame_n)
                            frame_p=d.data[t0-fbin+1:t0+fbin+1,
                                           i1+i0+x_range[0]-x_search,
                                           j1+j0+y_range[0]-y_search]
                            
                            frame_p=frame_p-np.mean(frame_p)
                            ccorr_n[i0,j0,i1,j1]=np.sum(frame_ref*frame_n)
                            ccorr_p[i0,j0,i1,j1]=np.sum(frame_ref*frame_p)
                            
            #Calculating the actual cross-correlation coefficients       
            for i0 in range(x_range[1]-x_range[0]+1):
                for j0 in range(y_range[1]-y_range[0]+1):
                    vcorr_p=np.zeros([2*x_search+1,2*y_search+1])
                    vcorr_n=np.zeros([2*x_search+1,2*y_search+1])
                    for i1 in range(2*x_search+1):
                        for j1 in range(2*y_search+1):
                            vcorr_p[i1,j1]=ccorr_p[i0,j0,i1,j1]/(acorr_pix_ref[i0,j0]*acorr_pix_p[i0+i1,j0+j1])
                            vcorr_n[i1,j1]=ccorr_n[i0,j0,i1,j1]/(acorr_pix_ref[i0,j0]*acorr_pix_n[i0+i1,j0+j1])
                    #Calculating the displacement in pixel coordinates
                    index_p=np.unravel_index(np.argmax(vcorr_p),shape=vcorr_p.shape)
                    index_n=np.unravel_index(np.argmax(vcorr_n),shape=vcorr_n.shape)
                    
                    cmax_p[i0,j0,t0]=vcorr_p[index_p]
                    cmax_n[i0,j0,t0]=vcorr_n[index_n]
                    #Transforming the coordinates into spatial coordinates
                    delta_index_p=np.asarray(index_p)-np.asarray([x_search,y_search])
                    delta_index_n=np.asarray(index_n)-np.asarray([x_search,y_search])
                                               
                    vpol_p[i0,j0,t0]=(coeff_z[0]*delta_index_p[0]+
                                      coeff_z[1]*delta_index_p[1])/sample_time                                        
                    vpol_n[i0,j0,t0]=(coeff_z[0]*delta_index_n[0]+
                                      coeff_z[1]*delta_index_n[1])/sample_time
                          
                    vrad_p[i0,j0,t0]=(coeff_r[0]*delta_index_p[0]+
                                      coeff_r[1]*delta_index_p[1])/sample_time                                        
                    vrad_n[i0,j0,t0]=(coeff_r[0]*delta_index_n[0]+
                                      coeff_r[1]*delta_index_n[1])/sample_time                       
        
                    #Calculating the average between the positive and negative shifted pixels
                    
                    vpol_tot = (vpol_p - vpol_n)/2.	 	# Average p and n correlations
                    vrad_tot = (vrad_p - vrad_n)/2.     # This is non causal
                    
        #Averaging in an fbin long time window
        for t0 in range(int(fbin/2),count-int(fbin/2)):
            vpol[:,:,t0] = np.mean(vpol_tot[:,:,t0-int(fbin/2):t0+int(fbin/2)], axis=2)
            vrad[:,:,t0] = np.mean(vrad_tot[:,:,t0-int(fbin/2):t0+int(fbin/2)], axis=2)
        
        results={'Time':d.coordinate('Time')[0][:,0,0],
                 'Radial velocity':vrad,
                 'Poloidal velocity':vpol,
                 'Maximum correlation p':cmax_p,
                 'Maximum correlation n':cmax_n}
        
        pickle.dump(results, open(pickle_filename, 'wb'))
    else:
        results=pickle.load(open(pickle_filename, 'rb'))
        print('Data loaded from pickle file.')
        
    if pdf:
        pdf=PdfPages(filename.replace('processed_data', 'plots')+'.pdf')
        
    if plot:
        plt.figure()
        plt.errorbar(results['Time'],
                     np.mean(results['Radial velocity'], axis=(0,1)),
                     np.sqrt(np.var(results['Radial velocity'], axis=(0,1))))
        plt.title('Radial velocity vs time')
        plt.xlabel('Time [s]')
        plt.ylabel('Radial velocity [m/s]')
        if pdf:
            pdf.savefig()
        
        plt.figure()
        plt.errorbar(results['Time'],
                     np.mean(results['Poloidal velocity'], axis=(0,1)),
                     np.sqrt(np.var(results['Poloidal velocity'], axis=(0,1))))
        
        plt.title('Poloidal velocity vs time')
        plt.xlabel('Time [s]')
        plt.ylabel('Poloidal velocity [m/s]')
        plt.pause(0.001)
        if pdf:
            pdf.savefig()
            
        plt.figure()
        plt.errorbar(results['Time'],
                     np.mean(results['Maximum correlation p'], axis=(0,1)),
                     np.sqrt(np.var(results['Maximum correlation p'], axis=(0,1))))
        
        plt.title('Maximum correlation p vs time')
        plt.xlabel('Time [s]')
        plt.ylabel('Maximum correlation p')
        plt.pause(0.001)
        if pdf:
            pdf.savefig()
            pdf.close()
            
    if return_results:
        return results