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

% function designed to estimate distance and quality of time data

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

ref_time = timein(iref)-tdist./gV(iref);
mm_t = length(tdist);


for i = 1 : mm_t
    tmptime = ref_time(i) + tdist(i)./gV';
    error(i) = sum(sqrt((tmptime - timein).^2));
end


[dumm,ierror]=min(error);
dist = tdist(ierror);

otime = ref_time(ierror);
tmptime = ref_time(ierror) + tdist(ierror)./gV';
qual(2:NT+1) = tmptime - timein;
qual(1) = sqrt(sum((tmptime - timein).^2)/NT);
