function mapstat
%
%--------------------------------------------------------------------
%   Written by Vassilis Zervakis, in February 1996, while at
%              AINCO - Interocean, Madrid, Spain.
%--------------------------------------------------------------------
%              Modified by Mike Sparrow Dec. 1996.
%                                       Nov. 1997 (add. of tchange.m)
%--------------------------------------------------------------------
%
global mapfigure
global minlat minlon maxlat maxlon
global sizelon sizelat
global mapfigure
global colors isobaths
global X Y Umean Vmean
global DATAPATH FLOATS skale
global sizevect gridsize Tu Tv
global HANDLERR T11 T22 dT11 dT22 R1 R2 lag
%
%  Convert latitudes and longitudes in minutes -------
Ymax = fix(maxlat)*60. + (maxlat-round(maxlat))*0.6;
Ymin = fix(minlat)*60. + (minlat-round(minlat))*0.6;
Xmax = fix(maxlon)*60. + (maxlon-round(maxlon))*0.6;
Xmin = fix(minlon)*60. + (minlon-round(minlon))*0.6;
if (sizelat*sizelon ~= 0.), 
  rows = ceil((Ymax - Ymin)/sizelat);
  cols = ceil((Xmax - Xmin)/sizelon);
 else,
  rows = 1.;
  clos = 1.;
end
boxes = rows * cols;
%

%   read a lot of floats
%
%
% ------Load floats---------------------------
%--Initialize the X, Y matrices --------------------------
X = zeros(rows,cols);
Y = zeros(rows,cols);
Umean = zeros(rows,cols) + 999.;
Vmean = zeros(rows,cols) + 999.;
Sumdata = zeros(rows,cols);
ans = 1;
numfloats = 0;
floatsread = [];
while ans == 1,
   [float, number, timer, temp, pres, lat, lon, u, v, www] = rfcread1;
%  Make sure this float has not already been read -------------
   flread = 0;
   k = 1;
   while (flread == 0) & (k <= numfloats),
      flread = strcmp( float, floatsread(k,:)) ;
      k = k + 1;        
   end
%  if the float has not been previously read, -----------------
   if flread == 0,
     numfloats = numfloats + 1 ;
     if floatsread == [],
        floatsread = str2mat(float);
      else
        floatsread = str2mat(floatsread, float);
     end
     NN = length(lat);
     clear number;
     clear temp;
     clear pres;
     clear wwww;
%    --Check if float data within the time limits ------
     if (min(lat)>maxlat)|(max(lat)<minlat)|(min(lon)>maxlon)|(max(lon)<minlon),
%    --if float data outside the space limits ------
       mbox('OK','Data out of space limits',' Select other float ' )
       numfloats = numfloats - 1;
       [fm,fn] = size(floatsread);
%    --subtract this float from the floatsread matrix-----
       floatmatr = floatsread(1:fm-1,:);
       clear floatsread;
       floatsread = floatmatr;
       clear floatmatr;
       clear fm;
       clear fn;
       ans = 1;
     else
%    --if float data within the space limits ------
%      ---Load the X, Y matrices with data from the floats -------
       for kk = 1:NN,
        if (lat(kk)<maxlat)&(lat(kk)>minlat)&(lon(kk)<maxlon)&(lon(kk)>minlon),
%    ---find the indeces of the matrixes X and Y that correspond to the 
%    ---geographical coordinates of the float -------------------------
          xminutes = (lon(kk)-minlon)*60.;
          yminutes = (maxlat-lat(kk))*60.;
          if sizelat == 0., 
            sizelat = yminutes;
          end
          if sizelon == 0., 
            sizelon = xminutes;
          end
          ipos = ceil(yminutes / sizelat);
          jpos = ceil(xminutes / sizelon);
          if Sumdata(ipos,jpos) == 0,
               Umean(ipos,jpos) =0.;
               Vmean(ipos,jpos) =0.;
          end
          Umean(ipos,jpos) = Umean(ipos,jpos) + u(kk);
          Vmean(ipos,jpos) = Vmean(ipos,jpos) + v(kk);
          Sumdata(ipos,jpos) = Sumdata(ipos,jpos) + 1;
        end
       end
     end
     clear u;
     clear v;
     clear lon;
     clear lat;
     charans = mbox('YESNO','Include another float in the statistics? ' );
     ans = strcmp(charans, 'YES');
   else
     charans = mbox('YESNO','THIS FLOAT HAS ALREADY BEEN LOADED.',...
                            'Load another one ? ');
     ans = strcmp(charans, 'YES');
   end
end
global FLOATS
for ipos = 1:rows,
  for jpos = 1:cols,
      if Sumdata(ipos,jpos) ~= 0,
         Umean(ipos,jpos) = Umean(ipos,jpos) / Sumdata(ipos,jpos);
         Vmean(ipos,jpos) = Vmean(ipos,jpos) / Sumdata(ipos,jpos);
      else
         Umean(ipos,jpos) = 999.;
         Vmean(ipos,jpos) = 999.;
      end
  end
end
% Make vectors containing the good X and Y data------------
x1 = 500.;
for ipos = 1:rows,
  for jpos = 1:cols,
     if Umean(ipos,jpos) == 999.,
       Y(ipos,jpos) = 999.+ipos;
       X(ipos,jpos) = 999.+jpos;
      else
       Y(ipos,jpos) = maxlat - (ipos - 0.5)*sizelat/60.;
       X(ipos,jpos) = minlon + (jpos - 0.5)*sizelon/60.;
     end
  end
end
if mapfigure == [],
   basemap(colors,isobaths,'big') ;
 else
   figure(mapfigure);
end
% draw a vector to show the magnitude of 10cm/sec.---
if (rows > 1) & (cols > 1),
     jlabel = rows - 1;
     ilabel = 1;
     Y(ilabel, jlabel) = maxlat - (jlabel - 0.2)*sizelat/60.;
     X(ilabel, jlabel) = minlon + (ilabel)*sizelon/60.;
     Umean(ilabel,jlabel) = sizevect;
     Vmean(ilabel,jlabel) = sizevect;
     y1 =  maxlat - (jlabel-0.2)*sizelat/60.;
     x1 =  minlon + (ilabel+(cols/15))*sizelon/60.;
     if colors == 'yes',
       textcolor = 'k';
     else
       textcolor = 'w';
     end
     text('Units','data',...
          'Position',[x1 y1],...
          'String',[ 'scale ' num2str(sizevect) ' cm/sec'],...
          'Color',textcolor,...
          'Fontsize',8);
end
  hold on
  gridsize = 1/sqrt(cols * rows) ;
  times = 100/(sizevect*gridsize) ;
  title('Mean Velocities (Eulerian Frame)');
if (rows > 1) & (cols > 1),
quiver(X, Y, Umean, Vmean, times);
else
mbox('OK','Rows or Cols. = 1.','Vectors not shown.');

     if colors == 'yes',
       textcolor = 'k';
     else
       textcolor = 'w';
     end

   for i=1:rows,
    for j=1:cols,
     text('Units','data',...
          'Position',[X(i,j) Y(i,j)+3],...
          'String',[ 'Umean = ' num2str(Umean(i,j)) ' cm/sec'],...
          'Color',textcolor,...
          'Fontsize',8);
     text('Units','data',...
          'Position',[X(i,j) Y(i,j)+2],...
          'String',[ 'Vmean = ' num2str(Vmean(i,j)) ' cm/sec'],...
          'Color',textcolor,...
          'Fontsize',8);
    end
   end
end
%
% Scales to errors and vectors - at the moment they don't always agree for
% some reason, hence the 'fiddle' below to correct the lengths when all the
% floats have been loaded. This probably has something to do with 'quiver'
% and will hopefully be corrected in the future. This does not affect the
% final error estimates in errorbar.asc.
%
%if (rows > 1) & (cols > 1),
%   if (numfloats > 7),
%    skale = 0.13
%   else
%    skale = 0.1
%   end
%
%     ik = 0 ;
%     for theta = 0 : 30 : 360 ;
%       ik = ik + 1 ;
%       radangle = theta * pi / 180 ;
%       uuu(ik)=X(ilabel,jlabel)+(skale)*Umean(ilabel,jlabel)*cos(radangle)/(sizevect*gridsize);
%       vvv(ik)=Y(ilabel,jlabel)+(skale)*Vmean(ilabel,jlabel)*sin(radangle)/(sizevect*gridsize);
%     end
%     line(uuu,vvv)
%      markarea('point',uuu,vvv)
%     drawnow
 
end
%
FLOATS = floatsread;
uicontrol('Style','pushbutton',...
          'String','Errors',...
          'Enable','on',...
          'Units','normalized',...
          'Position',[0.906 0.19 0.19 0.1],...
          'Callback','errstat');
uicontrol('Style','pushbutton',...
          'String','specifyT',...
          'Enable','on',...
          'Units','normalized',...
          'Position',[0.906 0.39 0.19 0.1],...
          'Callback','tchange2');
uicontrol('Style','pushbutton',...
          'String','About',...
          'Enable','on',...
          'Units','normalized',...
          'Position',[0.906 0.79 0.19 0.1],...
          'Callback','astats');
uicontrol('Style','pushbutton',...
          'String','K by autocorr.',...
          'Enable','on',...
          'Units','normalized',...
          'Position',[0.685 0.0 0.3 0.05],...
          'Callback','eddy01');
uicontrol('Style','pushbutton',...
          'String','K by str. functions',...
          'Enable','on',...
          'Units','normalized',...
          'Position',[0.05 0.0 0.3 0.05],...
          'Callback','eddy02');
pos = [ 0.906, 0.59, 0.19, 0.1 ];
uicontrol('style','pushbutton',...
          'string','Grid',...
          'Enable','on',...
          'Units','normalized',...
          'Position',pos,...
          'Callback',['global isobathy isolines,',...
                      'if isolines ~= [],',...
                      '    isobath2,',...
                      'end,',...
                      'statgrid']);
