%% get kriging parameters from the Kriging GUI panel

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

%% get coordinates parameters
pStr1=['xmin';'xmax';'delx';'ymin';'ymax';'dely'];
errCoord=zeros(1,length(pStr1));
errCoord=zeros(1,length(pStr1));
for i=1:length(pStr1)
  hc=findobj(gcf,'Tag',pStr1(i,:));
  StrCoord=get(hc,'String');
  if length(StrCoord) > 0
     if i <= 2
        eval([pStr1(i,:) '=' StrCoord '/DispScl;']);
     else
        eval([pStr1(i,:) '=' StrCoord '/DispScl;']);
     end
  else
     errCoord(i)=1;
  end
end
%disp(errCoord)
if Lat_long == 1
   hsy1=findobj(gcf,'Tag',pStr1(4,:));
   hsy2=findobj(gcf,'Tag',pStr1(5,:));
   yminStr=get(hsy1,'String');
   ymaxStr=get(hsy2,'String');
   eval(['lat_min = ' yminStr ';' ]);
   eval(['lat_max = ' ymaxStr ';']);
   latcoef1=min(cos(lat_min*pi/180),cos(lat_max*pi/180));
   latcoef2=max(cos(lat_min*pi/180),cos(lat_max*pi/180));
   hsx1=findobj(gcf,'Tag',pStr1(1,:));
   hsx2=findobj(gcf,'Tag',pStr1(2,:));
   xminStr=get(hsx1,'String');
   xmaxStr=get(hsx2,'String');
   eval(['long_min = ' xminStr ';' ]);
   eval(['long_max = ' xmaxStr ';']);
end
   
%% get semi-variogram parameters
pStr2=['vmod';'nugt';'sill';'lscl';'powr';'osci'];
errVario=zeros(1,length(pStr2));
errVario=zeros(1,length(pStr2));
for i=1:length(pStr2)
  hv=findobj(gcf,'Tag',pStr2(i,:));
  StrVario=get(hv,'String');
  if length(StrVario) > 0
     eval([pStr2(i,:) '=' StrVario ';']);
  else
     errVario(i)=1;
  end
end
lscl=lscl/DispScl;
%disp(errVario)

%%%%%%%%%  get kriging parameters

%%% get kriging model index
pStr3R=['RadioOA';'RadioOK';'RadioUK'];
value=0;
mod_opt=0;
while (value == 0 & mod_opt < 3 )
  mod_opt=mod_opt+1;
  hk=findobj(gcf,'Tag',pStr3R(mod_opt,:));
  value=get(hk,'value');
  if isempty(value) value =0;end
end
if value == 1
   errMod=0;
 % sprintf(pStr3R(mod_opt,:))
else
   errMod=1;
end


%% determine whether is point-point or point - block kriging
pStr3B=['RadioPP';'RadioPB'];
value=0;
blk_opt=0;
while (value == 0 & blk_opt < 2)
  blk_opt=blk_opt+1;
  hk=findobj(gcf,'Tag',pStr3B(blk_opt,:));
  value=get(hk,'value');
  if isempty(value) value =0;end
end
if value == 1
   errBlk=0;
else
   errBlk=1;
end

%% find other parameters
pStr3=['blkn';'srad';'kmin';'kmax';'elim'];
errKrig=zeros(1,length(pStr3));
for i=1:length(pStr3)
  hk=findobj(gcf,'Tag',pStr3(i,:));
  StrKrig=get(hk,'String');
  if length(StrKrig) > 0
     eval([pStr3(i,:) '=' StrKrig ';']);
  else
     errKrig(i)=1;
  end
end
srad=srad/DispScl;
errKrig=[errMod errBlk errKrig errAnisotropy];

if sum(errCoord)+sum(errVario)+sum(errKrig) > 0
   disp('Missing Parameters !!')
   break
else
   res=(delx+dely)/2;
   xg=xmin:res:xmax;
   yg=ymin:res:ymax;
   nx=length(xg);
   ny=length(yg);
   if Lat_long == 1
     Xg=xg(ones(ny,1),:);
     lat_grd=linspace(lat_min,lat_max,ny);
     coefxy=cos(lat_grd(:)*pi/180);
     LatCoef=coefxy(:,ones(1,nx));
     Xg=Xg.*LatCoef;
     xc=reshape(Xg,1,nx*ny);
   else
     xc=reshape(xg(ones(ny,1),:),1,nx*ny);
   end
   Yg=yg(ones(nx,1),:)';
   yc1=reshape(Yg,1,nx*ny);
   yc=reshape(yg(ones(nx,1),:)',1,nx*ny);
   nv=nx*ny;
   L=lscl;p=powr;b=osci;
   model_para=[nugt sill L b p];
   hk=findobj(gcf,'Tag','TextInfo');
   disp('Kriging #1 in progress, ...');
   tic
   set(hk,'String','Kriging #1 in progress, ...');
   hkJob=findobj(gcf,'Tag','JobPercent');
   set(hkJob,'String','');
   anisotrophy_para=[angle,aspect_ratio];
   krig_para=[blk_opt,blkn,srad,res,kmin,kmax,elim,anisotropy_para];
   [Ig,ep_g]=krig(mod_opt,xc,yc,x,y,var, ...
      krig_para,vmod,model_para);
   toc
   Ig=Ig(:);
   if max(var) < 0
      indx=find(Ig > 0);
   elseif min(var) > 0
      indx=find(Ig < 0 );
   else
      indx=[];
   end
   Ig(indx)=zeros(size(indx));
   krig_para(1)=1;
   set(hk,'String','Kriging #2 in progress, ...');
   set(hkJob,'String','');
   tic
   disp('Kriging #2 in progress, ...');
   [Is,ep_s]=krig(mod_opt,x,y,xc,yc,Ig,krig_para,model,model_para);
   toc
   Is=Is(:);
   if max(var) < 0
      indx=find(Is > 0);
   elseif min(var) > 0
      indx=find(Is < 0 );
   end
   Is(indx)=zeros(size(indx));
   Is=Is(:);
   set(hk,'String','Kriging is done !!');
end

status=3;