PRO IridiumMACCS2,filename
;This program is run by ftpsri.sh at :00, :15, :30, and :45 past every hour of every day.
;It creates Augsburg format ASCII files from the raw MACCS Iridium data.
;The ASCII files it creates can be used to produce line plots (with linex7.pro) as well as spectrograms
;Overhaul Modifications by Michael Murphy Augsut 1, 2007 to begin working for 2 Maccs station
;Modifications by Erik Johnson August 15, 2007 to run on yspace from the maccsprocessing.sh shell script
close, /all
;filename='/Volumes/physics_data/Ftp/MACCS_DATA/Raw/CD/2007ir/*.dat'
;filename=dialog_pickfile (path='C:\Documents and Settings\murphy\Research\Rawdata\*.dat')
print, filename
openr,unit,filename,error=err,/get_lun
test_array = bytarr(11)
readu,unit,test_array
reclen=test_array(10)+9
free_lun,unit
openr,unit,filename,error=err,/get_lun
file_info = fstat(unit)
file_size = file_info.size/reclen
data_array = bytarr(reclen,file_size)
if (err eq 0) then begin
print,'Now processing ',filename
readu,unit,data_array
free_lun,unit
endif else begin
print,'An error has occurred while trying to read ',filename
stop
endelse
numsamples=(test_array(10)-9)/3/3
xvalue=fltarr(numsamples,file_size)
yvalue=fltarr(numsamples,file_size)
zvalue=fltarr(numsamples,file_size)
timevalue=data_array(4,*)*3600.0+data_array(5,*)*60.0+data_array(6,*)
FOR i=0,numsamples-1 DO BEGIN
xvalue(i,*)=(data_array(18+(i*9),*)*65536l+data_array(19+(i*9),*)*256l+data_array(20+(i*9),*))*0.025
yvalue(i,*)=(data_array(21+(i*9),*)*65536l+data_array(22+(i*9),*)*256l+data_array(23+(i*9),*))*0.025
zvalue(i,*)=(data_array(24+(i*9),*)*65536l+data_array(25+(i*9),*)*256l+data_array(26+(i*9),*))*0.025
ENDFOR
n=findgen(file_size)
nstep=n
n=n*2
bx=fltarr(file_size*2l)
by=bx
bz=bx
bx[n]=xvalue[0,nstep]
bx[n+1]= xvalue[1,nstep]
by[n]=yvalue[0,nstep]
by[n+1]= yvalue[1,nstep]
bz[n]=zvalue[0,nstep]
bz[n+1]= zvalue[1,nstep]
Dtime=dblarr(file_size*2l)
Dtime[n]=timevalue[0,nstep]+ 0.25
Dtime[n+1]= timevalue[0,nstep]+ 0.75
;Section for writing data
lab =
idoc = LONARR(24)
label=strmid(filename,0,2)
labelx=strmid(filename,2,5)
year=strmid(labelx,0,2)
;label2=fix(strmid(filename,2,5))
;label3=strmid(filename,37,3)
;'C:\Documents and Settings\murphy\NA07159.dat'
idoc(0)=1
idoc(2)=n_elements(Dtime)
print, label
if label eq 'NA' then begin
idoc(3)=122
line3='Nain Iridium Data'
endif
if label eq 'CD' then begin
idoc(3)=104
line3='Cape Dorset Iridium Data'
endif
idoc(4)=labelx
newarray = fltarr(4,size(bx,/n_elements))
newarray[0,*]= Dtime
newarray[1,*]= bx
newarray[2,*]= by
newarray[3,*]= bz
;yyddd
;print, label
;print, label2
;print, idoc
;print, bx
;print, dtime
;label3=string(label2)
;label3=strmid(label3,3,5)
;print, label2
;print, label3
openw, 2,'/Volumes/physics_data/Ftp/MACCS_DATA/AugASCII/'+label+'/20'+year+'ir/'+label+labelx+'.dat',width=107
printf,2,idoc, format = '(12(i8),/,12(i8))'
printf,2,line3
printf,2,newarray,format='(f8.2,3f15.3)'
close, 2
print, 'program finished'
END

