pro norm_data,ndfile,file,avg

   restore,ndfile
   nant = header.nant
   nnd = 3
   ; Extract the three ND measurements
   nd = reform(avg(0:nant-1,0:nnd-1,*))
   ; Set NaN to zero and create a parallel flag array
   bad = where(finite(nd) eq 0,nbad)
   if (nbad ne 0) then nd[bad] = 0
   flag = nd ne 0
   ; Normalize the ND's to their own average, to create a time-dependent
   ; scale factor
   for i = 0, nant-1 do begin
      for j = 0, nnd-1 do begin
         nd[i,j,*] = nd[i,j,*] / (total(nd[i,j,*])/(total(flag[i,j,*])>1))
      endfor
   endfor

   ; Restore the real data
   restore,file
   ndp = avg
   nf = n_elements(f)
   ntimes=n_elements(tavg)
   ndf = [14.,15.,16.]
   ; Interpolate the ND factors at three frequencies to all frequencies
   for i = 0, nant-1 do begin
      for j = 0, ntimes-1 do begin
         good = where(flag[i,*,j] ne 0,ngood)
         case ngood of
           3: ndp[i,*,j] = interpol(reform(nd[i,*,j]),ndf,f)
           2: ndp[i,*,j] = interpol(reform(nd[i,good,j]),ndf[good],f)
           1: ndp[i,*,j] = replicate(nd[i,good,j],nf)
           0: ndp[i,*,j] = 0.0
         endcase
      endfor
   endfor

   nchan = n_elements(avg[*,0,0])
   a1 = [0,0,0,0,0,1,1,1,1,2,2,2,3,3,4]
   a2 = [1,2,3,4,5,2,3,4,5,3,4,5,4,5,5]
   ; Form proper combinations of the ND factors for baseline data
   for i = nant, nchan-1, 2 do begin
      k = (i-nant)/2
      ndp[i,*,*] = sqrt(ndp[a1[k],*,*]*ndp[a2[k],*,*])
      ndp[i+1,*,*] = sqrt(ndp[a1[k],*,*]*ndp[a2[k],*,*])
   endfor

   ; NDP is now a fully parallel array to the data in AVG, so we just
   ; multiply to apply the proper scaling
   avg = avg/ndp

return
end