[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> |
---|
[465] | 36 | #include <casa/Arrays/VectorSTLIterator.h> |
---|
[136] | 37 | #include <casa/BasicSL/String.h> |
---|
[37] | 38 | |
---|
[125] | 39 | #include "MathUtils.h" |
---|
| 40 | |
---|
| 41 | using namespace casa; |
---|
| 42 | |
---|
[209] | 43 | float mathutil::statistics(const String& which, |
---|
| 44 | const MaskedArray<Float>& data) |
---|
[136] | 45 | { |
---|
| 46 | String str(which); |
---|
| 47 | str.upcase(); |
---|
| 48 | if (str.contains(String("MIN"))) { |
---|
| 49 | return min(data); |
---|
| 50 | } else if (str.contains(String("MAX"))) { |
---|
| 51 | return max(data); |
---|
| 52 | } else if (str.contains(String("SUMSQ"))) { |
---|
| 53 | return sumsquares(data); |
---|
| 54 | } else if (str.contains(String("SUM"))) { |
---|
| 55 | return sum(data); |
---|
| 56 | } else if (str.contains(String("MEAN"))) { |
---|
| 57 | return mean(data); |
---|
| 58 | } else if (str.contains(String("VAR"))) { |
---|
| 59 | return variance(data); |
---|
| 60 | } else if (str.contains(String("STDDEV"))) { |
---|
| 61 | return stddev(data); |
---|
| 62 | } else if (str.contains(String("AVDEV"))) { |
---|
| 63 | return avdev(data); |
---|
| 64 | } else if (str.contains(String("RMS"))) { |
---|
| 65 | uInt n = data.nelementsValid(); |
---|
| 66 | return sqrt(sumsquares(data)/n); |
---|
| 67 | } else if (str.contains(String("MED"))) { |
---|
| 68 | return median(data); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
[176] | 72 | |
---|
[209] | 73 | void mathutil::replaceMaskByZero(Vector<Float>& data, const Vector<Bool>& mask) |
---|
[176] | 74 | { |
---|
| 75 | for (uInt i=0; i<data.nelements(); i++) { |
---|
| 76 | if (!mask[i]) data[i] = 0.0; |
---|
| 77 | } |
---|
| 78 | } |
---|
[382] | 79 | |
---|
| 80 | |
---|
[452] | 81 | void mathutil::scanBoundaries(Vector<uInt>& startInt, |
---|
| 82 | Vector<uInt>& endInt, |
---|
| 83 | const Vector<Int>& scanIDs) |
---|
| 84 | // Find integrations start and end for each Scan |
---|
[382] | 85 | { |
---|
[452] | 86 | uInt nInt = scanIDs.nelements(); |
---|
| 87 | startInt.resize(nInt); |
---|
| 88 | endInt.resize(nInt); |
---|
| 89 | |
---|
| 90 | startInt(0) = 0; |
---|
| 91 | uInt j = 0; |
---|
| 92 | Int currScanID = scanIDs(0); |
---|
| 93 | for (uInt i=0; i<nInt; i++) { |
---|
| 94 | if (scanIDs(i) != currScanID) { |
---|
| 95 | endInt(j) = i-1; |
---|
| 96 | currScanID = scanIDs(i); |
---|
| 97 | |
---|
| 98 | j += 1; |
---|
| 99 | startInt(j) = i; |
---|
| 100 | if (i==nInt-1) { |
---|
| 101 | endInt(j) = i; |
---|
| 102 | } |
---|
| 103 | } else { |
---|
| 104 | if (i==nInt-1) endInt(j) = i; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | startInt.resize(j+1,True); |
---|
| 108 | endInt.resize(j+1,True); |
---|
[382] | 109 | } |
---|
[465] | 110 | |
---|
| 111 | std::vector<std::string> mathutil::tovectorstring(const Vector<String>& in) |
---|
| 112 | { |
---|
| 113 | std::vector<std::string> out; |
---|
| 114 | VectorSTLIterator<String> it(in); |
---|
| 115 | for (uInt i=0; it != in.end(); ++it,++i) { |
---|
| 116 | out.push_back(*it); |
---|
| 117 | } |
---|
| 118 | return out; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | Vector<String> mathutil::toVectorString(const std::vector<std::string>& in) |
---|
| 122 | { |
---|
| 123 | Vector<String> out(in.size()); |
---|
| 124 | uInt i=0; |
---|
| 125 | std::vector<std::string>::const_iterator it; |
---|
| 126 | for (it=in.begin();it != in.end();++it,++i) { |
---|
| 127 | out[i] = casa::String(*it); |
---|
| 128 | } |
---|
| 129 | return out; |
---|
| 130 | } |
---|