MTP  1.0
 All Classes Files Functions Variables Macros Pages
rcf_set.cc
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <iostream>
4 #include <fstream>
5 #include <arpa/inet.h>
6 #include <boost/filesystem.hpp>
7 #include <math.h>
8 #include "rcf_set.h"
9 #include "rcf.h"
10 
11 using namespace std;
12 
14 {
15 
16  _RCFDir = Directory;
17  boost::filesystem::path RCFPath(_RCFDir);
18 
19  boost::filesystem::directory_iterator end_itr;
20 
21  for (boost::filesystem::directory_iterator itr(RCFPath); itr != end_itr;
22  ++itr)
23  {
24  // Ignore directories and files that don't have .RCF extension
25  int i = 0;
26  if (boost::filesystem::is_regular_file(itr->path()) &&
27  itr->path().extension() == ".RCF") {
28  string rcf_path = itr->path().string();
29  _RCFs.push_back(RetrievalCoefficientFile(rcf_path));
30  //cout << "Found RCF file:"<<rcf_path<<" with ID:"
31  // <<_RCFs[i].getId()<<"\n";
32  i++;
33  }
34  }
35 
36  return;
37 }
38 
40 {
41  for (std::vector<RetrievalCoefficientFile>::iterator
42  it = _RCFs.begin(); it != _RCFs.end(); ++it)
43  {
44  if (it->getId() == RCFId) return *it;
45  }
46 
47  cout<<"RetrievalCoefficientFile::getRCFbyId: ERROR: \n";
48  cout<<" Could not find RCF with ID: "<<RCFId.c_str()<<"\n";
49  return RetrievalCoefficientFile();
50 }
51 
53  int NumFlightLevels)
54 {
55  if (_RCFs.size() == 0)
56  {
57  cout<<"In: "<<__func__<<" call failed:\n";
58  cout<<"ERROR:There are currently no RCFs in the set\n";
59  return false;
60  }
61 
62  for (std::vector<RetrievalCoefficientFile>::iterator
63  it = _RCFs.begin(); it != _RCFs.end(); ++it)
64  {
65  if (!it->setFlightLevelsKm(FlightLevels, NumFlightLevels))
66  {
67  cout<<"In: "<<__func__<<" call failed:\n";
68  cout<<"ERROR: Failed to set flight levels for RCFID:"<<it->getId()<<"\n";
69  return false;
70  }
71  }
72 
73  return true;
74 }
75 
77  (std::vector<float> ScanBrightnessTemps,float PAltKm,
78  float BTBias)
79 {
80 
81  RC_Set_1FL AvgWtSet;
82  float Weight, SumWeights; // For RMS weighting
83  float SumWeightedAvg, SumSquares; // Sum of weighted differences
84  float Diff; // Measured BT - Model BT - BTBias
85  int NumBTsIncl; // Count of BTs included in Weighting
86  int BestRCIndex; // Index of "best" template (so far)
87  int BTIndex; // Index into Scan and Model BT arrays
88  int thisRCFIndex=0; // Which RCF index are we looking at?
89  float RCFBTWeightedMean; // The weighted mean of this RCF's BTs
90  float RCFBTStdDev; // Standard Deviation about weighted mean(?)
91  float Numerator, Denominator; // For RCFBTStdDev calculation
92  float BestlnP; // The sum of the ln of Probabilities for the BestRCIndex
93  float thislnP; // The sum of the ln of Probabilities for "this" RCF
94 
95 
96  // Step through the vector of Retrieval Coefficient files (aka templates)
97  // to obtain the best match for the scan at the input Altitude
98  for (std::vector<RetrievalCoefficientFile>::iterator
99  RCFit = _RCFs.begin(); RCFit != _RCFs.end(); ++RCFit)
100  {
101  SumWeightedAvg = SumSquares = SumWeights = NumBTsIncl = 0;
102 
103  // Get the weighted RC set for this template
104  AvgWtSet=RCFit->getRCAvgWt(PAltKm);
105 
106  // Compare the template Brightness Temperatures with the
107  // measured brightness temperatures to find the quality of match
108  for (BTIndex = 0; BTIndex < NUM_BRT_TEMPS; BTIndex++)
109  {
110  Weight = 1/(AvgWtSet.MBTRms[BTIndex]*AvgWtSet.MBTRms[BTIndex]);
111  SumWeights = SumWeights + Weight;
112  Diff = ScanBrightnessTemps[BTIndex]-AvgWtSet.MBTAvg[BTIndex];
113  if (Weight > 0)
114  {
115  NumBTsIncl+=1;
116  SumWeightedAvg = SumWeightedAvg+Weight*Diff;
117  SumSquares = SumSquares+Weight*(Diff*Diff);
118  }
119  }
120 
121  RCFBTWeightedMean = SumWeightedAvg/SumWeights;
122 
123  Numerator = (SumSquares-(SumWeights*(RCFBTWeightedMean*RCFBTWeightedMean)));
124  Denominator = ((NumBTsIncl-1)*SumWeights/NumBTsIncl);
125  if (Numerator/Denominator >= 0)
126  RCFBTStdDev = sqrt(Numerator/Denominator);
127  else
128  RCFBTStdDev = RCFBTWeightedMean; // MJ "kludge"
129 
130  // Calculate the Sum of the ln of probabilities (quality of match)
131  thislnP = 8 * sqrt(RCFBTWeightedMean*RCFBTWeightedMean +
132  RCFBTStdDev*RCFBTStdDev) / NUM_BRT_TEMPS;
133 
134  if (RCFit == _RCFs.begin())
135  {
136  BestlnP = thislnP;
137  BestRCIndex = 0;
138  }
139  else if (thislnP < BestlnP)
140  {
141  BestlnP = thislnP;
142  BestRCIndex = thisRCFIndex;
143  }
144  thisRCFIndex++;
145  }
146 
147  RC_Set_4Retrieval RC4R;
148  RC4R.SumLnProb = BestlnP;
149  RC4R.RCFFileName = _RCFs[BestRCIndex].getFileName();
150  RC4R.RCFId = _RCFs[BestRCIndex].getId();
151  RC4R.FL_RCs = _RCFs[BestRCIndex].getRCAvgWt(PAltKm);
152  return RC4R;
153 
154 }
155 
float MBTAvg[NUM_BRT_TEMPS]
Model Brightness Temperature Averages.
Definition: rcf_structs.h:101
This class provides for reading in an RCF, storing and providing access to its data.
RetrievalCoefficientFileSet(const std::string)
Definition: rcf_set.cc:13
#define NUM_BRT_TEMPS
Definition: mtp.h:18
RetrievalCoefficientFile getRCFbyId(std::string RCFId)
Definition: rcf_set.cc:39
This class encapsulates the whole set of RCFs for a project and provides a method for determining whi...
std::string RCFId
Definition: rcf_structs.h:114
float MBTRms[NUM_BRT_TEMPS]
1-sigma apriori Model Brightness Temp err
Definition: rcf_structs.h:100
RC_Set_1FL FL_RCs
Definition: rcf_structs.h:116
bool setFlightLevelsKm(float[], int)
Definition: rcf_set.cc:52
RC_Set_4Retrieval getBestWeightedRCSet(std::vector< float >, float, float)
Definition: rcf_set.cc:77
std::string RCFFileName
Definition: rcf_structs.h:113