;Program to make 1-hour averaged files from 1-minute averaged monthly
;files and to plot them in 1 or 6 month format
Pro houravrg, wdin=wdin, wdout=wdout, code=code, yr=yr
;cd,wdin
;print,'ls '+code+'*'+yr+'*.1m'
;spawn,'ls '+code+'*'+yr+'[a-z]*.1m*',fnames
search = FILEPATH(code+'*'+yr+'*.1m*', ROOT_DIR = wdin)
print, search
fnames = findfile(search) ;,count=num_raw)
fnames = file_basename(fnames)
print,fnames
numfiles=n_elements(fnames)
code=strarr(numfiles)
year=code
month=code
for i = 0L,numfiles-1 do begin
code(i) = strmid(fnames(i),0,2)
year(i) = strmid(fnames(i),2,2)
month(i) = strmid(fnames(i),4,3)
endfor
hor = '.1h'
for j = 0L,numfiles-1 do begin
name = fnames(j)
print,'Now processing file '+name+'....'
if month(j) eq 'jan' then begin
njours = 31.0
endif
if month(j) eq 'feb' then begin
if (year(j) eq 92) or (year(j) eq 96) or (year(j) eq 00) then begin
njours = 29.0
endif else begin
njours = 28.0
endelse
endif
if month(j) eq 'mar' then begin
njours = 31.0
endif
if month(j) eq 'apr' then begin
njours = 30.0
endif
if month(j) eq 'may' then begin
njours = 31.0
endif
if month(j) eq 'jun' then begin
njours = 30.0
endif
if month(j) eq 'jul' then begin
njours = 31.0
endif
if month(j) eq 'aug' then begin
njours = 31.0
endif
if month(j) eq 'sep' then begin
njours = 30.0
endif
if month(j) eq 'oct' then begin
njours = 31.0
endif
if month(j) eq 'nov' then begin
njours = 30.0
endif
if month(j) eq 'dec' then begin
njours = 31.0
endif
name=FILEPATH(name, ROOT_DIR = wdin)
openr,file,name,/get_lun
scale = 1000.0
print,'Reading all data into component arrays....'
len = 20160.0 * njours
data = bytarr(len)
numrecords = 1440.0 * njours
x = fltarr(numrecords)
y = x
z = x
buf = bytarr(numrecords)
readu,file,data
for k = 0L,numrecords-1 do begin
ind1 = 14 * k
ind2 = ind1 + 13
buf = data(ind1:ind2)
x(k) = (long(buf,2)) / scale
y(k) = (long(buf,6)) / scale
z(k) = (long(buf,10)) / scale
endfor
close,file
free_lun,file
print,'Averaging data....'
xhrav = fltarr(njours*24)
yhrav = xhrav
zhrav = xhrav
hrnum = findgen((njours*24) + 1)
newfile = wdout+code(j)+year(j)+month(j)+hor
; cd,wd
openw,nfile,newfile,/get_lun
for p = 0L,numrecords-1,60 do begin
divisx = 0
divisy = 0
divisz = 0
subx = 0
suby = 0
subz = 0
for q = 0L,59 do begin
if x(p+q) eq 32767.0 then begin
divisx = divisx
subx = subx
endif else begin
divisx = divisx + 1
subx = subx + x(p+q)
endelse
if y(p+q) eq 32767.0 then begin
divisy = divisy
suby = suby
endif else begin
divisy = divisy + 1
suby = suby + y(p+q)
endelse
if z(p+q) eq 32767.0 then begin
divisz = divisz
subz = subz
endif else begin
divisz = divisz + 1
subz = subz + z(p+q)
endelse
endfor
elem = (p + 1) / 60
if divisx lt 45 then begin
xhrav(elem) = 32767.0
endif else begin
xhrav(elem) = subx / divisx
endelse
if divisy lt 45 then begin
yhrav(elem) = 32767.0
endif else begin
yhrav(elem) = suby / divisy
endelse
if divisz lt 45 then begin
zhrav(elem) = 32767.0
endif else begin
zhrav(elem) = subz / divisz
endelse
xav = long(xhrav(elem) * 1000.0)
yav = long(yhrav(elem) * 1000.0)
zav = long(zhrav(elem) * 1000.0)
writeu,nfile,xav,yav,zav
endfor
print,'1-hour file written.'
close,nfile
free_lun,nfile
endfor
print,'DONE!'
end

