| 3453 | } |
| 3454 | |
| 3455 | GMT_LOCAL int gmtmath_MAD (struct GMT_CTRL *GMT, struct GMTMATH_INFO *info, struct GMTMATH_STACK *S[], unsigned int last, unsigned int col) |
| 3456 | /*OPERATOR: MAD 1 1 Median Absolute Deviation (L1 STD) of A. */ |
| 3457 | { |
| 3458 | uint64_t s, row, k; |
| 3459 | double mad, med, *z = NULL; |
| 3460 | struct GMT_DATATABLE *T = S[last]->D->table[0]; |
| 3461 | |
| 3462 | if (S[last]->constant) { /* Trivial case */ |
| 3463 | for (s = 0; s < info->T->n_segments; s++) gmt_M_memset (T->segment[s]->data[col], info->T->segment[s]->n_rows, double); |
| 3464 | return 0; |
| 3465 | } |
| 3466 | |
| 3467 | if (!info->local) z = gmt_M_memory (GMT, NULL, info->T->n_records, double); |
| 3468 | |
| 3469 | for (s = k = 0; s < info->T->n_segments; s++) { |
| 3470 | if (info->local) { |
| 3471 | gmt_sort_array (GMT, T->segment[s]->data[col], info->T->segment[s]->n_rows, GMT_DOUBLE); |
| 3472 | for (row = info->T->segment[s]->n_rows; row > 1 && gmt_M_is_dnan (T->segment[s]->data[col][row-1]); row--); |
| 3473 | if (row) { |
| 3474 | med = (row%2) ? T->segment[s]->data[col][row/2] : 0.5 * (T->segment[s]->data[col][(row-1)/2] + T->segment[s]->data[col][row/2]); |
| 3475 | gmt_getmad (GMT, T->segment[s]->data[col], row, med, &mad); |
| 3476 | } |
| 3477 | else |
| 3478 | mad = GMT->session.d_NaN; |
| 3479 | for (row = 0; row < info->T->segment[s]->n_rows; row++) T->segment[s]->data[col][row] = mad; |
| 3480 | } |
| 3481 | else { /* Just accumulate the total table */ |
| 3482 | gmt_M_memcpy (&z[k], T->segment[s]->data[col], info->T->segment[s]->n_rows, double); |
| 3483 | k += info->T->segment[s]->n_rows; |
| 3484 | } |
| 3485 | } |
| 3486 | if (info->local) return 0; /* Done with local */ |
| 3487 | gmt_sort_array (GMT, z, info->T->n_records, GMT_DOUBLE); |
| 3488 | for (row = info->T->n_records; row > 1 && gmt_M_is_dnan (z[row-1]); row--); |
| 3489 | if (row) { |
| 3490 | med = (row%2) ? z[row/2] : 0.5 * (z[(row-1)/2] + z[row/2]); |
| 3491 | gmt_getmad (GMT, z, row, med, &mad); |
| 3492 | } |
| 3493 | else |
| 3494 | mad = GMT->session.d_NaN; |
| 3495 | for (s = 0; s < info->T->n_segments; s++) for (row = 0; row < info->T->segment[s]->n_rows; row++) T->segment[s]->data[col][row] = mad; |
| 3496 | gmt_M_free (GMT, z); |
| 3497 | return 0; |
| 3498 | } |
| 3499 | |
| 3500 | GMT_LOCAL int gmtmath_MADW (struct GMT_CTRL *GMT, struct GMTMATH_INFO *info, struct GMTMATH_STACK *S[], unsigned int last, unsigned int col) { |
| 3501 | /*OPERATOR: MADW 2 1 Weighted Median Absolute Deviation (L1 STD) of A for weights in B. */ |
nothing calls this directly
no test coverage detected