pro MAKE_SPM_1_5_update, dl, ml, yl


;+
; NAME:
;       MAKE_SPM
; PURPOSE:
;       Update of SPM data, level 1.5
; EXPLANATION:
;
;
;
; CALLING SEQUENCE:
;       MAKE_SPM_UPDATE, dl, ml, yl
; INPUTS:
;       dl: day of last evaluated date
;       ml: month of last evaluated date
;       yl: year of last evaluated date
; OUTPUTS:
;       None.
;       The procedure saves the output files in the directories:
;           //obsun/space/soho/virgo/spm/level1.5/spm_detrend
;           //obsun/space/soho/virgo/spm/level1.5/spm_detrend_filled
;           //obsun/space/soho/virgo/spm/level1.5/spm_bad_minutes
;
;
; PROCEDURES USED:
;
;       Procedures:  FINDATTRACT, CUTATTRACT, DETREND_SPM, PATT, FILL_T, FILL_T_ATT, WRITEFITS,
;                    LEVEL0, LEVEL1, WRITENEWFILE
;
; MODIFICATION HISTORY:
;       Package 'SPM level 1.5' written between 2000 and 2003, Richard Wachter, PMOD/WRC
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

fname_dat=findfile('//obsun/space/soho/virgo/spm/level1.5/spm_detrend/green_*.*')

dat_day=fix(strmid(fname_dat, 63, 2))
dat_mon=fix(strmid(fname_dat, 65, 2))
dat_yea=fix(strmid(fname_dat, 67, 2))

if dat_yea ge 95 then dat_yea=dat_yea+1900 else dat_yea=dat_yea+2000

juld=mjulday(dat_mon, dat_day, dat_yea)
mcaldat, juld-14, dat_mon_s, dat_day_s, dat_yea_s


level0, dat_day_s[0], dat_mon_s[0], dat_yea_s[0], dl, ml, yl, red0, green0, blue0

level1, dat_day_s[0], dat_mon_s[0], dat_yea_s[0], dl, ml, yl, red1, green1, blue1

level_0=[[red0], [green0], [blue0]]
level_1=[[red1], [green1], [blue1]]


fname='//obsun/space/soho/virgo/spm/level1.5/'
str=['red', 'green', 'blue']
fac=[8.0, 7.0, 7.0]
l=(size(level_0))[1]

dtr=findgen(3,l)

att=ptrarr(3, /allocate_heap)

for i=0, 2 do begin

 level_0i=level_0[*,i]
 level_1i=level_1[*,i]


 print, 'search for attractors in the '+str[i]+' channel'

 findattract, level_0i, mh, klass_reihe

 *att[i]=klass_reihe


 patt, level_0i, klass_reihe, patt_reihe

 print, 'detrending '+str[i]+' channel'
 detrend_spm, level_1i*patt_reihe, 1440, factor=fac[i], dtreihe, bad_minutes

 cutattract, dtreihe, klass_reihe, creihe

 creihe[where(finite(creihe) eq 0)]=0.0


fname_cut=fname+'spm_detrend/'+str[i]+'_'
writenewfile, fname_cut, dl, ml, yl, creihe

fname_bad_minutes=fname+'spm_bad_minutes/'+str[i]+'_bad_minutes_'
writenewfile, fname_bad_minutes, dl, ml, yl, bad_minutes

 ;writefits, str[i]+'_cut_'+string(year, format='(i4.4)')+'.fits', creihe[7*1440:nn-7*1440L-1]
 ;writefits, str[i]+'_bad_minutes_' + string(year, format='(i4.4)')+'.fits', bad_minutes[7*1440:nn-7*1440L-1]



 dtr[i,*]=dtreihe


endfor

  ;;;;filling;;;;;;;;;;;;;;;;;;;

print, 'filling red channel'

fill_r=fill_t_att(dtr[0,*], *att[0], dtr[1,*], *att[1])

fill_t, fill_r, dtr[1,*], ffill_r
fill_t, ffill_r, dtr[2,*], ffill_r

ffill_r[where(finite(ffill_r) eq 0)]=0.0

writenewfile, fname+'spm_detrend_filled/red_filled_', dl, ml, yl, ffill_r
;writefits, 'red_filled_'+string(year, format='i4.4)')+'.fits', ffill_r[7*1440:nn-7*1440L-1]


print, 'filling green channel'

fill_r=fill_t_att(dtr[1,*], *att[1], dtr[2,*], *att[2])

fill_t, fill_r, dtr[2,*], ffill_r
fill_t, ffill_r, dtr[0,*], ffill_r

ffill_r[where(finite(ffill_r) eq 0)]=0.0

;writefits, 'green_filled_'+string(year, format='(i4.4)')+'.fits', ffill_r[7*1440:nn-7*1440L-1]
writenewfile, fname+'spm_detrend_filled/green_filled_', dl, ml, yl, ffill_r

print, 'filling blue channel'

fill_r=fill_t_att(dtr[2,*], *att[2], dtr[1,*], *att[1])

fill_t, fill_r, dtr[1,*], ffill_r
fill_t, ffill_r, dtr[0,*], ffill_r

ffill_r[where(finite(ffill_r) eq 0)]=0.0

;writefits, 'blue_filled_'+string(year, format='(i4.4)')+'.fits', ffill_r[7*1440:nn-7*1440L-1]
writenewfile, fname+'spm_detrend_filled/blue_filled_', dl, ml, yl, ffill_r





end






pro writenewfile, fname, dl, ml, yl, tsn

;+
; NAME:
;       WRITENEWFILE
; PURPOSE:
;       WRITING EXTENDED TIME SERIES OF SPM_DETREND, SPM_DETREND_FILLED, SPM_BAD_MINUTES
; EXPLANATION:
;
;
;
; CALLING SEQUENCE:
;       WRITENEWFILE, FNAME, DL, ML, YL, TSN
; INPUTS:
;       fname: trunc of filename (date of first and last day will be added)
;       dl: day of last evaluated date
;       ml: month of last evaluated date
;       yl: year of last evaluated date
;       tsn: extensiton for time series: Overlap with old time series is 14 days.
; OUTPUTS:
;       None.
;       The procedure saves the output files in the directories:
;           //obsun/space/soho/virgo/spm/level1.5/spm_detrend
;           //obsun/space/soho/virgo/spm/level1.5/spm_detrend_filled
;           //obsun/space/soho/virgo/spm/level1.5/spm_bad_minutes
;
;
; PROCEDURES USED:
;
;       Procedures:  WRITEFITS
; MODIFICATION HISTORY:
;       Package 'SPM level 1.5' written between 2000 and 2003, Richard Wachter, PMOD/WRC
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 dd=findfile(fname+'*')

 dd=dd[0]

 nstr=strlen(dd)

 fstring=strmid(dd, nstr-18, 6)

 ts=readfits(dd[0])

 n=n_elements(ts)

 diff=ts[n-1440L*8:n-1440L*7-1] - tsn[1440*7L:1440*8L-1]

 if total(abs(diff) gt 0) then stop

 ts_n=[ts[0:n-1440L*8-1], tsn[1440*7L:*]]

 writefits, fname+fstring+'_'+string(dl, format='(i2.2)')+string(ml, format='(i2.2)')+string((yl mod 100), format='(i2.2)')+'.fits', ts_n

end


