MTP  1.0
 All Classes Files Functions Variables Macros Pages
rcf.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 "rcf.h"
7 
8 using namespace std;
9 
11 {
12 
13  _RCFFileName = Filename;
14 
15  My_Rcf_Hdr_Un RcfHdr;
16  My_RC_FL_Un RcFlUn;
17  RC_Set_1FL FlRcSet;
18 
19  string temp = _RCFFileName;
20  size_t last_slash = temp.find_last_of("/");
21  size_t last_dot = temp.find_last_of(".");
22  _RCFId = temp.substr(last_slash+1, last_dot-last_slash-1);
23 
24  streampos size;
25 
26  ifstream file (_RCFFileName.c_str(), ios::in|ios::binary);
27  if (file.is_open())
28  {
29  //size = file.tellg();
30  size = sizeof(RCF_HDR);
31  file.seekg (0, ios::beg);
32  file.read (RcfHdr.Array, size);
33 
34  if (!file)
35  {
36  cout << "In: RetrievalCoefficientFile Constructor:\n";
37  cout << "ERROR!: Reading Header only " <<file.gcount()
38  <<" bytes were read.\n";
39  cout << "From RCF file:"<<_RCFFileName.c_str()<<"\n\n";
40  }
41 
42  file.seekg ((-2*sizeof(float)),ios::cur);
43 
44  for (int i=0; i<RcfHdr.Rcf_Hdr.NFL; i++) {
45  file.read (RcFlUn.Array, sizeof(RC_FL_Read));
46  if (!file)
47  {
48  cout << "In: RetrievalCoefficientFile Constructor:\n";
49  cout << " ERROR!: only " <<file.gcount()<<" bytes were read.\n";
50  cout << " From Flight Level #:" << i << "\n";
51  cout << " From RCF file:"<<_RCFFileName.c_str()<<"\n\n";
52  }
53  FlUn2FlRcSet(RcFlUn, &FlRcSet);
54  flightLevelRCInfoVec.push_back(RcFlUn);
55  _FlRcSetVec.push_back(FlRcSet);
56  if (i+1 == RcfHdr.Rcf_Hdr.NFL) _FlRcSetVec.push_back(FlRcSet);
57  }
58  //
59  _FlRcSetVec.pop_back();
60 
61 
62  file.close();
63 
64  // Fill first part of our header
65  // NOTE: for some damn reason the header read screws up right
66  // after NFL ... still don't know why, but have workaround
67  _RCFHdr.RCformat = RcfHdr.Rcf_Hdr.RCformat;
68  strncpy(_RCFHdr.CreationDateTime,RcfHdr.Rcf_Hdr.CreationDateTime,8);
69  strncpy(_RCFHdr.RAOBfilename, RcfHdr.Rcf_Hdr.RAOBfilename, 80);
70  strncpy(_RCFHdr.RCfilename, RcfHdr.Rcf_Hdr.RCfilename, 80);
71  _RCFHdr.RAOBcount=RcfHdr.Rcf_Hdr.RAOBcount;
72  _RCFHdr.LR1=RcfHdr.Rcf_Hdr.LR1;
73  _RCFHdr.zLRb=RcfHdr.Rcf_Hdr.zLRb;
74  _RCFHdr.LR2=RcfHdr.Rcf_Hdr.LR2;
75  _RCFHdr.RecordStep=RcfHdr.Rcf_Hdr.RecordStep;
76  _RCFHdr.RAOBmin=RcfHdr.Rcf_Hdr.RAOBmin;
77  _RCFHdr.ExcessTamplitude=RcfHdr.Rcf_Hdr.ExcessTamplitude;
78  _RCFHdr.Nobs=RcfHdr.Rcf_Hdr.Nobs;
79  _RCFHdr.Nret=RcfHdr.Rcf_Hdr.Nret;
80  for (int i=0; i<NUM_RETR_LVLS; i++)
81  _RCFHdr.dZ[i]=RcfHdr.Rcf_Hdr.dZ[i];
82  _RCFHdr.NFL=RcfHdr.Rcf_Hdr.NFL;
83 
84  // Resynch header decoding at SURC
85  My_EH EH;
86  int i = 0;
87  for (int j=1376; j<sizeof(END_HDR); j++,i++) EH.Array[i]=RcfHdr.Array[j];
88 
89  // Finish filling our header record with what we can get.
90  strncpy(_RCFHdr.SURC, EH.EH.SURC, 4);
91  _RCFHdr.RAOBbias=EH.EH.RAOBbias;
92  _RCFHdr.CH1LSBloss=EH.EH.CH1LSBloss;
93  //Convert from column major storage for SmatrixN1
94  int x=0,y=0,z=0;
95  for (int i=0; i<450;i++) {
96  _RCFHdr.SmatrixN1[x][y][z] = EH.EH.Gmatrix[i];
97  //cout << x << " " << y << " " << z << '\n';
98  x++;
99  if (x > 14) {
100  x=0;
101  y++;
102  if (y>2) {
103  y=0;
104  z++;
105  }
106  }
107  }
108 
109 /* - maybe at some point these debug statements will help resolve
110  * The decoding problems of the RCF header.
111  for (int i=0; i<10;i++) {
112  cout << "SMATRIXN1:"<<_RCFHdr.SmatrixN1[14][2][i]<<'\n';
113  }
114  cout << "SmatrixN1[5][1][5]:"<<_RCFHdr.SmatrixN1[5][1][5]<<'\n';
115  for (int i = 450; i<460;i++) {cout<<" GM["<<i<<"]:"<<EH.EH.Gmatrix[i];}
116  cout << "\n";
117 */
118 
119  //Convert from column major storage for SmatrixN2
120  // NOTE: about 1/2 way through this, we loose data and it all turns to
121  // zeros... why? Beats the heck out of me! Gratefully I found where
122  // the flight level data starts and resynch to it above.
123  x=0,y=0,z=0;
124  for (int i=450; i<900;i++) {
125  _RCFHdr.SmatrixN2[x][y][z] = EH.EH.Gmatrix[i];
126  x++;
127  if (x > 14) {
128  x=0;
129  y++;
130  if (y>2) {
131  y=0;
132  z++;
133  }
134  }
135  }
136 
137 /* - maybe at some point these debug statements will help resolve
138  * The decoding problems of the RCF header.
139  for (int i=0; i<10;i++) {
140  cout << "SMATRIXN2:"<<_RCFHdr.SmatrixN2[0][0][i]<<'\n';
141  }
142  cout << "SmatrixN2[5][1][5]:"<<_RCFHdr.SmatrixN2[5][1][5]<<'\n';
143 */
144 
145  }
146  else cout << "Unable to open file";
147  return;
148 }
149 
150 /*
151  * Null Constructor
152  */
154 {
155  _RCFFileName = std::string();
156  _RCFId = std::string();
157 }
158 
160 {
161  if (_RCFFileName.size() == 0) return false;
162  return true;
163 }
164 
166 {
167  RC_Set_1FL RcSetAvWt;
168 
169  // First check to see if PAltKm is outside the range of Flight Level PAltKms
170  // - if so then the weighted average observable will be the average
171  // observable associated with the flight level whose PAltKm is closest.
172  // Assumption is that the Flight Level Retrieval Coefficient Set vector
173  // is stored in increasing Palt (decreasing aircraft altitude).
174 std::vector<int>::size_type sz = _FlRcSetVec.size();
175  if (PAltKm >= _RCFHdr.Zr[0])
176  {
177  for (int i = 0; i< NUM_BRT_TEMPS; i++)
178  {
179  RcSetAvWt.MBTAvg[i] = _FlRcSetVec.begin()->MBTAvg[i];
180  RcSetAvWt.MBTRms[i] = _FlRcSetVec.begin()->MBTRms[i];
181  for (int j = 0; j < NUM_RETR_LVLS; j++)
182  {
183  RcSetAvWt.RC[j][i] = _FlRcSetVec.begin()->RC[j][i];
184  }
185  }
186  return RcSetAvWt;
187  }
188  if (PAltKm <= _RCFHdr.Zr[_RCFHdr.NFL-1])
189  {
190  for (int i = 0; i< NUM_BRT_TEMPS; i++)
191  {
192  RcSetAvWt.MBTAvg[i] = _FlRcSetVec.end()->MBTAvg[i];
193  RcSetAvWt.MBTRms[i] = _FlRcSetVec.end()->MBTRms[i];
194  for (int j = 0; j < NUM_RETR_LVLS; j++)
195  {
196  RcSetAvWt.RC[j][i] = _FlRcSetVec.end()->RC[j][i];
197  }
198  }
199  return RcSetAvWt;
200  }
201 
202  // Find two Flight Level Sets that are above and below the PAltKm provided.
203  // Calculate the weight for averaging and identify the RC sets.
204  vector<RC_Set_1FL>::const_iterator it, Botit, Topit;
205  float BotWt,TopWt;
206  int i = 0;
207  for(it=_FlRcSetVec.begin(); it!=_FlRcSetVec.end(); it+=1)
208  {
209  if (PAltKm <= _RCFHdr.Zr[i] and PAltKm >= _RCFHdr.Zr[i+1])
210  {
211  BotWt = 1-((PAltKm-_RCFHdr.Zr[i+1])/(_RCFHdr.Zr[i]-_RCFHdr.Zr[i+1]));
212  Topit = it;
213  Botit = it+1;
214  }
215  i++;
216  }
217  TopWt = 1-BotWt;
218 
219 
220  // Calculate the Weighted averages
221  RcSetAvWt.Palt = Botit->Palt*BotWt + Topit->Palt*TopWt;
222  for (int i = 0; i < NUM_BRT_TEMPS; i++)
223  {
224  RcSetAvWt.MBTRms[i] = Botit->MBTRms[i]*BotWt + Topit->MBTRms[i]*TopWt;
225  RcSetAvWt.MBTAvg[i] = Botit->MBTAvg[i]*BotWt + Topit->MBTAvg[i]*TopWt;
226  for (int j = 0; j < NUM_RETR_LVLS; j++)
227  {
228  RcSetAvWt.PAltRl[j] = Botit->PAltRl[j]*BotWt + Topit->PAltRl[j]*TopWt;
229  RcSetAvWt.TAvgRl[j] = Botit->TAvgRl[j]*BotWt + Topit->TAvgRl[j]*TopWt;
230  RcSetAvWt.TVarRl[j] = Botit->TVarRl[j]*BotWt + Topit->TVarRl[j]*TopWt;
231  RcSetAvWt.TRmsRl[j] = Botit->TRmsRl[j]*BotWt + Topit->TRmsRl[j]*TopWt;
232 
233  RcSetAvWt.RC[j][i] = Botit->RC[j][i]*BotWt + Topit->RC[j][i]*(1-BotWt);
234  }
235  }
236 
237 
238  return RcSetAvWt;
239 
240 }
241 
242 bool RetrievalCoefficientFile::setFlightLevelsKm(float FlightLevels[], int Len)
243 {
244  if (Len != _RCFHdr.NFL)
245  {
246  cout<<"In "<< __func__ << " for RCFID: " << getId() << "\n";
247  cout<<"ERROR:Number of flight levels input:";
248  cout<< Len;
249  cout<< " is not equal to number in RCF:";
250  cout<< _RCFHdr.NFL;
251  cout<<"\n";
252  return false;
253  }
254  for (int i = 0; i<Len; i++)
255  {
256  if ((i+1<Len) && (FlightLevels[i] <= FlightLevels[i+1]))
257  {
258  cout<<"In "<< __func__ << " for RCFID: " << getId() << "\n";
259  cout<<"ERROR: Flight Levels are not in decreasing altitude\n";
260  return false;
261  }
262  _RCFHdr.Zr[i] = FlightLevels[i];
263  }
264 
265  return true;
266 }
267 
268 void RetrievalCoefficientFile::FlUn2FlRcSet(My_RC_FL_Un RcUn, RC_Set_1FL *RcSet)
269 {
270  RcSet->Palt = RcUn.RC_read.sBP;
271  for (int i=0; i<NUM_BRT_TEMPS; i++)
272  {
273  RcSet->MBTRms[i] = RcUn.RC_read.sOBrms[i];
274  RcSet->MBTAvg[i] = RcUn.RC_read.sOBav[i];
275  }
276  for (int i=0; i<NUM_RETR_LVLS; i++)
277  {
278  RcSet->PAltRl[i] = RcUn.RC_read.sBPrl[i];
279  RcSet->TAvgRl[i] = RcUn.RC_read.sRTav[i];
280  RcSet->TVarRl[i] = RcUn.RC_read.sRMSa[i];
281  RcSet->TRmsRl[i] = RcUn.RC_read.sRMSe[i];
282  }
283  // Convert Retrieval coefficients from column major storage
284  int x=0,y=0;
285  for (int i=0; i<990; i++) {
286  RcSet->RC[x][y] = RcUn.RC_read.Src[i];
287  x++;
288  if (x>=NUM_RETR_LVLS) { x=0; y++; }
289  }
290 
291 /* Output the RC matrix
292 for (x=0; x<NUM_RETR_LVLS; x++) {
293  for (y=0; y<NUM_BRT_TEMPS; y++) {
294  if (y%4==0) cout << " ["<<x<<"]["<<y<<"]:"<<RcSet->RC[x][y]<<"\n";
295  else cout << "["<<x<<"]["<<y<<"]:"<<RcSet->RC[x][y];
296  }
297 }
298 cout<<'\n';
299 */
300 
301 }
char Array[sizeof(END_HDR)]
Definition: rcf_structs.h:72
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.
float TAvgRl[NUM_RETR_LVLS]
Average T at retrieval levels.
Definition: rcf_structs.h:103
bool setFlightLevelsKm(float[], int)
Definition: rcf.cc:242
#define NUM_BRT_TEMPS
Definition: mtp.h:18
float TRmsRl[NUM_RETR_LVLS]
Formal error in T at retrieval levels.
Definition: rcf_structs.h:105
short Nret
Number of retrieval levels.
Definition: rcf_structs.h:30
float RAOBmin
Minimum acceptable RAOB altitude.
Definition: rcf_structs.h:27
float sRMSa[NUM_RETR_LVLS]
Variance in T at retrieval levels.
Definition: rcf_structs.h:87
RC_FL_Read RC_read
Definition: rcf_structs.h:93
float Src[990]
33 retrieval levels, 30 observables
Definition: rcf_structs.h:89
float RC[NUM_RETR_LVLS][NUM_BRT_TEMPS]
Retrieval Coefficients.
Definition: rcf_structs.h:106
char RAOBfilename[80]
Definition: rcf_structs.h:20
float MBTRms[NUM_BRT_TEMPS]
1-sigma apriori Model Brightness Temp err
Definition: rcf_structs.h:100
short RCformat
Definition: rcf_structs.h:18
char RCfilename[80]
Definition: rcf_structs.h:21
short RAOBcount
Definition: rcf_structs.h:22
float sRTav[NUM_RETR_LVLS]
Average T at retrieval levels.
Definition: rcf_structs.h:86
float sBP
Flight level pressure altitude (hPa)
Definition: rcf_structs.h:82
float sBPrl[NUM_RETR_LVLS]
Pressure at retrieval levels.
Definition: rcf_structs.h:85
float ExcessTamplitude
Random Excess Noise Level on Ground.
Definition: rcf_structs.h:28
char CreationDateTime[8]
Definition: rcf_structs.h:19
float LR1
LR above top of RAOB.
Definition: rcf_structs.h:23
short Nobs
Number of observables.
Definition: rcf_structs.h:29
RCF_HDR Rcf_Hdr
Definition: rcf_structs.h:52
char Array[sizeof(RC_FL_Read)]
Definition: rcf_structs.h:93
#define NUM_RETR_LVLS
number of retrieval levels per flight level in RCFs
Definition: mtp.h:17
float TVarRl[NUM_RETR_LVLS]
Variance in T at retrieval levels.
Definition: rcf_structs.h:104
float LR2
LR above break altitude.
Definition: rcf_structs.h:25
RC_Set_1FL getRCAvgWt(float)
Definition: rcf.cc:165
float Palt
Flight level pressure altitude (hPa)
Definition: rcf_structs.h:99
float sOBrms[NUM_BRT_TEMPS]
1-sigma apriori observable errors
Definition: rcf_structs.h:83
float sRMSe[NUM_RETR_LVLS]
Formal error in T at retrieval levels.
Definition: rcf_structs.h:88
float zLRb
LR break altitude.
Definition: rcf_structs.h:24
float sOBav[NUM_BRT_TEMPS]
Archive Average observables.
Definition: rcf_structs.h:84
short NFL
Number of flight levels.
Definition: rcf_structs.h:32
char Array[sizeof(RCF_HDR)]
Definition: rcf_structs.h:52
float dZ[NUM_RETR_LVLS]
Retrieval offset levels wrt flight level.
Definition: rcf_structs.h:31
float RecordStep
Record Step through available RAOBs.
Definition: rcf_structs.h:26
float PAltRl[NUM_RETR_LVLS]
Pressure at retrieval levels.
Definition: rcf_structs.h:102