function [otime,dist,qual]=cal_GV_DIST(timein,gV,iref)

% function designed to estimate distance and quality of time data
% Update to consider origin time. 

NT=length(gV);
edist = 10:10:180;
tdist = edist*6371*pi/180;

ttime = -300:50:300;
mm_d = length(tdist);
mm_t = length(ttime);


for i = 1 : mm_d
    ottime = timein(iref)-tdist(i)/gV(iref);
    ttime1 = ttime + ottime;
    for j = 1 : mm_t
    tmptime = ttime1(j) + tdist(i)./gV';
    error(i,j) = sum(sqrt((tmptime - timein).^2));
    end
end

[dumm,ierror]=min(error);
[dumm1,ierror1]=min(dumm);

itime = ierror1;
idist = ierror(ierror1);

dist = tdist(idist);

otime = timein(iref)-dist/gV(iref)+ttime(itime);
tmptime = otime + dist./gV';
qual(2:NT+1) = tmptime - timein;
qual(1) = sqrt(sum((tmptime - timein).^2)/NT);
