-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathigrft04.c
324 lines (308 loc) · 9.5 KB
/
igrft04.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "mex.h"
#include "matrix.h"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// COMMON BLOCKS in geopack_08
extern struct {
double cb[34];
}GEOPACK1;
extern struct {
double cb[315];
}GEOPACK2;
// SUBROUTINE T04_S (IOPT,PARMOD,PS,X,Y,Z,BX,BY,BZ)
extern void T04_S(int*,double*,double*,double*,double*,double*,double*,double*,double*);
// SUBROUTINE RECALC_08 (IYEAR,IDAY,IHOUR,MIN,ISEC,VGSEX,VGSEY,VGSEZ)
extern void RECALC_08(int*,int*,int*,int*,int*,double*,double*,double*);
// SUBROUTINE IGRF_GSW_08 (XGSW,YGSW,ZGSW,HXGSW,HYGSW,HZGSW)
extern void IGRF_GSW_08(double*,double*,double*,double*,double*,double*);
// SUBROUTINE SMGSW_08 (XSM,YSM,ZSM,XGSW,YGSW,ZGSW,J)
extern void SMGSW_08(double*,double*,double*,double*,double*,double*,int*);
extern void MAGSM_08(double*,double*,double*,double*,double*,double*,int*);
extern void GEIGEO_08(double*,double*,double*,double*,double*,double*,int*);
extern void GEOMAG_08(double*,double*,double*,double*,double*,double*,int*);
extern void GSWGSE_08(double*,double*,double*,double*,double*,double*,int*);
extern void GEOGSW_08(double*,double*,double*,double*,double*,double*,int*);
void mexT04(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]){
// declaration
int iopt = 0;
size_t i;
mxArray *oBX,*oBY,*oBZ;
mxDouble *parmod,*ps,*x,*y,*z,*bx,*by,*bz;
// check nlhs/nrhs
if(nrhs!=5) {
mexErrMsgIdAndTxt("mex:igrft04","T04: 5 inputs required: PARMOD,PS,X,Y,Z");
}
if(nlhs>3) {
mexErrMsgIdAndTxt("mex:igrft04","T04: too many outputs. Max 3.");
}
// check parmod
if(mxGetNumberOfElements(prhs[0]) != 10){
mexErrMsgIdAndTxt("mex:igrft04","T04: len(Parmod) != 10");
}
// check XYZ dimensions
mwSize dim_in = mxGetNumberOfDimensions(prhs[2]);
if(dim_in != mxGetNumberOfDimensions(prhs[3]) || dim_in != mxGetNumberOfDimensions(prhs[4])){
mexErrMsgIdAndTxt("mex:igrft04","T04: X,Y,Z dimension dismatch");
}
const mwSize *sx = mxGetDimensions(prhs[2]);
const mwSize *sy = mxGetDimensions(prhs[3]);
const mwSize *sz = mxGetDimensions(prhs[4]);
const size_t totnum = mxGetNumberOfElements(prhs[2]);
for(i=0;i<dim_in;i++){
if(sx[i]!=sy[i] || sx[i]!=sz[i]){
mexErrMsgIdAndTxt("mex:igrft04","T04: X,Y,Z dimension dismatch");
}
}
// init BXBYBZ, to be assigned to plhs later
oBX = mxDuplicateArray(prhs[2]);
oBY = mxDuplicateArray(prhs[2]);
oBZ = mxDuplicateArray(prhs[2]);
// link in-data* to mxarray*
parmod = mxGetDoubles(prhs[0]);
ps = mxGetDoubles(prhs[1]);
x = mxGetDoubles(prhs[2]);
y = mxGetDoubles(prhs[3]);
z = mxGetDoubles(prhs[4]);
bx = mxGetDoubles(oBX);
by = mxGetDoubles(oBY);
bz = mxGetDoubles(oBZ);
// CALCULATION
for(i=0;i<totnum;i++){
T04_S(&iopt, parmod, ps, x+i, y+i, z+i, bx+i, by+i, bz+i);
}
// return process
switch(nlhs){
case 2:
plhs[0] = oBX;
plhs[1] = oBY;
mxDestroyArray(oBZ);
break;
case 3:
plhs[0] = oBX;
plhs[1] = oBY;
plhs[2] = oBZ;
break;
default: // 1 & 0
plhs[0] = oBX;
mxDestroyArray(oBY);
mxDestroyArray(oBZ);
}
}
void mexRecalc(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]){
if(nrhs!=8){
mexErrMsgIdAndTxt("mex:igrft04","Recalc: Too few inputs => 8");
}
if(nlhs!=0){
mexErrMsgIdAndTxt("mex:igrft04","Recalc: Too much outputs => 0");
}
int yy,dd,hh,mm,ss;
yy = mat2int(prhs[0]);
dd = mat2int(prhs[1]);
hh = mat2int(prhs[2]);
mm = mat2int(prhs[3]);
ss = mat2int(prhs[4]);
double *vx,*vy,*vz,ansout=0.;
vx = mxGetDoubles(prhs[5]);
vy = mxGetDoubles(prhs[6]);
vz = mxGetDoubles(prhs[7]);
RECALC_08(&yy,&dd,&hh,&mm,&ss,vx,vy,vz);
plhs[0] = mxCreateDoubleScalar(ansout);
}
void mexIGRFGSW(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]){
int i;
if(nrhs!=3){
mexErrMsgIdAndTxt("mex:igrft04","Recalc: Too few inputs => 3");
}
if(nlhs>3){
mexErrMsgIdAndTxt("mex:igrft04","Recalc: num of outputs => 1-3");
}
// check XYZ dimensions
mwSize dim_in = mxGetNumberOfDimensions(prhs[0]);
const mwSize *sz = mxGetDimensions(prhs[0]);
const size_t totnum = mxGetNumberOfElements(prhs[0]);
if(!matchecksize(2,dim_in,sz,prhs[1],prhs[2])){
mexErrMsgIdAndTxt("mex:igrft04","Recalc: X,Y,Z: dimension mismatch");
}
mxArray *oBX,*oBY,*oBZ;
oBX = mxDuplicateArray(prhs[0]);
oBY = mxDuplicateArray(prhs[0]);
oBZ = mxDuplicateArray(prhs[0]);
// get data
double *x,*y,*z,*bx,*by,*bz;
x = mxGetDoubles(prhs[0]);
y = mxGetDoubles(prhs[1]);
z = mxGetDoubles(prhs[2]);
bx = mxGetDoubles(oBX);
by = mxGetDoubles(oBY);
bz = mxGetDoubles(oBZ);
//CALCULATION
for(i=0;i<totnum;i++){
IGRF_GSW_08(x+i, y+i, z+i, bx+i, by+i, bz+i);
}
//arrange output
switch(nlhs){
case 2:
plhs[0] = oBX;
plhs[1] = oBY;
mxDestroyArray(oBZ);
break;
case 3:
plhs[0] = oBX;
plhs[1] = oBY;
plhs[2] = oBZ;
break;
default: // 1 & 0
plhs[0] = oBX;
mxDestroyArray(oBY);
mxDestroyArray(oBZ);
}
}
void mex_cvtfun(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[],
void(*cvtfun)(double*,double*,double*,double*,double*,double*,int*)){
int i;
int direction=1;
if(nrhs==4){
direction = mat2int(prhs[3]);
}else if(nrhs!=3){
mexErrMsgIdAndTxt("mex:igrft04","cvtfun: Too few inputs => 3,4");
}
if(nlhs>3){
mexErrMsgIdAndTxt("mex:igrft04","cvtfun: num of outputs => 1-3");
}
// check XYZ dimensions
mwSize dim_in = mxGetNumberOfDimensions(prhs[0]);
const mwSize *sz = mxGetDimensions(prhs[0]);
const size_t totnum = mxGetNumberOfElements(prhs[0]);
if(!matchecksize(2,dim_in,sz,prhs[1],prhs[2])){
mexErrMsgIdAndTxt("mex:igrft04","SMGSW: X,Y,Z: dimension mismatch");
}
// get data
double *x1,*x2,*y1,*y2,*z1,*z2;
x1 = mxGetDoubles(prhs[0]);
y1 = mxGetDoubles(prhs[1]);
z1 = mxGetDoubles(prhs[2]);
mxArray *oBX,*oBY,*oBZ;
oBX = mxDuplicateArray(prhs[0]);
oBY = mxDuplicateArray(prhs[0]);
oBZ = mxDuplicateArray(prhs[0]);
x2 = mxGetDoubles(oBX);
y2 = mxGetDoubles(oBY);
z2 = mxGetDoubles(oBZ);
//CALCULATION
if(direction > 0){
for(i=0;i<totnum;i++){
cvtfun(x1+i, y1+i, z1+i, x2+i, y2+i, z2+i, &direction);
}
}else{
for(i=0;i<totnum;i++){
cvtfun(x2+i, y2+i, z2+i, x1+i, y1+i, z1+i, &direction);
}
}
//arrange output
switch(nlhs){
case 2:
plhs[0] = oBX;
plhs[1] = oBY;
mxDestroyArray(oBZ);
break;
case 3:
plhs[0] = oBX;
plhs[1] = oBY;
plhs[2] = oBZ;
break;
default: // 1 & 0
plhs[0] = oBX;
mxDestroyArray(oBY);
mxDestroyArray(oBZ);
}
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]){
// first input is mode number
if(nrhs<2){
mexErrMsgIdAndTxt("mex:igrft04","mexMain: few params");
}
const int mode = mat2int(prhs[0]);
// execution
switch(mode){
case 1:
mexT04(nlhs,plhs,nrhs-1,prhs+1);
break;
case 2:
mexRecalc(nlhs,plhs,nrhs-1,prhs+1);
break;
case 3:
mexIGRFGSW(nlhs,plhs,nrhs-1,prhs+1);
break;
case 4:
mex_cvtfun(nlhs,plhs,nrhs-1,prhs+1,SMGSW_08);
break;
case 5:
mex_cvtfun(nlhs,plhs,nrhs-1,prhs+1,MAGSM_08);
break;
case 6:
mex_cvtfun(nlhs,plhs,nrhs-1,prhs+1,GEIGEO_08);
break;
case 7:
mex_cvtfun(nlhs,plhs,nrhs-1,prhs+1,GEOMAG_08);
break;
case 8:
mex_cvtfun(nlhs,plhs,nrhs-1,prhs+1,GSWGSE_08);
break;
case 9:
mex_cvtfun(nlhs,plhs,nrhs-1,prhs+1,GEOGSW_08);
break;
default:
mexErrMsgIdAndTxt("mex:igrft04","mexMain: wrong mode");
}
}
int mat2int(const mxArray *mat){
// convenient get int args from double/int64 mxArray
void *modeptr;
int mode;
if(mxIsInt64(mat)){
modeptr = mxGetInt64s(mat);
mode = (int)*(mxInt64*)modeptr;
}else if(mxIsDouble(mat)){
modeptr = mxGetDoubles(mat);
mode = (int)*(mxDouble*)modeptr;
}else if(mxIsSingle(mat)){
modeptr = mxGetSingles(mat);
mode = (int)*(mxSingle*)modeptr;
}
else{
mexErrMsgIdAndTxt("mex:igrft04","mat2int: cannot convert to int");
}
return mode;
}
int matchecksize(int narray, mwSize ndim, mwSize *sz, ...){
// fast routine to check whether multiple matrices have same size
va_list args;
va_start(args, sz);
const mxArray *mat;
int i=0,j=0;
mwSize ndim_,*sz_;
for(i=0;i<narray;i++){
mat = va_arg(args, mxArray*);
ndim_ = mxGetNumberOfDimensions(mat);
if(ndim != ndim_){
va_end(args);
return 0;
}
sz_ = mxGetDimensions(mat);
for(j=0;j<ndim;j++){
if(sz[j]!=sz_[j]){
va_end(args);
return 0;
}
}
}
va_end(args);
return 1;
}