00001 #include "mex.h"
00002 #include <stdio.h>
00003 #include <strings.h>
00004 #include <string.h>
00005 #include <math.h>
00006 #include "mexhead.h"
00007 #include "Doc/roi_stats_mag_docstring.h"
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 static const char *progname = "roi_stats_mag";
00085 #define PROGNAME roi_stats_mag
00086 #define SHORTNAME rsm
00087
00088 #define NARGIN_MIN 6
00089 #define NARGIN_MAX 6
00090 #define NARGOUT_MIN 0
00091 #define NARGOUT_MAX 3
00092
00093 #define ARG_X 0
00094 #define ARG_Y 1
00095 #define ARG_MAG 2
00096 #define ARG_GEOM 3
00097 #define ARG_NROI 4
00098 #define ARG_MODE 5
00099
00100 #define ARG_S 0
00101 #define ARG_NAMES 1
00102 #define ARG_COMBO 2
00103
00104 static const char *in_specs[NARGIN_MAX] = {
00105 "RM",
00106 "RM",
00107 "RM",
00108 "RV(5)",
00109 "IS",
00110 "SV"};
00111 static const char *in_names[NARGIN_MAX] = {
00112 "x",
00113 "y",
00114 "mag",
00115 "geom",
00116 "nroi",
00117 "mode"};
00118 static const char *out_names[NARGOUT_MAX] = {
00119 "s",
00120 "names",
00121 "combo"};
00122
00123 #include "roi_stats_mag_defs.h"
00124
00125
00126
00127
00128
00129
00130
00131
00132 #define ROTATE_CONST_K0 (14.6-0.9865)
00133 #define ROTATE_CONST_K1 (-2.2)
00134 #define ROTATE_CONST_K2 0.0
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 static
00150 char *
00151 extract_mode(char *mode, int *Msesw)
00152 {
00153 char *word;
00154 char *sep = ", \t";
00155
00156 if (!mode) return "null";
00157 for (word = strtok(mode, sep); word; word = strtok(NULL, sep)) {
00158
00159 if (strcasecmp(word, "sesw" ) == 0) *Msesw = 1;
00160 else if (strcasecmp(word, "sene" ) == 0) *Msesw = 0;
00161
00162 else return word;
00163 }
00164 return 0;
00165 }
00166
00167
00168 #define WithinROI(y) ((!isnan(y)) && ((y) > 0))
00169 #define ActiveLabel(y) ((!isnan(y)) && ((y) > 0))
00170
00171
00172
00173
00174 static
00175 void
00176 stat_compute(double *roi,
00177 double *ar,
00178 double *mag,
00179 double **s,
00180 int Nr,
00181 int maxx,
00182 int maxy,
00183 int strx,
00184 int stry,
00185
00186 double x0,
00187 double y0,
00188 double R,
00189 double B,
00190 double P)
00191
00192 {
00193 int x, y;
00194 double r_dbl;
00195 int r;
00196 int offset;
00197 double lat, lon;
00198 double p1x, p1y, p1z;
00199 double p2x, p2y, p2z;
00200 double temp;
00201 double area1;
00202 double tau;
00203 double mag1, magM1;
00204 const double nan = mxt_getnand();
00205 const double cosB = cos(B);
00206 const double sinB = sin(B);
00207 const double cosP = cos(P);
00208 const double sinP = sin(P);
00209 const double Rinv = 1/R;
00210
00211
00212
00213
00214
00215 bzero(&(s[0][0]), ((size_t)RS_num_stats)*Nr*sizeof(s[0][0]));
00216 for (r = 0; r < Nr; r++) {
00217
00218
00219
00220
00221
00222
00223 s[RS_rgn_min_lat][r] = s[RS_rgn_min_lon][r] = 10;
00224 s[RS_rgn_max_lat][r] = s[RS_rgn_max_lon][r] = -10;
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 }
00244
00245
00246
00247
00248 for (x = 0; x < maxx; x++)
00249 for (y = 0; y < maxy; y++) {
00250 offset = x*strx + y*stry;
00251 r_dbl = roi[offset];
00252 if (!WithinROI(r_dbl)) continue;
00253 r = (int) floor(r_dbl);
00254 if (r > Nr || r < 1) continue;
00255 r--;
00256 mag1 = mag ? mag[offset] : nan;
00257 magM1 = fabs(mag1);
00258
00259
00260
00261
00262 p2x = x - x0;
00263 p2y = y - y0;
00264 temp = (R + p2x) * (R - p2x) - p2y*p2y;
00265
00266 if (temp <= 0) continue;
00267 p2z = sqrt(temp);
00268 area1 = R/p2z;
00269 if (area1 > 1e4) area1 = 1e4;
00270
00271 p1x = cosP * p2x + sinP * p2y;
00272 p1y = -sinP * p2x + cosP * p2y;
00273 p1z = p2z;
00274
00275 p2x = p1x;
00276 p2y = cosB * p1y + sinB * p1z;
00277 p2z = -sinB * p1y + cosB * p1z;
00278
00279 lon = atan2(p2x, p2z);
00280 lat = asin(p2y * Rinv);
00281
00282
00283
00284
00285
00286
00287
00288
00289 s[RS_rgn_num ][r]++;
00290 s[RS_rgn_size][r]++;
00291 s[RS_rgn_area][r] += area1;
00292
00293 if (lat < s[RS_rgn_min_lat][r]) s[RS_rgn_min_lat][r] = lat;
00294 if (lat > s[RS_rgn_max_lat][r]) s[RS_rgn_max_lat][r] = lat;
00295 if (lon < s[RS_rgn_min_lon][r]) s[RS_rgn_min_lon][r] = lon;
00296 if (lon > s[RS_rgn_max_lon][r]) s[RS_rgn_max_lon][r] = lon;
00297
00298
00299 s[RS_rgn_btot][r] += magM1;
00300 s[RS_rgn_bnet][r] += mag1;
00301 s[RS_rgn_bpos][r] += (mag1 > 0) ? magM1 : 0.0;
00302 s[RS_rgn_bneg][r] += (mag1 < 0) ? magM1 : 0.0;
00303
00304
00305 s[RS_rgn_bsum1][r] += mag1;
00306 s[RS_rgn_bsum2][r] += mag1*mag1;
00307 s[RS_rgn_bsum3][r] += mag1*mag1*mag1;
00308 s[RS_rgn_bsum4][r] += mag1*mag1*mag1*mag1;
00309 if (ActiveLabel(ar[offset])) {
00310
00311 s[RS_ar_num ][r]++;
00312 s[RS_ar_size][r]++;
00313 s[RS_ar_area][r] += area1;
00314
00315 s[RS_ar_btot][r] += magM1;
00316 s[RS_ar_bnet][r] += mag1;
00317 s[RS_ar_bpos][r] += (mag1 > 0) ? magM1 : 0.0;
00318 s[RS_ar_bneg][r] += (mag1 < 0) ? magM1 : 0.0;
00319
00320 s[RS_ar_area_lat][r] += lat * area1;
00321 s[RS_ar_area_lon][r] += lon * area1;
00322
00323 s[RS_ar_fwt_lat][r] += lat * magM1;
00324 s[RS_ar_fwt_lon][r] += lon * magM1;
00325
00326 s[RS_ar_fwtpos_lat][r] += (mag1 > 0) ? lat*magM1 : 0.0;
00327 s[RS_ar_fwtpos_lon][r] += (mag1 > 0) ? lon*magM1 : 0.0;
00328 s[RS_ar_fwtneg_lat][r] += (mag1 < 0) ? lat*magM1 : 0.0;
00329 s[RS_ar_fwtneg_lon][r] += (mag1 < 0) ? lon*magM1 : 0.0;
00330 }
00331 }
00332
00333
00334
00335
00336
00337
00338 for (r = 0; r < Nr; r++) {
00339
00340 s[RS_ar_area_lat ][r] /= s[RS_ar_area][r];
00341 s[RS_ar_area_lon ][r] /= s[RS_ar_area][r];
00342
00343 s[RS_ar_fwt_lat ][r] /= s[RS_ar_btot][r];
00344 s[RS_ar_fwt_lon ][r] /= s[RS_ar_btot][r];
00345
00346 s[RS_ar_fwtpos_lat][r] /= s[RS_ar_bpos][r];
00347 s[RS_ar_fwtpos_lon][r] /= s[RS_ar_bpos][r];
00348
00349 s[RS_ar_fwtneg_lat][r] /= s[RS_ar_bneg][r];
00350 s[RS_ar_fwtneg_lon][r] /= s[RS_ar_bneg][r];
00351 }
00352
00353
00354
00355
00356
00357
00358
00359 for (r = 0; r < Nr; r++) {
00360 double ar_lat;
00361
00362 if (s[RS_ar_num][r] > 0)
00363
00364 ar_lat = s[RS_ar_fwt_lat][r];
00365 else
00366
00367 ar_lat = (s[RS_rgn_min_lat][r] + s[RS_rgn_max_lat][r]) * 0.5;
00368
00369 tau = (sinB/cosB) * tan(ar_lat);
00370 if (fabs(tau) >= 1) continue;
00371 tau = acos(tau);
00372
00373 temp = sin(ar_lat);
00374 temp *= temp;
00375 temp = ROTATE_CONST_K0 +
00376 temp * (ROTATE_CONST_K1 +
00377 temp * (ROTATE_CONST_K2));
00378 temp *= (M_PI/180.0);
00379
00380
00381
00382 s[RS_rgn_daysgone][r] = (tau - s[RS_rgn_min_lon][r])/temp;
00383 s[RS_rgn_daysback][r] = ((2*M_PI - tau) - s[RS_rgn_max_lon][r])/temp;
00384 }
00385
00386 for (r = 0; r < Nr; r++) {
00387 double N;
00388 double mu, sig2;
00389 double M_3, M_4;
00390
00391
00392 N = s[RS_rgn_num ][r];
00393 mu = s[RS_rgn_bsum1][r] / N;
00394 sig2 = (s[RS_rgn_bsum2][r] / N) - mu*mu;
00395 if (sig2 < 0) sig2 = 0;
00396 if (N < 2) sig2 = nan;
00397
00398
00399
00400 M_3 = (s[RS_rgn_bsum3][r]/N) - 3*mu*(s[RS_rgn_bsum2][r]/N) + 2*mu*mu*mu;
00401
00402
00403
00404 M_4 = (s[RS_rgn_bsum4][r]/N) - 4*mu*(s[RS_rgn_bsum3][r]/N) +
00405 6*mu*mu*(s[RS_rgn_bsum2][r]/N) - 3*mu*mu*mu*mu;
00406
00407 s[RS_rgn_bmean][r] = mu;
00408 s[RS_rgn_bsdev][r] = sqrt(sig2);
00409 s[RS_rgn_bskew][r] = M_3 / (sig2 * sqrt(sig2));
00410 s[RS_rgn_bkurt][r] = (M_4 / (sig2*sig2)) - 3.0;
00411 }
00412
00413 for (r = 0; r < Nr; r++) {
00414 const double rad2deg = 180.0/M_PI;
00415 const double size2uhemi = 1e6/(M_PI*R*R);
00416 const double area2uhemi = 1e6/(2*M_PI*R*R);
00417
00418
00419
00420 s[RS_rgn_size][r] *= size2uhemi;
00421 s[RS_rgn_area][r] *= area2uhemi;
00422
00423 s[RS_rgn_min_lat][r] *= rad2deg;
00424 s[RS_rgn_min_lon][r] *= rad2deg;
00425 s[RS_rgn_max_lat][r] *= rad2deg;
00426 s[RS_rgn_max_lon][r] *= rad2deg;
00427
00428
00429
00430 s[RS_ar_size][r] *= size2uhemi;
00431 s[RS_ar_area][r] *= area2uhemi;
00432
00433
00434 s[RS_ar_area_lat ][r] *= rad2deg;
00435 s[RS_ar_area_lon ][r] *= rad2deg;
00436 s[RS_ar_fwt_lat ][r] *= rad2deg;
00437 s[RS_ar_fwt_lon ][r] *= rad2deg;
00438 s[RS_ar_fwtpos_lat][r] *= rad2deg;
00439 s[RS_ar_fwtpos_lon][r] *= rad2deg;
00440 s[RS_ar_fwtneg_lat][r] *= rad2deg;
00441 s[RS_ar_fwtneg_lon][r] *= rad2deg;
00442 }
00443
00444
00445 for (r = 0; r < Nr; r++) {
00446 if (s[RS_rgn_num][r] == 0) {
00447 s[RS_rgn_min_lat ][r] = s[RS_rgn_min_lon ][r] = nan;
00448 s[RS_rgn_max_lat ][r] = s[RS_rgn_max_lon ][r] = nan;
00449 s[RS_rgn_daysgone][r] = s[RS_rgn_daysback][r] = nan;
00450 }
00451 if (s[RS_ar_num][r] == 0) {
00452 s[RS_ar_area_lat ][r] = s[RS_ar_area_lon ][r] = nan;
00453 s[RS_ar_fwt_lat ][r] = s[RS_ar_fwt_lon ][r] = nan;
00454 s[RS_ar_fwtpos_lat][r] = s[RS_ar_fwtpos_lon][r] = nan;
00455 s[RS_ar_fwtneg_lat][r] = s[RS_ar_fwtneg_lon][r] = nan;
00456 }
00457 }
00458 }
00459
00460
00461
00462
00463 static
00464 int
00465 count_classes(double *y,
00466 int N)
00467 {
00468 double Nr = 0;
00469 int Nri;
00470 int n;
00471
00472 for (n = 0; n < N; n++)
00473 if (!isnan(y[n])) {
00474 if (y[n] < 0 || floor(y[n]) != y[n])
00475 return -1;
00476 if (y[n] > Nr)
00477 Nr = y[n];
00478 }
00479 Nri = (int) Nr;
00480 if (Nri != Nr)
00481 return -1;
00482 else
00483 return Nri;
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496 #ifdef StaticP
00497 StaticP
00498 #endif
00499 void
00500 mexFunction(
00501 int nlhs,
00502 mxArray *plhs[],
00503 int nrhs,
00504 const mxArray *prhs[])
00505 {
00506 int m, n;
00507 int maxx, maxy;
00508 int strx, stry;
00509 int Nr;
00510 int Nroi;
00511 int mode_sesw;
00512 char *mode, *word;
00513 double *g;
00514 double x0, y0, r, b, p;
00515 double *mag;
00516 double **stat2;
00517 char errstr[256];
00518
00519
00520 if (nrhs < 0) {
00521 plhs[0] = mxt_PackSignature((mxt_Signature) (-nrhs),
00522 NARGIN_MIN, NARGIN_MAX,
00523 NARGOUT_MIN, NARGOUT_MAX,
00524 in_names, in_specs, out_names, docstring);
00525 return;
00526 }
00527
00528
00529
00530 if ((nrhs < NARGIN_MIN) || (nrhs > NARGIN_MAX))
00531 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00532 "%s: Expect %d <= input args <= %d",
00533 progname, NARGIN_MIN, NARGIN_MAX), errstr));
00534 if ((nlhs < NARGOUT_MIN) || (nlhs > NARGOUT_MAX))
00535 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00536 "%s: Expect %d <= output args <= %d",
00537 progname, NARGOUT_MIN, NARGOUT_MAX), errstr));
00538 mexargparse(nrhs, prhs, in_names, in_specs, NULL, progname);
00539 start_sizechecking();
00540
00541 sizeinit(prhs[ARG_Y]);
00542 sizeagree(prhs[ARG_X]);
00543 sizecheck_msg(progname, in_names, ARG_Y);
00544
00545 if (mxGetNumberOfElements(prhs[ARG_MAG]) > 0) {
00546 sizeinit(prhs[ARG_MAG]);
00547 sizeagree(prhs[ARG_X]);
00548 sizecheck_msg(progname, in_names, ARG_MAG);
00549 }
00550
00551
00552 m = (int) mxGetM(prhs[ARG_X]);
00553 n = (int) mxGetN(prhs[ARG_X]);
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563 g = mxGetPr(prhs[ARG_GEOM]);
00564 x0 = g[0] - 1;
00565 y0 = g[1] - 1;
00566 r = g[2];
00567 b = g[3]*M_PI/180.0;
00568 p = g[4]*M_PI/180.0;
00569
00570
00571
00572
00573
00574 if (mxGetNumberOfElements(prhs[ARG_NROI]))
00575 Nroi = mxGetScalar(prhs[ARG_NROI]);
00576 else
00577 Nroi = -1;
00578
00579
00580
00581
00582
00583 if ((mode = mxArrayToString(prhs[ARG_MODE])) == NULL)
00584 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00585 "%s: bad mode (non-string?). "
00586 "Could not convert mode arg to string.",
00587 progname), errstr));
00588
00589 mode_sesw = -1;
00590 if ((word = extract_mode(mode, &mode_sesw)) != NULL)
00591 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00592 "%s: bad word <%s> in mode <%.80s>",
00593 progname, word,
00594 mxArrayToString(prhs[ARG_MODE])), errstr));
00595 if (mode_sesw < 0)
00596 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00597 "%s: need sesw/sene in mode <%.80s>",
00598 progname,
00599 mxArrayToString(prhs[ARG_MODE])), errstr));
00600 mxFree(mode);
00601
00602
00603
00604
00605
00606 m = mxGetM(prhs[ARG_X]);
00607 n = mxGetN(prhs[ARG_X]);
00608
00609
00610
00611 if ((Nr = count_classes(mxGetPr(prhs[ARG_X]), m*n)) < 0)
00612 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00613 "%s: Regions x must be NaN or integers >= 0",
00614 progname), errstr));
00615
00616 if (Nroi < 0)
00617 Nroi = Nr;
00618
00619 plhs[ARG_S] = mxCreateDoubleMatrix(Nroi, (mwSize) RS_num_stats, mxREAL);
00620 if (!plhs[ARG_S])
00621 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00622 "%s: Failed to allocate space for s output",
00623 progname), errstr));
00624
00625
00626
00627
00628
00629
00630
00631 if (mode_sesw) {
00632
00633 strx = 1; stry = m;
00634 maxx = m; maxy = n;
00635 } else {
00636
00637 strx = m; stry = 1;
00638 maxx = n; maxy = m;
00639 }
00640
00641
00642 stat2 = mxt_make_matrix2(plhs[ARG_S], -1, -1, 0.0);
00643 if (!stat2)
00644 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00645 "%s: Failed to allocate space for s pointers",
00646 progname), errstr));
00647
00648 mag = (mxGetNumberOfElements(prhs[ARG_MAG]) > 0) ?
00649 mxGetPr(prhs[ARG_MAG]) : NULL;
00650
00651
00652
00653
00654 stat_compute(mxGetPr(prhs[ARG_X]),
00655 mxGetPr(prhs[ARG_Y]),
00656 mag,
00657 stat2,
00658 Nroi,
00659 maxx, maxy,
00660 strx, stry,
00661 x0, y0, r, b, p);
00662
00663 if (0) {
00664 int r, sn;
00665 double *stats = mxGetPr(plhs[ARG_S]);
00666
00667 for (sn = 0; sn < RS_num_stats; sn++)
00668 printf("\t%s", RS_index2name[sn]);
00669 printf("\n");
00670 for (r = 0; r < Nroi; r++) {
00671 printf("P%d: ", r);
00672 for (sn = 0; sn < RS_num_stats; sn++)
00673 printf("\t%.3g", stats[sn*Nroi+r]);
00674 printf("\n");
00675 }
00676 }
00677
00678 mxFree(stat2);
00679
00680
00681
00682
00683 if (nlhs > ARG_NAMES) {
00684 plhs[ARG_NAMES] = mxCreateCharMatrixFromStrings((int) RS_num_stats,
00685 RS_index2name);
00686 if (plhs[ARG_NAMES] == NULL)
00687 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00688 "%s: Failed to create `%s' output for %d names",
00689 progname, out_names[ARG_NAMES],
00690 (int) RS_num_stats),
00691 errstr));
00692 }
00693
00694
00695
00696
00697 if (nlhs > ARG_COMBO) {
00698 plhs[ARG_COMBO] = mxCreateCharMatrixFromStrings((int) RS_num_stats,
00699 RS_index2combo);
00700 if (plhs[ARG_COMBO] == NULL)
00701 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00702 "%s: Failed to create `%s' output for %d combo-specs",
00703 progname, out_names[ARG_COMBO],
00704 (int) RS_num_stats),
00705 errstr));
00706 }
00707 }
00708
00709
00710
00711 #ifdef MEX2C_TAIL_HOOK
00712 #include "mex2c_tail.h"
00713 #endif
00714