def test_coordinates(): print() print( "\n>>>>>>>>>>>>>>>>>>> Testing adding coordinates <<<<<<<<<<<<<<<<<<<<<<<<" ) flap.delete_data_object('*', exp_id='*') 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("**** Storage contents") flap.list_data_objects() print() print("**** Adding Device x coordinate") flap.add_coordinate('TEST-1-1', exp_id='*', coordinates=['Device x', 'Device z', 'Device y']) print("**** Storage contents") flap.list_data_objects() print() 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("**** Storage contents") flap.list_data_objects() print() print("**** Adding Device x coordinate") flap.add_coordinate('TESTDATA', exp_id='*', coordinates=['Device x', 'Device z', 'Device y']) print("**** Storage contents") flap.list_data_objects() print( "**** Getting the time coordinate. The result will be a 3D object with 1 element in all dimensions except along the time." ) t = flap.get_data_object_ref('TESTDATA').coordinate( 'Time', options={'Chang': True})[0].flatten() plt.figure() plt.plot(t) plt.xlabel('Index') plt.ylabel('Time')
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 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)
n_lag = 40 sample_time = 2.5e-6 time_window = 0.001 n_frames = int(time_window / sample_time) correlation_evolution = np.zeros([n_frames + 1, n_lag + 1]) time_ranges = np.asarray([ np.arange(0, n_lag + 1) / (n_lag) * (time_range[1] - time_range[0]) + time_range[0], np.arange(0, n_lag + 1) / (n_lag) * (time_range[1] - time_range[0]) + time_range[0] + time_window ]).T flap.get_data('NSTX_GPI', exp_id=exp_id, name='', object_name='GPI') for j in range(n_lag + 1): sample0 = flap.get_data_object_ref('GPI').slice_data( slicing={ 'Time': time_ranges[j, 0] }).coordinate('Sample')[0][0, 0] d = flap.slice_data( 'GPI', slicing={'Sample': flap.Intervals(sample0, sample0 + n_frames)}, output_name='GPI_SLICED') d.data = d.data / np.mean(d.data, axis=0) d = flap_nstx.analysis.detrend_multidim('GPI_SLICED', order=1, coordinates=['Image x', 'Image y'], output_name='GPI_SLICED_DETREND') time = flap.get_data_object_ref('GPI_SLICED_DETREND').coordinate( 'Time')[0][:, 0, 0] time = time - time[0]
def show_nstx_gpi_video_frames(exp_id=None, time_range=None, start_time=None, n_frame=20, logz=False, z_range=[0,512], plot_filtered=False, normalize=False, cache_data=False, plot_flux=False, plot_separatrix=False, flux_coordinates=False, device_coordinates=False, new_plot=True, save_pdf=False, colormap='gist_ncar', save_for_paraview=False, colorbar_visibility=True ): if time_range is None and start_time is None: print('time_range is None, the entire shot is plotted.') if time_range is not None: if (type(time_range) is not list and len(time_range) != 2): raise TypeError('time_range needs to be a list with two elements.') if start_time is not None: if type(start_time) is not int and type(start_time) is not float: raise TypeError('start_time needs to be a number.') if not cache_data: #This needs to be enhanced to actually cache the data no matter what flap.delete_data_object('*') if exp_id is not None: print("\n------- Reading NSTX GPI data --------") if cache_data: try: d=flap.get_data_object_ref(exp_id=exp_id,object_name='GPI') except: print('Data is not cached, it needs to be read.') d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI') else: d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI') object_name='GPI' else: raise ValueError('The experiment ID needs to be set.') if time_range is None: time_range=[start_time,start_time+n_frame*2.5e-6] if normalize: flap.slice_data(object_name, slicing={'Time':flap.Intervals(time_range[0]-1/1e3*10, time_range[1]+1/1e3*10)}, output_name='GPI_SLICED_FOR_FILTERING') norm_obj=flap.filter_data('GPI_SLICED_FOR_FILTERING', exp_id=exp_id, coordinate='Time', options={'Type':'Lowpass', 'f_high':1e3, 'Design':'Elliptic'}, output_name='GAS_CLOUD') norm_obj.data=np.flip(norm_obj.data,axis=0) norm_obj=flap.filter_data('GAS_CLOUD', exp_id=exp_id, coordinate='Time', options={'Type':'Lowpass', 'f_high':1e3, 'Design':'Elliptic'}, output_name='GAS_CLOUD') norm_obj.data=np.flip(norm_obj.data,axis=0) coefficient=flap.slice_data('GAS_CLOUD', exp_id=exp_id, slicing={'Time':flap.Intervals(time_range[0],time_range[1])}, output_name='GPI_GAS_CLOUD').data data_obj=flap.slice_data('GPI', exp_id=exp_id, slicing={'Time':flap.Intervals(time_range[0],time_range[1])}) data_obj.data = data_obj.data/coefficient flap.add_data_object(data_obj, 'GPI_SLICED_DENORM') object_name='GPI_SLICED_DENORM' if plot_filtered: print("**** Filtering GPI") object_name='GPI_FILTERED' try: flap.get_data_object_ref(object_name, exp_id=exp_id) except: flap.filter_data(object_name, exp_id=exp_id, coordinate='Time', options={'Type':'Highpass', 'f_low':1e2, 'Design':'Chebyshev II'}, output_name='GPI_FILTERED') #Data is in milliseconds if plot_flux or plot_separatrix: print('Gathering MDSPlus EFIT data.') oplot_options={} if plot_separatrix: flap.get_data('NSTX_MDSPlus', name='\EFIT01::\RBDRY', exp_id=exp_id, object_name='SEP X OBJ' ) flap.get_data('NSTX_MDSPlus', name='\EFIT01::\ZBDRY', exp_id=exp_id, object_name='SEP Y OBJ' ) if plot_flux: d=flap.get_data('NSTX_MDSPlus', name='\EFIT01::\PSIRZ', exp_id=exp_id, object_name='PSI RZ OBJ' ) x_axis='Device R' y_axis='Device z' else: oplot_options=None if flux_coordinates: print("**** Adding Flux r coordinates") d.add_coordinate(coordinates='Flux r',exp_id=exp_id) x_axis='Flux r' y_axis='Device z' elif device_coordinates: x_axis='Device R' y_axis='Device z' if (not device_coordinates and not plot_separatrix and not flux_coordinates): x_axis='Image x' y_axis='Image y' if start_time is not None: start_sample_num=flap.slice_data(object_name, slicing={'Time':start_time}).coordinate('Sample')[0][0,0] if n_frame == 30: ny=6 nx=5 if n_frame == 20: ny=5 nx=4 gs=GridSpec(nx,ny) for index_grid_x in range(nx): for index_grid_y in range(ny): plt.subplot(gs[index_grid_x,index_grid_y]) if start_time is not None: slicing={'Sample':start_sample_num+index_grid_x*ny+index_grid_y} else: time=time_range[0]+(time_range[1]-time_range[0])/(n_frame-1)*(index_grid_x*ny+index_grid_y) slicing={'Time':time} d=flap.slice_data(object_name, slicing=slicing, output_name='GPI_SLICED') slicing={'Time':d.coordinate('Time')[0][0,0]} if plot_flux: flap.slice_data('PSI RZ OBJ',slicing=slicing,output_name='PSI RZ SLICE',options={'Interpolation':'Linear'}) oplot_options['contour']={'flux':{'Data object':'PSI RZ SLICE', 'Plot':True, 'Colormap':None, 'nlevel':51}} if plot_separatrix: flap.slice_data('SEP X OBJ',slicing=slicing,output_name='SEP X SLICE',options={'Interpolation':'Linear'}) flap.slice_data('SEP Y OBJ',slicing=slicing,output_name='SEP Y SLICE',options={'Interpolation':'Linear'}) oplot_options['path']={'separatrix':{'Data object X':'SEP X SLICE', 'Data object Y':'SEP Y SLICE', 'Plot':True, 'Color':'red'}} visibility=[True,True] if index_grid_x != nx-1: visibility[0]=False if index_grid_y != 0: visibility[1]=False flap.plot('GPI_SLICED', plot_type='contour', exp_id=exp_id, axes=[x_axis,y_axis,'Time'], options={'Z range':z_range, 'Interpolation': 'Closest value', 'Clear':False, 'Equal axes':True, 'Plot units':{'Device R':'m', 'Device z':'m'}, 'Axes visibility':visibility, 'Colormap':colormap, 'Colorbar':colorbar_visibility, 'Overplot options':oplot_options, }, plot_options={'levels':255}, ) actual_time=d.coordinate('Time')[0][0,0] #plt.title(str(exp_id)+' @ '+f"{actual_time*1000:.4f}"+'ms') plt.title(f"{actual_time*1000:.3f}"+'ms') if save_pdf: if time_range is not None: plt.savefig('NSTX_GPI_video_frames_'+str(exp_id)+'_'+str(time_range[0])+'_'+str(time_range[1])+'_nf_'+str(n_frame)+'.pdf') else: plt.savefig('NSTX_GPI_video_frames_'+str(exp_id)+'_'+str(start_time)+'_nf_'+str(n_frame)+'.pdf')
def calculate_magnetics_spectrogram(exp_id=None, time_range=None, channel=1, time_res=1e-3, freq_res=None, frange=None, recalc=False, plot=True, pdf=False, pdfobject=None, ): wd=flap.config.get_all_section('Module NSTX_GPI')['Working directory'] filename=flap_nstx.analysis.filename(exp_id=exp_id, working_directory=wd+'/processed_data', time_range=time_range, comment='magnetic_spectrogram_hf_ch'+str(channel)+'_tr_'+str(time_res)+'_frange_'+str(frange[0])+'_'+str(frange[1]), extension='pickle') if not recalc and not os.path.exists(filename): print('File doesn\'t exist, needs to be calculated!') recalc=True if recalc or not os.path.exists(filename): if freq_res is None: freq_res=2/time_res magnetics=flap.get_data('NSTX_MDSPlus', name='\OPS_PC::\\BDOT_L1DMIVVHF'+str(channel)+'_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]))) n_time=int((time_range[1]-time_range[0])/time_res) spectrum=[] for i in range(n_time-1): spectrum.append(flap.apsd('MIRNOV', coordinate='Time equi', intervals={'Time equi':flap.Intervals(time_range[0]+(i-0.5)*time_res, time_range[0]+(i+1.5)*time_res)}, options={'Res':freq_res, 'Range':frange, 'Interval':1, 'Trend':None, 'Logarithmic':False, 'Hanning':True}, output_name='MIRNOV_TWIN_APSD').data) time=np.arange(n_time-1)*time_res+time_range[0] freq=flap.get_data_object_ref('MIRNOV_TWIN_APSD').coordinate('Frequency')[0] data=np.asarray(spectrum).T pickle.dump((time,freq,data), open(filename, 'wb')) else: time, freq, data = pickle.load(open(filename, 'rb')) if plot: import matplotlib matplotlib.use('QT5Agg') import matplotlib.pyplot as plt else: import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt if pdf: filename=flap_nstx.analysis.filename(exp_id=exp_id, working_directory=wd+'/plots', time_range=time_range, comment='magnetic_spectrogram_hf_ch'+str(channel)+'_tr_'+str(time_res)+'_frange_'+str(frange[0])+'_'+str(frange[1]), extension='pdf') spectrogram_pdf=PdfPages(filename) plt.figure() plt.contourf(time, freq/1000., data, locator=ticker.LogLocator(), cmap='jet', levels=101) plt.title('BDOT_L1DMIVVHF'+str(channel)+' spectrogram for '+str(exp_id)+' with fres '+str(1/time_res/1000.)+'kHz') plt.xlabel('Time [s]') plt.ylabel('Frequency [kHz]') plt.pause(0.001) if pdf: spectrogram_pdf.savefig() spectrogram_pdf.close()
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'], plot_options={'levels': 51})
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 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_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)