File: msgcommand.cpp

package info (click to toggle)
gdal 1.10.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,320 kB
  • ctags: 74,726
  • sloc: cpp: 677,199; ansic: 162,820; python: 13,816; cs: 11,163; sh: 10,446; java: 5,279; perl: 4,429; php: 2,971; xml: 1,500; yacc: 934; makefile: 494; sql: 112
file content (464 lines) | stat: -rw-r--r-- 13,971 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/******************************************************************************
 * $Id: msgcommand.cpp 15085 2008-07-31 13:41:31Z mloskot $
 *
 * Purpose:  Implementation of MSGCommand class. Parse the src_dataset
 *           string that is meant for the MSG driver.
 * Author:   Bas Retsios, retsios@itc.nl
 *
 ******************************************************************************
 * Copyright (c) 2004, ITC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ******************************************************************************/

#include "msgcommand.h"
#include <cstdlib>
using namespace std;

#ifdef _WIN32
#define PATH_SEP '\\'
#else
#define PATH_SEP '/'
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

#define min(a,b) (((a)<(b))?(a):(b))

MSGCommand::MSGCommand() :
    cDataConversion('N'),
    iNrCycles(1),
    sRootFolder(""),
    sTimeStamp(""),
    iStep(1),
    fUseTimestampFolder(true)
{
  for (int i = 0; i < 12; ++i)
    channel[i] = 0;
}

MSGCommand::~MSGCommand()
{

}

std::string MSGCommand::sTrimSpaces(std::string const& str)
{
  std::string::size_type iStart = 0;

  while ((iStart < str.length()) && (str[iStart] == ' '))
    ++iStart;

  std::string::size_type iLength = str.length() - iStart;

  while ((iLength > 0) && (str[iStart + iLength - 1] == ' '))
    --iLength;

  return str.substr(iStart, iLength);
}

std::string MSGCommand::sNextTerm(std::string const& str, int & iPos)
{
  std::string::size_type iOldPos = iPos;
  iPos = str.find(',', iOldPos);
  iPos = min(iPos, str.find(')', iOldPos));
  if (iPos > iOldPos)
  {
    std::string sRet = str.substr(iOldPos, iPos - iOldPos);
    if (str[iPos] != ')')
      ++iPos;
    return sTrimSpaces(sRet);
  }
  else
    return "";
}

bool fTimeStampCorrect(std::string const& sTimeStamp)
{
  if (sTimeStamp.length() != 12)
    return false;

  for (int i = 0; i < 12; ++i)
  {
    if (sTimeStamp[i] < '0' || sTimeStamp[i] > '9')
      return false;
  }

  return true;
}

std::string MSGCommand::parse(std::string const& command_line)
{
  // expected:
  // MSG(folder,timestamp,channel,in_same_folder,data_conversion,nr_cycles,step)
  // or
  // MSG(folder,timestamp,(channel,channel,...,channel),in_same_folder,data_conversion,nr_cycles,step)
  // or
  // <path>\H-000-MSG1__-MSG1________.....

  std::string sErr ("");

  std::string sID = command_line.substr(0, 4);
  if (sID.compare("MSG(") == 0)
  {
    int iPos = 4; // after bracket open
    sRootFolder = sNextTerm(command_line, iPos);
    if (sRootFolder.length() > 0)
    {
      if (sRootFolder[sRootFolder.length() - 1] != PATH_SEP)
      sRootFolder += PATH_SEP;
      sTimeStamp = sNextTerm(command_line, iPos);
      if (fTimeStampCorrect(sTimeStamp))
      {
        try // for eventual exceptions
        {
          while ((iPos < command_line.length()) && (command_line[iPos] == ' '))
            ++iPos;
          if (command_line[iPos] == '(')
          {
            ++iPos; // skip the ( bracket
            int i = 1;
            std::string sChannel = sNextTerm(command_line, iPos);
            while (command_line[iPos] != ')')
            {
              int iChan = atoi(sChannel.c_str());
              if (iChan >= 1 && iChan <= 12)
                channel[iChan - 1] = i;
              else
                sErr = "Channel numbers must be between 1 and 12";
              sChannel = sNextTerm(command_line, iPos);
              ++i;
            }
            int iChan = atoi(sChannel.c_str());
            if (iChan >= 1 && iChan <= 12)
              channel[iChan - 1] = i;
            else
              sErr = "Channel numbers must be between 1 and 12";
            ++iPos; // skip the ) bracket
            while ((iPos < command_line.length()) && (command_line[iPos] == ' '))
              ++iPos;
            if (command_line[iPos] == ',')
              ++iPos;
          }
          else
          {
            std::string sChannel = sNextTerm(command_line, iPos);
            int iChan = atoi(sChannel.c_str());
            if (iChan >= 1 && iChan <= 12)
              channel[iChan - 1] = 1;
            else
              sErr = "Channel numbers must be between 1 and 12";
          }
          std::string sInRootFolder = sNextTerm(command_line, iPos);
          if ((sInRootFolder.compare("N") != 0) && (sInRootFolder.compare("Y") != 0))
            sErr = "Please specify N for data that is in a date dependent folder or Y for data that is in specified folder.";
          else
            fUseTimestampFolder = (sInRootFolder.compare("N") == 0);
          std::string sDataConversion = sNextTerm(command_line, iPos);
          cDataConversion = (sDataConversion.length()>0)?sDataConversion[0]:'N';
          std::string sNrCycles = sNextTerm(command_line, iPos);
          iNrCycles = atoi(sNrCycles.c_str());
          if (iNrCycles < 1)
            iNrCycles = 1;
          std::string sStep = sNextTerm(command_line, iPos);
          iStep = atoi(sStep.c_str());
          if (iStep < 1)
            iStep = 1;
          while ((iPos < command_line.length()) && (command_line[iPos] == ' '))
            ++iPos;
          // additional correctness checks
          if (command_line[iPos] != ')')
            sErr = "Invalid syntax. Please review the MSG(...) statement.";
          else if ((iNrChannels() > 1) && (channel[11] != 0))
            sErr = "It is not possible to combine channel 12 (HRV) with the other channels.";
          else if (iNrChannels() == 0 && sErr.length() == 0)
            sErr = "At least one channel should be specified.";
          else if ((cDataConversion != 'N') && (cDataConversion != 'B') && (cDataConversion != 'R') && (cDataConversion != 'L') && (cDataConversion != 'T'))
            sErr = "Please specify N(o change), B(yte conversion), R(adiometric calibration), L(radiometric using central wavelength) or T(reflectance or temperature) for data conversion.";
        }
        catch(...)
        {
          sErr = "Invalid syntax. Please review the MSG(...) statement.";
        }
      }
      else
        sErr = "Timestamp should be exactly 12 digits.";
    }
    else
      sErr = "A folder must be filled in indicating the root of the image data folders.";
  }
  else if (command_line.find("H-000-MSG") >= 0)
  {
    int iPos = command_line.find("H-000-MSG");
    if ((command_line.length() - iPos) == 61)
    {
      fUseTimestampFolder = false;
      sRootFolder = command_line.substr(0, iPos);
      sTimeStamp = command_line.substr(iPos + 46, 12);
      if (fTimeStampCorrect(sTimeStamp))
      {
        int iChan = iChannel(command_line.substr(iPos + 26, 9));
        if (iChan >= 1 && iChan <= 12)
        {
          channel[iChan - 1] = 1;
          cDataConversion = 'N';
          iNrCycles = 1;
          iStep = 1;
        }
        else
          sErr = "Channel numbers must be between 1 and 12";
      }
      else
        sErr = "Timestamp should be exactly 12 digits.";
    }
    else
      sErr = "-"; // the source data set it is not for the MSG driver
  }
  else
    sErr = "-"; // the source data set it is not for the MSG driver
  return sErr;
}

int MSGCommand::iNrChannels()
{
  int iRet = 0;
  for (int i=0; i<12; ++i)
    if (channel[i] != 0)
      ++iRet;

  return iRet;
}

int MSGCommand::iChannel(int iChannelNumber)
{
  // return the iChannelNumber-th channel
  // iChannelNumber is a value between 1 and 12
  // note that channels are ordered. their order number is the value in the array
  // As we can't combine channel 12 with channels 1 to 11, it does not make sense to inquire for iNr == 12
  int iRet = 0;
  if (iChannelNumber <= iNrChannels())
  {
    while ((iRet < 12) && (channel[iRet] != iChannelNumber))
      ++iRet;
  }

  // will return a number between 1 and 12
  return (iRet + 1);
}

int MSGCommand::iNrStrips(int iChannel)
{
  if (iChannel == 12)
    return 24;
  else if (iChannel >= 1 && iChannel <= 11)
    return 8;
  else
    return 0;
}

int MSGCommand::iChannel(std::string const& sChannel)
{
  if (sChannel.compare("VIS006___") == 0)
    return 1;
  else if (sChannel.compare("VIS008___") == 0)
    return 2;
  else if (sChannel.compare("IR_016___") == 0)
    return 3;
  else if (sChannel.compare("IR_039___") == 0)
    return 4;
  else if (sChannel.compare("WV_062___") == 0)
    return 5;
  else if (sChannel.compare("WV_073___") == 0)
    return 6;
  else if (sChannel.compare("IR_087___") == 0)
    return 7;
  else if (sChannel.compare("IR_097___") == 0)
    return 8;
  else if (sChannel.compare("IR_108___") == 0)
    return 9;
  else if (sChannel.compare("IR_120___") == 0)
    return 10;
  else if (sChannel.compare("IR_134___") == 0)
    return 11;
  else if (sChannel.compare("HRV______") == 0)
    return 12;
  else
    return 0;
}

std::string MSGCommand::sChannel(int iChannel)
{
  switch (iChannel)
  {
    case 1:
      return "VIS006___";
      break;
    case 2:
      return "VIS008___";
      break;
    case 3:
      return "IR_016___";
      break;
    case 4:
      return "IR_039___";
      break;
    case 5:
      return "WV_062___";
      break;
    case 6:
      return "WV_073___";
      break;
    case 7:
      return "IR_087___";
      break;
    case 8:
      return "IR_097___";
      break;
    case 9:
      return "IR_108___";
      break;
    case 10:
      return "IR_120___";
      break;
    case 11:
      return "IR_134___";
      break;
    case 12:
      return "HRV______";
      break;
    default:
      return "_________";
      break;
  }
}

std::string MSGCommand::sTimeStampToFolder(std::string & sTimeStamp)
{
  std::string sYear (sTimeStamp.substr(0,4));
  std::string sMonth (sTimeStamp.substr(4, 2));
  std::string sDay (sTimeStamp.substr(6, 2));
  return (sYear + PATH_SEP + sMonth + PATH_SEP + sDay + PATH_SEP);
}

int MSGCommand::iDaysInMonth(int iMonth, int iYear)
{
  int iDays;

  if ((iMonth == 4) || (iMonth == 6) || (iMonth == 9) || (iMonth == 11))
    iDays = 30;
  else if (iMonth == 2)
  {
    iDays = 28;
    if (iYear % 100 == 0) // century year
    {
      if (iYear % 400 == 0) // century leap year
        ++iDays;
    }
    else
    {
      if (iYear % 4 == 0) // normal leap year
        ++iDays;
    }
  }
  else
    iDays = 31;

  return iDays;
}

std::string MSGCommand::sCycle(int iCycle)
{
  // find nth full quarter
  // e.g. for n = 1, 200405311114 should result in 200405311115
  // 200405311115 should result in 200405311130
  // 200405311101 should result in 200405311115
  // 200412312345 should result in 200501010000
  
  std::string sYear (sTimeStamp.substr(0, 4));
  std::string sMonth (sTimeStamp.substr(4, 2));
  std::string sDay (sTimeStamp.substr(6, 2));
  std::string sHours (sTimeStamp.substr(8, 2));
  std::string sMins (sTimeStamp.substr(10, 2));

  int iYear = atoi(sYear.c_str());
  int iMonth = atoi(sMonth.c_str());
  int iDay = atoi(sDay.c_str());
  int iHours = atoi(sHours.c_str());
  int iMins = atoi(sMins.c_str());

  iMins += (iCycle - 1)*15*iStep;

  // round off the mins found down to a multiple of 15 mins
  iMins = ((int)(iMins / 15)) * 15;
  // now handle the whole chain back to the year ...
  while (iMins >= 60)
  {
    iMins -= 60;
    ++iHours;
  }
  while (iHours >= 24)
  {
    iHours -= 24;
    ++iDay;
  }
  while (iDay > iDaysInMonth(iMonth, iYear))
  {
    iDay -= iDaysInMonth(iMonth, iYear);
    ++iMonth;
  }
  while (iMonth > 12)
  {
    iMonth -= 12;
    ++iYear;
  }

  char sRet [100];
  sprintf(sRet, "%.4d%.2d%.2d%.2d%.2d", iYear, iMonth, iDay, iHours, iMins);

  return sRet;  
}

std::string MSGCommand::sFileName(int iSatellite, int iSequence, int iStrip)
{
  int iNr = iNrChannels();
  int iChannelNumber = 1 + (iSequence - 1) % iNr;;
  int iCycle = 1 + (iSequence - 1) / iNr;
  char sRet [4096];
  std::string siThCycle (sCycle(iCycle));
  if (fUseTimestampFolder)
    sprintf(sRet, "%s%sH-000-MSG%d__-MSG%d________-%s-%.6d___-%s-C_", sRootFolder.c_str(), sTimeStampToFolder(siThCycle).c_str(), iSatellite, iSatellite, sChannel(iChannel(iChannelNumber)).c_str(), iStrip, siThCycle.c_str());
  else
    sprintf(sRet, "%sH-000-MSG%d__-MSG%d________-%s-%.6d___-%s-C_", sRootFolder.c_str(), iSatellite, iSatellite, sChannel(iChannel(iChannelNumber)).c_str(), iStrip, siThCycle.c_str());
  return sRet;
}

std::string MSGCommand::sPrologueFileName(int iSatellite, int iSequence)
{
  int iCycle = 1 + (iSequence - 1) / iNrChannels();
  char sRet [4096];
  std::string siThCycle (sCycle(iCycle));
  if (fUseTimestampFolder)
    sprintf(sRet, "%s%sH-000-MSG%d__-MSG%d________-_________-PRO______-%s-__", sRootFolder.c_str(), sTimeStampToFolder(siThCycle).c_str(), iSatellite, iSatellite, siThCycle.c_str());
  else
    sprintf(sRet, "%sH-000-MSG%d__-MSG%d________-_________-PRO______-%s-__", sRootFolder.c_str(), iSatellite, iSatellite, siThCycle.c_str());
  return sRet;
}