#! /usr/bin/python

import string, sys, cgi	
from math import *	

from functions import *

Filter_choice = sys.argv[4]
Filter = {'J-0403' : ['filter/J_0403.csv',0,0.0,0.0,0.0], 'Test' : ['filter/test.csv',0,0.0,0.0,0.0]}
Filter_def = {}
Filter_mag = sys.argv[4]
Filter_def_mag = {}
Mode = 3
Detector = 1
Correction = {'image_1' : ['correction/correction_image_1.csv',0,0.0,0.0,0.0]}
Correction_def = {}
Standard_star_spectrum = {'A0V_cal':['spectra/A0V.csv',0,0.0,0.0,0.0]}
Standard_star_spectrum_def = {}
Object_choice = []
Object_choice.append(sys.argv[1])
Object_choice.append(sys.argv[2])
Object_choice.append(sys.argv[3])
Object_brightness_choice = 20.0
Spectrum = {'M0V':['spectra/M0V.csv',0,0.0,0.0,0.0]}
Spectrum_def = {}
Spectral_resolution = 0.0005
Photons_standard_1 = 0.0
Photons_object_1 = 0.0
Color = 0.0
String = ""
Wavelength_start = 0.0
Wavelength_end = 0.0
Wavelength_step = 0.0
i = 0
Redshift = 0

######################################################	
#	physical constants	
######################################################	
	
c=2.9983e8	
h=6.63e-34	
k=1.3807e-23	


File=open("test.csv","w")
if 1:
#try:
	######################################################
	#
	#	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 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)
	######################################################
	#
	#	creating uniform fluxdensity
	#
	######################################################
	
	if Object_choice[1] == 'uniform':
		i=.2
		while i<=2.5:
			String="%.9f" % i
			Spectrum_def[String]=string.atof(sys.argv[3])
			i=i+Spectral_resolution
	######################################################
	#
	#	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)
	######################################################
	#
	#	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)
	######################################################
#except:
	#print "Einlesefehler"
	
#if 1:
try:
	if 1:
		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
			Temp = "%s\t%s\n" % (String, Correction_def[String])
			File.write(Temp)
			Photons_standard_1=Photons_standard_1+Standard_star_spectrum_def[String]*i*1000.*Filter_def_mag[String]
			Photons_object_1=Photons_object_1+Spectrum_def[String]*i*1000.*Filter_def_mag[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" % (Colour)
		#Colour = pow(10, -0.4*Object_brightness_choice)
		Photons_test = string.atof(sys.argv[3])/(h*c)*1.25e-6*Colour*300*pow(4,2)*pi
		print "Objekt Rechteckfilter+50m^2:   %s" % Photons_test
		print "Standard_0mag:                 %s" % Photons_standard_1
		print "Standard_0mag+50m^2:           %e" % (Photons_standard_1*pow(4,2)*pi)
		print "Objekt_0mag:                   %s" % Photons_object_1
		print "Colour:                        %s" % Colour
		print "Objekt_0mag*Colour:            %e" % (Photons_object_1*Colour)
	else:
		Colour=1.
except:
	print "Ausgabefehler"
File.close()