General Purpose Geodetic Library
SgMasterRecord.cpp
Go to the documentation of this file.
1 /*
2  *
3  * This file is a part of Space Geodetic Library. The library is used by
4  * nuSolve, a part of CALC/SOLVE system, and designed to make analysis of
5  * geodetic VLBI observations.
6  * Copyright (C) 2010-2020 Sergei Bolotin.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #include <iostream>
24 #include <stdlib.h>
25 
26 #include <QtCore/QDir>
27 #include <QtCore/QFile>
28 #include <QtCore/QStringList>
29 #include <QtCore/QRegularExpression>
30 #include <QtCore/QTextStream>
31 
32 
33 #include <SgMasterRecord.h>
34 #include <SgLogger.h>
35 
36 
37 
38 /*=======================================================================================================
39 *
40 * METHODS:
41 *
42 *======================================================================================================*/
43 //
44 // static first:
46 {
47  return "SgMasterRecord";
48 };
49 
50 
51 
52 //
54 {
55  isValid_ = r.isValid_;
56  name_ = r.name_;
57  type_ = r.type_;
58  code_ = r.code_;
59  date_ = r.date_;
60  stations_ = r.stations_;
61  dbcCode_ = r.dbcCode_;
65  return *this;
66 };
67 
68 
69 
70 //
71 void SgMasterRecord::parseString_v1(const QString& str)
72 {
73  isValid_ = false;
74  if (str.at(0) != QChar('|') ||
75  str.at(str.size()-1)!=QChar('|') ||
76  str.count(QChar('|')) != 16)
77  {
78  //complain (?):
79  //logger->write(SgLogger::DBG, SgLogger::IO_TXT, className() +
80  // ": got a not valid MR-string: [" + str + "]; skipped");
81  return;
82  };
83  QStringList fields = str.split("|");
84  name_ = fields.at(1).simplified();
85  code_ = fields.at(2).simplified().toLower();
86  date_ = fields.at(3).simplified();
87  stations_ = fields.at(7).simplified();
88  dbcCode_ = fields.at(12).simplified();
89  scheduledBy_ = fields.at(8).simplified();
90  correlatedBy_ = fields.at(9).simplified();
91  submittedBy_ = fields.at(13).simplified();
92  if (name_.size() && code_.size() && dbcCode_.size() && date_.size())
93  isValid_ = true;
94 };
95 
96 
97 
98 //
99 void SgMasterRecord::parseString_v2(const QString& str)
100 {
101  isValid_ = false;
102  if (str.at(0) != QChar('|') ||
103  str.at(str.size()-1)!=QChar('|') ||
104  str.count(QChar('|')) != 14)
105  {
106  //complain (?):
107  //logger->write(SgLogger::DBG, SgLogger::IO_TXT, className() +
108  // ": got a not valid MR-string: [" + str + "]; skipped");
109  return;
110  };
111  QStringList fields = str.split("|");
112  //
113  // SESSION DATE SESSION DOY TIME DUR STATIONS SKED CORR STATUS DBC SUBM DEL
114  // TYPE yyyymmdd CODE ddd hh:mm h:mm yyyymmdd CODE days
115  //---------------------------------------------------------------------------------------------------------------------------------------------
116  //|IVS-R1 |20220103|r11032 | 3|17:00|24:00|HtIsKvMaNsNtNyOnSaWzYg -Ag |NASA|BONN|20220118| XA |NASA| 14|
117  // 1 2 3 4 5 6 7 8 9 10 11 12 13
118  type_ = fields.at(1).simplified();
119  date_ = fields.at(2).simplified();
120  code_ = fields.at(3).simplified();
121  //
122  stations_ = fields.at(7).simplified();
123  scheduledBy_ = fields.at(8).simplified();
124  correlatedBy_ = fields.at(9).simplified();
125  //
126  dbcCode_ = fields.at(11).simplified();
127  submittedBy_ = fields.at(12).simplified();
128  name_ = "";
129 
130  if (type_.size() && code_.size() && date_.size())
131  isValid_ = true;
132 };
133 
134 
135 
136 //
137 bool SgMasterRecord::lookupRecordByName(const QString& dbName, const QString& path)
138 {
139  QRegularExpression reOldDbName("(\\d{2})([A-Z]{3})(\\d{2})([A-Z0-9]{1,2})");
140  QRegularExpression reNewDbName("(\\d{8})-([\\S]{2,12})");
141  QRegularExpressionMatch match;
142  QDir dir(path);
143 
144  if (!dir.exists())
145  {
147  "::lookupRecordByName(): the masterfile directory \"" + path +
148  "\" does not exist; master file records are unavailable");
149  return false;
150  };
151 
152  QString sYear("");
153  QString sDate("");
154  QString sCode("");
155  if ((match=reOldDbName.match(dbName)).hasMatch()) // dbName: 12JUL16XA
156  {
157  sYear = match.captured(1);
158  sDate = match.captured(2) + match.captured(3);
159  sCode = match.captured(4);
161  }
162  else if ((match=reNewDbName.match(dbName)).hasMatch()) // dbName: 20120716-r1542
163  {
164  sYear = match.captured(1).left(4);
165  sDate = "";
166 // sCode = match.captured(2).toUpper();
167  sCode = match.captured(2).toLower();
169  }
170  else
172  "::lookupRecordByName(): the database name, " + dbName + ", is of unknown pattern");
173 
174  if (findRecordByName(sDate, sCode, path + "/" + "master" + sYear + "-loc.txt") ||
175  findRecordByName(sDate, sCode, path + "/" + "master" + sYear + ".txt") ||
176  findRecordByName(sDate, sCode, path + "/" + "master" + sYear + "-int.txt") ||
177  findRecordByName(sDate, sCode, path + "/" + "master" + sYear + "-vgos.txt") )
178  return true;
179  return false;
180 };
181 
182 
183 
184 //
185 bool SgMasterRecord::findRecordByName(const QString& date, const QString& dbcCode,
186  const QString& fileName)
187 {
188  if (mfVersion_ == MFV_UNDEF)
189  {
191  "::findRecordByName(): cannot guess masterfile version");
192  return false;
193  };
194  //
195  SgMasterRecord mr;
196  QString str;
197  QFile f(fileName);
198  if (!f.exists())
199  {
201  "::findRecordByName(): the masterfile \"" + fileName + "\" does not exist");
202  return false;
203  };
204  //
205  if (f.open(QFile::ReadOnly))
206  {
207  QTextStream s(&f);
208  while (!s.atEnd())
209  {
210  str = s.readLine();
211  if (str.size()>16)
212  {
213  if (mfVersion_ == MFV_V01)
214  mr.parseString_v1(str);
215  else
216  mr.parseString_v2(str);
217  if (0 < date.size())
218  {
219  if (mr.getDate() == date && mr.getDbcCode() == dbcCode)
220  {
221  *this = mr;
222  f.close();
223  s.setDevice(NULL);
224  return true;
225  };
226  }
227  else
228  {
229  if (mr.getCode() == dbcCode)
230  {
231  *this = mr;
232  f.close();
233  s.setDevice(NULL);
234  return true;
235  };
236  };
237  };
238  };
239  f.close();
240  s.setDevice(NULL);
241  };
242  return false;
243 };
244 
245 
246 
247 //
248 bool SgMasterRecord::lookupRecordByCode(const QString& sessionCode, const QString& sYear,
249  const QString& path)
250 {
251  if (sYear.size() == 2)
253  else if (sYear.size() == 4)
255 
256  QDir dir(path);
257  if (!dir.exists())
258  {
260  "::lookupRecordByCode(): the masterfile directory \"" + path +
261  "\" does not exist; master file records are unavailable");
262  return false;
263  };
264  if (findRecordByCode(sessionCode.toLower(), path + "/" + "master" + sYear + "-loc.txt") ||
265  findRecordByCode(sessionCode.toLower(), path + "/" + "master" + sYear + ".txt") ||
266  findRecordByCode(sessionCode.toLower(), path + "/" + "master" + sYear + "-int.txt") ||
267  findRecordByCode(sessionCode.toLower(), path + "/" + "master" + sYear + "-vgos.txt") )
268  return true;
269  return false;
270 };
271 
272 
273 
274 //
275 bool SgMasterRecord::findRecordByCode(const QString& sessionCode, const QString& fileName)
276 {
277  if (mfVersion_ == MFV_UNDEF)
278  {
280  "::findRecordByName(): cannot guess masterfile version");
281  return false;
282  };
283  //
284  SgMasterRecord mr;
285  QString str;
286  QFile f(fileName);
287  if (!f.exists())
288  {
290  "::findRecordByCode(): the masterfile \"" + fileName + "\" does not exist");
291  return false;
292  };
293  //
294  if (f.open(QFile::ReadOnly))
295  {
297  "::findRecordByCode(): processing the masterfile \"" + fileName + "\"");
298  QTextStream s(&f);
299  while (!s.atEnd())
300  {
301  str = s.readLine();
302  if (str.size()>16)
303  {
304  if (mfVersion_ == MFV_V01)
305  mr.parseString_v1(str);
306  else
307  mr.parseString_v2(str);
308  if (mr.getCode() == sessionCode) // some correlators provide lower case here
309  {
310  *this = mr;
311  f.close();
312  s.setDevice(NULL);
313  return true;
314  };
315  };
316  };
317  f.close();
318  s.setDevice(NULL);
319  };
320  return false;
321 };
322 /*=====================================================================================================*/
323 //
324 // FRIENDS:
325 //
326 /*=====================================================================================================*/
327 //
328 
329 /*=====================================================================================================*/
330 //
331 // aux functions:
332 //
333 
334 /*=====================================================================================================*/
335 //
336 // constants:
337 //
338 
339 /*=====================================================================================================*/
SgLogger * logger
Definition: SgLogger.cpp:231
virtual void write(LogLevel, quint32, const QString &, bool=false)
Definition: SgLogger.cpp:88
@ IO_TXT
Definition: SgLogger.h:65
const QString & getDate() const
static const QString className()
bool lookupRecordByCode(const QString &sessionCode, const QString &sYear, const QString &path)
bool findRecordByCode(const QString &sessionCode, const QString &fileName)
void parseString_v1(const QString &)
void parseString_v2(const QString &)
QString scheduledBy_
QString correlatedBy_
const QString & getCode() const
SgMasterRecord & operator=(const SgMasterRecord &r)
bool findRecordByName(const QString &date, const QString &dbcCode, const QString &fileName)
const QString & getDbcCode() const
MasterFileVersion mfVersion_
QString submittedBy_
bool lookupRecordByName(const QString &nickName, const QString &path)