;=============================================================================
; 
; this file is for being used with xop_ifc
;
; created: srio@esrf.fr 97-04-07
;
; changed: srio@esrf.fr 98/12/14. Adapted for xop2.0. Version 1.1.
; changed: srio@esrf.fr 03/02/03. Adapted for xop2.1. Version 1.2.
;          Removed dependencies on Shadow library. Creates now an ad-hoc
;          file with optical constants with DABAX (mlayer.f12)
;
;=============================================================================
;
;
;=============================================================================
; menu definitions
;=============================================================================
;
;
; This is the section that defines the menus in the main xop window.
; It may be customizable:
;    1) To add menus: use w(x) 20<x<100
;    2) to add calls: use wtmp=widget_...
;    3) To remove menus: comment the corresponding entry and ALL THE
;       SUBSEQUENT CALLS
;    4) To remove a call: just comment it
;    5) To change a call from one menu to a different one: just change
;       the widget identifies w(x) in the corresponding call.
<start>menu
w(1) = widget_button(w(0),VALUE='File',/MENU)
  wtmp0 = widget_button(w[1],VALUE=state.ifc.title+' input parameters', /Menu)
    wtmp = widget_button(wtmp0,VALUE='Load from file...',UValue='FILEINPUT')
    wtmp = widget_button(wtmp0,VALUE='Save to file...',UValue='FILEINPUT')
    wtmp = widget_button(wtmp0,VALUE='Save as default',UValue='FILEINPUT')
  wtmp= widget_button(w(1),VALUE='Quit', UVALUE='QUIT',/SEPARATOR)
w(2) = widget_button(w(0),VALUE='Set_Parameters', /MENU)
  wtmp= widget_button(w(2),VALUE='Set Parameters', UVALUE='SETPAR')
  wtmp= widget_button(w(2),VALUE='Set Defaults', UVALUE='SETDEF')
w(3) = widget_button(w(0),VALUE='Show',/MENU)
  wtmp= widget_button(w(3),VALUE='Plot Results',UVALUE='plot')
  wtmp= widget_button(w(3),VALUE='Show/Edit material file mlayer.f12',UVALUE='f12')
w(4) = widget_button(w(0),VALUE='Help', /Help)
  wtmp= widget_button(w(4),VALUE='mlayer', UVALUE='help_mlayer')
;
wtmp = widget_button(wbase,VALUE='Set Parameters', UVALUE='SETPAR')
if sdep() EQ 'WINDOWS' then font = 'VERDANA*BOLD*ITALIC*24' else font = '-adobe-helvetica-bold-o-normal--18-180-75-75-p-104-iso8859-1'
wtmp = WIDGET_LABEL( wbase, FONT=font,VALUE=state.ifc.window_label)
wtmp = WIDGET_LABEL( wbase, FONT=font,VALUE='  '+state.ifc.about+'  ')
<end>menu

;
;=============================================================================
; parameter definitions
;=============================================================================
;
; this section contains the parameters for creating the set_parameters window
; in xop_ifc
; Note: Do not use continuation symbols
;
<start>parameters
strarr(3)|mode|           ['0','Periodic Layers','Individual Layers']
strarr(3)|scan|           ['0','Grazing Angle','Photon Energy']
strarr(3)|f12_flag|       ['0','Create on the fly','Use existing file: mlayers.f12']
''       |Substrate|      'Si'
''       |Odd_material|   'Si'
''       |Even_material|  'W'
0.0      |energy|         8050.
0.0      |Theta|          0.00
0.0      |Scan_step|      0.01
1        |Npoints|        600
0.0      |Odd_thickness|  25.0
0.0      |Even_thickness| 25.0
1        |Nlayers|        50
''       |File|           'layers.dat'
<end>parameters
;
;=============================================================================
; interface definitions
;=============================================================================
;
; this file contains the parameters for creating the interface with xop_ifc
; Note: ALL KEYWORDS MUST BE FILLED
; Note: Do not use continuation symbols
; Note: use "run" = <none> for applications that do not use spawn
; Note: "ncol" is the number of columns in the XScrMenu screen
;
<start>interface
strarr(14)    |titles|              ['Layer periodicity: ','Scanning variable: ','Material parameters:','Substrate: ','Odd layer material (closer to vacuum): ','Even layer material (closer to substrate): ', 'Photon energy [eV]:','Grazing angle [degrees]:','Scanning variable step: ','Number of scanning points','Thickness [A] for odd material:','Thickness [A] for even material:','Number of layer pairs:','File with layer thicknesses:']
strarr(14)    |flags|               ['1','1','1','w(2) EQ 0','w(2) EQ 0','w(2) EQ 0','1','1','1','1','w(0) eq 0 and w(2) EQ 0','w(0) eq 0 and w(2) EQ 0','w(0) eq 0 and w(2) EQ 0','w(0) eq 1'] 
''            |title|               'mlayer'
''            |application|         'mlayer'
''            |window_label|        'Mlayer'
''            |version|             '1.2'
''            |about|               '  Multilayer   Reflectivity  '
StrArr(4)     |rm|                  ['xmlayer.par','mlayer.inp','mlayer.out','mlayer.f12']
0             |ncol|                2
''            |run|                 'mlayer < mlayer.inp'
0             |setWindowsHome|      1
<end>interface
;
;=============================================================================
; "write_inp" section: write input files instructions
;=============================================================================
;
; This section is used to write the input file (mlayer.inp) for the
; mlayer application. It is used by xop_ifc
; Note: All lines are executed with the "execute" command of idl,
;       thus, no continuation symbolks are allowed
; Note: Input variables are in the "par" structure (which is defined 
;       in the file mlayer.parameters .
; Note: variables a0,...,a10 can be used for scratch calculations. No 
;       other variable names are allowed.
; Note: Use as many comment lines as you want.
;
<start>write_inp
;
; write file with optical constants: mlayer.f12
;
IF StrCompress(par.f12_flag[0],/Rem) EQ 0 THEN cff =1 ELSE cff=0
IF cff THEN list = dabax_ls(NF=nf)
IF cff THEN IF nf EQ 0 THEN Message,'No dabax files found'
IF cff THEN f1f2prefix = 'f1f2'
IF cff THEN filef1f2 = list(where(strpos(list,f1f2prefix) NE -1))
IF cff THEN itmp = 0

IF cff THEN materials = [par.Substrate,par.Even_material,par.Odd_material]
IF cff THEN energy=0
IF cff THEN a=temporary(energy)
IF cff THEN f12_s=f1f2_calc(filef1f2[itmp],par.Substrate,energy)
IF cff THEN f12_e=f1f2_calc(filef1f2[itmp],par.Even_material,energy)
IF cff THEN f12_o=f1f2_calc(filef1f2[itmp],par.Odd_material,energy)

IF cff THEN openw,unit,'mlayer.f12',/get_lun
IF cff THEN printf,unit,'; File created by xop/dabax ('+filef1f2[itmp]+') on '+SysTime()+' for materials [substrate,even,odd]: '+Vect2String(materials)
IF cff THEN printf,unit,'; Atomic mass: '
IF cff THEN printf,unit,atomic_constants(materials[0],return='AtomicMass'), atomic_constants(materials[1],return='AtomicMass'), atomic_constants(materials[2],return='AtomicMass')
IF cff THEN printf,unit,'; Density: '
IF cff THEN printf,unit,atomic_constants(materials[0],return='Density'), atomic_constants(materials[1],return='Density'), atomic_constants(materials[2],return='Density')
IF cff THEN printf,unit,'; Number of energy points: '
IF cff THEN printf,unit,N_Elements(energy)
IF cff THEN printf,unit,'; For each energy point, energy[eV], f1[substrate], f2[substrate], f1[even], f2[even], f1[odd], f2[off]: '
IF cff THEN FOR ijj=0,N_Elements(energy)-1 DO printf,unit,energy[ijj], f12_s[0,ijj],f12_s[1,ijj], f12_e[0,ijj],f12_e[1,ijj], f12_o[0,ijj],f12_o[1,ijj],Format='(f12.4,6f14.8)'
IF cff THEN free_lun,unit

IF CheckFile('mlayer.f12') EQ 0 THEN itmp = Dialog_Message(/Error,'File not found: mlayer.f12')

;
; write input file for Fortran mlayer: mlayer.inp
;
openw,a10,'mlayer.inp',/get_lun
if ((strcompress(par.scan(0),/rem) eq '0') and (strcompress(par.mode(0),/rem) eq '0')) then a0 = 1
if ((strcompress(par.scan(0),/rem) eq '1') and (strcompress(par.mode(0),/rem) eq '0')) then a0 = 5
if ((strcompress(par.scan(0),/rem) eq '0') and (strcompress(par.mode(0),/rem) eq '1')) then a0 = 3
if ((strcompress(par.scan(0),/rem) eq '1') and (strcompress(par.mode(0),/rem) eq '1')) then a0 = 5
printf,a10,a0
printf,a10,'N'

; printf,a10,p_table(par.Substrate,/SHADOW)
; printf,a10,p_table(par.Substrate,/DENS)
; printf,a10,p_table(par.Odd_material,/SHADOW)
; printf,a10,p_table(par.Odd_material,/DENS)
; printf,a10,p_table(par.Even_material,/SHADOW)
; printf,a10,p_table(par.Even_material,/DENS)
;srio printf,a10,par.Substrate+' '
;srio printf,a10,Atomic_Constants(par.Substrate,ret='Density')
;srio printf,a10,par.Odd_material+' '
;srio printf,a10,Atomic_Constants(par.Odd_material,ret='Density')
;srio printf,a10,par.Even_material+' '
;srio printf,a10,Atomic_Constants(par.Even_material,ret='Density')

printf,a10,Physical_Constants('hc')/par.energy
printf,a10,par.theta
a1=par.scan_step
if (strcompress(par.scan(0),/REM) eq '0') then printf,a10,a1
a2=Physical_Constants('hc')/par.energy
a3=Physical_Constants('hc')/(par.energy+par.scan_step)
a4=a3-a2
if (strcompress(par.scan(0),/REM) ne '0') then printf,a10,a4
printf,a10,par.Npoints
if (strcompress(par.mode(0),/rem) eq '0') then printf,a10,par.Nlayers
if (strcompress(par.mode(0),/rem) eq '0') then  if (a0 ne 5) then printf,a10,par.odd_thickness,par.even_thickness else for i=1,par.Nlayers do printf,a10,par.odd_thickness,par.even_thickness
if (strcompress(par.mode(0),/rem) ne '0') then  a5=read_textfile(par.file)
if (strcompress(par.mode(0),/rem) ne '0') then  par.Nlayers=n_elements(a5)
if (strcompress(par.mode(0),/rem) ne '0') then PRINT,'Number of layers in '+par.file+' file is: '+strcompress(par.Nlayers,/REM)
if (strcompress(par.mode(0),/rem) ne '0') then printf,a10,par.Nlayers
if (strcompress(par.mode(0),/rem) ne '0') then for i=0, par.Nlayers-1 do printf,a10,a5(i)
printf,a10,'xmlayer.par'
printf,a10,'mlayer.out'
printf,a10,'6'
free_lun,a10
<end>write_inp
;
;=============================================================================
; "plot" section: plotting results
;=============================================================================
;
;
; This section is used to plot the output data of the mlayer application.
; It is used by xop_ifc
; Note: All lines are executed with the "execute" command of idl,
;       thus, no continuation symbols are allowed
; Note: Input variables are in the "par" structure (which is defined 
;       in the file mlayer.parameters .
; Note: variables a0,...,a20 can be used for scratch calculations. No 
;       other variable names are allowed.
; Note: Use as many comment lines as you want.
;
<start>plot
Widget_Control,/HourGlass
IF (checkfile('mlayer.out') NE 1) THEN a0 =widget_message(/ERROR,['mlayer data not found...','Set Parameters before...  '])
IF (checkfile('mlayer.out') NE 1) THEN return
IF StrCompress(par.f12_flag[0],/Rem) EQ 0 THEN a1='ML '+strcompress(par.odd_material,/REM)+'('+ strcompress(par.odd_thickness,/rem)+' A):'+strcompress(par.even_material,/REM)+'('+strcompress(par.even_thickness,/rem)+' A) '+strcompress(par.Nlayers,/rem)+' pairs; '
IF StrCompress(par.f12_flag[0],/Rem) EQ 1 THEN a1='ML materials from file mlayer.f12 - '
if (strcompress(par.scan(0),/rem) eq '0') then a2 = 'grazing angle !4 h !3 [deg]'
if (strcompress(par.scan(0),/rem) eq '0') then a1 = a1+'E = '+strcompress(par.energy,/rem)+' eV'
if (strcompress(par.scan(0),/rem) ne '0') then begin a2 = 'Energy [eV]'
if (strcompress(par.scan(0),/rem) ne '0') then a1=a1+'!4h!3 = '+strcompress(par.theta,/re)+' deg'
a0 = rascii('mlayer.out')
if (strcompress(par.scan(0),/REM) eq '1') then a0(0,*) = Physical_Constants('hc')/a0(0,*)
a3 = [a2,'s reflectivity','p reflectivity','average reflectivity','s phase shift','p phase shift','(s electric field)^2','(p electric field)^2']
XPLOT,a0,YCOL=2,COLTITLES=a3,XTITLE='-1',ytitle='-1',TITLE=a1,/YLOG
<end>plot


;
;=============================================================================
; "show" section: Edit/View file mlayer.f12
;=============================================================================
;
<start>f12
Widget_Control,/HourGlass
XDisplayFileNative,'mlayer.f12'
<end>f12




;
;=============================================================================
; "help_mlayer" section: help for xmlayer
;=============================================================================
;
<start>help_mlayer
a0 = sdep(/ds)
a1=getenv('XOP_HOME')+a0+'doc'+a0+'txt'+a0+'mlayer.txt'
Xdisplayfile1,a1
<end>help_mlayer
