def test_detrend(): plt.close('all') print() print('>>>>>>>>>>>>>>>>>>> Test detrend <<<<<<<<<<<<<<<<<<<<<<<<') flap.delete_data_object('*') print("**** Generating 8 sine signals with variable frequency.") d = flap.get_data('TESTDATA', name='TEST-1-*', object_name='TEST-1', options={ 'Signal': 'Sin', 'Freq': [1e3, 5E3], 'Length': 0.005 }) print("**** Detrending in 2 intervals with second order poly fit.") plt.figure() flap.plot('TEST-1', axes='Time') flap.detrend('TEST-1', intervals={ 'Time': flap.Intervals(0.001, 0.0015, step=0.003, number=2) }, options={'Trend': ['Poly', 2]}, output_name='TEST-1_detrend') flap.plot('TEST-1_detrend', axes='Time')
def test_cpsd(): plt.close('all') print() print( '>>>>>>>>>>>>>>>>>>> Test cpsd (Cross Spectral Power Density) <<<<<<<<<<<<<<<<<<<<<<<<' ) flap.delete_data_object('*') print("**** Generating 8 random data, 1 million points each.") d = flap.get_data('TESTDATA', name='TEST-1-[1-8]', options={ 'Signal': 'Random', 'Length': 1 }, object_name='TESTDATA') print("**** Calculating all cpsd") flap.cpsd('TESTDATA', options={ 'Norm': True, 'Interval': 50, 'Log': True, 'Res': 10, 'Range': [100, 1e5] }, output_name='TESTDATA_cpsd') flap.abs_value('TESTDATA_cpsd', output_name='TESTDATA_cpsd_abs') print( "**** Plotting coherency between channels 1-2 and its significance level." ) plt.figure() flap.plot('TESTDATA_cpsd_abs', axes='Frequency', slicing={ 'Row (Ref)': 1, 'Row': 2 }, options={ 'Log y': True, 'Log x': True, 'Error': False }) flap.error_value('TESTDATA_cpsd_abs').plot(slicing={ 'Row (Ref)': 1, 'Row': 2 }) plt.figure() print( "**** Plotting mean coherence in 1e4-1e5 frequency range as a function of row index." ) flap.slice_data('TESTDATA_cpsd_abs', slicing={ 'Frequency': flap.Intervals(1e4, 1e5) }, summing={ 'Frequency': 'Mean' }).plot(axes='Row (Ref)', options={'Y sep': 1.5})
def test_mdsplus(): plt.close('all') print("**** Reading an explicit MDS signal.") flap.delete_data_object('*') try: # Explicit MDSPlus reference #d=flap.get_data('NSTX_MDSPlus', # name='IP', # exp_id=141398, # object_name='TEST_MDS' # ) d = flap.get_data('NSTX_MDSPlus', name='\EFIT01::\PSIRZ', exp_id=141399, object_name='TEST_MDS') #print(d.coordinate('Time')[0].shape) #print(d.coordinate('Device R')[0].shape) #print(d.coordinate('Device Z')[0].shape) #print(d.coordinate('Dimension 1')[0]) print(d.data) #print(d.data.shape) except Exception as e: raise e flap.plot('TEST_MDS', plot_type='animation', axes=['Device R', 'Device z', 'Time'], options={ 'Z range': [0, 0.05], 'Wait': 0.0, 'Clear': False }) #flap.plot('TEST_MDS',plot_type='contour',axes=['Time','Dimension 1'],options={}) #flap.plot('TEST_MDS') flap.list_data_objects()
def test_stft(): print( '>>>>>>>>>>>>>>>>>>> Test Short Time Fourier Transform (STFT) <<<<<<<<<<<<<<<<<<<<<<<<' ) print( "**** Generating 0.1 s long test data signal with linearly changing frequency: 10-100kHz" ) f = np.linspace(1e4, 1e5, num=11) coord = flap.Coordinate(name='Time', start=0.0, step=0.01, mode=flap.CoordinateMode(equidistant=True), dimension_list=[0]) f_obj = flap.DataObject(data_array=f, coordinates=[coord], data_unit=flap.Unit(name='Frequency', unit='Hz')) d = flap.get_data('TESTDATA', name='TEST-1-1', options={ 'Scaling': 'Volt', 'Frequency': f_obj, 'Length': 0.1, 'Row number': 1, 'Column number': 1 }, object_name='TESTDATA') flap.stft('TESTDATA', output_name='TEST_STFT') flap.abs_value('TEST_STFT', output_name='TEST_STFT') flap.list_data_objects() plt.figure() flap.plot('TEST_STFT', axes=['Time', 'Frequency'], plot_type='image')
def test_get_data(): try: flap.get_data('CAMERA_APSD') apsd_available = True except: apsd_available = False if (not apsd_available): try: # flap.get_data('W7X_CAMERA',exp_id="20181018.032", name="AEQ20_EDICAM_ROIP1", coordinates={'Time':[3,4]}, object_name="EDI_ROIP1") flap.get_data('W7X_CAMERA', exp_id="20181018.012", name="AEQ21_PHOTRON_ROIP1", coordinates={'Time':[6.05,6.25]}, no_data=False, object_name="CAMERA") except Exception as e: raise e flap.list_data_objects() # flap.plot("CAMERA",plot_type='anim-image',axes=['Image y','Image x','Time'],options={'Wait':0.01,'Clear':True}) print("Slicing start") flap.slice_data('CAMERA', slicing={'Image x':flap.Intervals(0,4,step=5),'Image y':flap.Intervals(0,4,step=5)}, summing={'Interval(Image x) sample index':'Mean','Interval(Image y) sample index':'Mean'}, output_name='CAMERA_sliced') print("Slicing stop") # flap.plot("CAMERA_sliced", # plot_type='anim-image', # axes=['Start Image y in int(Image y)','Start Image x in int(Image x)','Time'], # options={'Wait':0.01,'Clear':True, 'Z range':[0,3000]}) print("*** APSD start") start = time.time() flap.apsd("CAMERA_sliced",coordinate='Time',options={'Res':200,'Range':[0,1e4]},output_name='CAMERA_APSD') stop = time.time() print('**** APSD STOP') print("**** Calculation time: {:5.2f} second".format(stop-start)) plt.close('all') # flap.plot('CAMERA_APSD', # slicing={'Start Image y in int(Image y)':50}, # plot_type='image', # axes=['Frequency','Start Image x in int(Image x)'], # options={'Z range':[0,5],'Aspect':'auto'}) # plt.figure() return flap.plot('CAMERA_APSD', plot_type='anim-image', axes=['Frequency','Start Image x in int(Image x)','Start Image y in int(Image y)'], options={'Z range':[0,5],'Aspect':'auto','Wait':0.1}) flap.list_data_objects()
def test_plot_multi_xy(): print() print("\n>>>>>>>>>>>>>>>>>>> Test plot multi x-y --------") plt.close('all') plt.figure() flap.delete_data_object('*') d = flap.get_data('TESTDATA', name='TEST-1-*', options={'Scaling': 'Volt'}, object_name='TEST') print("**** Storage contents") flap.list_data_objects() flap.plot('TEST', axes='Time', options={'All points': False, 'Y sep': 4})
def test_binning(): print() print() print('>>>>>>> Test image binning through multi-slice<<<<<<<<<<<') print("**** Generating a sequence of test images") flap.get_data('TESTDATA', name='VIDEO', object_name='TEST_VIDEO', options={ 'Length': 0.05, 'Samplerate': 1e3, 'Width': 500, 'Height': 800, 'Image': 'Gauss', 'Spotsize': 10 }) print("***** Showing one image") plt.figure() flap.plot('TEST_VIDEO', slicing={'Time': 30e-3 / 3}, plot_type='image', axes=['Image x', 'Image y'], options={ 'Clear': True, 'Interpolation': None, 'Aspect': 'equal' }) flap.slice_data('TEST_VIDEO', slicing={ 'Image x': flap.Intervals(0, 4, step=5), 'Image y': flap.Intervals(0, 9, step=10) }, summing={ 'Interval(Image x) sample index': 'Mean', 'Interval(Image y) sample index': 'Mean' }, output_name='TEST_VIDEO_binned') print("***** Showing one image of the (5,10) binned video ") plt.figure() flap.plot('TEST_VIDEO_binned', slicing={'Time': 30e-3 / 3}, plot_type='image', axes=['Image x', 'Image y'], options={ 'Clear': True, 'Interpolation': None, 'Aspect': 'equal' }) flap.list_data_objects()
def test_select_multislice(): plt.close('all') print() print( '>>>>>>>>>>>>>>>>>>> Test select on maxima and multi slice <<<<<<<<<<<<<<<<<<<<<<<<' ) flap.delete_data_object('*') d = flap.get_data('TESTDATA', name='TEST-1-1', object_name='TEST-1-1', options={'Length': 0.050}) print( "**** Selecting 100 microsec long intervals around the maxima of the signal." ) d_int = flap.select_intervals('TEST-1-1', coordinate='Time', options={ 'Select': None, 'Length': 0.0001, 'Event': { 'Type': 'Max-weight', 'Threshold': 1, 'Thr-type': 'Sigma' } }, plot_options={'All points': True}, output_name='SELECT') flap.list_data_objects() d_int.plot(axes=['__Data__', 0], plot_type='scatter', options={'Force': True}) if (d_int is not None): print("**** Overplotting the signals in the selected intervals.") flap.slice_data('TEST-1-1', slicing={'Time': d_int}, output_name='TEST-1-1_sliced') flap.list_data_objects() plt.figure() n_int = d_int.shape[0] for i in range(n_int): flap.plot('TEST-1-1_sliced', slicing={'Interval(Time)': i}, axes='Rel. Time in int(Time)')
def test_NSTX_GPI_norm_flux_coord(exp_id=141918): flap.delete_data_object('*') print("\n------- test data read with NSTX GPI data and plotting it as a function of normalized flux coordinates --------") d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI') print("**** Storage contents") flap.list_data_objects() plt.close('all') print("**** Adding normalized flux coordinates.") print("--- %s seconds ---" % (time.time() - start_time)) #d.add_coordinate(coordinates='Flux r',exp_id=exp_id) print("--- %s seconds ---" % (time.time() - start_time)) d.add_coordinate(coordinates='Flux theta',exp_id=exp_id) flap.list_data_objects() print('Coordinate was read.') print("--- %s seconds ---" % (time.time() - start_time)) flap.plot('GPI',plot_type='animation',\ slicing={'Time':flap.Intervals(250.,260.)},\ axes=['Flux R','Flux theta','Time'],\ options={'Z range':[0,512],'Wait':0.0,'Clear':False}) print("--- %s seconds ---" % (time.time() - start_time))
def test_pdf(): print( '>>>>>>>>>>>>>>>>>>> Test Probability Distribution Function (PDF) <<<<<<<<<<<<<<<<<<<<<<<<' ) print( "**** Generating 10x15 random test signals, 5000 points each, 1 MHz sampling." ) flap.get_data('TESTDATA', name='TEST-*-*', options={ 'Length': 0.005, 'Signal': 'Sin' }, object_name='TESTDATA') flap.pdf('TESTDATA', coordinate='Time', options={'Number': 30}, output_name='PDF') flap.list_data_objects() plt.figure() flap.plot('PDF', slicing={'Column': 3}, axes=['Signal']) plt.title('PDF of sine waves')
def test_plot_xy(): print() print("\n>>>>>>>>>>>>>>>>>>> Test plot x-y <<<<<<<<<<<<<<<<<<<<<<<<") plt.close('all') flap.delete_data_object('*') print("**** Reading signal TEST-2-5 for time range [0,0.1]") d = flap.get_data('TESTDATA', name='TEST-2-5', options={'Scaling': 'Volt'}, object_name='TEST-1-1', coordinates={'Time': [0, 0.1]}) print("**** Default plot") plt.figure() d.plot() print("**** Plotting time vs data") plt.figure() d.plot(axes=['__Data__', 'Time']) print("**** Reading all test signals for time range [0,0.001]") d = flap.get_data('TESTDATA', name='TEST-*', options={'Scaling': 'Volt'}, object_name='TESTDATA', coordinates={'Time': [0, 0.001]}) print("**** Adding Device coordinates") flap.add_coordinate('TESTDATA', exp_id='*', coordinates=['Device x', 'Device z', 'Device y']) flap.list_data_objects() print("**** Plotting measurement points in device corodinates.") plt.figure() flap.plot('TESTDATA', axes=['Device x', 'Device z'], plot_type='scatter') print("**** Plotting Device x as a function of Row.") plt.figure() flap.plot( 'TESTDATA', axes=['Row', 'Device x'], plot_type='scatter', )
def calculate_nstx_gpi_crosspower(exp_id=None, time_range=None, normalize_signal=False, #Normalize the amplitude for the average frame for the entire time range reference_pixel=None, reference_position=None, reference_flux=None, reference_area=None, #In the unit of the reference, [psi,z] if reference_flux is not None fres=1., #in KHz due to data being in ms flog=False, frange=None, interval_n=8., filename=None, options=None, cache_data=False, normalize=False, #Calculate coherency if True plot=False, plot_phase=False, axes=['Image x', 'Image y', 'Frequency'], hanning=True, colormap=None, video_saving_only=False, video_filename=None, save_video=False, comment=None, zlog=False, save_for_paraview=False ): #139901 [300,307] #This function returns the crosspower between a single signal and all the other signals in the GPI. #A separate function is dedicated for multi channel reference channel #e.g 3x3 area and 64x80 resulting 3x3x64x80 cross power spectra #Read data from the cine file 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: 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='Chebyshev II', # 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 #Calculate the crosspower spectra for the timerange between the reference pixel and all the other pixels if reference_pixel is None and reference_position is None and reference_flux is None: calculate_apsd=True print('No reference is defined, returning autopower spectra.') else: calculate_apsd=False reference_signal=flap_nstx.analysis.calculate_nstx_gpi_reference('GPI', exp_id=exp_id, time_range=time_range, reference_pixel=reference_pixel, reference_area=reference_area, reference_position=reference_position, reference_flux=reference_flux, output_name='GPI_REF') flap.slice_data('GPI',exp_id=exp_id, slicing={'Time':flap.Intervals(time_range[0],time_range[1])}, output_name='GPI_SLICED') if calculate_apsd: object_name='GPI_APSD' d=flap.apsd('GPI_SLICED',exp_id=exp_id, coordinate='Time', options={'Resolution':fres, 'Range':frange, 'Logarithmic':flog, 'Interval_n':interval_n, 'Hanning':hanning, 'Trend removal':None, }, output_name=object_name) else: object_name='GPI_CPSD' flap.cpsd('GPI_SLICED',exp_id=exp_id, ref=reference_signal, coordinate='Time', options={'Resolution':fres, 'Range':frange, 'Logarithmic':flog, 'Interval_n':interval_n, 'Hanning':hanning, 'Normalize':normalize, 'Trend removal':None, }, output_name=object_name) flap.abs_value(object_name,exp_id=exp_id, output_name='GPI_CPSD_ABS') flap.phase(object_name,exp_id=exp_id, output_name='GPI_CPSD_PHASE') if not save_video: if plot: if calculate_apsd: object_name='GPI_APSD' else: if plot_phase: object_name='GPI_CPSD_PHASE' else: object_name='GPI_CPSD_ABS' flap.plot(object_name, exp_id=exp_id, plot_type='animation', axes=axes, options={'Force axes':True, 'Colormap':colormap, 'Plot units':{'Device R':'mm', 'Device z':'mm', 'Frequency':'kHz'}, 'Log z':zlog}) else: if video_filename is None: if time_range is not None: video_filename='NSTX_GPI_'+str(exp_id) if calculate_apsd: video_filename+='_APSD' else: video_filename+='_CPSD' if plot_phase: video_filename+='_PHASE' else: video_filename+='_ABS' video_filename+='_'+str(time_range[0])+'_'+str(time_range[1]) if reference_pixel is not None: video_filename+='_PIX_'+str(reference_pixel[0])+'_'+str(reference_pixel[1]) if reference_position is not None: video_filename+='_POS_'+str(reference_position[0])+'_'+str(reference_position[1]) if reference_flux is not None: video_filename+='_FLX_'+str(reference_flux[0])+'_'+str(reference_flux[1]) video_filename+='_FRES_'+str(fres) if comment is not None: video_filename+=comment video_filename+='.mp4' else: video_filename='NSTX_GPI_CPSD_'+str(exp_id)+'_FULL.mp4' if video_saving_only: import matplotlib current_backend=matplotlib.get_backend() matplotlib.use('agg') waittime=0. else: waittime=1. if calculate_apsd: object_name='GPI_APSD' else: if plot_phase: object_name='GPI_CPSD_PHASE' else: object_name='GPI_CPSD_ABS' flap.plot(object_name, exp_id=exp_id, plot_type='anim-contour', axes=axes, options={'Force axes':True, 'Colormap':colormap, 'Plot units':{'Device R':'mm', 'Device z':'mm', }, 'Waittime':waittime, 'Video file':video_filename, 'Video format':'mp4', 'Log z':zlog}) if video_saving_only: import matplotlib matplotlib.use(current_backend)
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}" )
def test_apsd(): plt.close('all') print() print( '>>>>>>>>>>>>>>>>>>> Test apsd (Auto Power Spectral Density) <<<<<<<<<<<<<<<<<<<<<<<<' ) flap.delete_data_object('*') #plt.close('all') print( '**** Generating test signals with frequency changing from channel to channel.' ) d = flap.get_data('TESTDATA', name='TEST*', object_name='TEST-1_S', options={ 'Signal': 'Sin', 'F': [1e3, 1e4], 'Length': 1. }) print('**** Calculating 150 APSDs, each 1 million sample.') print('**** APSD START') start = time.time() flap.apsd('TEST-1_S', output_name='TEST-1_APSD_Sin1', options={ 'Res': 12, 'Int': 10 }) stop = time.time() print('**** APSD STOP') print("**** Calculation time: {:5.2f} second/signal".format( (stop - start) / 150.)) plt.figure() flap.plot('TEST-1_APSD_Sin1', slicing={'Row': 1}, axes='Frequency', options={ 'All': True, 'X range': [0, 5e3] }) plt.title('TEST-1-1_APSD_Sin1') print("**** Testing with a complex signal.") flap.delete_data_object('*') d = flap.get_data('TESTDATA', name='TEST-1-1', object_name='TEST-1-1_CS', options={'Signal': 'Complex-Sin'}) flap.apsd('TEST-1-1_CS', coordinate='Time', output_name='TEST-1-1_APSD_Complex-Sin', options={ 'Res': 10, 'Range': [-1e5, 1e5] }) flap.slice_data('TEST-1-1_APSD_Complex-Sin', slicing={'Frequency': flap.Intervals(-5e3, 5e3)}, output_name='TEST-1-1_APSD_Complex-Sin_sliced') flap.list_data_objects() plt.figure() flap.plot('TEST-1-1_APSD_Complex-Sin_sliced', axes='Frequency', options={'All': True}) plt.title('TEST-1-1_APSD_Complex-Sin_sliced') print( "**** Testing interval selection in apsd. APSD from 8 intervals, each 80 ms long." ) d = flap.get_data('TESTDATA', name='TEST-1-1', object_name='TEST-1-1', options={ 'Signal': 'Sin', 'Length': 1 }) intervals = flap.Intervals(0, 0.08, step=0.1, number=8) flap.apsd('TEST-1-1', output_name='TEST-1-1_APSD', intervals=intervals, options={ 'Res': 12, 'Int': 10 }) plt.figure() flap.plot('TEST-1-1_APSD', options={'X range': [0, 5e3]})
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'], plot_options={'levels': 51}) plt.title('139901 GPI ' + str(order) + ' detrend @ 0.324s') plt.savefig('139901_0.324s_' + str(order) + '_detrend.pdf') plt.figure() flap.plot('GPI_RAW_SLICED_2_DETREND', plot_type='contour',
output_name='test', poloidal_velocity=10e3, radial_velocity=5e3, poloidal_size=0.02, radial_size=0.03, start_position=[1.5, 0.3], amplitude=1., gaussian=True, add_background=False) flap.slice_data('test', slicing={'Sample': 2}, output_name='test_0') flap.slice_data('test', slicing={'Sample': 3}, output_name='test_1') #flap.ccf('test_0', 'test_1', # coordinate=['Image x'], # options={'Resolution':1, 'Range':[-63,63], 'Trend removal':None, 'Normalize':True, 'Interval_n': 1}, # output_name='test_01_correlation') flap.ccf('test_0', 'test_1', coordinate=['Image x', 'Image y'], options={ 'Resolution': 1, 'Range': [[-63, 63], [-79, 79]], 'Trend removal': None, 'Normalize': True, 'Interval_n': 1 }, output_name='test_01_correlation') flap.plot('test_01_correlation', plot_type='contour', axes=['Image x lag', 'Image y lag'])
def test_NSTX_GPI_data_animation(exp_id=141918): flap.delete_data_object('*') print("\n------- test data read with NSTX GPI data --------") d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI') print("**** Storage contents") flap.list_data_objects() plt.close('all') print("**** Filtering GPI") #d_filter=flap.filter_data('GPI',output_name='GPI_filt',coordinate='Time', # options={'Type':'Highpass','f_low':1e2/1e3,'Design':'Chebyshev II'}) #Data is in milliseconds print('Gathering MDSPlus data') 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' ) # flap.get_data('NSTX_MDSPlus', # name='\EFIT01::\RLIM', # exp_id=exp_id, # object_name='LIM X OBJ' # ) # # flap.get_data('NSTX_MDSPlus', # name='\EFIT01::\ZLIM', # exp_id=exp_id, # object_name='LIM Y OBJ' # ) d=flap.get_data('NSTX_MDSPlus', name='\EFIT01::\PSIRZ', exp_id=exp_id, object_name='PSI RZ OBJ' ) d=flap.get_data('NSTX_MDSPlus', name='\EFIT01::\GAPIN', exp_id=exp_id, object_name='GAPIN' ) d=flap.get_data('NSTX_MDSPlus', name='\EFIT01::\SSIMAG', exp_id=exp_id, object_name='SSIMAG' ) d=flap.get_data('NSTX_MDSPlus', name='\EFIT01::\SSIBRY', exp_id=exp_id, object_name='SSIBRY' ) efit_options={'Plot separatrix': True, 'Separatrix X': 'SEP X OBJ', 'Separatrix Y': 'SEP Y OBJ', 'Separatrix color': 'red', # 'Plot limiter': True, # 'Limiter X': 'LIM X OBJ', # 'Limiter Y': 'LIM Y OBJ', # 'Limiter color': 'white', 'Plot flux': True, 'Flux XY': 'PSI RZ OBJ',} print("**** Plotting filtered GPI") flap.plot('GPI',plot_type='animation', slicing={'Time':flap.Intervals(250.,260.)}, axes=['Device R','Device z','Time'], options={'Z range':[0,512],'Wait':0.0,'Clear':False, 'EFIT options':efit_options}) flap.list_data_objects()
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)
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')
def test_plot(): print() print( "\n>>>>>>>>>>>>>>>>>>> Testing various plot modes <<<<<<<<<<<<<<<<<<<<<<<<" ) flap.delete_data_object('*') plt.close('all') print("**** Generating some test data.") length = 0.01 flap.get_data('TESTDATA', name='TEST-1-1', options={ 'Signal': 'Sin', 'Length': length }, object_name='TEST-1-1') flap.get_data('TESTDATA', name='TEST-1-2', options={ 'Signal': 'Const.', 'Length': length }, object_name='TEST-1-2') flap.get_data('TESTDATA', name='TEST-1-3', options={ 'Signal': 'Const.', 'Length': length }, object_name='TEST-1-3') flap.get_data('TESTDATA', name='TEST-1-[1-5]', options={ 'Signal': 'Sin', 'Length': length }, object_name='TESTDATA') flap.get_data('TESTDATA', name='TEST-1-1', options={ 'Signal': 'Complex-Sin', 'Length': length }, object_name='TEST-1-1_comp') flap.get_data('TESTDATA', name='TEST-1-2', options={ 'Signal': 'Complex-Sin', 'Length': length }, object_name='TEST-1-2_comp') flap.get_data('TESTDATA', name='TEST-1-[1-5]', options={ 'Signal': 'Complex-Sin', 'Length': length }, object_name='TESTDATA_comp') print("**** Creating a single plot in the upper left corner.") plt.figure() gs = GridSpec(2, 2) plt.subplot(gs[0, 0]) plot_1 = flap.plot('TEST-1-1') print("**** Creating a multi xy on the right side.") plt.subplot(gs[:, 1]) plot_2 = flap.plot('TESTDATA') plot_2 = flap.plot('TESTDATA', slicing={'Signal name': ['TEST-1-2', 'TEST-1-3']}) print("**** Overplotting into the first plot.") flap.plot('TEST-1-3', plot_id=plot_1) print("**** Plotting two complex signals into the same plot.") plt.figure() plot_3 = flap.plot('TEST-1-1_comp') plot_3 = flap.plot('TEST-1-2_comp') print( "**** Plotting absolute value and phase of multiple complex signals.") plt.figure() gs = GridSpec(1, 2) plt.subplot(gs[0, 0]) plot_4 = flap.abs_value('TESTDATA_comp').plot() plt.subplot(gs[0, 1]) plot_5 = flap.phase('TESTDATA_comp').plot(options={'Y sep': 10}) # plot_4 = flap.plot('TESTDATA_comp') print("**** Image plot of testdata and some single plots.") plt.figure() gs = GridSpec(2, 2) plt.subplot(gs[0, 0]) plot_5 = flap.plot('TESTDATA', axes=['Time', 'Row'], plot_type='image', options={ 'Colormap': 'bwr', 'Z range': [-2, 2] }) plt.subplot(gs[1, 0]) plot_6 = flap.plot('TESTDATA', slicing={'Signal name': 'TEST-1-1'}, axes=['Time'], options={'Y range': [-2, 2]}, plot_options={'linestyle': '-'}) flap.plot('TESTDATA', slicing={'Signal name': 'TEST-1-2'}, axes=['Time'], plot_options={'linestyle': '--'}) legend = ['Row 1', 'Row 2'] plot_6.plt_axis_list[0].legend(legend) plt.subplot(gs[:, 1]) plot_7 = flap.plot('TESTDATA', plot_type='multi xy', axes='Time')
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]},)
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)
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' })
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')
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()
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()
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)
) except Exception as e: raise e d1.plot(axes='Time') print("**** Reading multiple complex virtual signals in part of the time.") try: # Explicit MDSPlus reference d2=flap.get_data('W7X_MDSPlus', name=['CR-B','CR-C','CR-D','CR-E'], exp_id='20181018.003', coordinates={'Time':[4,4.1]}, object_name="CR" ) except Exception as e: raise e plt.figure() d2.abs_value().plot(axes='Time',options={'All':True}) flap.list_data_objects() # Reading configuration file in the test directory thisdir = os.path.dirname(os.path.realpath(__file__)) fn = os.path.join(thisdir,"test_w7x_mdsplus.cfg") flap.config.read(file_name=fn) test_mdsplus() d=0 d.plot(axes='Time') flap.plot('TEST_MDS',axes='Time')
def show_nstx_mdsplus_signal(exp_id=None, time_range=None, tree=None, node=None, new_plot=True, save_pdf=False, overplot=False, scale=1.0): plot_options = {} if (exp_id is None and time_range is None): print('The correct way to call the code is the following:\n') print( 'show_nstx_mdsplus_signal(exp_id=141918, time_range=[250.,260.], tree=\'wf\', node=\'\\IP\'' ) print( 'INPUTs: \t Description: \t\t\t\t Type: \t\t\t Default values: \n') print('exp_id: \t The shot number. \t\t\t int \t\t\t Default: None') print( 'time_range: \t The time range in ms. \t\t\t [float,float] \t\t Default: None' ) print( 'tree: \t\t Name of the MDSplus tree. \t\t string \t\t Default: None' ) print( 'node: \t\t Name of the MDSplus tree node. \t string \t\t Default: None' ) return if (tree is None) or (node is None): raise ValueError( 'The MDSplus tree and the node both needs to be given as an input.' ) if time_range is None: print('time_range is None, the entire shot is plot.') 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], time_range[1]] plot_options['X range'] = time_range object_name = str(exp_id) + '_mds_t.' + tree + '_n.' + node d = flap.get_data('NSTX_MDSPlus', name='\\' + tree + '::' + node, exp_id=exp_id, object_name=object_name, options={'Time unit': 'ms'}) if (len(d.data.shape) > 1): raise ValueError( 'The read data are not a single channel time series. Returning...') 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 plot_options['Force axes'] = True flap.plot(object_name, exp_id=exp_id, axes=['Time', '__Data__'], options=plot_options) if save_pdf: if time_range is None: filename = 'NSTX_' + str( exp_id) + '_MDS_' + tree + '_' + node + '.pdf' else: filename = 'NSTX_' + str( exp_id) + '_MDS_' + tree + '_' + node + '_' + str( time_range[0]) + '_' + str(time_range[1]) + '.pdf' for c in ['\\', ':']: filename = filename.replace(c, '_') plt.savefig(filename)