;=============================================================================
; 
; this file is for being used with xop_ifc
;
; Black Body distribution application
;
; created: srio@esrf.fr 97-09-12 
; modified:
;   02/06/04 srio@esrf.fr customized for black body radiation
;
;=============================================================================
;
;=============================================================================
; 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='Write files for xop/Optics', UVALUE='write_f_optics')
  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='View Results',UVALUE='view')
w(4) = widget_button(w(0),VALUE='Help', /Help)
  wtmp= widget_button(w(4),VALUE='Black body', UVALUE='help_black_body')
;
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
'Thermal source: Planck distribution' |TITLE
1.2D6        |TEMPERATURE
10.0         |E_MIN
1000.0       |E_MAX
500          |NPOINTS
<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(5)    |titles| ['Title','Temperature [K]','Min energy [eV]','Max energy [eV]','Number of points ']
strarr(5)     |flags| ['1','1','1','1','1']
''            |title|               'black_body'
''            |application|         'black_body'
''            |window_label|        'Black Body Radiation'
''            |version|             '1.0'
''            |about|               'Planck distribution '
''            |rm|                  ''
0             |ncol|                1
''            |run|                 '<none>'
0             |setWindowsHome|      0
<end>interface
;
;=============================================================================
; "plot" section: plotting results
;=============================================================================
;
;
<start>plot
Widget_Control,/HourGlass
a0 = par.TEMPERATURE
kb = physical_constants('kb')/physical_constants('ec') ; 8.6e-5 eV/K
print,'T [K]: ',a0
print,'kb*T [eV]: ',a0*kb
e_ev = MakeArray1(par.NPoints,par.e_min,par.e_max)
e_kt = e_ev/(a0*kb)
brightness=3.146D11*(a0*kb)^3*e_kt^3/(exp(e_kt)-1)
;print,3.146D11*(a0*kb)^3*e_kt^3;/(exp(e_kt)-1)
;print,e_kt
a3 = DblArr(4,par.NPoints)
a3[0,*]=reform(e_ev)
a3[1,*]=reform(e_kt)
a3[2,*]=brightness
a3[3,*]=brightness*1d3*physical_constants('ec')
xplot,a3,coltitles=['Photon energy [eV]','Photon energy/(Kb*T)','Brightness [Photons/sec/mm2/mrad2/0.1%bw','Spectral Power [Watts/eV/mrad2/mm2]'],xtitle='-1',ytitle='-1',wtitle='Black body radiation at T='+StrCompress(par.Temperature)
<end>plot
;
;=============================================================================
; "view" Show/View Results menu
;=============================================================================
;
<start>view
a0 = par.TEMPERATURE
kb = physical_constants('kb')/physical_constants('ec') ; 8.6e-5 eV/K
a6 = [' ','Results of Black Body Radiation: Planck distribution',' ']
a6 = [a6,'-------------------------------------------------------------']
a6 = [a6,'Temperature          T[K] = '+strcompress(a0,/rem)]
a6 = [a6,'Minimum photon energy [eV] = '+strcompress(par.E_min,/rem)]
a6 = [a6,'Maximum photon energy [eV] = '+strcompress(par.E_max,/rem)]
a6 = [a6,'-------------------------------------------------------------']
a6 = [a6,'Kb*T                [eV] = '+strcompress(a0*kb,/rem)]
a6 = [a6,'Peak at 2.822*Kb*T  [eV] = '+strcompress(2.822*a0*kb,/rem)]
a6 = [a6,'-------------------------------------------------------------']
Xdisplayfile1,text=a6,title='black_body results'
<end>view
;
;=============================================================================
; "write_f_optics" section: write files for nop/optics
;=============================================================================
;
<start>write_f_optics

Widget_Control,/HourGlass
a0 = par.TEMPERATURE
kb = physical_constants('kb')/physical_constants('ec') ; 8.6e-5 eV/K
print,'T [K]: ',a0
print,'kb*T [eV]: ',a0*kb
e_ev = MakeArray1(par.NPoints,par.e_min,par.e_max)
e_kt = e_ev/(a0*kb)
brightness = 3.146D11*(a0*kb)^3*e_kt^3/(exp(e_kt)-1)
a3 = DblArr(4,par.NPoints)
a3[0,*]=reform(e_ev)
a3[1,*]=reform(e_kt)
a3[2,*]=brightness
a3[3,*]=brightness*1d3*physical_constants('ec')
a6=a3[[0,2],*]
a7=a3[[0,3],*]


openw,a8,'SRCOMPE',/get_lun
printf,a8,'!    File written by Xop/black_body containig a black body radiation spectrum'
printf,a8,'!    Column 1:  Photon Energy [eV] Column 2: Brightness[Photons/sec/mrad2/mm2/0.1%bw]'
printf,a8,'!    '
printf,a8,'!    The next commented lines include the inputs for XOP/black_body'
printf,a8,'!    Save these lines to a file (e.g., black_body.xop) and removed the !  symbols'
printf,a8,'!    Then you can load it in Xop/black_body  '
printf,a8,"!; xop/xop_ifc,'black_body'(v"+ifc.version+") input file on "+SysTime()
printf,a8,'!'+Write_Str(par)
printf,a8,'!    '
printf,a8,a6
free_lun,a8
openw,a8,'SRCOMPW',/get_lun
printf,a8,'!    File written by Xop/black_body containig a black body radiation spectrum'
printf,a8,'!    Column 1:  Photon Energy [eV] Column 2: Spectral power [Watts/eV/mrad2/mm2'
printf,a8,'!    '
printf,a8,'!    The next commented lines include the inputs for XOP/black_body'
printf,a8,'!    Save these lines to a file (e.g., black_body.xop) and removed the !  symbols'
printf,a8,'!    Then you can load it in Xop/black_body  '
printf,a8,"!; xop/xop_ifc,'black_body'(v"+ifc.version+") input file on "+SysTime()
printf,a8,'!'+Write_Str(par)
printf,a8,'!    '
printf,a8,a7
free_lun,a8
a8 = Widget_Message(/Question,['Files SRCOMPE and SRCOMPW contaning the','Flux and Power spectra, respectively, written to disk.','They can be used for optics calculation with Xop|Optics|XPower',' ','Do you want to plot them [for checking purposes]?'])
IF a8 EQ 'Yes' THEN Xplot,/no_block,'SRCOMPE',Title='File: SRCOMPE',XTitle='Photon energy [eV]',YTitle='Brightness[Photons/sec/0.1%bw/mm2/mrad2]'
IF a8 EQ 'Yes' THEN Xplot,/no_block,'SRCOMPW',Title='File: SRCOMPW',XTitle='Photon energy [eV]',YTitle='Spectral power [Watts/eV/mrad2/mm2]'


<end>write_f_optics
;
;=============================================================================
; "help" section: help for black body distribution 
;=============================================================================
;
<start>help_black_body
a0 = sdep(/ds)
a1=getenv('XOP_HOME')+a0+'doc'+a0+'txt'+a0+'black_body.txt'
Xdisplayfile1,a1
<end>help_black_body
