	;revise_fits

	pro replace_keyword,header,Replace

	for k=0,n_elements(Replace(0,*))-1 do begin
Pos=strpos(header,Replace(0,k))
N=where(Pos ge 0)
if n(0) ge 0 then begin
tmp=header(N)
strput,tmp,Replace(1,k),Pos(N(0))
header(N)=tmp
endif
	endfor

	end

;path=getenv('optics_dir')

path='E:\GRECH\IDL\TEMPO'
filter='*.fts'

files=pickfile(path=path,filt=filter, tit='Select a file to indicate a path')
if files eq '' then goto, exit

if !version.OS eq 'windows' then Delim='\' else Delim='/'
all_files=findfile(subdir(files)+Delim+filter,count=N_files)

if N_files eq 0 then goto,exit

short_names=strarr(N_files,2)
for j=0,N_files-1 do short_names(j,*)=(name_extract(all_files(j)))([0,1])

Error_file=path+Delim+'revise.log'

tmp=findfile(Error_file, count=count)

if count lt 1 then openw,lun_er,Error_file,/get else $
	openu,lun_er,Error_file,/get

	FOR I=0,N_files-1 DO BEGIN

old_header=headfits(all_files(I))
xx=rfits(all_files(I))
modified_header=old_header

	CASE 1 OF

strpos(strlowcase(short_names(I,1)), 'v') ge 0: Stokes='Polarization'
strpos(strlowcase(short_names(I,1)), 'i') ge 0: Stokes='Intensity'
strpos(strlowcase(short_names(I,1)), 'r') ge 0: Stokes='Right'
strpos(strlowcase(short_names(I,1)), 'l') ge 0: Stokes='Left'

	ELSE:	message, 'Illegal file name'

	ENDCASE

sxaddpar, modified_header,'paramete',Stokes, AFTER ='TELESCOP';, Comment

X_obs=fh_r_key(modified_header,'x-extent',error=error)
if error ne 1 then sxaddpar, modified_header,'x-obs',X_obs, AFTER ='WAVE'
Y_obs=fh_r_key(modified_header,'y-extent',error=error)
if error ne 1 then sxaddpar, modified_header,'y-obs',Y_obs, AFTER ='X-OBS'

sxdelpar,modified_header,'x-extent'
sxdelpar,modified_header,'y-extent'


Length=n_elements(modified_header)
headlength=(((Length-1)/36)+1)*36
new_header=bytarr(80,headlength)
new_header(0,0)=byte(modified_header)
header=string(new_header)


Replace=[['UT START','UTSTART '], ['UT STOP','UTSTOP '], ['PO','P0'],	$
['UT-START','UTSTART '], ['UT-STOP','UTSTOP ']]

replace_keyword,header,Replace


Old_Date=fh_r_key(header,'date-obs',/char)
Date=strsubst(Old_Date,' ','0')
Date=strmid(Date,3,2)+'-'+strmid(Date,0,2)+'-'+strmid(Date,6,2)
replace_keyword,header,[Old_date,date]

times=['time-obs','utstart','utstop']

	for j=0,n_elements(times)-1 do begin

Old_Time=fh_r_key(header,times(j),/char)
	if Old_Time ne 0 then begin
Time=strsubst(Old_Time,' ','0')
replace_keyword,header,[Old_Time,Time]
	endif else begin
print,'Error! The file '+short_names(I,0)+' could not be processed.'
printf,lun_er,short_names(I)
goto,Jump
	endelse

	endfor


	openw,lun,path+delim+short_names(I,1)+'.fit',/get
writeu,lun,byte(header)
writeu,lun,xx
	free_lun,lun
Sz=size(xx)
window,2*(I mod 2),xs=sz(1),ys=sz(2),tit=short_names(I,1)
tvscl,xx
print,'File '+short_names(I,1)+'.fit'+' is recorded.'
Jump:
	ENDFOR

	free_lun,lun_er
exit:
	end