pro even_light_platescale

;+
; NAME:
;	even_light_platescale
; PURPOSE:
; INCLUDE:
	@compile_opt.pro		; Return to caller
; MODIFICATION HISTORY:
;	???-????, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;-

files = [		$
	'ucsd_psa.nic',				$		; No temperature
	'ucsd_psb.nic',				$		; Only CCD temperature
	'ucsd_psc.nic',				$
	'ucsd_psd.nic',				$
	'ucsd_pse.nic',				$
	'ucsd_psf.nic',				$
	'ucsd_psg.nic',				$
	'ucsd_psh.nic',				$
	'ucsd_psi.nic',				$
	'ucsd_psj.nic',				$
	'ucsd_psk.nic',				$
	'ucsd_psl.nic',				$
	'ucsd_psm.nic',				$
	'ucsd_psn.nic'				]

nfiles = n_elements(files)

temperature = [		$
	[+25.0d0, +25.0d0],	$
	[+25.0d0, +25.0d0],	$
	[-21.8d0, +26.0d0],	$
	[-21.8d0, +26.0d0],	$
	[-18.5d0, +27.0d0],	$
	[-18.5d0, +27.0d0],	$
	[-15.0d0, +30.0d0],	$
	[-15.0d0, +30.0d0],	$
	[-11.5d0, +33.0d0],	$
	[-11.5d0, +33.0d0],	$
;	[- 8.5d0, +33.0d0],	$
;	[- 8.5d0, +33.0d0],	$
;	[+28.0d0, +33.0d0],	$
;	[+28.0d0, +33.0d0]	]

	[- 8.5d0, +55.0d0],	$
	[- 8.5d0, +55.0d0],	$
	[+28.0d0, +55.0d0],	$
	[+28.0d0, +55.0d0]	]



com_bin02 =	$
	[	[0.973381d0, 0.85475d0],$
		[0.972677d0, 0.81101d0],$
		[0.798949d0, 0.19689d0],$
		[0.794961d0, 0.19900d0],$
		[0.779258d0, 0.21837d0],$
		[0.775677d0, 0.21700d0],$
		[0.758694d0, 0.23002d0],$
		[0.759693d0, 0.24155d0],$
		[0.723273d0, 0.25508d0],$
		[0.718898d0, 0.25580d0],$
		[0.728344d0, 0.65871d0],$
		[0.735783d0, 0.67195d0],$
		[0.963693d0, 1.46400d0],$
		[0.928138d0, 1.47335d0] ]

com_bin02 = [73,238]#replicate(1,nfiles)+com_bin02

com_bin40 =	$
	[	[0.0329d0, 0.65040d0],$
		[0.0356d0, 0.66123d0],$
		[0.2656d0, 0.27882d0],$
		[0.2561d0, 0.27698d0],$
		[0.2330d0, 0.32603d0],$
		[0.2346d0, 0.32752d0],$
		[0.2745d0, 0.40131d0],$
		[0.2754d0, 0.40593d0],$
		[0.3057d0, 0.44128d0],$
		[0.2990d0, 0.44786d0],$
		[1.1541d0, 0.84461d0],$
		[1.1524d0, 0.84814d0],$
		[0.9575d0, 1.42042d0],$
		[0.9508d0, 1.47112d0] ]

com_bin40 = [1209, 260]#replicate(1,nfiles)+com_bin40


;files = [		$
;	'fltw2psa.nic',				$		; No temperature
;	'fltw2psb.nic',				$		; Only CCD temperature
;	'fltw2psc.nic',				$
;	'fltw2psd.nic',				$
;	'fltw2pse.nic',				$
;	'fltw2psf.nic'				]

;temperature = [		$
;	[+22.0, +22.0],	$
;	[+22.0, +22.0],	$
;	[-18.0, +21.7],	$
;	[-18.0, +21.7],	$
;	[+22.0, +21.7],	$
;	[+22.0, +22.0]	]

;com_bin02 =	$
;	[	[70.648379d0, 241.25757d0],	$
;		[70.656340d0, 241.25826d0],	$
;		[70.505294d0, 240.90720d0],	$
;		[70.503499d0, 240.89799d0],	$
;		[70.698274d0, 241.37213d0],	$
;		[70.702369d0, 241.38071d0]	]
;
;com_bin40 =	$
;	[	[1211.6675d0, 253.36467d0],	$
;		[1211.6680d0, 253.35092d0],	$
;		[1211.9196d0, 252.92276d0],	$
;		[1211.9171d0, 252.92370d0],	$
;		[1211.7264d0, 253.44018d0],	$
;		[1211.7397d0, 253.42641d0]	]

plate_scale = com_bin02-com_bin40
plate_scale = sqrt( total(plate_scale*plate_scale,1) )

nfiles = n_elements(files)


;d_warm = mean( plate_scale[ [0,1,4,5] ] )
;d_cold = mean( plate_scale[ [2,3] ] )

;print, (1-d_warm/d_cold)/40.


ff = plate_scale
xx = reform(temperature[0,*])
yy = reform(temperature[1,*])

;nn = [0,1,2,3,4,5,6,7,8,9,10,11,12,13]
nn = [0,1,2,3,4,5,6,7,8,9]

xx = xx[nn]
yy = yy[nn]
ff = ff[nn]

x = xx
y = yy
f = ff

Tx = mean(x)
Ty = mean(y)
Tf = mean(f)

x = x-Tx
y = y-Ty
f = f-Tx

Sxf = total(x*f)
Syf = total(y*f)
Sxx = total(x*x)
Syy = total(y*y)
Sxy = total(x*y)

Q = Sxx*Syy-Sxy*Sxy

a = (Sxf*Syy-Syf*Sxy)/Q
b = (Syf*Sxx-Sxf*Sxy)/Q

d0 = Tf-(a*Tx+b*Ty)

dfit = d0+a*xx+b*yy-ff
dfit = sqrt(mean(dfit*dfit))

plot , xx, ff, psym=2, /ynozero
oplot, xx, d0+a*xx+b*yy, psym=-4

xs = gridgen(61,range=[-30,30])
ys = replicate(1, 61)
for i=25,34,2 do oplot, xs, 1134.7412d0*(1d0-(7.0318574d-6)*xs +  (2.5698232e-5)*(i*ys)), linestyle=2



print, d0, a,b, a/d0, b/d0

results = transpose( [ [xx], [yy], [ff], [d0+a*xx+b*yy], [d0+a*xx+b*yy-ff] ] )
print, results
print, stddev([d0+a*xx+b*yy-ff])
return  &  end
