#! /usr/bin/python	
	
import string, sys, cgi, cgitb
from math import *
#cgitb.enable()


######################################################	
#	user input and Web from generation	
######################################################	
	
Formdata  = cgi.FieldStorage();
Flag = 0
Debug = 0

if Formdata.has_key('Button') == False and len(sys.argv) == 1:
	from input import html_java
	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)	# web form generation with JavaScript
	sys.exit()	
elif Formdata.has_key('Button') or (len(sys.argv)) > 1:
	if len(sys.argv) > 1:
		Formdata = sys.argv[1]
	from input import user_input  # text-based input and transfere of the data to the vaiables
######################################################	
#	loading of output.ini	
######################################################	
	
Output={}	
Ini_read=open('output.ini','r')	
Data=Ini_read.readline()
while Data <> "":
	Raw=string.split(Data)
	Output[Raw[0]]=Raw[2]
	Data=Ini_read.readline()
Ini_read.close()
cgi_path = Output["cgi_path"]	
	
######################################################	
#	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	
######################################################	
	
Standard_star_spectrum_def={}
Spectrum_def={}
EW_def={}
Camera_def={}
Filter_def={}
Grating_def={}
Filter_def_mag={}
Atmosphere_def={}
Background_def={}
Hawaii_def={}
Correction_def={}
Water_vapor=0.
Airmass=0.	
Colour=0.0	
Photons_standard_1=0	
Photons_object_1=0	
Photons_sky_1=0	
Center_pixel=0.0	
Center_pixel_1=0.0
Dispersion_choice=0.0	
Dispersion_wavelength_list=[]	
Spectral_resolution=0.0	
Wavelength_list=[]	
Slit_begin=0.0	
Slit_end=0.0	
Photons=0.0	
Sky=0.0	
Two=0	
Source = ''#Point source or enxtendet source	
Object_choice = ['','','']	
Filter_mag = '' #filter for the given magnitude	
Object_brightness_choice = 0.0 #magnitude	
Filter_choice = '' #filter for observation	
Camera_choice = '' #camera	
Detector = 0	
Slith_width=0.0	
Strehl=0.0	
Grating_wavelength=0.0	
Mode=0	
Atmosphere_choice = ''	
Background_choice = ''	
Sky_brightness = 0.0	
Seeing = 0.0	
DIT = 0.0	
Photons_object=0.	
Photons_sky=0.
Photons_max_dit_1=0.0
Photons_max_dit_2=0.0
SN_DIT = 0.0	
Spectral_resolution=0.0	
Redshift = 0.0
	
	
######################################################	
#	
#	reading the ini-files	
#	
######################################################	
	
ini_filename='etc.ini'	
ini_spectrum_filename='etc_spectrum.ini'	
ini_read_main(ini_filename)		#initiates the basic values	
ini_read_spectrum(ini_spectrum_filename) #initiates the additional values for spectroscopy mode	

######################################################	
#	
#	user input	
#	
######################################################	
	
if len(sys.argv) > 1:	
	if sys.argv[1] == 'NoJavaScript':	
		Web = 1	
	else:	
		Web = 2	
else:	
	Web = 1	
	
#print Grating_choice_old
(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)
#print Background_choice

Scale=LUCIFER[Camera_choice][1]	
if Error == 1:	
	Title = "Wrong input!!"	
	output_header(Web, Title, Error)
	#output_summary(Web, Source, Object_choice[0], Object_choice[1], Object_choice[2], Filter[Filter_mag][5], Object_brightness_choice, Detector, Hawaii_conditions, Filter[Filter_choice][5], Camera_choice, Slit_width, Atmosphere_conditions[Atmosphere_choice][1], Atmosphere_conditions[Atmosphere_choice][2], Background_choice, Sky_brightness, Strehl, Seeing, Central_wavelength, Lpmm, SN, DIT, Tau, Scale, Mode, Error)
	output_summary(Web, Source, Object_choice[0], Object_choice[1], Object_choice[2], 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)
	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
	#Slit_pixel = 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])/10000. # wavelength given in A conversion in micron	
	except:	
		Line_width=Object_choice[2]	
		
if Debug == 1:
	File=open("test.csv","w")
	#File.write("Photons_object\tSpectrum_def[String]\ti\tFilter_def[String]\tHawaii_def[String]\tAtmosphere_def[String]\tT\tEW_def[String]\tCamera_def[String]\tColour\tCorrection_def[String]\n")
try:
#if 1:
#	header("LUCIFER - Exposure Time Calculator", Output['css'])
#	body()
	######################################################
	#
	#	Transmission of light in the technical equipment
	#
	######################################################
	T=LBT['Transmission']
	LBT_area = pi*LBT['Diameter/m']*LBT['Diameter/m']*0.25
	
	######################################################
	#
	#	loading model spectrum
	#
	######################################################
	
	if Object_choice[0] == 't' and Object_choice[1] <> 'uniform':
		i=.2
		while i<=2.5:
			String = "%.9f" % i
			Spectrum_def[String] = 0.0
			i=i+Spectral_resolution		
		(Spectrum_def, Spectrum[Object_choice[1]])=linear_interpolation(Spectrum[Object_choice[1]][0],Spectrum_def, Spectrum[Object_choice[1]], Spectral_resolution, Redshift)
	######################################################
	#
	#	loading standard-star's fluxdensity
	#
	######################################################
	(Standard_star_spectrum_def, Standard_star_spectrum)=linear_interpolation(Standard_star_spectrum['A0V_cal'][0],Standard_star_spectrum_def, Standard_star_spectrum['A0V_cal'], Spectral_resolution, 0.0)
	######################################################
	#
	#	creating blackbody fluxdensity
	#
	######################################################
	
	if Object_choice[0]=='b':
		i=.2
		while i<=2.5:
			String="%.9f" % i
			Spectrum_def[String]=(1/pow(i/1e6,5)/(exp(h*c/(k*i/1e6*float(Blackbody_temperature)))-1))
			i=i+Spectral_resolution
	######################################################
	#
	#	creating uniform fluxdensity
	#
	######################################################
	
	if Object_choice[1] =='uniform':
		i=.2
		while i<=2.5:
			String="%.9f" % i
			Spectrum_def[String]=1
			i=i+Spectral_resolution

	######################################################
	#
	#	creating single-line fluxdensity
	#
	######################################################
	
	if Object_choice[0] <> 'b' and Object_choice[0] <> 't':
		i=.2
#		File=open('test.csv', 'w')
		while i<=2.5:
			String="%.9f" % i
			#Spectrum_def[String]=Line_flux/(sqrt(2.*pi)*(Line_width/2.35))*exp((-1.)*(i-Line_wavelength)*(i-Line_wavelength)/(2.*(Line_width/2.35)*(Line_width/2.35))) # Gauss
			#Spectrum_def[String]=Line_flux/(sqrt(2.*pi)*(Line_width/2.35))*exp((-1.)*(i*1e3-Line_wavelength)*(i*1e3-Line_wavelength)/(2.*(Line_width/2.35)*(Line_width/2.35))) # Gauss
			Spectrum_def[String]=Line_flux/(sqrt(2.*pi)*(Line_width*1e3/2.35))*exp((-1.)*(i-Line_wavelength)*(i-Line_wavelength)/(2.*(Line_width/2.35)*(Line_width/2.35))) # Gauss
#			Temp="%s\t%E\n" % (String, Spectrum_def[String])
#			File.write(Temp)
			i=i+Spectral_resolution
		#File.close()
	######################################################
	#
	#	loading filter transmission
	#
	######################################################
	(Filter_def, Filter[Filter_choice])=linear_interpolation(Filter[Filter_choice][0],Filter_def, Filter[Filter_choice], Spectral_resolution, 0.0)
	if Object_choice[0] == 'b' or Object_choice[0] == 't':
		(Filter_def_mag, Filter[Filter_mag])=linear_interpolation(Filter[Filter_mag][0],Filter_def_mag, Filter[Filter_mag], Spectral_resolution, 0.0)
	
	######################################################
	#
	#	loading entrance-window transmission
	#
	######################################################
	(EW_def, EW[Detector])=linear_interpolation(EW[Detector][0],EW_def, EW[Detector], Spectral_resolution, 0.0)
	######################################################
	#
	#	loading grating efficiency
	#
	######################################################
	if Mode % 2 == 0:
		#print Grating_choice
		(Grating_def, Grating[Grating_choice])=linear_interpolation(Grating[Grating_choice][0],Grating_def, Grating[Grating_choice], Spectral_resolution, 0.0)
	
	######################################################
	#
	#	loading atmosphere transmission
	#
	######################################################
	(Atmosphere_def, Atmosphere[Atmosphere_choice])=linear_interpolation(Atmosphere[Atmosphere_choice][0],Atmosphere_def, Atmosphere[Atmosphere_choice], Spectral_resolution, 0.0)
	
	######################################################
	#
	#	loading/creating background spectrum
	#
	######################################################
	
	if Background_choice <> "0" and Sky_brightness==-27:
		(Background_def, Background[Background_choice])=linear_interpolation(Background[Background_choice][0],Background_def, Background[Background_choice], Spectral_resolution, 0.0)
	if Background_choice <> "0" and Sky_brightness==-17:
		Background[Background_choice]=[Background_choice, 0, Atmosphere[Atmosphere_choice][2], Atmosphere[Atmosphere_choice][3], Spectral_resolution]
		(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], Spectral_resolution)
		#print Background_choice
	if Background_choice == "0":
		i=.2
		while i<=2.5:
			String="%.9f" % i
			Background_def[String]=Standard_star_spectrum_def[String]*pow(10, -0.4*Sky_brightness)
			i=i+Spectral_resolution
	
	######################################################
	#
	#	loading transmission of camera
	#
	######################################################
	(Camera_def, Camera[Camera_choice])=linear_interpolation(Camera[Camera_choice][0],Camera_def, Camera[Camera_choice], Spectral_resolution, 0.0)
	
	######################################################
	#
	#	loading QE of Hawaii-2 detector
	#
	######################################################
	Temp="File_%d" % int(Detector)
	(Hawaii_def, Hawaii[Temp])=linear_interpolation(Hawaii[Temp][0],Hawaii_def, Hawaii[Temp], Spectral_resolution, 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(Correction[Temp][0],Correction_def, Correction[Temp], Spectral_resolution, 0.0)
	######################################################
	######################################################
	#
	#	Colour: The factor of 0-mag object and user-mag 
	#	Photons=sum(from Wavelength_start to Wavelength_end)(Fluxdensity*Wavelength_step*Wavelength/c/h)
	#
	######################################################
	
	if Object_choice[0] == 'b' or Object_choice[0] == 't':
		Wavelength_start=Filter[Filter_mag][2]
		Wavelength_end=Filter[Filter_mag][3]
		Wavelength_step=Filter[Filter_mag][4]
		i=Wavelength_start
		while i	<=Wavelength_end:
			String="%.9f" % 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)
		Colour=pow(10, -0.4*(Object_brightness_choice-(2.5*log10(Photons_standard_1/Photons_object_1))))
		#print "%s" % (Photons_standard_1)
	else:
		Colour=1.
	######################################################
	#
	#	Definition of the wavelength range for the 
	#	calculations
	#
	######################################################
	
	if Mode % 2 <> 0:
		Wavelength_start=Filter[Filter_choice][2]
		Wavelength_end=Filter[Filter_choice][3]
		Wavelength_step=Filter[Filter_choice][4]
	else:
		Wavelength_start=Grating[Grating_choice][2]
		Wavelength_end=Grating[Grating_choice][3]
		Wavelength_step=Grating[Grating_choice][4]
		#Pixel_start = 1./Dispersion_choice * (Wavelength_start - Central_wavelength) + 1024.0
		#Pixel_end = 1./Dispersion_choice * (Wavelength_end - Central_wavelength) + 1024.0
		#Pixel_step = 2000.0
		#print Dispersion_choice, Wavelength_step
		#print Grating[Grating_choice][4]
	
		
	######################################################
	#
	#	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 % 2 <> 0:
			N_pix=pi*(Seeing/Scale)*(Seeing/Scale)
		#if Mode % 2 <> 0:
			#N_pix=pi*(2.046*Seeing/Scale)*(2.046*Seeing/Scale)
		elif Mode == 2:
			N_pix=2*(Seeing/Scale)
			Slit_transmission=gauss_func_int(-Slit_width/2., Slit_width/2., -Seeing, Seeing, Seeing)
			#N_pix=2*(2.046*Seeing/Scale)
			#Slit_transmission=moffat_func_int(Beta, -Slit_width/2., Slit_width/2., -Seeing, Seeing, Seeing)
		elif Mode == 4:
			N_pix=2*(2.046*Seeing/Scale)
			Slit_transmission=psf_func_int(Beta, -Slit_width/2., Slit_width/2., -Seeing, Seeing, Seeing, (Wavelength_end+Wavelength_start)*1e-6/2., LBT['Diameter/m'], Strehl)
	if Source == 'Extended source':
		if Mode % 2 <> 0:
			N_pix=1
		else:
			N_pix=1#./Scale
			
	######################################################
	#
	#	Number of photons per second from object+Sky
	#	Photons=sum(from Wavelength_start to Wavelength_end)(Fluxdensity*Wavelength_step*Wavelength/c/h)
	#
	######################################################
	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="%.9f" % 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]*Colour*Correction_def[String]
			if Debug == 1:
				File.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (Photons_object,Spectrum_def[String],i,Filter_def[String],Hawaii_def[String],Atmosphere_def[String],T,EW_def[String],Camera_def[String],Colour,Correction_def[String]))
			Photons_object_1=Photons_object_1+Spectrum_def[String]*i*1000.*Filter_def[String]*Colour*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]
			################
			#Temp = "%s\t%s\n" % (String, Filter_def[String]*Atmosphere_def[String])
			#File.write(Temp)
			################
			if Background_choice <> "0":
				Photons_sky=Photons_sky+Background_def[String]*Wavelength_step*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)
		#print Photons_standard*pow(10, -0.4*Sky_brightness)*Scale*Scale*(-0.000278719*Airmass*Airmass*Airmass-0.0653841*Airmass*Airmass+1.11979*Airmass-0.0552132)
		#print "%e" % (Photons_object)
		if Background_choice <> "0":
			Photons_sky=Photons_sky*LBT_area
			Photons_sky=Photons_sky*Scale*Scale
		else:	
			Photons_sky=Photons_standard*pow(10, -0.4*Sky_brightness)*Scale*Scale*(-0.000278719*Airmass*Airmass*Airmass-0.0653841*Airmass*Airmass+1.11979*Airmass-0.0552132)
		#print "%s" % (Photons_sky)
		#print "%s" % (Photons_object)
	####################################################################################
	#
	#	Integration over the seeing disc or over one pixel
	#
	####################################################################################
	
		if Mode <> 3 and Mode <> 7:
			if Source == 'Point source':
				#Photons_object = Photons_object*moffat_int(2.046*Seeing, Beta, Seeing)
				#Photons_object = Photons_object*moffat_int(Seeing, Beta, Seeing)
				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(Seeing, Beta, Seeing, (Wavelength_end-Wavelength_start)*1e-6/2., LBT['Diameter/m'], Strehl)
				Center_pixel=Photons_object*psf_func_int(Beta, -Scale/2., Scale/2., -Scale/2., Scale/2., Seeing, (Wavelength_end-Wavelength_start)*1e-6/2., 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
#		print Photons_object_dit
		Photons_sky_dit=Photons_sky*DIT
#		print 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 SN <> '0':
			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 Tau <> '0':
			try:
				#SN=Photons_object_dit*(Tau/DIT)/sqrt(Photons_object_dit*(Tau/DIT)+N_pix*(Photons_sky_dit*(Tau/DIT)+Hawaii_conditions['RON_%d' % int(Detector)]*Hawaii_conditions['RON_%d' % int(Detector)]*(Tau/DIT)+Hawaii_conditions['DC_%d' % int(Detector)]*Tau))
				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
			#print Photons_object_tau
		except:
			Error = 1
		
	##############################################################
	#
	#	Calculations for the spectroscopic mode
	#	Photons is 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="%.9f" % 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()
		#print len(Dispersion_wavelength_list)
	
	#######################################################
	
		i=0
		for j in range(0,len(Wavelength_list)):
			String="%.9f" % 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
#			Temp = "%f\t%s\t%f\n" % (Slit_begin, String, Slit_end)
#			File.write(Temp)
#			Temp = "\n%d\t%.9f\t%s\t%.9f\n" % (j+2, Slit_begin, String, Slit_end)
#			File.write(Temp)
			Photons=Spectrum_def[String]*float(String)*1000.*Filter_def[String]*Hawaii_def[String]*Atmosphere_def[String]*T*EW_def[String]*Camera_def[String]*Spectral_resolution*1e-6/(h*c)*LBT_area*DIT*Grating_def[String]*Colour*Correction_def[String]
			if Background_choice <> "0":
				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]
			else:	
				Sky=Standard_star_spectrum_def[String]*float(String)*1000.*Filter_def[String]*Hawaii_def[String]*Atmosphere_def[String]*T*EW_def[String]*Camera_def[String]*Spectral_resolution*1e-6/(h*c)*LBT_area*pow(10, -0.4*Sky_brightness)*Scale*Scale*DIT*Grating_def[String]*Correction_def[String]*(-0.000278719*Airmass*Airmass*Airmass-0.0653841*Airmass*Airmass+1.11979*Airmass-0.0552132)
			if Source == 'Extended source':
				Photons=Photons*Scale*Scale
			else:
				Photons=Photons*Slit_transmission
##########################
#			Temp="%f\t%.9f\n" % (Limit2, Slit_end)
#			File.write(Temp)
##########################
			Photons_max_dit_2=gauss_func_int_lowres(-Scale/2., Scale/2., -Scale/2., Scale/2., Seeing)*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:
					#String = "%f\t%f\t%f\n" % (i,Limit2,Slit_end)
					#File.write(String)
					Center=Dispersion_wavelength_list[i]
					String2 = "%.9f" % (Center)
				else:
					Two = 0
				Fraction_pixel = (Limit2-Limit1)/Dispersion_choice
				Fraction_slit = (Limit2-Limit1)/Spectral_resolution
#				Temp="%d\t%.9f\t%s\t%.9f\t%.9f\t%.9f\n" % (i+2, Limit1, String2, Limit2, Fraction_slit, Fraction_pixel)
				Photons_object_dit[String2]=Photons_object_dit[String2]+Photons*Fraction_slit
				Photons_sky_dit[String2]=Photons_sky_dit[String2]+Sky*Fraction_pixel
#				Temp = "%f\t%f\t%s\t%f\t%f\n" % (Slit_begin, Limit1, String2, Limit2, Slit_end)
#				File.write(Temp)
				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 = "%.9f" % (Center)
				Limit2=Slit_end
#				Temp = "%f\t%f\t%s\t%f\t%f\n" % (Slit_begin, Limit1, String2, Limit2, Slit_end)
#				File.write(Temp)
				if (Limit2 - Limit1) > 1e-10:
#					Temp="%d\t%.9f\t%s\t%.9f\t%.9f\t%.9f\n" % (i+2, Limit1, String2, Limit2, Fraction_slit, Fraction_pixel)
#					File.write(Temp)
					Photons_object_dit[String2]=Photons_object_dit[String2]+Photons*Fraction_slit
					Photons_sky_dit[String2]=Photons_sky_dit[String2]+Sky*Fraction_pixel
#					print Photons_object_dit[String2], String2
			else:
				break
			#print String
	##############################################################
	#
	#	Calculation of the SNR for DIT and Tau
	#
	##############################################################
		Liste=Photons_object_dit.keys()
		Liste.sort()
#		print Photons_object_dit['1.081437326']
		for String in Liste:
			#if Debug == 1:
				#Temp="%s\t%.9f\n" % (String, Photons_object_dit[String])
				#File.write(Temp)
			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='LUCIFER-Imaging (seeing-limited)'
	elif Mode == 2:
		Title='LUCIFER-spectroscopy (seeing-limited)'
	elif Mode == 3 or Mode == 7:
		Title='LUCIFER-Imaging (diffraction-limited)'
	elif Mode == 4:
		Title='LUCIFER-spectroscopy (diffraction-limited)'
#################
except:
	Title = "Wrong input!!"
if Error == 1:
	Title = "Wrong input!!"
if Error == 2:
	Title = str(Tau)
#################
output_header(Web, Title, Error)
if Mode % 2 <> 0:
	output_summary(Web, Source, Object_choice[0], Object_choice[1], Object_choice[2], 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)
	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)
else:
	output_summary(Web, Source, Object_choice[0], Object_choice[1], Object_choice[2], 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)
	if Error == 0:
		Temp = "Number_of_pixel_w_"+Detector
		#output_spectrum(Web, SN_tau, Photons_object_dit, Photons_sky_dit, Pixel_start, Pixel_end, Dispersion_choice, Hawaii_conditions[Temp], N_pix, Central_wavelength, DIT, Tau)
		output_spectrum(Web, Detector, Hawaii_conditions, SN_tau, Photons_object_dit, Photons_sky_dit, Dispersion_choice, Hawaii_conditions[Temp], N_pix, Central_wavelength, DIT, Tau, Photons_max_dit_1)

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