function [evla2,evlo2,error,torigin]=cal_loc1(stla,stlo,time,baz,refgvel)

error = 0;

% defining initial points for location.

potini = [0.900000E+02 -180;
0.820706E+02 -126;
0.820706E+02 -54;
0.741413E+02 -126;
0.770568E+02 -90;
0.741413E+02 -54;
0.662119E+02 -126;
0.698679E+02 -103.39;
0.698679E+02 -76.61;
0.662119E+02 -54;
0.582825E+02 -126;
0.620571E+02 -109.502;
0.634350E+02 -90;
0.620571E+02 -70.498;
0.582825E+02 -54;
0.503532E+02 -126;
0.539614E+02 -112.867;
0.559107E+02 -97.8769;
0.559107E+02 -82.1231;
0.539614E+02 -67.133;
0.503532E+02 -54;
0.424238E+02 -126;
0.456838E+02 -114.956;
0.477653E+02 -102.811;
0.484832E+02 -90;
0.477653E+02 -77.189;
0.456838E+02 -65.044;
0.424238E+02 -54;
0.344944E+02 -126;
0.372707E+02 -116.367;
0.392137E+02 -106.114;
0.402160E+02 -95.4242;
0.402160E+02 -84.5758;
0.392137E+02 -73.886;
0.372707E+02 -63.633;
0.344944E+02 -54;
0.265651E+02 -126;
0.287551E+02 -117.393;
0.303792E+02 -108.467;
0.313795E+02 -99.299;
0.317175E+02 -90;
0.313795E+02 -80.701;
0.303792E+02 -71.533;
0.287551E+02 -62.607;
0.265651E+02 -54;
0.820706E+02 18;
0.770568E+02 -18;
0.741413E+02 18;
0.698679E+02 -31.39;
0.698679E+02 -4.61;
0.662119E+02 18;
0.620571E+02 -37.502;
0.634350E+02 -18;
0.620571E+02 1.502;
0.582825E+02 18;
0.539614E+02 -40.867;
0.559107E+02 -25.877;
0.559107E+02 -10.123;
0.539614E+02 4.867;
0.503532E+02 18;
0.456838E+02 -42.956;
0.477653E+02 -30.811;
0.484832E+02 -18;
0.477653E+02 -5.189;
0.456838E+02 6.956;
0.424238E+02 18;
0.372707E+02 -44.367;
0.392137E+02 -34.114;
0.402160E+02 -23.424;
0.402160E+02 -12.576;
0.392137E+02 -1.886;
0.372707E+02 8.367;
0.344944E+02 18;
0.287551E+02 -45.393;
0.303792E+02 -36.467;
0.313795E+02 -27.299;
0.317175E+02 -18;
0.313795E+02 -8.701;
0.303792E+02 0.467;
0.287551E+02 9.393;
0.265651E+02 18;
0.820706E+02 90;
0.770568E+02 54;
0.741413E+02 90;
0.698679E+02 40.61;
0.698679E+02 67.39;
0.662119E+02 90;
0.620571E+02 34.498;
0.634350E+02 54;
0.620571E+02 73.502;
0.582825E+02 90;
0.539614E+02 31.133;
0.559107E+02 46.123;
0.559107E+02 61.877;
0.539614E+02 76.867;
0.503532E+02 90;
0.456838E+02 29.044;
0.477653E+02 41.189;
0.484832E+02 54;
0.477653E+02 66.811;
0.456838E+02 78.956;
0.424238E+02 90;
0.372707E+02 27.633;
0.392137E+02 37.886;
0.402160E+02 48.576;
0.402160E+02 59.424;
0.392137E+02 70.114;
0.372707E+02 80.367;
0.344944E+02 90;
0.287551E+02 26.607;
0.303792E+02 35.533;
0.313795E+02 44.701;
0.317175E+02 54;
0.313795E+02 63.299;
0.303792E+02 72.467;
0.287551E+02 81.393;
0.265651E+02 90;
0.820706E+02 162;
0.770568E+02 126;
0.741413E+02 162;
0.698679E+02 112.61;
0.698679E+02 139.39;
0.662119E+02 162;
0.620571E+02 106.498;
0.634350E+02 126;
0.620571E+02 145.502;
0.582825E+02 162;
0.539614E+02 103.133;
0.559107E+02 118.123;
0.559107E+02 133.877;
0.539614E+02 148.867;
0.503532E+02 162;
0.456838E+02 101.044;
0.477653E+02 113.189;
0.484832E+02 126;
0.477653E+02 138.811;
0.456838E+02 150.956;
0.424238E+02 162;
0.372707E+02 99.633;
0.392137E+02 109.886;
0.402160E+02 120.576;
0.402160E+02 131.424;
0.392137E+02 142.114;
0.372707E+02 152.367;
0.344944E+02 162;
0.287551E+02 98.607;
0.303792E+02 107.533;
0.313795E+02 116.701;
0.317175E+02 126;
0.313795E+02 135.299;
0.303792E+02 144.467;
0.287551E+02 153.393;
0.265651E+02 162;
0.770568E+02 -162;
0.698679E+02 -175.39;
0.698679E+02 -148.61;
0.620571E+02 178.498;
0.634350E+02 -162;
0.620571E+02 -142.498;
0.539614E+02 175.133;
0.559107E+02 -169.877;
0.559107E+02 -154.123;
0.539614E+02 -139.133;
0.456838E+02 173.044;
0.477653E+02 -174.811;
0.484832E+02 -162;
0.477653E+02 -149.189;
0.456838E+02 -137.044;
0.372707E+02 171.633;
0.392137E+02 -178.114;
0.402160E+02 -167.424;
0.402160E+02 -156.576;
0.392137E+02 -145.886;
0.372707E+02 -135.633;
0.287551E+02 170.607;
0.303792E+02 179.533;
0.313795E+02 -171.299;
0.317175E+02 -162;
0.313795E+02 -152.701;
0.303792E+02 -143.533;
0.287551E+02 -134.607;
0.200667E+02 -121.048;
0.134416E+02 -116.494;
0.219590E+02 -112.721;
0.673914E+01 -112.188;
0.150431E+02 -108.288;
0.232750E+02 -104.07;
0.174962E-06 -108;
0.804317E+01 -103.948;
0.160451E+02 -99.7323;
0.239577E+02 -95.1634;
-0.673914E+01 -103.812;
0.992240E+00 -99.578;
0.871818E+01 -95.3241;
0.163948E+02 -90.8903;
0.239710E+02 -86.0932;
-0.134416E+02 -99.5057;
-0.607455E+01 -95.0565;
0.132787E+01 -90.7283;
0.872267E+01 -86.3739;
0.160659E+02 -81.8435;
0.233066E+02 -76.967;
-0.200667E+02 -94.9523;
-0.131138E+02 -90.2594;
-0.608065E+01 -85.827;
0.987660E+00 -81.5098;
0.805034E+01 -77.174;
0.150659E+02 -72.684;
0.219872E+02 -67.89;
-0.265651E+02 -90;
-0.200667E+02 -85.0477;
-0.134416E+02 -80.4943;
-0.673914E+01 -76.188;
-0.349924E-06 -72;
0.673914E+01 -67.812;
0.134416E+02 -63.506;
0.200667E+02 -58.952;
0.200667E+02 -49.048;
0.136218E+02 -54;
0.134416E+02 -44.494;
0.691570E+01 -58.605;
0.691570E+01 -49.395;
0.673914E+01 -40.188;
0.154742E-14 -63;
0.103581E-14 -54;
0.498698E-15 -45;
-0.900393E-08 -36;
-0.707892E+01 -67.319;
-0.725093E+01 -58.441;
-0.725093E+01 -49.559;
-0.707892E+01 -40.681;
-0.673914E+01 -31.812;
-0.142730E+02 -71.707;
-0.147809E+02 -62.867;
-0.149518E+02 -54;
-0.147809E+02 -45.133;
-0.142730E+02 -36.293;
-0.134416E+02 -27.506;
-0.215244E+02 -76.329;
-0.225217E+02 -67.462;
-0.230284E+02 -58.499;
-0.230284E+02 -49.501;
-0.225217E+02 -40.538;
-0.215244E+02 -31.671;
-0.200667E+02 -22.952;
-0.287551E+02 -81.3931;
-0.303792E+02 -72.467;
-0.313795E+02 -63.299;
-0.317175E+02 -54;
-0.313795E+02 -44.701;
-0.303792E+02 -35.533;
-0.287551E+02 -26.607;
-0.265651E+02 -18;
0.219590E+02 -40.721;
0.150431E+02 -36.288;
0.232750E+02 -32.071;
0.804317E+01 -31.948;
0.160451E+02 -27.732;
0.239577E+02 -23.163;
0.992239E+00 -27.578;
0.871818E+01 -23.324;
0.163948E+02 -18.89;
0.239710E+02 -14.093;
-0.607455E+01 -23.057;
0.132787E+01 -18.728;
0.872267E+01 -14.374;
0.160659E+02 -9.843;
0.233066E+02 -4.967;
-0.131138E+02 -18.259;
-0.608065E+01 -13.827;
0.987660E+00 -9.51;
0.805034E+01 -5.174;
0.150659E+02 -0.684;
0.219872E+02 4.11;
-0.200667E+02 -13.048;
-0.134416E+02 -8.494;
-0.673914E+01 -4.188;
-0.349924E-06 0;
0.673914E+01 4.188;
0.134416E+02 8.494;
0.200667E+02 13.048;
0.200667E+02 22.952;
0.136218E+02 18;
0.134416E+02 27.506;
0.691570E+01 13.395;
0.691570E+01 22.605;
0.673914E+01 31.812;
0.154742E-14 9;
0.103581E-14 18;
0.498698E-15 27;
-0.900391E-08 36;
-0.707892E+01 4.681;
-0.725093E+01 13.559;
-0.725093E+01 22.441;
-0.707892E+01 31.319;
-0.673914E+01 40.188;
-0.142730E+02 0.293;
-0.147809E+02 9.133;
-0.149518E+02 18;
-0.147809E+02 26.867;
-0.142730E+02 35.707;
-0.134416E+02 44.494;
-0.215244E+02 -4.329;
-0.225217E+02 4.538;
-0.230284E+02 13.501;
-0.230284E+02 22.499;
-0.225217E+02 31.462;
-0.215244E+02 40.329;
-0.200667E+02 49.048;
-0.287551E+02 -9.393;
-0.303792E+02 -0.467;
-0.313795E+02 8.701;
-0.317175E+02 18;
-0.313795E+02 27.299;
-0.303792E+02 36.467;
-0.287551E+02 45.393;
-0.265651E+02 54;
0.219590E+02 31.279;
0.150431E+02 35.712;
0.232750E+02 39.929;
0.804317E+01 40.052;
0.160451E+02 44.268;
0.239577E+02 48.837;
0.992239E+00 44.422;
0.871818E+01 48.676;
0.163948E+02 53.11;
0.239710E+02 57.907;
-0.607455E+01 48.943;
0.132787E+01 53.272;
0.872267E+01 57.626;
0.160659E+02 62.157;
0.233066E+02 67.033;
-0.131138E+02 53.741;
-0.608065E+01 58.173;
0.987659E+00 62.49;
0.805034E+01 66.826;
0.150659E+02 71.316;
0.219872E+02 76.11;
-0.200667E+02 58.952;
-0.134416E+02 63.506;
-0.673914E+01 67.812;
-0.349924E-06 72;
0.673914E+01 76.188;
0.134416E+02 80.494;
0.200667E+02 85.048;
0.200667E+02 94.952;
0.136218E+02 90;
0.134416E+02 99.506;
0.691570E+01 85.395;
0.691570E+01 94.605;
0.673914E+01 103.812;
0.277658E-14 81;
0.240691E-14 90;
0.197797E-14 99;
0.174962E-06 108;
-0.707892E+01 76.681;
-0.725093E+01 85.559;
-0.725093E+01 94.441;
-0.707892E+01 103.319;
-0.673914E+01 112.188;
-0.142730E+02 72.293;
-0.147809E+02 81.133;
-0.149518E+02 90;
-0.147809E+02 98.867;
-0.142730E+02 107.707;
-0.134416E+02 116.494;
-0.215244E+02 67.671;
-0.225217E+02 76.538;
-0.230284E+02 85.501;
-0.230284E+02 94.499;
-0.225217E+02 103.462;
-0.215244E+02 112.329;
-0.200667E+02 121.048;
-0.287551E+02 62.607;
-0.303792E+02 71.533;
-0.313795E+02 80.701;
-0.317175E+02 90;
-0.313795E+02 99.299;
-0.303792E+02 108.467;
-0.287551E+02 117.393;
-0.265651E+02 126;
0.219590E+02 103.279;
0.150431E+02 107.712;
0.232750E+02 111.929;
0.804317E+01 112.052;
0.160451E+02 116.268;
0.239577E+02 120.837;
0.992240E+00 116.422;
0.871818E+01 120.676;
0.163948E+02 125.11;
0.239710E+02 129.907;
-0.607455E+01 120.943;
0.132787E+01 125.272;
0.872267E+01 129.626;
0.160659E+02 134.157;
0.233066E+02 139.033;
-0.131138E+02 125.741;
-0.608065E+01 130.173;
0.987660E+00 134.49;
0.805034E+01 138.826;
0.150659E+02 143.316;
0.219872E+02 148.11;
-0.200667E+02 130.952;
-0.134416E+02 135.506;
-0.673914E+01 139.812;
-0.165958E-06 144;
0.673914E+01 148.188;
0.134416E+02 152.494;
0.200667E+02 157.048;
0.200667E+02 166.952;
0.136218E+02 162;
0.134416E+02 171.506;
0.691570E+01 157.395;
0.691570E+01 166.605;
0.673914E+01 175.812;
0.346516E-14 153;
0.350835E-14 162;
0.346516E-14 171;
0.174962E-06 180;
-0.707892E+01 148.681;
-0.725093E+01 157.559;
-0.725093E+01 166.441;
-0.707892E+01 175.319;
-0.673914E+01 -175.812;
-0.142730E+02 144.293;
-0.147809E+02 153.133;
-0.149518E+02 162;
-0.147809E+02 170.867;
-0.142730E+02 179.707;
-0.134416E+02 -171.506;
-0.215244E+02 139.671;
-0.225217E+02 148.538;
-0.230284E+02 157.501;
-0.230284E+02 166.499;
-0.225217E+02 175.462;
-0.215244E+02 -175.671;
-0.200667E+02 -166.952;
-0.287551E+02 134.607;
-0.303792E+02 143.533;
-0.313795E+02 152.701;
-0.317175E+02 162;
-0.313795E+02 171.299;
-0.303792E+02 -179.533;
-0.287551E+02 -170.607;
-0.265651E+02 -162;
0.219590E+02 175.279;
0.150431E+02 179.712;
0.232750E+02 -176.071;
0.804317E+01 -175.948;
0.160451E+02 -171.732;
0.239577E+02 -167.163;
0.992240E+00 -171.578;
0.871818E+01 -167.324;
0.163948E+02 -162.89;
0.239710E+02 -158.093;
-0.607455E+01 -167.056;
0.132787E+01 -162.728;
0.872267E+01 -158.374;
0.160659E+02 -153.844;
0.233066E+02 -148.967;
-0.131138E+02 -162.259;
-0.608065E+01 -157.827;
0.987660E+00 -153.51;
0.805034E+01 -149.174;
0.150659E+02 -144.684;
0.219872E+02 -139.89;
-0.200667E+02 -157.048;
-0.134416E+02 -152.494;
-0.673914E+01 -148.188;
-0.165958E-06 -144;
0.673914E+01 -139.812;
0.134416E+02 -135.506;
0.200667E+02 -130.952;
0.136218E+02 -126;
0.691570E+01 -130.605;
0.691570E+01 -121.395;
0.346516E-14 -135;
0.350835E-14 -126;
0.346516E-14 -117;
-0.707892E+01 -139.319;
-0.725093E+01 -130.441;
-0.725093E+01 -121.559;
-0.707892E+01 -112.681;
-0.142730E+02 -143.707;
-0.147809E+02 -134.867;
-0.149518E+02 -126;
-0.147809E+02 -117.133;
-0.142730E+02 -108.293;
-0.215244E+02 -148.329;
-0.225217E+02 -139.462;
-0.230284E+02 -130.499;
-0.230284E+02 -121.501;
-0.225217E+02 -112.538;
-0.215244E+02 -103.671;
-0.287551E+02 -153.393;
-0.303792E+02 -144.467;
-0.313795E+02 -135.299;
-0.317175E+02 -126;
-0.313795E+02 -116.701;
-0.303792E+02 -107.533;
-0.287551E+02 -98.6069;
-0.344944E+02 -162;
-0.424238E+02 -162;
-0.367217E+02 -152.546;
-0.503532E+02 -162;
-0.447551E+02 -151.261;
-0.383360E+02 -142.542;
-0.582825E+02 -162;
-0.528389E+02 -149.274;
-0.463531E+02 -139.614;
-0.392315E+02 -132.095;
-0.662119E+02 -162;
-0.609365E+02 -146.028;
-0.543402E+02 -135.052;
-0.470314E+02 -127.253;
-0.393232E+02 -121.384;
-0.741413E+02 -162;
-0.689502E+02 -140.123;
-0.620982E+02 -127.556;
-0.545371E+02 -119.814;
-0.466386E+02 -114.547;
-0.385606E+02 -110.641;
-0.820706E+02 -162;
-0.765465E+02 -127.086;
-0.691330E+02 -114.279;
-0.612570E+02 -108.123;
-0.532149E+02 -104.466;
-0.450965E+02 -101.975;
-0.369375E+02 -100.109;
-0.900000E+02 -180;
-0.820706E+02 -90;
-0.741413E+02 -90;
-0.662119E+02 -90;
-0.582825E+02 -90;
-0.503532E+02 -90;
-0.424238E+02 -90;
-0.344944E+02 -90;
-0.367217E+02 -80.5463;
-0.447551E+02 -79.261;
-0.383360E+02 -70.542;
-0.528389E+02 -77.274;
-0.463531E+02 -67.614;
-0.392315E+02 -60.095;
-0.609365E+02 -74.028;
-0.543402E+02 -63.052;
-0.470314E+02 -55.253;
-0.393232E+02 -49.384;
-0.689502E+02 -68.123;
-0.620982E+02 -55.556;
-0.545371E+02 -47.814;
-0.466386E+02 -42.547;
-0.385606E+02 -38.641;
-0.765465E+02 -55.086;
-0.691330E+02 -42.279;
-0.612570E+02 -36.123;
-0.532149E+02 -32.465;
-0.450965E+02 -29.975;
-0.369375E+02 -28.109;
-0.820706E+02 -18;
-0.741413E+02 -18;
-0.662119E+02 -18;
-0.582825E+02 -18;
-0.503532E+02 -18;
-0.424238E+02 -18;
-0.344944E+02 -18;
-0.367217E+02 -8.546;
-0.447551E+02 -7.261;
-0.383360E+02 1.458;
-0.528389E+02 -5.274;
-0.463531E+02 4.386;
-0.392315E+02 11.905;
-0.609365E+02 -2.028;
-0.543402E+02 8.948;
-0.470314E+02 16.747;
-0.393232E+02 22.616;
-0.689502E+02 3.877;
-0.620982E+02 16.444;
-0.545371E+02 24.186;
-0.466386E+02 29.453;
-0.385606E+02 33.359;
-0.765465E+02 16.914;
-0.691330E+02 29.721;
-0.612570E+02 35.877;
-0.532149E+02 39.535;
-0.450965E+02 42.025;
-0.369375E+02 43.891;
-0.820706E+02 54;
-0.741413E+02 54;
-0.662119E+02 54;
-0.582825E+02 54;
-0.503532E+02 54;
-0.424238E+02 54;
-0.344944E+02 54;
-0.367217E+02 63.454;
-0.447551E+02 64.739;
-0.383360E+02 73.458;
-0.528389E+02 66.726;
-0.463531E+02 76.386;
-0.392315E+02 83.905;
-0.609365E+02 69.972;
-0.543402E+02 80.948;
-0.470314E+02 88.747;
-0.393232E+02 94.616;
-0.689502E+02 75.877;
-0.620982E+02 88.444;
-0.545371E+02 96.186;
-0.466386E+02 101.453;
-0.385606E+02 105.359;
-0.765465E+02 88.914;
-0.691330E+02 101.721;
-0.612570E+02 107.877;
-0.532149E+02 111.534;
-0.450965E+02 114.025;
-0.369375E+02 115.891;
-0.820706E+02 126;
-0.741413E+02 126;
-0.662119E+02 126;
-0.582825E+02 126;
-0.503532E+02 126;
-0.424238E+02 126;
-0.344944E+02 126;
-0.367217E+02 135.454;
-0.447551E+02 136.739;
-0.383360E+02 145.458;
-0.528389E+02 138.726;
-0.463531E+02 148.386;
-0.392315E+02 155.905;
-0.609365E+02 141.972;
-0.543402E+02 152.948;
-0.470314E+02 160.747;
-0.393232E+02 166.616;
-0.689502E+02 147.877;
-0.620982E+02 160.444;
-0.545371E+02 168.186;
-0.466386E+02 173.453;
-0.385606E+02 177.359;
-0.765465E+02 160.914;
-0.691330E+02 173.721;
-0.612570E+02 179.877;
-0.532149E+02 -176.465;
-0.450965E+02 -173.975;
-0.369375E+02 -172.109];

len_ini=size(potini);

for i = 1 : len_ini(1)
    [ini_error(i),tmporigin(i)] = ...
    cal_errorinloc1(stla,stlo,time,baz,potini(i,1),potini(i,2),refgvel);
end

[dumm,imin0] = min(ini_error');
evla0 = potini(imin0,1);
evlo0 = potini(imin0,2);
iniorigin = tmporigin(imin0);

[potpos] = cal_potpos(evla0,evlo0,15);
len_pot = size(potpos);

% We need a search for origin time. 

for j = 1 : len_pot(1)
    [tmp_error(j),totemp(j)] = ...
    cal_errorinloc1(stla,stlo,time,baz,potpos(j,1),potpos(j,2),refgvel);
end

[dumm,imin1] = min(tmp_error');
evla2 = potpos(imin1,1);
evlo2 = potpos(imin1,2);
error = dumm;
torigin = totemp(imin1);
