#! /usr/bin/python	
	
import string, sys, cgi, cgitb
from math import *

######################################################	
#	loading of input_...()	
######################################################	
	
from input import *

######################################################	
#	loading of output_...()	
######################################################	
	
from output import *
	
######################################################	
#	loading of ini_read() and ini_read_spectrum()	
#####################################################	
	
from ini_read import *	
	
######################################################	
#	loading of functions()	
######################################################	
	
from functions import *	
	
######################################################	
#	loading of html_elements()	
######################################################	
	
from html_elements import *	
	
######################################################	
#	physical constants	
######################################################	
	
c=2.9983e8	
h=6.63e-34	
k=1.3807e-23	
	
#####################################################	
#	some variable pre-definitions
######################################################	
	
ini_filename='etc.ini'
ini_spectrum_filename='etc_spectrum.ini'
ini_output_filename='etc_output.ini'
Airmass=0.
Atmosphere_def={}
Atmosphere_choice = ''
Background_def={}
Background_choice = ''
Camera_choice = '' #camera	
Camera_def={}
Center_pixel=0.0
Center_pixel_1=0.0
Correction_def={}
Debug = 0
Detector = 0
Dispersion_choice=0.0
Dispersion_wavelength_list=[]
DIT = 0.0
EW_def={}
Filter_choice = '' #filter for observation
Filter_def={}
Filter_def_mag={}
Filter_mag = '' #filter for the given magnitude
Flag = 0
Fluxfactor=0.0
Grating_def={}
Grating_wavelength=0.0
Hawaii_def={}
Mode=0
Object_brightness_choice = 0.0 #magnitude
Object_choice = ['','','']
Output={}
Photons=0.0
Photons_max_dit_1=0.0
Photons_max_dit_2=0.0
Photons_object=0.
Photons_object_1=0
Photons_sky=0.
Photons_sky_1=0
Photons_standard_1=0
Redshift = 0.0
Seeing = 0.0
Sky=0.0
Sky_brightness = 0.0
Slit_begin=0.0
Slit_end=0.0
Slith_width=0.0
SN_DIT = 0.0
Source = ''#Point source or enxtendet source
Spectral_resolution=0.0
Spectrum_def={}
Standard_star_spectrum_def={}
Strehl=0.0
Two=0
Water_vapor=0.
Wavelength_list=[]
	
	
######################################################	
#	
#	web input or command input
#	
######################################################	
	
if len(sys.argv) > 1:
	Web = 2
else:
	Web = 1

######################################################	
#	
#	reading the ini-files	
#	
######################################################	

ini_read_main(ini_filename)			#initiates the basic values
ini_read_spectrum(ini_spectrum_filename) 	#initiates the additional values for spectroscopy mode
Output=ini_read_output(ini_output_filename,Web)	#initiates the output parameters

######################################################	
#	user input and Web form generation	
######################################################	
	
Formdata  = cgi.FieldStorage()

if Formdata.has_key("Calibration"):
	Debug = 2

if len(sys.argv) > 1:
	try:
		Debug=int(sys.argv[2])
	except:
		Debug = 0

if Debug == 1 or Formdata.has_key("Debug"):
	File_debug=open("debug.txt","w")
	cgitb.enable()

if len(sys.argv) > 1:
	Formdata = sys.argv[1]

if Web < 2:
	if Formdata.has_key('Button') == False and len(sys.argv) == 1:
		Central_filter = ""
		try:
			if Formdata['Lucifermode'].value == "Spectroscopy":
				Grating_choice_old = Formdata['Hidden_old'].value
				try:
					Grating_choice_new = Formdata['Central_wavelength'].value
				except:
					Grating_choice_new = Formdata['Hidden_actual'].value
			if Grating_choice_old != Grating_choice_new:
				Flag = 1
		except:
			pass
		html_java(Formdata,Flag,Output,Debug)	# web form generation with JavaScript
		sys.exit()	

(Source, Object_choice[0], Object_choice[1], Object_choice[2], Filter_mag, Object_brightness_choice, Detector, Filter_choice, Camera_choice, Slit_width, Atmosphere_choice, Background_choice, Sky_brightness, Strehl, Seeing, Grating_choice, Central_wavelength, Lpmm, DIT, SN, Tau, Mode, Error)=user_input(Web,Formdata,Output)

Scale=LUCIFER[Camera_choice][1]
if Error == 1:	
	Title = "Wrong input!!"
	output_header(Web, Title, Error,Output)
	output_summary(Web, Source, Object_choice, Filter[Filter_mag][5], Object_brightness_choice, Detector, Hawaii_conditions, Filter[Filter_choice][5], Camera_choice, Slit_width, Dispersion_choice, Water_vapor, Airmass, Background_choice, Sky_brightness, Strehl, Seeing, Central_wavelength, Lpmm, SN, DIT, Tau, Scale, Mode, Error,Output)
	sys.exit()

if Debug == 2 and (Mode != 1 and Mode != 5) and Web == 2:
	print "Calibration mode can be used in seeing-limited imaging mode only"
	sys.exit()

if Mode % 2 <> 0:	
	Photons_object=0.	
	Photons_sky=0.	
	SN_DIT = 0.0	
	Spectral_resolution=0.0005	
else:	
	Photons_object_dit={}	
	Photons_sky_dit={}	
	Photons_standard={}	
	SN = {}	
	SN_DIT = {}	
	SN_tau={}	
	String="%s_%.3f_%d_%s" % (Camera_choice, Central_wavelength, Lpmm, Detector)
	Dispersion_choice = Dispersion[String]
	Spectral_resolution=Slit_width / Scale * Dispersion_choice	
	if Spectral_resolution < Dispersion_choice:	
		Spectral_resolution = Dispersion_choice
		Slit_width=Scale
Water_vapor=Atmosphere_conditions[Atmosphere_choice][1]	
Airmass=Atmosphere_conditions[Atmosphere_choice][2]	
	
if Object_choice[0]=='t':
	if Object_choice[1].find('galaxy') <> -1:
		Redshift = float(Object_choice[2])
	else:
		Redshift = 0.0
if Object_choice[0]=='b':	
	Blackbody_temperature=Object_choice[1]
if Object_choice[0] <> 'b' and Object_choice[0] <> 't':	
	try:	
		Line_wavelength=float(Object_choice[0]) # every wavelength in micron	
	except:	
		Line_wavelength=Object_choice[0]	
	try:	
		Line_flux=float(Object_choice[1])/1e3 # flux given in ergs/s/cm^2. Conversion in W/m^2	
	except:	
		Line_flux=Object_choice[1]	
	try:	
		Line_width=float(Object_choice[2])/1e4 # wavelength given in A -> conversion in micron	
	except:	
		Line_width=Object_choice[2]	
j = 0
List = []
try:
#if 1:
	######################################################
	#
	#	Transmission of light in the technical equipment
	#
	######################################################
	T=LBT['Transmission']
	LBT_area = pi*LBT['Diameter/m']*LBT['Diameter/m']*0.25
	
	if Object_choice[0] == "b" or Object_choice[0] == "t":
		######################################################
		#
		#	calculation of the flux factor from 0 mag to 
		#	observed mag
		#
		######################################################

		######################################################
		#
		#	loading filter transmission
		#
		######################################################
		(Filter_def_mag, Filter[Filter_mag])=linear_interpolation_new(Filter[Filter_mag][0],Filter_def_mag, Filter[Filter_mag],0.0,0.0,0.0005, 0.0)
		Wavelength_end=Filter[Filter_mag][3]
		Wavelength_start=Filter[Filter_mag][2]
		Wavelength_step=Filter[Filter_mag][4]

		######################################################
		#
		#	loading standard-star's fluxdensity
		#
		######################################################
		(Standard_star_spectrum_def, Standard_star_spectrum['A0V_cal'])=linear_interpolation_new(Standard_star_spectrum['A0V_cal'][0],Standard_star_spectrum_def, Standard_star_spectrum['A0V_cal'],Wavelength_start,Wavelength_end,Wavelength_step, 0.0)
		######################################################
		#
		#	loading model spectrum
		#
		######################################################
		if Object_choice[0] == 't':
			if Object_choice[1] <> 'uniform':
				(Spectrum_def, Spectrum[Object_choice[1]])=linear_interpolation_new(Spectrum[Object_choice[1]][0],Spectrum_def, Spectrum[Object_choice[1]],Wavelength_start,Wavelength_end,Wavelength_step, Redshift)
			else:
				i=Wavelength_start
				while i<=Wavelength_end:
					String="%.10f" % i
					Spectrum_def[String]=1
					i=i+Wavelength_step
		if Object_choice[0]=='b':
			i=Wavelength_start
			while i<=Wavelength_end:
				String="%.10f" % i
				Spectrum_def[String]=(1/pow(i/1e6,5)/(exp(h*c/(k*i/1e6*float(Blackbody_temperature)))-1))
				i=i+Wavelength_step
		######################################################
		#
		#	Fluxfactor: The factor of 0-mag object and user-mag 
		#	Photons=sum(from Wavelength_start to Wavelength_end)(Fluxdensity*Wavelength_step*Wavelength/c/h)
		#
		######################################################
		i=Wavelength_start
		while i <Wavelength_end:
			String="%.10f" % i
			Photons_standard_1=Photons_standard_1+Standard_star_spectrum_def[String]*i*1000.*Filter_def_mag[String]#*Atmosphere_def[String]
			Photons_object_1=Photons_object_1+Spectrum_def[String]*i*1000.*Filter_def_mag[String]#*Atmosphere_def[String]
			i=i+Wavelength_step
		Photons_standard_1=Photons_standard_1*Wavelength_step*1e-6/(h*c)
		Photons_object_1=Photons_object_1*Wavelength_step*1e-6/(h*c)
		Fluxfactor=pow(10, -0.4*(Object_brightness_choice-(2.5*log10(Photons_standard_1/Photons_object_1))))
		######################################################
	else:
		Fluxfactor=1.
	######################################################
	#
	#	Definition of the wavelength range for the 
	#	calculations
	#
	######################################################

	if Mode % 2 == 0:
		Wavelength_shift = float(Output['wavelength_shift'])
		Pixel_shift = Wavelength_shift / Dispersion_choice
		Wavelength_step = Spectral_resolution
		Temp = Central_wavelength - (float(Hawaii_conditions["Number_of_pixel_w_%s" % Detector])/2. + Pixel_shift)*Dispersion_choice
		Wavelength_start = int((Temp-.2)/Wavelength_step)*Wavelength_step+0.2
		Temp = Central_wavelength + (float(Hawaii_conditions["Number_of_pixel_w_%s" % Detector])/2. + Pixel_shift)*Dispersion_choice
		Wavelength_end = int((Temp-.2)/Wavelength_step)*Wavelength_step+0.2+Wavelength_step
		#print Wavelength_start, Wavelength_end, Wavelength_step
	else:
		Filter[Filter_choice]=header_read(Filter[Filter_choice][0],Filter[Filter_choice])
		Wavelength_step=Filter[Filter_choice][4]
		Wavelength_start = int((float(Filter[Filter_choice][2])-.2)/Filter[Filter_choice][4])*Filter[Filter_choice][4]+0.2
		if Wavelength_start < float(Filter[Filter_choice][2]):
			Wavelength_start = Wavelength_start + Wavelength_step
		Wavelength_end = int((float(Filter[Filter_choice][3])-.2)/Filter[Filter_choice][4])*Filter[Filter_choice][4]+0.2+Filter[Filter_choice][4]
		if Wavelength_end > float(Filter[Filter_choice][3]):
			Wavelength_end = Wavelength_end - Wavelength_step

	######################################################
	#
	#	loading filter transmission
	#
	######################################################
	(Filter_def, Filter[Filter_choice])=linear_interpolation_new(Filter[Filter_choice][0],Filter_def, Filter[Filter_choice],Wavelength_start, Wavelength_end, Wavelength_step, 0.0)
	######################################################
	#
	#	loading standard-star's fluxdensity
	#
	######################################################
	Standard_star_spectrum_def={}
	(Standard_star_spectrum_def, Standard_star_spectrum['A0V_cal'])=linear_interpolation_new(Standard_star_spectrum['A0V_cal'][0],Standard_star_spectrum_def, Standard_star_spectrum['A0V_cal'],Wavelength_start,Wavelength_end,Wavelength_step, 0.0)
	######################################################
	#
	#	loading model spectrum
	#
	######################################################
	# template: galaxy, star, uniform
	if Object_choice[0] == 't':
		if Object_choice[1] <> 'uniform':
			Spectrum_def = {}
			(Spectrum_def, Spectrum[Object_choice[1]])=linear_interpolation_new(Spectrum[Object_choice[1]][0],Spectrum_def, Spectrum[Object_choice[1]],Wavelength_start,Wavelength_end,Wavelength_step, Redshift)
		else:
			i=Wavelength_start
			while i<=Wavelength_end:
				String="%.10f" % i
				Spectrum_def[String]=1
				i=i+Wavelength_step
	# blackbody
	if Object_choice[0]=='b':
		i=Wavelength_start
		while i<=Wavelength_end:
			String="%.10f" % i
			Spectrum_def[String]=(1/pow(i/1e6,5)/(exp(h*c/(k*i/1e6*float(Blackbody_temperature)))-1))
			i=i+Wavelength_step
	# single line
	if Object_choice[0] <> 'b' and Object_choice[0] <> 't':
		i=Wavelength_start
		Sigma = Line_width / (2.*sqrt(2.*log(2.)))
		while i<=Wavelength_end:
			String="%.10f" % i
			Spectrum_def[String]=Line_flux/(sqrt(2.*pi)*Sigma*1e3)*exp(-0.5*(i-Line_wavelength)*(i-Line_wavelength)/(Sigma*Sigma)) # Gauss
			# The factor 1e3 after Sigma is the conversion of the Line_flux from W/cm^2/micron -> W/cm^2/nm
			if Debug == 1:
				Temp="%s\t%E\n" % (String, Spectrum_def[String])
				File_debug.write(Temp)
			i=i+Wavelength_step
	######################################################
	#
	#	loading entrance-window transmission
	#
	######################################################
	(EW_def, EW[Detector])=linear_interpolation_new(EW[Detector][0],EW_def, EW[Detector],Wavelength_start, Wavelength_end, Wavelength_step, 0.0)
	######################################################
	#
	#	loading grating efficiency
	#
	######################################################
	if Mode % 2 == 0:
		(Grating_def, Grating[Grating_choice])=linear_interpolation_new(Grating[Grating_choice][0],Grating_def, Grating[Grating_choice],Wavelength_start, Wavelength_end, Wavelength_step, 0.0)
	######################################################
	#
	#	loading atmosphere transmission
	#
	######################################################
	(Atmosphere_def, Atmosphere[Atmosphere_choice])=linear_interpolation_new(Atmosphere[Atmosphere_choice][0],Atmosphere_def, Atmosphere[Atmosphere_choice],Wavelength_start, Wavelength_end, Wavelength_step, 0.0)
	######################################################
	#
	#	loading/creating background spectrum
	#
	######################################################
	if Background_choice <> "0" and Sky_brightness==-27:
		(Background_def, Background[Background_choice])=linear_interpolation_new(Background[Background_choice][0],Background_def, Background[Background_choice],Wavelength_start, Wavelength_end, Wavelength_step, 0.0)
	if Background_choice <> "0" and Sky_brightness==-17:
		Background[Background_choice]=[Background_choice, 0, Atmosphere[Atmosphere_choice][2], Atmosphere[Atmosphere_choice][3], Wavelength_step]
		(Background_def, Background[Background_choice])=background_theoretical(Atmosphere_def, Atmosphere_conditions[Atmosphere_choice][2], Background_choice, Background_def, Background[Background_choice][2], Background[Background_choice][3], Wavelength_step)
	if Background_choice == "0":
		##############################################
		# uniform sky for a given sky brightness
		##############################################
		i=Wavelength_start
		Photons_standard_1 = 0.0
		Photons_sky_1 = 0.0
		while i<=Wavelength_end:
			String="%.10f" % i
			Photons_standard_1=Photons_standard_1+Standard_star_spectrum_def[String]*Wavelength_step*1e-6/(h*c)*i*1000.*Filter_def[String]
			Photons_sky_1=Photons_sky_1+Wavelength_step*1e-6/(h*c)*i*1000.*Filter_def[String]
			i=i+Wavelength_step
		Fluxfactor_sky=pow(10, -0.4*(Sky_brightness-(2.5*log10(Photons_standard_1/Photons_sky_1))))
		##############################################
		i=Wavelength_start
		while i<=Wavelength_end:
			String="%.10f" % i
			Background_def[String]=i*1e-6/(h*c)*Fluxfactor_sky*(-0.000278719*Airmass*Airmass*Airmass-0.0653841*Airmass*Airmass+1.11979*Airmass-0.0552132)
			i=i+Wavelength_step
	######################################################
	#
	#	loading transmission of camera
	#
	######################################################
	(Camera_def, Camera[Camera_choice])=linear_interpolation_new(Camera[Camera_choice][0],Camera_def, Camera[Camera_choice],Wavelength_start, Wavelength_end, Wavelength_step, 0.0)
	######################################################
	#
	#	loading QE of Hawaii-2 detector
	#
	######################################################
	Temp="File_%d" % int(Detector)
	(Hawaii_def, Hawaii[Temp])=linear_interpolation_new(Hawaii[Temp][0],Hawaii_def, Hawaii[Temp],Wavelength_start, Wavelength_end, Wavelength_step, 0.0)
	######################################################
	#
	#	loading correction
	#
	######################################################
	if Mode % 2 <> 0:
		Temp = "image_%d" % int(Detector)
	else:
		Temp = "spec_%d" % int(Detector)
	(Correction_def, Correction[Temp])=linear_interpolation_new(Correction[Temp][0],Correction_def, Correction[Temp],Wavelength_start, Wavelength_end, Wavelength_step, 0.0)
	######################################################
	#
	#	Definition of Beta, N_pix and Slit_transmission
	#
	######################################################
	if Source == 'Point source':
		Beta=2.5	#Exponent of the Moffatfunction (1+(R/parameter_a)^2)^parameter_b.
		if Mode == 1 or Mode == 5:
			N_pix=pi*(Seeing/Scale)*(Seeing/Scale)
		elif Mode == 3 or Mode == 7:
			N_pix=pi*((2.*degrees(asin(3.8317/pi*Filter[Filter_choice][6]*1e-6/LBT['Diameter/m']))*3600.)/Scale)**2.
		elif Mode == 2:
			N_pix=2.*(Seeing/Scale)
			Slit_transmission=gauss_func_int(-Slit_width/2., Slit_width/2., -Seeing, Seeing, Seeing)
		elif Mode == 4:
			N_pix=2.*((2.*degrees(asin(3.8317/pi*Filter[Filter_choice][6]*1e-6/LBT['Diameter/m']))*3600.)/Scale)
			Slit_transmission=psf_func_int(Beta, -Slit_width/2., Slit_width/2., -N_pix/2.*Scale, N_pix/2.*Scale, Seeing, Filter[Filter_choice][6]*1e-6/2., LBT['Diameter/m'], Strehl)
	if Source == 'Extended source':
		N_pix=1



	######################################################
	#
	#	Number of photons per second from object+Sky
	#	Photons=sum(from Wavelength_start to Wavelength_end)(Fluxdensity*Wavelength_step*Wavelength/c/h)
	#	Fluxdensity = [W/m^2/nm]
	#	Sky = [photons/s/nm/arcsec^2/m^2]
	#
	######################################################
	if Mode % 2 <> 0:
		i=Wavelength_start
		Photons_standard=0
		Photons_standard_1=0
		Photons_object=0
		Photons_object_1=0
		Photons_sky=0
		while i<=Wavelength_end:
			String="%.10f" % i
			Photons_object=Photons_object+Spectrum_def[String]*i*1000.*Filter_def[String]*Hawaii_def[String]*Atmosphere_def[String]*T*EW_def[String]*Camera_def[String]*Fluxfactor*Correction_def[String]
			Photons_object_1=Photons_object_1+Spectrum_def[String]*i*1000.*Filter_def[String]*Fluxfactor*Correction_def[String]
			Photons_standard=Photons_standard+Standard_star_spectrum_def[String]*i*1000.*Filter_def[String]*Hawaii_def[String]*Atmosphere_def[String]*T*EW_def[String]*Camera_def[String]*Correction_def[String]
			Photons_standard_1=Photons_standard_1+Standard_star_spectrum_def[String]*i*1000.*Filter_def[String]*Correction_def[String]
			Photons_sky=Photons_sky+Background_def[String]*1000.*Filter_def[String]*Hawaii_def[String]*T*EW_def[String]*Camera_def[String]*Correction_def[String]
			i=i+Wavelength_step
		Photons_object=Photons_object*Wavelength_step*1e-6/(h*c)*LBT_area
		Photons_object_1=Photons_object_1*Wavelength_step*1e-6/(h*c)
		Photons_standard=Photons_standard*Wavelength_step*1e-6/(h*c)*LBT_area
		Photons_standard_1=Photons_standard_1*Wavelength_step*1e-6/(h*c)
		Photons_sky=Photons_sky*Wavelength_step*LBT_area*Scale*Scale
		if Debug == 2:
			Photons_cali = Photons_object
		else:
			Photons_cali = 0.0
	####################################################################################
	#
	#	Integration over the psf or over one pixel
	#
	####################################################################################
		if Mode <> 3 and Mode <> 7:
			if Source == 'Point source':
				Photons_object = Photons_object*gauss_int(Seeing,Seeing)
				Center_pixel=Photons_object*gauss_func_int(-Scale/2., Scale/2., -Scale/2., Scale/2., Seeing)#+Photons_sky
			if Source == 'Extended source':
				Photons_object=Photons_object*Scale*Scale
				Center_pixel=Photons_object#+Photons_sky	
		else:
			if Source == 'Point source':
				Photons_object = Photons_object*psf_int(2.*degrees(asin(1.22*Filter[Filter_choice][6]*1e-6/LBT['Diameter/m']))*3600., Beta, Seeing, Filter[Filter_choice][6]*1e-6, LBT['Diameter/m'], Strehl)
				Center_pixel=Photons_object*psf_func_int(Beta, -Scale/2., Scale/2., -Scale/2., Scale/2., Seeing, Filter[Filter_choice][6]*1e-6, LBT['Diameter/m'], Strehl)#+Photons_sky
			if Source == 'Extended source':
				Photons_object=Photons_object*Scale*Scale
				Center_pixel=Photons_object#+Photons_sky
	##############################################################
	#
	#	Calculations of the Photons for 1 DIT
	#
	##############################################################
		if DIT < Hawaii_conditions['Min_DIT_%d' % int(Detector)]:
			DIT = Hawaii_conditions['Min_DIT_%d' % int(Detector)]
		Photons_object_dit=Photons_object*DIT
		Photons_sky_dit=Photons_sky*DIT
		Center_pixel_dit=Center_pixel*DIT
	##############################################################
	#
	#	Calculation of the SNR for 1 DIT
	#
	##############################################################
		try:
			SN_DIT=Photons_object_dit/sqrt(Photons_object_dit+N_pix*(Photons_sky_dit+Hawaii_conditions['RON_%d' % int(Detector)]*Hawaii_conditions['RON_%d' % int(Detector)]+Hawaii_conditions['DC_%d' % int(Detector)]*DIT))
			SN_center_DIT = Center_pixel_dit/sqrt(Center_pixel_dit+(Photons_sky_dit+Hawaii_conditions['RON_%d' % int(Detector)]*Hawaii_conditions['RON_%d' % int(Detector)]+Hawaii_conditions['DC_%d' % int(Detector)]*DIT))
		except:
			Error = 1
	##############################################################
	#
	#	Computation of the nescessary exposuretime
	#
	##############################################################
		if Mode == 1 or Mode == 3:
			try:	
				Tau=(SN/SN_DIT)*(SN/SN_DIT)*DIT
				if Tau < DIT:
					Tau = DIT
			except:
				Error = 1
	##############################################################
	#
	#	Computation of the SNR for a given exposuretime
	#
	##############################################################
		if Mode == 5 or Mode == 7:
			try:
				SN=sqrt(Tau/DIT)*SN_DIT
			except:
				Error = 1
	##############################################################
	#
	#	Calculations for a given exposuretime
	#
	##############################################################	
		try:
			SN_center_tau=sqrt(Tau/DIT)*SN_center_DIT
			Center_pixel_tau=Center_pixel*Tau
			Photons_object_tau=Photons_object*Tau
			Photons_sky_tau=Photons_sky*Tau
		except:
			Error = 1



	##############################################################
	#
	#	Calculations for the spectroscopic mode
	#	Photons per DIT
	#
	##############################################################
	if Mode % 2 == 0:
		if DIT < Hawaii_conditions['Min_DIT_%d' % int(Detector)]:
			DIT = Hawaii_conditions['Min_DIT_%d' % int(Detector)]
	######################################################
	#
	#	Creation of the Dictionaries and the 
	#	list of the wavelengths
	#
	######################################################
		i=Wavelength_start
		while i<=Wavelength_end - Spectral_resolution/2.:
			Wavelength_list.append(i)
			i=i+Spectral_resolution
		Wavelength_list.sort()
		i=Wavelength_start+Dispersion_choice/2.
		while i<=Wavelength_end:
			String="%.10f" % i
			Photons_object_dit[String]=0.0
			Photons_sky_dit[String]=0.0
			Dispersion_wavelength_list.append(i)
			i=i+Dispersion_choice
		Dispersion_wavelength_list.sort()
	#######################################################
		if Mode == 2:
			PSF_factor=gauss_func_int_lowres(-Scale/2., Scale/2., -Scale/2., Scale/2., Seeing)
		if Mode == 4:
			PSF_factor=psf_func_int(Beta, -Scale/2., Scale/2., -Scale/2., Scale/2., Seeing, Filter[Filter_choice][6]*1e-6, LBT['Diameter/m'], Strehl)
		i=0
		for j in range(0,len(Wavelength_list)):
			String="%.10f" % Wavelength_list[j]
			if not Hawaii_def.has_key(String):
				Hawaii_def[String]=0.0
			if not Atmosphere_def.has_key(String):
				Atmosphere_def[String]=0.0
			if not Grating_def.has_key(String):
				Grating_def[String]=0.0
			if not Filter_def.has_key(String):
				Filter_def[String]=0.0
			Slit_begin = Wavelength_list[j] - Spectral_resolution/2.
			Slit_end = Wavelength_list[j] + Spectral_resolution/2.
			if j==0 and i == 0:
				Limit1 = Wavelength_start
			else:
				Limit1 = Slit_begin
			if Two == 0:
				Limit2 = Limit1 + Dispersion_choice
			else:
				Limit2 = Limit1 + (1.-Fraction_pixel)*Dispersion_choice
			Photons=Spectrum_def[String]*1000.*Wavelength_list[j]*1e-6/(h*c)*Spectral_resolution*Filter_def[String]*Hawaii_def[String]*Atmosphere_def[String]*T*EW_def[String]*Camera_def[String]*Grating_def[String]*Correction_def[String]*Fluxfactor*LBT_area*DIT
			Sky=Background_def[String]*Spectral_resolution*1000.*Filter_def[String]*Hawaii_def[String]*T*Camera_def[String]*EW_def[String]*LBT_area*Scale*Scale*DIT*Grating_def[String]*Correction_def[String]
			if Source == 'Extended source':
				Photons=Photons*Scale*Scale
			else:
				Photons=Photons*Slit_transmission
			if Mode == 2:
				Photons_max_dit_2=PSF_factor*Photons+Sky
			if Mode == 4:
				Photons_max_dit_2=PSF_factor*Photons+Sky
			if Photons_max_dit_1 < Photons_max_dit_2:
				Photons_max_dit_1 = Photons_max_dit_2
			while Limit2 <= (Slit_end+1e-10):
				if Two == 0:
					Center=Dispersion_wavelength_list[i]
					String2 = "%.10f" % (Center)
				else:
					Two = 0
				Fraction_pixel = (Limit2-Limit1)/Dispersion_choice
				Fraction_slit = (Limit2-Limit1)/Spectral_resolution
				Photons_object_dit[String2]=Photons_object_dit[String2]+Photons*Fraction_slit
				Photons_sky_dit[String2]=Photons_sky_dit[String2]+Sky*Fraction_pixel
				Limit1=Limit2
				Limit2=Limit2+Dispersion_choice
				i=i+1
			Two = 1
			Fraction_pixel = (Slit_end-Limit1)/Dispersion_choice
			Fraction_slit = (Slit_end-Limit1)/Spectral_resolution
			if i < len(Dispersion_wavelength_list):
				Center=Dispersion_wavelength_list[i]
				String2 = "%.10f" % (Center)
				Limit2=Slit_end
				if (Limit2 - Limit1) > 1e-10:
					Photons_object_dit[String2]=Photons_object_dit[String2]+Photons*Fraction_slit
					Photons_sky_dit[String2]=Photons_sky_dit[String2]+Sky*Fraction_pixel
			else:
				break
	##############################################################
	#
	#	Calculation of the SNR for DIT and Tau
	#
	##############################################################
		List=Photons_object_dit.keys()
		List.sort()
		for String in List:
			Photons_sky_dit[String] = Photons_sky_dit[String] + Hawaii_conditions['Stray_light_%d' % int(Detector)]*DIT*N_pix
			SN_DIT[String]=Photons_object_dit[String]/sqrt(Photons_object_dit[String]+N_pix*(Photons_sky_dit[String]+(Hawaii_conditions['RON_%d' % int(Detector)]*Hawaii_conditions['RON_%d' % int(Detector)]+Hawaii_conditions['DC_%d' % int(Detector)]*DIT)))
			SN_tau[String]=sqrt(Tau/DIT)*SN_DIT[String]
	##############################################################
	#
	#	output of the calulated data
	#
	##############################################################
	if Mode == 1 or Mode == 5:
		Title='LUCI-Imaging (seeing-limited)'
	elif Mode == 2:
		Title='LUCI-spectroscopy (seeing-limited)'
	elif Mode == 3 or Mode == 7:
		Title='LUCI-Imaging (diffraction-limited)'
	elif Mode == 4:
		Title='LUCI-spectroscopy (diffraction-limited)'
##################
except:
	Title = "Wrong input!!"
if Error == 1:
	Title = "Wrong input!!"
if Error == 2:
	Title = str(Tau)
##################

output_header(Web, Title, Error,Output)
output_summary(Web, Source, Object_choice, Filter[Filter_mag][5], Object_brightness_choice, Detector, Hawaii_conditions, Filter[Filter_choice][5], Camera_choice, Slit_width, Dispersion_choice, Water_vapor, Airmass, Background_choice, Sky_brightness, Strehl, Seeing, Central_wavelength, Lpmm, SN, DIT, Tau, Scale, Mode, Error,Output)
if Mode % 2 <> 0:
	if Error == 0:
		output_imaging(Web, Source, Detector, Hawaii_conditions, Photons_object_tau, Photons_sky_tau, Center_pixel_tau, SN, SN_center_tau, DIT, SN_DIT, Tau, Mode, N_pix, Seeing, Debug, Photons_cali,Output)
else:
	if Error == 0:
		output_spectrum(Web, Detector, Hawaii_conditions, SN_tau, Photons_object_dit, Photons_sky_dit, Dispersion_choice, N_pix, Central_wavelength, DIT, Tau, Photons_max_dit_1,Output)

if Web != 2:
	coda_body(Output["help_web"] + 'index.html', 'Help')
	
if Debug == 1:
	File_debug.close()