%% Load data file and specify necessary specification

%%  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.

global status

clr_max=length(colormap);

hv3=findobj(gcf,'Tag','Loading');
if ~strncmp(get(hv3,'string'),'Load',4)  % delete previous labels
   set(hv1a,'xticklabel','','xtick',[],'yticklabel','','ytick',[])
   set(hvl1,'string','');
   set(hv2a,'xticklabel','','xtick',[],'yticklabel','','ytick',[])
   set(hvl2,'string','')
end

% load Data Type (long/lat) or x/y km
%% Lat/Long or X/Y (km)
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 List Box
TransformList={'none','log10(z+1)', 'log10(z)', 'ln(z+1)','ln(z)','10log10(z)'};
ListHndl=findobj(gcf,'Tag','TransformOpt');
set(ListHndl,'String',TransformList,'fontweight','bold');
TransformInd=get(ListHndl,'Value');


%%%%%%%%%%%%%%% Load Data File %%%%%%%%%%%%%%%%%%%%%
if isstr(filepath) 
   if ~isempty(filepath)
      eval(['chdir ' '''' filepath '''']);
   end
end
if PlatForm == 1
   [filename, filepath]=uigetfile('*.dat; *.txt','Select an Input File');
elseif PlatForm == 2
   [filename, filepath]=uigetfile('*','Select an Input File');
elseif PlatForm == 3
   [filename, filepath]=uigetfile('*.dat','Select an Input File');
end
eval(['chdir ' '''' HDIR '''']);

if isstr(filename)				% file exists
   truncated_filename=filenametruncation(filename);
   filename1=[filepath filename];
   [x0,y0,var0,Lat_Long_Dat]=loaddatfile(filename1,index);
%%%%%%%%%%%%%%%%%%%%%%%% Data have been loaded into the memory %%%%%

%%  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

%% find display Scaling factor
  hd1=findobj(gcf,'Tag','RadioDispXY');
  DispFormat=get(hd1,'value');
  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
   for i=1:inc:n
     plot(long(i),lat(i),'.','color',cmap(clr(i),:),'markersize',markersize);
     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),:),'markersize',markersize);
     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),:),'markersize',markersize);
  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(FileIDstr)
     FileIDstr=filename;
     set(hdhdl,'string',FileIDstr);
  end
  
  if ~isempty(var)
   status=1;						% data have been loaded
  end
  save default_coord x y long lat
end				%%% load file is cancelled
