%% Load Data with different options
%%  Type:  Lat/Long or X/Y (km)
%%  Filter (w/ support): Mean, Median, Simple (skip)
%%  Data reduction:
%%  Data transformation: (LOG)
%%  Anisotropy:
%%  Display:  in Lat/Long or in X/Y (km)

%%  Kriging Software Package  version 1.0 beta,   October 29, 1998
%%  Copyright (c) 1998, property of Dezhang Chu and Woods Hole Oceanographic
%%  Institution.  All Rights Reserved.

DataTypeList={'Long/Lat(deg)', 'Lat/Long (deg)', 'X/Y (km)', 'Y/X km)'};
ListHndl=findobj(gcf,'Tag','DataXY');
set(ListHndl,'String',DataTypeList,'fontweight','bold');
index=get(ListHndl,'Value');

%% Data Filter
FilterList={'Mean', 'Median', 'Simple'};
ListHndl=findobj(gcf,'Tag','Filter');
set(ListHndl,'String',FilterList,'fontweight','bold');
FilterInd=get(ListHndl,'Value');

%% Data Filter Support
hdlsupport=findobj(gcf,'Tag','FilterSupport');
Support=round(str2num(get(hdlsupport,'String')));

%% Data reduction factor
hdlfactor=findobj(gcf,'Tag','ReductionFactor');
ReductFac=round(str2num(get(hdlfactor,'String')));


%% Data Transformation
set(ListTransformHndl,'String',TransformList,'fontweight','bold');
TransformInd=get(ListTransformHndl,'Value');

   [x0,y0,var0,Lat_Long_Dat]=loaddatfile(filename1,index);
%%  delete nans, filtering and data reduction
   [x,y,var,indxs,long,lat]=datachk(x0,y0,var0,ReductFac,FilterInd,Support,Lat_Long_Dat);
 %% Data Transformation 
   if TransformInd > 1
      var=datatransform(1,TransformInd,var);
      TransformStatus = 1;
   else
      TransformStatus = 2;  
   end
%%%%%%%%%%%%%%%%%%%%%%%% Data have been loaded into the memory %%%%%

%% find display Scaling factor
  hd1=findobj(gcf,'Tag','RadioDispXY');
  DispFormat=get(hd1,'value');
  if index >= 3 & DispFormat == 0 % X/Y data can't display in Lat/Long
     radiodispform(1);
     hd1=findobj(gcf,'Tag','RadioDispXY');
     DispFormat=get(hd1,'value');
  end
  if DispFormat == 1
   Lat_long=0;
   DispScl=1;
  else
   Lat_long=1;
   DispScl=1/111;
  end

  disp('plotting data')
%%%%%%%%%%%%%%%%%  plot the data  %%%%%%%%%%%%%%%%%
  hld=findobj(gcf,'Tag','DataPreparation');		% find handle of display kriging result f
  hv1=findobj(gcf,'Tag','Axes1');
  hv1=hv1(1);

  hld1i=get(hv1(1),'children');
  for i=1:length(hld1i)
    delete(hld1i(i))
  end

  n=length(x);
  sta=1:n;
  Pos1=get(hv1,'position');
  set(hv1,'xticklabel','','xtick',[])
  set(hv1,'yticklabel','','ytick',[])
  set(hv1,'yticklabel','','xticklabel','')
  set(get(hv1,'xlabel'),'string','')
  set(get(hv1,'ylabel'),'string','')
  hv1a=axes('parent',hld(1),'position',Pos1,'Tag','Axes1','NextPlot','add');
  clr=max(0,floor(clr_max*(var-min(var))/(max(var)-min(var))))+1;
  clr=min(clr,clr_max);
  cmap=colormap;
  deg2km=111;		% conversion factor from Longitude/Latitude to km
  inc=max(1,round(n/1600));
  if Lat_long == 1 & ~isempty(long)
   for i=1:inc:n
     plot(long(i),lat(i),'.','color',cmap(clr(i),:));
     if i == 1;hold on,end
   end
   hold off
   hx1=xlabel('Longitude ');
   hy1=ylabel('Latitude');
  else
   for i=1:inc:n
     plot(x(i),y(i),'.','color',cmap(clr(i),:));
     if i == 1;hold on,end
   end
   hold off
   hx1=xlabel('X (km)');
   hy1=ylabel('Y (km)');
  end  
  hvl1=[hx1 hy1];
  set(hvl1,'fontweight','bold')
  drawnow

  hv2=findobj(gcf,'Tag','Axes2');
  hv2=hv2(1);
  hld2i=get(hv2(1),'children');
  for i=1:length(hld2i)
   delete(hld2i(i))
  end

  set(hv2,'xticklabel','','xtick',[])
  set(hv2,'yticklabel','','ytick',[])
  set(hv2,'yticklabel','','xticklabel','')
  set(get(hv2,'xlabel'),'string','')
  set(get(hv2,'ylabel'),'string','')

  Pos2=get(hv2,'position');
  hv2a=axes('parent',hld(1),'position',Pos2,'Tag','Axes2','NextPlot','add');
  cla
  plot(sta,var,'-')
  hold on
  for i=1:inc:n
   plot(sta(i),var(i),'.','color',cmap(clr(i),:));
  end
  hold off

  hx2=xlabel('Sample Sequence');
  hy2=ylabel('Observation Value');
  hvl2=[hx2 hy2];
  set(hvl2,'fontweight','bold')

  hv3=findobj(gcf,'Tag','Loading');
  if strncmp(get(hv3,'string'),'Load',4)
   set(hv3,'string','Reload')
  end
  
  errAnisotropy=[0 0];
%%% find anisotropy parameters
  hk=findobj(gcf,'Tag','angle');
  ValStr=get(hk,'String');
  if ~isnan(ValStr)
    angle=str2num(ValStr);
  else
    errAnisotropy(1)=1;
  end
  hk=findobj(gcf,'Tag','ratio');
  ValStr=get(hk,'String');
  if ~isnan(ValStr)
   aspect_ratio=str2num(ValStr);
  else
   errAnisotropy(2)=1;
  end
  anisotropy_para=[angle aspect_ratio];
  
  %% File ID
  hdhdl=findobj(gcf,'Tag','FileID');
  FileIDstr=get(hdhdl,'string');
  
  if ~isempty(var)
   status=1;						% data have been loaded
  end
  save default_coord x y long lat
