;update spm from chris databank

pro update_spm_1min

set_plot,'WIN'
simple_color

filedir='D:/Data/VIRGO/SPM/Level1/'
file=file_search(filedir+'spm_lev1_180196_*.idl')
n_file=n_elements(file)
date_st=lonarr(n_file)
y=strarr(n_file) & m=y & d=m
for i=0,n_file-1 do begin
    y[i]=strmid(file[i],5,2,/reverse_o)
    m[i]=strmid(file[i],7,2,/reverse_o)
    d[i]=strmid(file[i],9,2,/reverse_o)
    date_st[i]=long(y[i])*10000L+long(m[i])*100L+long(d[i])
endfor
max_file=max(date_st,ind_max)
file_in=file(ind_max)
print,file_in

start_day=floor(date2soho_md(long(y[ind_max])+2000L,long((m[ind_max])),long((d[ind_max]))))
stop_day=-1
data=get_db1spm(start_day,stop_day)
stop_day=(reverse(data.md))[0]
print,start_day,stop_day
if stop_day eq start_day then goto, no_spmupdate      ;no update needed

restore,file=file_in,/verb
help,irot,igruen,iblau
stop

ntot=n_elements(irot)
spmday=dindgen(ntot)/1440.d0+mjulday(1,18,1996)-mjulday(12,1,1995)
ind=where(irot gt 90,cnt)
if cnt gt 0 then irot[ind]=!values.F_NaN
ind=where(igruen gt 90,cnt)
if cnt gt 0 then igruen[ind]=!values.F_NaN
ind=where(iblau gt 90,cnt)
if cnt gt 0 then iblau[ind]=!values.F_NaN

first_date=strmid(file_in,16,6,/revers)
first_date=date2soho_md(long(strmid(first_date,4,2))+1900L,long(strmid(first_date,2,2)),long(strmid(first_date,0,2)))
print,first_date	;mission day
last_date=strmid(file_in,9,6,/revers)
first_day=date2soho_md(long(strmid(last_date,4,2))+2000L,long(strmid(last_date,2,2)),long(strmid(last_date,0,2)))+1.d0
print,first_day
stop
last_day=-1
darr=get_db1spm(first_day, last_day,path=filedir)
help,darr,/struct
print,darr[0].md,darr[0].mn,darr[0].spm
stop
print,((reverse(double(darr.md)))[0:10])+((reverse(double(darr.mn)))[0:10])/1440.0
print,((reverse(darr.spm[0]))[0:10])
print,((reverse(darr.spm[1]))[0:10])
print,((reverse(darr.spm[2]))[0:10])
stop
irot=[irot,darr.spm[0]]
index=where(irot lt 0.5,cnt)
print,cnt
if cnt gt 0 then irot[index]=!values.F_NaN
dd=findgen(n_elements(irot))*60.d0/86400.d0+floor(first_date)
print,dd[0]
window,0,title='rot'
plot,dd,irot,xrange=[3612,4598]
stop
;index=where(dd gt first_day-100.d0 and dd lt first_day+100.d0)
;plot,dd[index],irot[index]
;stop
igruen=[igruen,darr.spm[1]]
ind=where(igruen lt 0.4,cnt)
print,cnt
if cnt gt 0 then igruen[ind]=!values.F_NaN
index=where((dd gt 4510 and dd lt 4520))
indmax=max(igruen[index],maxind)
print,'Green:',dd[index[maxind]]
igruen[index[maxind-1:maxind+1]]=!values.F_NaN

window,1,title='green'
plot,dd,igruen,xrange=[3612,4598]
stop
;plot,dd[index],igruen[index]
;stop
iblau=[iblau,darr.spm[2]]
ind=where(iblau lt 0.13,cnt)
print,cnt
if cnt gt 0 then iblau[ind]=!values.F_NaN
index=where((dd gt 4510 and dd lt 4520))
indmax=max(iblau[index],maxind)
print,'Blue:',dd[index[maxind]]
print,iblau[index[maxind-1:maxind+1]]
iblau[index[maxind-1:maxind+1]]=!values.F_NaN

window,2,title='blue'
plot,dd,iblau,xrange=[3612,4598],yrange=[0.14,0.25]
;stop
;plot,dd[index],iblau[index]
;oplot,[first_day,first_day],[0,1.]

stop
last_date=soho_md2date((reverse(darr.md))[0])
l_date=string(last_date[2],format='(i2.2)')+string(last_date[1],format='(i2.2)')+string(last_date[0] mod 100,format='(i2.2)')
save,irot,igruen,iblau,file=filedir+'spm_lev1_180196_'+l_date+'.idl'
stop

no_spmupdate:
;run instead get spm_temperature
goto,no_update

;update temperatures
;D:\Data\VIRGO\SPM\Level1\SPM_Temperature_960204_060411.idl
file=file_search(filedir+'SPM_Temperature_960204_*.idl')
file_in=file((reverse(sort(file)))[0])
print,file_in
restore,file=file_in,/verb
help,temp_a,temp_b
mdd=md
temp_aa=temp_a
temp_bb=temp_b

first_date=strmid(file_in,16,6,/revers)
first_day=date2soho_md(long(strmid(first_date,0,2))+1900L,long(strmid(first_date,2,2)),$
    long(strmid(first_date,4,2)))
print,first_day  ;mission day
last_date=strmid(file_in,9,6,/revers)
start_day=floor(date2soho_md(long(strmid(last_date,0,2))+2000L,long(strmid(last_date,2,2)),$
    long(strmid(last_date,4,2)))+1.d0)
stop_day=-1
data=get_db1spm(start_day,stop_day)
stop_day=(reverse(data.md))[0]
print,start_day,stop_day
if start_day ge stop_day then goto, no_update      ;no update needed
stop_date=soho_md2date(stop_day)
end_date=string(floor(stop_date[0]-2000.),floor(stop_date[1:2]),format='(3i2.2)')
print,first_date,' ', end_date
stop

temp=fltarr(3)
make_t=0
if make_t then begin
  for ik=0L,stop_day-start_day do begin
    index=where(data.md eq start_day+ik and data.dq lt 10)
    if index[0] ne -1 then begin
      temp0=mean(data[index].tmp[0])
      temp1=mean(data[index].tmp[1])
      t=[float(start_day+ik)+0.5,temp0,temp1]
    endif else begin
      t=[float(start_day+ik)+0.5,!values.F_NaN,!values.F_NaN]
      print,'we have an invalid day at', start_day+ik
    endelse
    if ik eq 0 then temp=t else temp=[temp,t]
  endfor
  help,temp
  stop
  tmp=reform(float(temp),3L,n_elements(temp)/3L)
  save,tmp,file=filedir+'scratch.idl'
endif else restore,file=filedir+'scratch.idl'

temp_a=reform(tmp[1,*])
;index=where(temp_a lt 304.)
;temp_a[index]=
temp_b=reform(tmp[2,*])
md=reform(tmp[0,*])
window,3,title='temperatures SPMA'
plot,md,temp_a,xrange=[start_day-10.,stop_day+10],yrange=[298.,312.]  ;-mean(temp_a)

rem_outl,temp_a,2,numb,ind   ;,/print
temp_a=nan_interp_lt(temp_a,less=30,/fcast)
oplot,md,temp_a,color=3
rem_outl,temp_a,2,numb,ind   ;,/print
oplot,md,temp_a,color=2
stop

window,4,title='temperatures SPMB'
plot,md,temp_b,xrange=[start_day-10.,stop_day+10],yrange=[298.,312.]  ;-mean(temp_a)

rem_outl,temp_b,2,numb,ind   ;,/print
temp_b=nan_interp_lt(temp_b,less=30,/fcast)
oplot,md,temp_b,color=3
rem_outl,temp_b,2,numb,ind   ;,/print
oplot,md,temp_b,color=2

print,md[0],(reverse(mdd))[0]
stop
md=[mdd,md]
temp_a=[temp_aa,temp_a]
temp_b=[temp_bb,temp_b]
file_out='D:/Data/VIRGO/SPM/Level1/SPM_Temperature_'+first_date+'_'+end_date+'.idl'
save,md,temp_a,temp_b,file=file_out,/verb
print,'Temeratures saved in '+file_out

plot,md,temp_a-mean(temp_a)
oplot,md,temp_b-mean(temp_b),color=2

no_update:
stop
end
