;speichern der SPMA daten als ascii file

pro spm_lev2_ascii

;file_out_h=strmid(file_in,0,strpos(file_in,'lev16')+3)+'20a_h_'+period+'.idl'
;file_out_d=strmid(file_in,0,strpos(file_in,'lev16')+3)+'20a_d_'+period+'.idl'

spm_dir='D:/Data/VIRGO/SPM/Level1/'  ;'D:/Data/VIRGO/SPM/level1/'
file=file_search(spm_dir+'spm_lev20a_h_*.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)
date_str=y[ind_max]+m[ind_max]+d[ind_max]
date_string=string(long(d[ind_max]),long(m[ind_max]),long(y[ind_max]),format='(3i2.2)')
print,date_string
file_in=file[ind_max]   ;spm_dir+'SPM_lev15_'+strmid(spm_file,strpos(spm_file,'1_',0,/reverse_s,/reverse_o)+2,13)+'.idl'
print,'SPMa file to be read: ',file_in
file=strmid(file_in,0,strlen(file_in)-3)+'dat'
file_out=strmid(file,0,strpos(file,'1'))+'2'+strmid(file,strpos(file,'1')+1)
print,file_out
stop

;save,md0,rot,gruen,blau,file=file_out_d    daily file
restore,file=file_in,/verb
mdd=dindgen(n_elements(rot))/24.d0+md0

rot[where(finite(rot) eq 0)]=-9.99999
gruen[where(finite(gruen) eq 0)]=-9.99999
blau[where(finite(blau) eq 0)]=-9.99999

;*****************************************************************
;SPEICHERN DER StundenMITTEL IN EINEM ASCII FILE
;FUER OFFENTLICHKEIT

length=n_elements(mdd)
year=replicate(0L, length)
month=replicate(0L, length)
day=replicate(0.0, length)

for i=0L, length-1L do begin
	date=soho_md2date(mdd[i])
	day(i)=date[2]
	month(i)=long(date[1])
	year(i)=long(date[0])
endfor

close,1
openw, 1,file_out
printf,1,'24  ;total number of comment lines including this one'
printf,1, ';SPECTRAL SOLAR IRRADIANCE (SSI) DATA FROM VIRGO/SOHO'
printf,1, ';This dataset is freely available under one restriction:'
printf,1,';If you use the data or some product based on the data in a published  '
printf,1,';paper, refer to Fr\"ohlich et al. (1997) and acknowledge receipt'
printf,1,';of the dataset from VIRGO team.'
printf,1,';REFERENCES:'
printf,1,';Fr{\"o}hlich, C., Crommelynck, D., Wehrli, C., Anklin, M., Dewitte, S., Fichot,'
printf,1,';  A., Finsterle, W., Jim{\e}nez, A., Chevalier, A. and Roth, H.~J.: 1997,'
printf,1,';  In-flight performances of {VIRGO} solar irradiance instruments on {SOHO},'
printf,1,';  {\em Sol. Phys.} {\bf 175},~267--286.'
printf,1,';This data set has been created by using the corrected SPMB (backup) data for detrending  '
printf,1,';SPMA. The values are given in Wm-2nm-1.'
printf,1,';The details of this data evaluation is descibed at http://www.pmodwrc.ch/virgo/virgo_spm.html'
printf,1,';Describtion of columns:'
printf,1,';1st col: Date (YYYY-MM-DD.ddddd)'
printf,1,';2nd col: SPMA-red: 862nm'
printf,1,';3d  col: SPMA-green: 500nm'
printf,1,';4th col: SPMA-blue: 402nm'
printf,1,';format (i4 i2.2 f8.4,3f10.5)'
printf,1,';Col 2-5 contain the hourly average of the corresponding hour of the day.'
printf,1,';Missing values are indicated by -0.9999'
printf,1,';File created ',strmid(systime(),4,6)+strmid(systime(),19,5)
printf,1,';DATA:'

for i=0L,length-1L do printf,1,format=' (i4,''-'',i2.2,''-'',f8.5,3f10.5)', transpose([[year[i]],$
	[month[i]],[day[i]],[rot[i]],[gruen[i]],[blau[i]]])
close,1
stop

;get daily file
file=strmid(file_in,0,strpos(file_in,'h_'))+'d'+strmid(file_in,strpos(file_in,'h_')+1)
print,file
stop
file_in=file
file=strmid(file_in,0,strlen(file_in)-3)+'dat'
file_out=strmid(file,0,strpos(file,'1'))+'2'+strmid(file,strpos(file,'1')+1)
print,file_out

;save,md0,rot,gruen,blau,file=file_out_d    daily file
restore,file=file_in,/verb
mdd=dindgen(n_elements(rot))+md0

rot[where(finite(rot) eq 0)]=-9.99999
gruen[where(finite(gruen) eq 0)]=-9.99999
blau[where(finite(blau) eq 0)]=-9.99999
stop

;*****************************************************************
;SPEICHERN DES TAGESMITTELS IN EINEM ASCII FILE
;FUER OFFENTLICHKEIT

length=n_elements(mdd)
year=replicate(0L, length)
month=replicate(0L, length)
day=replicate(0L, length)

for i=0L, length-1L do begin
	date=soho_md2date(mdd[i])
	day(i)=long(date[2])
	month(i)=long(date[1])
	year(i)=long(date[0])
endfor

close,1
openw, 1,file_out
printf,1,'24  ;total number of comment lines including this one'
printf,1, ';SPECTRAL SOLAR IRRADIANCE (SSI) DATA FROM VIRGO/SOHO'
printf,1, ';This dataset is freely available under one restriction:'
printf,1,';If you use the data or some product based on the data in a published  '
printf,1,';paper, refer to Fr\"ohlich et al. (1997) and acknowledge receipt'
printf,1,';of the dataset from VIRGO team.'
printf,1,';REFERENCES:'
printf,1,';Fr{\"o}hlich, C., Crommelynck, D., Wehrli, C., Anklin, M., Dewitte, S., Fichot,'
printf,1,';  A., Finsterle, W., Jim{\e}nez, A., Chevalier, A. and Roth, H.~J.: 1997,'
printf,1,';  In-flight performances of {VIRGO} solar irradiance instruments on {SOHO},'
printf,1,';  {\em Sol. Phys.} {\bf 175},~267--286.'
printf,1,';This data set has been created by using the corrected SPMB (backup) data for detrending  '
printf,1,';SPMA. The values are given in Wm-2nm-1.'
printf,1,';The details of this data evaluation is descibed at http://www.pmodwrc.ch/virgo/virgo_spm.html'
printf,1,';Describtion of columns:'
printf,1,';1st col: Date (YYYYMMDD) at noon'
printf,1,';2nd col: SPMA-red: 862nm (ppm)'
printf,1,';3d  col: SPMA-green: 500nm (ppm)'
printf,1,';4th col: SPMA-blue: 402nm (ppm)'
printf,1,';format (i4 2i2.2 3f10.5)'
printf,1,';Col 2-5 contain the daily average of the corresponding day.'
printf,1,';Missing values are indicated by -0.9999'
printf,1,';File created ',strmid(systime(),4,6)+strmid(systime(),19,5)
printf,1,';DATA:'

for i=0L,length-1L do printf,1,format=' (i4,2i2.2,3f10.5)', transpose([[year[i]],$
	[month[i]],[day[i]],[rot[i]],[gruen[i]],[blau[i]]])
close,1

end