function E0000(IENTRY:integer; maxdeg:pinteger; alt,glat,glon, time:double;dec, dip,ti, gv:pdouble):double;
label GEOMAG,GEOMG1,s3,s4,s50;
var
list1,list2:tstringlist;
u:array[0..6] of string;
maxord,i,icomp,n,m,j,D1,D2,D3,D4:integer;
c,cd,tc,dp,k:array[0..13,0..13] of double;
pp, fn,cp ,sp, fm:array [0..13]of double;
snorm:array [0..169]of double;
pi,dtr,a,b,re,a2,b2,c2,a4,b4,c4,epoch,gnm,hnm,dgnm,dhnm,flnmj,otime,oalt,olat,olon,dt,rlon,rlat,srlon,srlat,
crlon,crlat,srlat2, crlat2,q,q1,q2,ct,st,r2,r,d,ca,sa,aor,ar,br,bt,bp,bpp,par,temp1,temp2,parp,bx,by,bz,bh:double;
model:array [0..20] of char; c_new:array [0..5]of double;
fname:textfile;
begin
if IENTRY=0 then goto GEOMAG;
if IENTRY=0 then goto GEOMG1; GEOMAG:
begin
assignfile(fname,'WMM.COF');
reset(fname);
readln(fname); maxord := maxdeg^;
sp[0] := 0.0;
cp[0] := 1.0;
pp[0] := 1.0;
dp[0][0] := 0.0;
a := 6378.137;
b := 6356.7523142;
re := 6371.2;
a2 := a*a;
b2 := b*b;
c2 := a2-b2;
a4 := a2*a2;
b4 := b2*b2;
c4 := a4 - b4; c[0][0] := 0.0;
cd[0][0] := 0.0; list1:= tstringlist.Create;
list2:=tstringlist.Create;
list1.LoadFromFile('WMM.COF');
list2.Delimiter:=' ';
list2.DelimitedText:=list1.Text;
list1.Free;
for i:=0 to 5 do
u[i]:=list2.Strings[i];
list2.Free; S3:
if (u[i]= '') then goto S4; if (u[i] <> '') then
begin
for i:=0 to 3 do
BEGIN
c_new[i] := strtofloat(u[i]);
c_new[i+1] := strtofloat('\0');
END;
end; if c_new[i]=9999 then goto S4; n:=strtoint(u[0]);
m:=strtoint(u[1]);
gnm:=strtofloat(u[2]);
hnm:=strtofloat(u[3]);
dgnm :=strtofloat(u[4]);
dhnm:=strtofloat(u[5]); if (n > maxord)then goto S4; if (m <= n) then
begin
c[m][n] := gnm;
cd[m][n] := dgnm;
if (m <> 0) then
begin
c[n][m-1] := hnm;
cd[n][m-1] := dhnm;
end;
end; goto S3; S4:
snorm[0]:=1.0;
fm[0] := 0.0;
for n := 1 to maxord do
begin
snorm[n]:=snorm[n-1]*strtofloat(inttostr(2*n-1))/strtofloat(inttostr(n)) ;
j:=2;
m:=0;
for D2 := (n-m+1) to 0 do
begin
k[m][n]:=strtofloat(inttostr((n-1)*(n-1)-(m*n)))/strtofloat(inttostr((2*n-1)*(2*n-3)));
if m>0 then
begin
flnmj:= strtofloat(inttostr((n-m+1)*j))/strtofloat(inttostr(m+n));
snorm[n+m*13]:=snorm[n+(m-1)*13]*sqrt(flnmj);
j:=1;
c[n][m-1]:=snorm[n+m*13]*c[n][m-1];
cd[n][m-1] := snorm[n+m*13]*cd[n][m-1];
end;
end;
fn[n] := strtofloat(inttostr(n+1));
fm[n] := strtofloat(inttostr(n));
end;
k[1][1] := 0.0; otime :=-1000.0;
oalt :=-1000.0;
olat :=-1000.0;
olon := -1000.0;
closefile(fname);
//exit;
end;
GEOMG1:
begin
dt := time - epoch; pi := 3.14159265359;
dtr := pi/180.0;
rlon := glon*dtr;
rlat := glat*dtr;
srlon := sin(rlon);
srlat := sin(rlat);
crlon := cos(rlon);
crlat := cos(rlat);
srlat2 := srlat*srlat;
crlat2 := crlat*crlat;
sp[1] := srlon;
cp[1] := crlon;//* CONVERT FROM GEODETIC COORDS. TO SPHERICAL COORDS. */
if (alt <> oalt) or (glat <> olat) then
begin
q := sqrt(a2-c2*srlat2);
q1 := alt*q;
q2 := ((q1+a2)/(q1+b2))*((q1+a2)/(q1+b2));
ct := srlat/sqrt(q2*crlat2+srlat2);
st := sqrt(1.0-(ct*ct));
r2 := (alt*alt)+2.0*q1+(a4-c4*srlat2)/(q*q);
r := sqrt(r2);
d := sqrt(a2*crlat2+b2*srlat2);
ca := (alt+d)/r;
sa := c2*crlat*srlat/(r*d);
end;
if (glon <> olon)then
begin
for m:=2 to maxord do
begin
sp[m] := sp[1]*cp[m-1]+cp[1]*sp[m-1];
cp[m] := cp[1]*cp[m-1]-sp[1]*sp[m-1];
end;
end;
aor := re/r;
ar := aor*aor;
br := 0.0;
bt := 0.0;
bp := 0.0;
bpp := 0.0;
for n := 1 to maxord do
begin
ar:=ar*aor;
ct := srlat/sqrt(q2*crlat2+srlat2);
st := sqrt(1.0-(ct*ct)); m:=0;
for D4 := (n+m+1) to 0 do
begin
if (alt <>oalt) or (glat <>olat) then
begin
if n = m then
begin
snorm[n+m*13]:= st*snorm[n+(m-1)*13];
dp[m][n] := st*dp[m-1][n-1]+ct*snorm[n-1+(m-1)*13];
end else if (n = 1 )and (m = 0) then
begin
snorm[n+m*13] := ct*snorm[n-1+m*13];
dp[m][n] := ct*dp[m][n-1]-st*snorm[n-1+m*13];
goto s50;
end;
if (n > 1) and (n <> m) then
begin
if m > n-2 then snorm[n-2+m*13] := 0.0;
if m > n-2 then dp[m][n-2] := 0.0;
snorm[n+m*13]:= ct*snorm[n-1+m*13]-k[m][n]*snorm[n-2+m*13];
dp[m][n] := ct*dp[m][n-1] - st*snorm[n-1+m*13]-k[m][n]*dp[m][n-2];
end;
end;
S50: //TIME ADJUST THE GAUSS COEFFICIENTS if (time <> otime)then tc[m][n]:= c[m][n]+dt*cd[m][n];
if (m <> 0) then tc[n][m-1] := c[n][m-1]+dt*cd[n][m-1]; par := ar*snorm[n+m*13];
if m = 0 then
begin
temp1 := tc[m][n]*cp[m];
temp2 := tc[m][n]*sp[m];
end else
begin
temp1 := tc[m][n]*cp[m]+tc[n][m-1]*sp[m];
temp2 := tc[m][n]*sp[m]-tc[n][m-1]*cp[m];
end;
bt := bt-ar*temp1*dp[m][n];
bp := bp+(fm[m]*temp2*par);
br :=br+(fn[n]*temp1*par); //SPECIAL CASE: NORTH/SOUTH GEOGRAPHIC POLES if (st = 0.0) and (m = 1) then
begin
if n =1 then pp[n] := pp[n-1]
else
begin
pp[n] := ct*pp[n-1]-k[m][n]*pp[n-2];
parp := ar*pp[n];
bpp:= bpp+(fm[m]*temp2*parp);
end;
end;
m:=m+1 ;
end;
end;
if st = 0.0 then bp := bpp
else bp :=bp / st; bx := -bt*ca-br*sa;
by := bp;
bz := bt*sa-br*ca; bh := sqrt((bx*bx)+(by*by));
ti^ := sqrt((bh*bh)+(bz*bz));
dec^ := (arctan2(by,bx))/dtr;
dip^ := (arctan2(bz,bh))/dtr;
oalt := alt;
olat := glat;
olon := glon;
end;
end;
function geomag(maxdeg:pinteger):double;
begin
E0000(0,@maxdeg,0.0,0.0,0.0,0.0,nil,nil,nil,nil);
end;
function geomg1( alt, glat, glon, time:double;dec,dip, ti, gv:pdouble):double;
begin
E0000(1,NiL,alt,glat,glon,time,@dec,@dip,@ti,@gv);
end;
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
fname:textfile;
maxdeg,warn_H, warn_H_strong, warn_P:integer;
warn_H_val, warn_H_strong_val:double;
alt, time, dec, dip, gv:double ;
altm,dlat, dlon,dt,epochlowlim,epochuplim,epochrange,epoch:double ;
time1, dec1, dip1, ti1:double ; x1,y1,z1,h1,ti:double ;
rTd:double;
begin
assignfile(fname,'WMM.cof');
reset(fname);
readln(fname);
maxdeg := 12;
warn_H := 0;
warn_H_val := 99999.0;
warn_H_strong := 0;
warn_H_strong_val := 99999.0;
warn_P := 0;
rTD:=0.017453292;
epochrange := 5.0;
geomag(@maxdeg);
dlat:=strtofloat(edit1.text);
dlon:=strtofloat(edit2.text);
altm:=strtofloat(edit3.text);
alt := altm;
epochuplim := epochlowlim + epochrange;
time:=strtofloat(combobox1.Text);
dt := time - epoch;
geomg1(alt,dlat,dlon,time,@dec,@dip,@ti,@gv);
time1 := time;
dec1 := dec;
dip1 := dip;
ti1 := ti;
time := time1 + 1.0;
x1:=ti1*(cos((dec1*rTd))*cos((dip1*rTd))); y1:=ti1*(cos((dip1*rTd))*sin((dec1*rTd))); z1:=ti1*(sin((dip1*rTd))); h1:=ti1*(cos((dip1*rTd))); memo1.Text:=floattostr(ti);
end;
错误提示是access violation at address 0046c88d in mordul 'project1.exe' ,write of address.
我想应该是我指针用错了,但我不知道错该怎么改?用断点找错误也找到了,
是在:ti^ := sqrt((bh*bh)+(bz*bz));
dec^ := (arctan2(by,bx))/dtr;
dip^ := (arctan2(bz,bh))/dtr;
oalt := alt;
olat := glat;
olon := glon;
end;
end;
function geomag(maxdeg:pinteger):double;
begin
E0000(0,@maxdeg,0.0,0.0,0.0,0.0,nil,nil,nil,nil);
end;
function geomg1( alt, glat, glon, time:double;dec,dip, ti, gv:pdouble):double;
begin
E0000(1,NiL,alt,glat,glon,time,@dec,@dip,@ti,@gv);
end;
请大家帮帮忙,万分感谢!!
label GEOMAG,GEOMG1,s3,s4,s50;
var
list1,list2:tstringlist;
u:array[0..6] of string;
maxord,i,icomp,n,m,j,D1,D2,D3,D4:integer;
c,cd,tc,dp,k:array[0..13,0..13] of double;
pp, fn,cp ,sp, fm:array [0..13]of double;
snorm:array [0..169]of double;
pi,dtr,a,b,re,a2,b2,c2,a4,b4,c4,epoch,gnm,hnm,dgnm,dhnm,flnmj,otime,oalt,olat,olon,dt,rlon,rlat,srlon,srlat,
crlon,crlat,srlat2, crlat2,q,q1,q2,ct,st,r2,r,d,ca,sa,aor,ar,br,bt,bp,bpp,par,temp1,temp2,parp,bx,by,bz,bh:double;
model:array [0..20] of char; c_new:array [0..5]of double;
fname:textfile;
begin
if IENTRY=0 then goto GEOMAG;
if IENTRY=0 then goto GEOMG1; GEOMAG:
begin
assignfile(fname,'WMM.COF');
reset(fname);
readln(fname); maxord := maxdeg^;
sp[0] := 0.0;
cp[0] := 1.0;
pp[0] := 1.0;
dp[0][0] := 0.0;
a := 6378.137;
b := 6356.7523142;
re := 6371.2;
a2 := a*a;
b2 := b*b;
c2 := a2-b2;
a4 := a2*a2;
b4 := b2*b2;
c4 := a4 - b4; c[0][0] := 0.0;
cd[0][0] := 0.0; list1:= tstringlist.Create;
list2:=tstringlist.Create;
list1.LoadFromFile('WMM.COF');
list2.Delimiter:=' ';
list2.DelimitedText:=list1.Text;
list1.Free;
for i:=0 to 5 do
u[i]:=list2.Strings[i];
list2.Free; S3:
if (u[i]= '') then goto S4; if (u[i] <> '') then
begin
for i:=0 to 3 do
BEGIN
c_new[i] := strtofloat(u[i]);
c_new[i+1] := strtofloat('\0');
END;
end; if c_new[i]=9999 then goto S4; n:=strtoint(u[0]);
m:=strtoint(u[1]);
gnm:=strtofloat(u[2]);
hnm:=strtofloat(u[3]);
dgnm :=strtofloat(u[4]);
dhnm:=strtofloat(u[5]); if (n > maxord)then goto S4; if (m <= n) then
begin
c[m][n] := gnm;
cd[m][n] := dgnm;
if (m <> 0) then
begin
c[n][m-1] := hnm;
cd[n][m-1] := dhnm;
end;
end; goto S3; S4:
snorm[0]:=1.0;
fm[0] := 0.0;
for n := 1 to maxord do
begin
snorm[n]:=snorm[n-1]*strtofloat(inttostr(2*n-1))/strtofloat(inttostr(n)) ;
j:=2;
m:=0;
for D2 := (n-m+1) to 0 do
begin
k[m][n]:=strtofloat(inttostr((n-1)*(n-1)-(m*n)))/strtofloat(inttostr((2*n-1)*(2*n-3)));
if m>0 then
begin
flnmj:= strtofloat(inttostr((n-m+1)*j))/strtofloat(inttostr(m+n));
snorm[n+m*13]:=snorm[n+(m-1)*13]*sqrt(flnmj);
j:=1;
c[n][m-1]:=snorm[n+m*13]*c[n][m-1];
cd[n][m-1] := snorm[n+m*13]*cd[n][m-1];
end;
end;
fn[n] := strtofloat(inttostr(n+1));
fm[n] := strtofloat(inttostr(n));
end;
k[1][1] := 0.0; otime :=-1000.0;
oalt :=-1000.0;
olat :=-1000.0;
olon := -1000.0;
closefile(fname);
//exit;
end;
GEOMG1:
begin
dt := time - epoch; pi := 3.14159265359;
dtr := pi/180.0;
rlon := glon*dtr;
rlat := glat*dtr;
srlon := sin(rlon);
srlat := sin(rlat);
crlon := cos(rlon);
crlat := cos(rlat);
srlat2 := srlat*srlat;
crlat2 := crlat*crlat;
sp[1] := srlon;
cp[1] := crlon;//* CONVERT FROM GEODETIC COORDS. TO SPHERICAL COORDS. */
if (alt <> oalt) or (glat <> olat) then
begin
q := sqrt(a2-c2*srlat2);
q1 := alt*q;
q2 := ((q1+a2)/(q1+b2))*((q1+a2)/(q1+b2));
ct := srlat/sqrt(q2*crlat2+srlat2);
st := sqrt(1.0-(ct*ct));
r2 := (alt*alt)+2.0*q1+(a4-c4*srlat2)/(q*q);
r := sqrt(r2);
d := sqrt(a2*crlat2+b2*srlat2);
ca := (alt+d)/r;
sa := c2*crlat*srlat/(r*d);
end;
if (glon <> olon)then
begin
for m:=2 to maxord do
begin
sp[m] := sp[1]*cp[m-1]+cp[1]*sp[m-1];
cp[m] := cp[1]*cp[m-1]-sp[1]*sp[m-1];
end;
end;
aor := re/r;
ar := aor*aor;
br := 0.0;
bt := 0.0;
bp := 0.0;
bpp := 0.0;
for n := 1 to maxord do
begin
ar:=ar*aor;
ct := srlat/sqrt(q2*crlat2+srlat2);
st := sqrt(1.0-(ct*ct)); m:=0;
for D4 := (n+m+1) to 0 do
begin
if (alt <>oalt) or (glat <>olat) then
begin
if n = m then
begin
snorm[n+m*13]:= st*snorm[n+(m-1)*13];
dp[m][n] := st*dp[m-1][n-1]+ct*snorm[n-1+(m-1)*13];
end else if (n = 1 )and (m = 0) then
begin
snorm[n+m*13] := ct*snorm[n-1+m*13];
dp[m][n] := ct*dp[m][n-1]-st*snorm[n-1+m*13];
goto s50;
end;
if (n > 1) and (n <> m) then
begin
if m > n-2 then snorm[n-2+m*13] := 0.0;
if m > n-2 then dp[m][n-2] := 0.0;
snorm[n+m*13]:= ct*snorm[n-1+m*13]-k[m][n]*snorm[n-2+m*13];
dp[m][n] := ct*dp[m][n-1] - st*snorm[n-1+m*13]-k[m][n]*dp[m][n-2];
end;
end;
S50: //TIME ADJUST THE GAUSS COEFFICIENTS if (time <> otime)then tc[m][n]:= c[m][n]+dt*cd[m][n];
if (m <> 0) then tc[n][m-1] := c[n][m-1]+dt*cd[n][m-1]; par := ar*snorm[n+m*13];
if m = 0 then
begin
temp1 := tc[m][n]*cp[m];
temp2 := tc[m][n]*sp[m];
end else
begin
temp1 := tc[m][n]*cp[m]+tc[n][m-1]*sp[m];
temp2 := tc[m][n]*sp[m]-tc[n][m-1]*cp[m];
end;
bt := bt-ar*temp1*dp[m][n];
bp := bp+(fm[m]*temp2*par);
br :=br+(fn[n]*temp1*par); //SPECIAL CASE: NORTH/SOUTH GEOGRAPHIC POLES if (st = 0.0) and (m = 1) then
begin
if n =1 then pp[n] := pp[n-1]
else
begin
pp[n] := ct*pp[n-1]-k[m][n]*pp[n-2];
parp := ar*pp[n];
bpp:= bpp+(fm[m]*temp2*parp);
end;
end;
m:=m+1 ;
end;
end;
if st = 0.0 then bp := bpp
else bp :=bp / st; bx := -bt*ca-br*sa;
by := bp;
bz := bt*sa-br*ca; bh := sqrt((bx*bx)+(by*by));
ti^ := sqrt((bh*bh)+(bz*bz));
dec^ := (arctan2(by,bx))/dtr;
dip^ := (arctan2(bz,bh))/dtr;
oalt := alt;
olat := glat;
olon := glon;
end;
end;
function geomag(maxdeg:pinteger):double;
begin
E0000(0,@maxdeg,0.0,0.0,0.0,0.0,nil,nil,nil,nil);
end;
function geomg1( alt, glat, glon, time:double;dec,dip, ti, gv:pdouble):double;
begin
E0000(1,NiL,alt,glat,glon,time,@dec,@dip,@ti,@gv);
end;
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
fname:textfile;
maxdeg,warn_H, warn_H_strong, warn_P:integer;
warn_H_val, warn_H_strong_val:double;
alt, time, dec, dip, gv:double ;
altm,dlat, dlon,dt,epochlowlim,epochuplim,epochrange,epoch:double ;
time1, dec1, dip1, ti1:double ; x1,y1,z1,h1,ti:double ;
rTd:double;
begin
assignfile(fname,'WMM.cof');
reset(fname);
readln(fname);
maxdeg := 12;
warn_H := 0;
warn_H_val := 99999.0;
warn_H_strong := 0;
warn_H_strong_val := 99999.0;
warn_P := 0;
rTD:=0.017453292;
epochrange := 5.0;
geomag(@maxdeg);
dlat:=strtofloat(edit1.text);
dlon:=strtofloat(edit2.text);
altm:=strtofloat(edit3.text);
alt := altm;
epochuplim := epochlowlim + epochrange;
time:=strtofloat(combobox1.Text);
dt := time - epoch;
geomg1(alt,dlat,dlon,time,@dec,@dip,@ti,@gv);
time1 := time;
dec1 := dec;
dip1 := dip;
ti1 := ti;
time := time1 + 1.0;
x1:=ti1*(cos((dec1*rTd))*cos((dip1*rTd))); y1:=ti1*(cos((dip1*rTd))*sin((dec1*rTd))); z1:=ti1*(sin((dip1*rTd))); h1:=ti1*(cos((dip1*rTd))); memo1.Text:=floattostr(ti);
end;
错误提示是access violation at address 0046c88d in mordul 'project1.exe' ,write of address.
我想应该是我指针用错了,但我不知道错该怎么改?用断点找错误也找到了,
是在:ti^ := sqrt((bh*bh)+(bz*bz));
dec^ := (arctan2(by,bx))/dtr;
dip^ := (arctan2(bz,bh))/dtr;
oalt := alt;
olat := glat;
olon := glon;
end;
end;
function geomag(maxdeg:pinteger):double;
begin
E0000(0,@maxdeg,0.0,0.0,0.0,0.0,nil,nil,nil,nil);
end;
function geomg1( alt, glat, glon, time:double;dec,dip, ti, gv:pdouble):double;
begin
E0000(1,NiL,alt,glat,glon,time,@dec,@dip,@ti,@gv);
end;
请大家帮帮忙,万分感谢!!
function E0000(IENTRY:integer; maxdeg:pinteger; alt,glat,glon, time:double;dec, dip,ti, gv:pdouble):double;
调用
function geomag(maxdeg:pinteger):double;
begin
E0000(0,@maxdeg,0.0,0.0,0.0,0.0,nil,nil,nil,nil);
end;你设置dec, dip,ti, gv四个值为nil,但在E000中你对ti有赋值操作,你对一个nil指针赋值操作必然发生错误。你的调用过程有问题。