;+
; NAME:
;	smei_star_cleanup
; PURPOSE:
; CATEGORY:
;	camera/idl/star
; CALLING SEQUENCE:
	PRO smei_star_cleanup, file, silent=silent
; INPUTS:
; OPTIONAL INPUTS:
; OUTPUTS:
; INCLUDE:
	@compile_opt.pro		; On error, return to caller
; CALLS:
; PROCEDURE:
; MODIFICATION HISTORY:
;-

; - remove duplicate 'ghost' records
; - fix "close' column
; - add Astd column
; - fix Istd column ( x 0.01 if > 10^6 )

status = txt_read(file, lines, silent=silent)
IF NOT status THEN message, 'oops: '+hide_env(file)

old_title = smei_star_formatpnt(/get_titles,/old_format)
new_title = smei_star_formatpnt(/get_titles)
new_format = flt_format(smei_star_formatpnt(/get_format))

IF (where(lines EQ new_title))[0] NE -1 THEN BEGIN
	message, /info, 'already clean, '+file
	RETURN
ENDIF

stars_in_lines = strmid(lines,0,12)

star_names = smei_star_list(cat=['brightstars','duhstars','stars_not_subtracted','nova_list'])
star_names = smei_star_info(star_names, /get_name)
boost, star_names, [	$
	'Mercury     '	, $
	'Venus       '	, $
	'Earth       '	, $
	'Mars        '	, $
	'Jupiter     '	, $
	'Saturn      '	, $
	'Uranus      '	, $
	'Neptune     '	, $
	'Pluto       '	, $
	'Ceres       '	, $
	'Pallas      '	, $
	'Juno        '	, $
	'Vesta       '	, $
	'Hebe        '	, $
	'Iris        '	, $
	'Flora       '	, $
	'Metis       '	, $
	'Hygiea      '	, $
	'Eunomia     '	, $
	'Psyche      '	, $
	'Europa      '	, $
	'Cybele      '	, $
	'Davida      '	, $
	'Interamnia  '	]

;IF (file_search(GetFileSpec(file,upto='name')+'.clean'))[0] NE '' THEN BEGIN
;	message, /info, 'clean file already exists, '+file
;	RETURN
;ENDIF

openw, /get_lun, iu, GetFileSpec(file,upto='name')+'.clean'

FOR i=0,n_elements(lines)-1 DO BEGIN
	line = lines[i]
	star = strmid(line,0,12)

	IF strpos(line,'; CAMERA  :') NE -1 THEN camera  = round(sfloat(line))
	IF strpos(line,'; PSFAREA :') NE -1 THEN psfarea = sfloat(line)

	IF line EQ old_title THEN BEGIN
		printf, iu, '; FORMAT  : '+new_format
		line = new_title
	ENDIF ELSE IF strmid(line,0,1) NE ';' THEN BEGIN
		IF strlen(line) NE 355 THEN message, strcompress(strlen(line),/rem)+' is wrong record length'
		
		; F10.6, F10.3
		stdadus = double( strmid(line,123,12) )
		IF stdadus GT 1.0E6 THEN BEGIN
			line = strmid(line,0,123)+string(stdadus*0.01,format='(F10.3)')+strmid(line,135)
			line = strmid(line,0,123)+string(psfarea     ,format='(F10.6)')+strmid(line,123)
		ENDIF ELSE IF stdadus EQ 0 THEN BEGIN
			line = strmid(line,0,123)+string(         0.0,format='(F10.3)')+strmid(line,135)
			line = strmid(line,0,123)+string(         0.0,format='(F10.6)')+strmid(line,123)
		ENDIF ELSE BEGIN
			line = strmid(line,0,123)+string(stdadus     ,format='(F10.3)')+strmid(line,135)
			line = strmid(line,0,123)+string(psfarea     ,format='(F10.6)')+strmid(line,123)
		ENDELSE

		close_star = strmid(line,112,11)
		IF close_star EQ '          -' THEN BEGIN
			line = strmid(line,0,112)+'           -'+strmid(line,123)
		ENDIF ELSE BEGIN
			n = where( strpos(star_names,close_star) EQ 0 )
			IF n_elements(n) GT 1 THEN message, '"'+close_star+'" is not unique'
			IF n[0] EQ -1 THEN BEGIN
				IF strpos(close_star,'V* Xi Her'   ) EQ 0 THEN continue
				IF strpos(close_star,'V* Xi UMa'   ) EQ 0 THEN continue
				IF strpos(close_star,'CAPELLA  '   ) EQ 0 THEN continue
				IF strpos(close_star,'V598 Puppis' ) EQ 0 THEN continue
				IF strpos(close_star,'GJ 188B'     ) EQ 0 THEN continue
				IF strpos(close_star,'GJ 105.4B'   ) EQ 0 THEN continue
				IF strpos(close_star,'CCDM J05218' ) EQ 0 THEN continue
				IF strpos(close_star,'GJ 351B'     ) EQ 0 THEN continue
				message, '"'+close_star+'" is not in catalogues'
			ENDIF
			close_star = star_names[n[0]]
			n = where( stars_in_lines EQ star )
			nn = n_elements(n)
			IF nn GT 2 THEN message, strcompress(nn,/rem)+' occurrences ?????'
			IF nn EQ 2 THEN BEGIN
				message, /info, strcompress(nn,/rem)+' occurrences; discarding: '+star+' hiding behind '+close_star
				IF star NE close_star THEN stars_in_lines[i] = 'XXXXXXXXXXXX'
				continue
			ENDIF
			line = strmid(line,0,112)+close_star+strmid(line,123)
		ENDELSE
		line = strmid(line,0,35)+' 9'+strmid(line,35)
	ENDIF

	printf, iu, line

	IF strpos(line,'; PSFAREA :') NE -1 AND (where(strpos(lines,'; PSFADUS :') NE -1))[0] EQ -1 THEN BEGIN
		map	= smei_star_standard(camera, scale=scale, /degrees, in_psf=in_psf)
		in_psf  = where(in_psf)
		psfadus = scale*scale*total(double(map[in_psf]))        ; Integrated adus in core psf
		printf, iu, '; PSFADUS : '+string(psfadus,format='(F9.3)')
	ENDIF
ENDFOR

free_lun, iu

RETURN  &  END
