[37] | 1 | //#---------------------------------------------------------------------------
|
---|
| 2 | //# MathUtilities.cc: General math operations
|
---|
| 3 | //#---------------------------------------------------------------------------
|
---|
| 4 | //# Copyright (C) 2004
|
---|
[125] | 5 | //# ATNF
|
---|
[37] | 6 | //#
|
---|
| 7 | //# This program is free software; you can redistribute it and/or modify it
|
---|
| 8 | //# under the terms of the GNU General Public License as published by the Free
|
---|
| 9 | //# Software Foundation; either version 2 of the License, or (at your option)
|
---|
| 10 | //# any later version.
|
---|
| 11 | //#
|
---|
| 12 | //# This program is distributed in the hope that it will be useful, but
|
---|
| 13 | //# WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
---|
| 15 | //# Public License for more details.
|
---|
| 16 | //#
|
---|
| 17 | //# You should have received a copy of the GNU General Public License along
|
---|
| 18 | //# with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
| 19 | //# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
|
---|
| 20 | //#
|
---|
| 21 | //# Correspondence concerning this software should be addressed as follows:
|
---|
| 22 | //# Internet email: Malte.Marquarding@csiro.au
|
---|
| 23 | //# Postal address: Malte Marquarding,
|
---|
| 24 | //# Australia Telescope National Facility,
|
---|
| 25 | //# P.O. Box 76,
|
---|
| 26 | //# Epping, NSW, 2121,
|
---|
| 27 | //# AUSTRALIA
|
---|
| 28 | //#
|
---|
| 29 | //# $Id:
|
---|
| 30 | //#---------------------------------------------------------------------------
|
---|
| 31 |
|
---|
[125] | 32 | #include <casa/aips.h>
|
---|
[176] | 33 | #include <casa/Arrays/Vector.h>
|
---|
[137] | 34 | #include <casa/Arrays/MaskedArray.h>
|
---|
[136] | 35 | #include <casa/Arrays/MaskArrMath.h>
|
---|
| 36 | #include <casa/BasicSL/String.h>
|
---|
[37] | 37 |
|
---|
[125] | 38 | #include "MathUtils.h"
|
---|
| 39 |
|
---|
| 40 | using namespace casa;
|
---|
| 41 |
|
---|
[209] | 42 | float mathutil::statistics(const String& which,
|
---|
| 43 | const MaskedArray<Float>& data)
|
---|
[136] | 44 | {
|
---|
| 45 | String str(which);
|
---|
| 46 | str.upcase();
|
---|
| 47 | if (str.contains(String("MIN"))) {
|
---|
| 48 | return min(data);
|
---|
| 49 | } else if (str.contains(String("MAX"))) {
|
---|
| 50 | return max(data);
|
---|
| 51 | } else if (str.contains(String("SUMSQ"))) {
|
---|
| 52 | return sumsquares(data);
|
---|
| 53 | } else if (str.contains(String("SUM"))) {
|
---|
| 54 | return sum(data);
|
---|
| 55 | } else if (str.contains(String("MEAN"))) {
|
---|
| 56 | return mean(data);
|
---|
| 57 | } else if (str.contains(String("VAR"))) {
|
---|
| 58 | return variance(data);
|
---|
| 59 | } else if (str.contains(String("STDDEV"))) {
|
---|
| 60 | return stddev(data);
|
---|
| 61 | } else if (str.contains(String("AVDEV"))) {
|
---|
| 62 | return avdev(data);
|
---|
| 63 | } else if (str.contains(String("RMS"))) {
|
---|
| 64 | uInt n = data.nelementsValid();
|
---|
| 65 | return sqrt(sumsquares(data)/n);
|
---|
| 66 | } else if (str.contains(String("MED"))) {
|
---|
| 67 | return median(data);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[176] | 71 |
|
---|
[209] | 72 | void mathutil::replaceMaskByZero(Vector<Float>& data, const Vector<Bool>& mask)
|
---|
[176] | 73 | {
|
---|
| 74 | for (uInt i=0; i<data.nelements(); i++) {
|
---|
| 75 | if (!mask[i]) data[i] = 0.0;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
[382] | 78 |
|
---|
| 79 |
|
---|
| 80 | void mathutil::scanBoundaries (Vector<uInt>& startInt,
|
---|
| 81 | Vector<uInt>& endInt,
|
---|
| 82 | const Vector<Int>& scanIDs)
|
---|
| 83 | //
|
---|
| 84 | // FInd integrations start and end for each Scan
|
---|
| 85 | //
|
---|
| 86 | {
|
---|
| 87 | uInt nInt = scanIDs.nelements();
|
---|
| 88 | startInt.resize(nInt);
|
---|
| 89 | endInt.resize(nInt);
|
---|
| 90 | //
|
---|
| 91 | startInt(0) = 0;
|
---|
| 92 | uInt j = 0;
|
---|
| 93 | Int currScanID = scanIDs(0);
|
---|
| 94 | for (uInt i=0; i<nInt; i++) {
|
---|
| 95 | if (scanIDs(i) != currScanID) {
|
---|
| 96 | endInt(j) = i-1;
|
---|
| 97 | currScanID = scanIDs(i);
|
---|
| 98 | //
|
---|
| 99 | j += 1;
|
---|
| 100 | startInt(j) = i;
|
---|
| 101 | if (i==nInt-1) {
|
---|
| 102 | endInt(j) = i;
|
---|
| 103 | }
|
---|
| 104 | } else {
|
---|
| 105 | if (i==nInt-1) endInt(j) = i;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | startInt.resize(j+1,True);
|
---|
| 109 | endInt.resize(j+1,True);
|
---|
| 110 | }
|
---|