From e1613dfe78510c3d9cea0eca045b8f9d9bc1680d Mon Sep 17 00:00:00 2001
From: "Oliver Ford (IPP)" <cxrsgit@oliford.co.uk>
Date: Sun, 29 Jul 2018 16:20:54 +0200
Subject: [PATCH 1/3] Add basic infrastructure for loading W7-X data.

---
 idl/cxf_e.pro                      |   8 +++
 idl/cxf_process_command.pro        |   5 ++
 idl/cxf_site.pro                   |   4 ++
 idl/cxf_w.pro                      |  16 ++++++
 machines/w7x/cxf_load_w7x_data.pro | 100 +++++++++++++++++++++++++++++++++++++
 machines/w7x/cxf_w7x_read_e.pro    |  84 +++++++++++++++++++++++++++++++
 machines/w7x/cxf_w7x_read_w.pro    |  38 ++++++++++++++
 7 files changed, 255 insertions(+)
 create mode 100644 machines/w7x/cxf_load_w7x_data.pro
 create mode 100644 machines/w7x/cxf_w7x_read_e.pro
 create mode 100644 machines/w7x/cxf_w7x_read_w.pro

diff --git a/idl/cxf_e.pro b/idl/cxf_e.pro
index 029b8f8..e8c5983 100644
--- a/idl/cxf_e.pro
+++ b/idl/cxf_e.pro
@@ -405,6 +405,10 @@ pro cxf_e,event
 		cxf_hl2a_save_w,event.top
 		return	
 	endif
+        if event.id eq info.results_w7xbase_button then begin
+		cxf_w7x_save_w,event.top
+		return	
+	endif
 	if event.id eq info.results_ufile_button then begin
 		cxf_ufile_save_w,event.top
 		return	
@@ -499,6 +503,10 @@ pro cxf_e,event
 		cxf_hl2a_read_w,event.top
 		return	
 	endif
+        if event.id eq info.w7x_button then begin
+		cxf_w7x_read_w,event.top
+		return	
+	endif
 	if event.id eq info.spinfo_button then begin
 		
 		if ptr_valid(info.expdata) then begin
diff --git a/idl/cxf_process_command.pro b/idl/cxf_process_command.pro
index e6a0a44..89e6404 100644
--- a/idl/cxf_process_command.pro
+++ b/idl/cxf_process_command.pro
@@ -359,6 +359,11 @@ pro cxf_process_command,inputtext,tlb,info,err
  	    					cxf_load_hl2a_data,specdata,err
 					endif
 
+					if site eq 'w7x' then begin
+     						specdata.machine.name='W7X'
+ 	    					cxf_load_w7x_data,specdata,err
+					endif
+
 					if err eq '' then begin
       						ptr_free,info.expdata
 	     					info.expdata=ptr_new(specdata)
diff --git a/idl/cxf_site.pro b/idl/cxf_site.pro
index a7545b4..1a545d5 100644
--- a/idl/cxf_site.pro
+++ b/idl/cxf_site.pro
@@ -40,6 +40,10 @@ function cxf_site
 	if strpos(res[0],'hl2a') ne -1 then begin
 		return,'hl2a'
 	endif
+
+	if strpos(res[0],'w7x') ne -1 then begin
+		return,'w7x'
+	endif
       
 	return,'unknown'
 end
diff --git a/idl/cxf_w.pro b/idl/cxf_w.pro
index 663b7d5..2db7c47 100644
--- a/idl/cxf_w.pro
+++ b/idl/cxf_w.pro
@@ -580,6 +580,13 @@ endif else begin
 	hl2a_button=widget_button(spectrum_menu,value='Read HL2A DATA...')
 endelse
 
+if site eq 'w7x' then begin
+	if nativeacc then w7x_button=widget_button(spectrum_menu,value='Read W7X DATA...',accelerator='ctrl+R')
+	if not nativeacc then w7x_button=widget_button(spectrum_menu,value='Read W7X DATA...',resource_name='readbutton')
+endif else begin
+	w7x_button=widget_button(spectrum_menu,value='Read W7X DATA...')
+endelse
+
 spinfo_button=widget_button(spectrum_menu,value='Spectra information',/separator)
 
 
@@ -656,6 +663,13 @@ endif else begin
 	results_hl2abase_button=widget_button(results_menu,value='Write HL2A data')
 endelse
 
+if site eq 'w7x' then begin
+	if nativeacc then results_w7xbase_button=widget_button(results_menu,value='Write W7X data',accelerator='ctrl+W')
+	if not nativeacc then results_w7xbase_button=widget_button(results_menu,value='WriteW7X data',resource_name='writebutton')
+endif else begin
+	results_w7xbase_button=widget_button(results_menu,value='Write W7X data')
+endelse
+
 results_ufile_button=widget_button(results_menu,value='Write individual ufile',/separator)
 results_idl_button=widget_button(results_menu,value='Write IDL savefile')
 results_ascii_button=widget_button(results_menu,value='Write ASCII file')
@@ -857,6 +871,7 @@ info=	{	tlb			:tlb, $
 	        east_button	        :east_button, $
                 sos_button	        :sos_button, $
  		hl2a_button	        :hl2a_button, $
+ 		w7x_button	        :w7x_button, $
 ;	 	clean_button		:clean_button, $
 ;	 	jetbodge_button		:jetbodge_button, $
 		spinfo_button		:spinfo_button, $
@@ -897,6 +912,7 @@ info=	{	tlb			:tlb, $
                 results_eastbase_button :results_eastbase_button, $
                 results_sosbase_button  :results_sosbase_button, $
                 results_hl2abase_button :results_hl2abase_button, $
+                results_w7xbase_button  :results_w7xbase_button, $
 		view_fitted_components_button	:view_fitted_components_button, $
                 view_initial_components_button	:view_initial_components_button, $
                 view_initial_spectrum_button	:view_initial_spectrum_button, $
diff --git a/machines/w7x/cxf_load_w7x_data.pro b/machines/w7x/cxf_load_w7x_data.pro
new file mode 100644
index 0000000..3acf1c0
--- /dev/null
+++ b/machines/w7x/cxf_load_w7x_data.pro
@@ -0,0 +1,100 @@
+pro cxf_load_w7x_data,specdata,err
+	; cache root
+	cacheRoot = '/data/minerva/'
+
+	err=''
+
+	;One day I'll try to load all the necessary information directly from the archive in IDL
+
+	;In the meantime, we use a NetCDF 'dataInfo' file prepared by ImageProc-W7X/SpecCalProcessor to give all the calibration
+	; information as well as the necessary details to steal the raw data from the DataSignals cache.
+	;
+	; The saved in [cacheRoot]/cxsfit/dataInfo-[spectrometerName]-[shotNr].nc
+	; shotNr date and W7X program: YYMMddsss (same idea as the MDSPlus shot number but definitively W7X program number)
+
+	; The NetCDF file contains:
+	; Attributes:
+	;	t1Nanos 	- nano timestamp of 'T1' (usually start of ECRH startup -100ms )
+	;	dbPath 		- ArchiveDB path to the raw files e.g. "w7x/AchiveDB/raw/W7X/QSK_CXRS/ILS_Green/..."
+	;	exposureTime	- exposure time MS from camera metadata
+	;
+	; Variables:
+	;  	nanoTime 	- Nanosecond timestamps, needed to know what files the images are in when loading from the DataSignals cache
+	;   	wavelength 	- 2D array containing wavelength cal for image
+	;   	intensity cal 	- 2D array containing intensity cal for image
+	;   	instr func cal 	- 2D array containing instrument function width cal for image "
+	;
+
+	;   LOS info - via lightPath, LOSes from raytracer, should be stored in DB somewhere
+	;   exposure time - metadata
+
+	fileName = cacheRoot + '/cxsfit/dataInfo-w7x-' + specdata.spectrometer.name + '-' + strtrim(specdata.shot_nr,2) + '.nc'
+
+	print, "Looking for CXSFit information package at '", fileName, "'.";
+
+	;load the information package
+	id = NCDF_OPEN(fileName)
+
+	NCDF_ATTGET, id, 'dbPath', dbPath, /GLOBAL
+	dbPath = String(dbPath)
+
+	dimID = NCDF_DIMID(id, 'imageWidth');
+	NCDF_DIMINQ, id, dimID, dimName, imageWidth
+	dimID = NCDF_DIMID(id, 'imageHeight');
+	NCDF_DIMINQ, id, dimID, dimName, imageHeight
+
+	NCDF_ATTGET, id, 't1Nano', t1Nano, /GLOBAL
+	
+	NCDF_VARGET, id, 'nanoTime', nanoTime
+	NCDF_VARGET, id, 'wavelength', wavelength
+	
+	NCDF_CLOSE, id
+
+	; Now read the raw images from dbPath
+	nImages = n_elements(nanoTime)
+	print, 'Reading ', nImages, ' images sized (', imageWidth, ' x ', imageHeight, ') from ''', dbPath, '''.'
+
+	images = intarr(imageHeight, imageWidth, nImages);
+	time = fltarr(nImages);
+	for i=0, nImages-1 do begin
+	
+		fileName = cacheRoot + '/' + dbPath + '/' + strtrim(nanoTime[i],2) + '_' + strtrim(nanoTime[i],2) + '.nc'
+		;print, fileName
+
+		id = NCDF_OPEN(fileName)	
+		NCDF_VARGET, id, 'data', data
+		NCDF_CLOSE, id
+
+		;data comes from NetCDF signed for some reason, probably Java's fault
+		data = uint(data);
+
+		images[*,*,i] = data
+
+		;use the nanoTime and the t1 trigger to generate a human timebase
+		time[i] = (nanoTime[i] - t1Nano) / 1e9;
+	endfor
+
+
+	;Populate the sizing parameters for specdata
+	specdata.npixel = imageWidth
+	specdata.ntrack = imageHeight
+	specdata.nframe = nImages
+	specdata.ninst  = 1
+
+	; Allocation/sizing checks
+	cxf_define_spectrometer_data,specdata,err
+	if err ne '' then begin
+		print,'cxf_define_spectrometer_data error',err
+	endif
+
+	;Populate the specdata structure with the loaded data
+	specdata.intensity.data = images	
+	specdata.time = time
+	specdata.wavelength.data[*] = wavelength
+
+	specdata.exposure = 1
+
+	;stop
+	save,specdata,filename='specdata.sav'
+
+end
diff --git a/machines/w7x/cxf_w7x_read_e.pro b/machines/w7x/cxf_w7x_read_e.pro
new file mode 100644
index 0000000..56057f0
--- /dev/null
+++ b/machines/w7x/cxf_w7x_read_e.pro
@@ -0,0 +1,84 @@
+pro cxf_w7x_read_e,event
+
+	widget_control,event.top,get_uvalue=info
+
+	if event.id eq info.cancel_button then begin
+		widget_control,event.top,/destroy
+		return
+	endif
+
+	
+	if event.id eq info.ok_button then begin
+		widget_control,info.pulse,get_value=pulse
+		diagnostic=widget_info(info.diagnostic,/droplist_select)
+		
+		pulse=long(pulse)
+		
+		case diagnostic of
+			0:begin
+				signalname='ILS_Red'
+			end
+			
+			1:begin
+				signalname='ILS_Green'
+			end
+			
+			2:begin
+				signalname='ILS_Blue'
+			end
+			
+			3:begin
+				signalname='NIFS_H'
+			end
+			
+			4:begin
+				signalname='NIFS_He'
+			end
+			
+			5:begin
+				signalname='AUG1'
+			end
+			
+			6:begin
+				signalname='AUG2'
+			end
+
+			else:
+
+		endcase
+		
+	
+
+		cxf_define_spectrometer_data,specdata,err
+		if err ne '' then begin
+			print,err
+			stop
+		endif
+		
+		specdata.shot_nr=pulse
+		specdata.spectrometer.name=signalname
+		specdata.machine.name='W7X'
+
+		previousshot=long(pulse)
+		save,previousshot,filename='~/previous_shotnumber.sav'
+
+		cxf_load_w7x_data,specdata,err
+
+
+		if err ne '' then begin
+			res = dialog_message(err,/ERROR)
+			return
+		endif else begin
+			widget_control,info.topinfo_base,get_uvalue=topinfo
+
+			ptr_free,topinfo.expdata
+			topinfo.expdata=ptr_new(specdata)
+    
+			widget_control,info.topinfo_base,set_uvalue=topinfo
+			cxf_data_loaded,topinfo
+			widget_control,event.top,/destroy
+
+			return
+		endelse
+	endif
+end
diff --git a/machines/w7x/cxf_w7x_read_w.pro b/machines/w7x/cxf_w7x_read_w.pro
new file mode 100644
index 0000000..cca9658
--- /dev/null
+++ b/machines/w7x/cxf_w7x_read_w.pro
@@ -0,0 +1,38 @@
+pro cxf_w7x_read_w,topinfo_base
+
+tlb=widget_base(title='Read Data',/column)
+
+base=widget_base(tlb,/row)
+label=widget_label(base,value='Diagnostic:')
+diagnostic=widget_droplist(base,value=['ILS_Red','ILS_Green','ILS_Blue','NIFS_H','NIFS_He','AUG1','AUG2'])
+widget_control,diagnostic,set_droplist_select=2
+
+base=widget_base(tlb,/row)
+label=widget_label(base,value='Shot (YYMMDDsss:')
+; remember last number
+ out=file_test('~/previous_shotnumber.sav')
+if out eq 1 then begin
+restore,'~/previous_shotnumber.sav'
+endif
+
+if n_elements(previousshot) lt 1 then previousshot=180725038
+pulse=widget_text(base,value=string(previousshot),uvalue=string(previousshot),/editable)
+
+base=widget_base(tlb,/row)
+ok_button=widget_button(base,value='Ok')
+cancel_button=widget_button(base,value='Cancel')
+
+
+info={	topinfo_base:topinfo_base , $
+	diagnostic:diagnostic, $
+	pulse:pulse, $
+	ok_button:ok_button, $
+	cancel_button:cancel_button $
+	}
+
+widget_control,tlb,set_uvalue=info      								
+widget_control, tlb, /realize
+xmanager, 'cxf_w7x_read_w',tlb,event_handler='cxf_w7x_read_e'
+
+
+end
-- 
2.7.4

