%%% get parameters for running Least-Square-Fit function
%% Least-square-fit preparation 
%%  get required parameters from the 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.


global ndigits

if ~isempty(lag)
ModIndx=[1:3 5 20:22 24:28 30];
ListHndl=findobj(gcf,'Tag','VariogramModel');
index=get(ListHndl,'Value');
model=ModIndx(index);
hvar=findobj(gcf,'Tag','radioVAR');
value=get(hvar,'value');
if isempty(value) | value == 0
   model=-abs(model);						% covariance model
end

ht=title('Least Square Fitting in progress ...');
hvar0=findobj(gcf,'Tag','txtnugt');
nugt=str2num(get(hvar0,'String'));
hvar1=findobj(gcf,'Tag','txtsill');
sill=str2num(get(hvar1,'String'));
hvar2=findobj(gcf,'Tag','txtlscl');
lscl=str2num(get(hvar2,'String'));
hvar3=findobj(gcf,'Tag','txtosci');
ocsi=str2num(get(hvar3,'String'));
hvar4=findobj(gcf,'Tag','txtpowr');
powr=str2num(get(hvar4,'String'));

wgt=cnt/sum(cnt);
data=[lag(:) gammah(:) wgt(:)];
[Ng, Sil, L, b, p]=variogramlsqfit(model,data,nugt,sill,lscl,ocsi,powr);
%% ignore the imaginary partof the fitting coefficeint
Ng=abs(real(Ng));Sil=abs(real(Sil));L=abs(real(L));b=abs(real(b));p=abs(real(p));
model_para=[Ng Sil L b p];
gammah_theo=variogrammodel(model,lag,model_para);
if model > 0
   hv0=plot(lag,gammah,'o',lag,gammah_theo);
   hy=ylabel('Variogram');
else
   covfun=1-gammah;covfun_theo=1-gammah_theo;
   hv0=plot(lag,covfun,'o',lag,covfun_theo);
   hy=ylabel('Correlogram');
end
hx=xlabel('Lag (km)');
set([hx hy],'fontsize',12,'fontweight','bold')
set(get(hv0(1),'parent'),'Tag','variogramAxes1')

hv=findobj(gcf,'Tag','delx');
res=str2num(get(hv(1),'string'));
Ngstr=num2str(Ng);
set(hvar0,'String',Ngstr(1:min(ndigits,length(Ngstr))));
Silstr=num2str(Sil);
set(hvar1,'String',Silstr(1:min(ndigits,length(Silstr))));
Lstr=num2str(L);
set(hvar2,'String',Lstr(1:min(ndigits,length(Lstr))));
bstr=num2str(b);
set(hvar3,'String',bstr(1:min(ndigits,length(bstr))));
pstr=num2str(p);
set(hvar4,'String',pstr(1:min(ndigits,length(pstr))));


save default_variogram model Ng Sil L b p res
checksliderposition
else
   dd=warningtext;
end