averagemip.pro 3.81 KB
; $Id:   $
;====================================================================
;---------- Special MIP averaging  2D array
;====================================================================

pro AVERAGEMIP, AverStructure, T, Val 
common GraphC, Graph, GraphN, Item

 Yinfo = SIZE(Val)  

 if (Yinfo[0] eq 2) then begin
      Val = reform(Val, Yinfo[1]*Yinfo[2]);
      RetSize = Yinfo[2];
endif else RetSize = Yinfo[1];
 
 if (Graph[GraphN].Step GT AverStructure.Sampling*2.0) then begin 
      MF =  AverStructure.Nres eq -1L ? Val : [*(AverStructure.LastVal),VAL]
      Time = AverStructure.Nres eq -1L ? T : [*(AverStructure.LastTime),T]
      N_Time = N_elements(Time)
      NAve = long(((Time[N_Time-1] < Graph[GraphN].TotalTime) - Time[0]) / Graph[GraphN].Step + 1)> 1L   
   
      MF = REFORM(MF, Yinfo[1], N_Time) 

       numStart = where(abs((*AverStructure.Time)-Time[0]) LE Graph[GraphN].Step/2.0, IndexStart)
       if (IndexStart le 0) then begin
            AverStructure.Nres = -1L
            return
       endif

      NStart = numStart[0]
             
      for i = NStart, NStart+NAve-1L  do begin 
  
         num = where(abs((*AverStructure.Time)[i]-Time) LE Graph[GraphN].Step/2.0, Index)
         if (Index LE 0) AND (Graph[GraphN].Sampling[Item] LT Graph[GraphN].MaxSampling[Item]) then $
              num = where(abs((*AverStructure.Time)[i]-Time) LE Graph[GraphN].MaxSampling[Item]/2.0, Index)

         if (Index GT 0) then begin         
                 for k = 0, Yinfo[1]-1 do begin
                    if (Index GT 1) then begin
                        temp = 0.0;
                        numK = 0;
                        for kk = 0, Index-1 do begin                         
                            ;    if (finite(MF[k,num[kk]])) then begin
                                if (MF[k,num[kk]] GT  Graph[GraphN].FillValue[Item]) then begin
                                    temp += MF[k,num[kk]];
                                    numK++;
                                endif                        
                         endfor                   
                        (*AverStructure.Val)[i*Yinfo[1]+k]  = numK GT 0 ? temp/ numK : Graph[GraphN].FillValue[Item] ; 
                    endif else begin 
                         (*AverStructure.Val)[i*Yinfo[1]+k] = MF[k,num[0]]
                     endelse
               endfor                      
         endif
  
      endfor  
      AverStructure.Nres = Index GT 0 ? (num[Index-1]+1) < (N_Time-1) : -1L  
      
      if (Graph[GraphN].CurrentSec LT (Graph[GraphN].Sections-1)) AND (AverStructure.Nres ne -1) then begin
            *(AverStructure.LastTime) = Time[AverStructure.NRes:*]; 
            *(AverStructure.LastVal) = REFORM(MF[*,AverStructure.NRes:N_Time-1], Yinfo[1]*(N_Time-AverStructure.Nres))
  endif
      
      if (Graph[GraphN].CurrentSec eq (Graph[GraphN].Sections-1)) then begin
        if ptr_valid(AverStructure.LastTime) then ptr_free, AverStructure.LastTime
        if ptr_valid(AverStructure.LastVal) then ptr_free, AverStructure.LastVal
      endif
      
   endif else begin ;if (Step GT Sampling*2.0)

     records = N_elements(*AverStructure.Time);

     if (records eq 0) then begin
         (*AverStructure.Time) = T; : [*AverStructure.Time,T]
         (*AverStructure.Val) =  Val;
     endif else begin 
         numBefore = where((*AverStructure.Time)[records-1] GT T, IndexBefore);
         if (IndexBefore eq 0) then begin
                (*AverStructure.Time) =  [*AverStructure.Time,T];
                (*AverStructure.Val) =   [*AverStructure.Val,Val];
         endif else begin
                (*AverStructure.Time) =  [*AverStructure.Time,T[IndexBefore-1:RetSize-1]];              
                (*AverStructure.Val) =  [*AverStructure.Val,Val[Yinfo[1]*(IndexBefore-1):Yinfo[1]*RetSize-1]]  
         endelse
     endelse
    endelse  

return
end