OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l1bextract_meris.cpp
Go to the documentation of this file.
1 /*
2  * File: extract_envsat.cpp
3  * Author: dshea
4  *
5  * Created on November 28, 2012, 1:01 PM
6  */
7 
8 #include "EnvsatFile.h"
9 
10 #include <string>
11 #include <cstdlib>
12 #include <stdio.h>
13 
14 #define BOUNDS_ERROR 110
15 #undef DEBUG
16 // #define DEBUG
17 
18 using namespace std;
19 
20 /*
21  *
22  */
23 int main(int argc, char** argv) {
24  string inFilename;
25  int spixl = -1;
26  int epixl = -1;
27  int sscan;
28  int escan;
29  string outFilename;
30 
31  if (argc == 5) {
32  inFilename = argv[1];
33  sscan = atoi(argv[2]) - 1;
34  escan = atoi(argv[3]) - 1;
35  outFilename = argv[4];
36  } else if (argc == 7) {
37  inFilename = argv[1];
38  spixl = atoi(argv[2]) - 1;
39  epixl = atoi(argv[3]) - 1;
40  sscan = atoi(argv[4]) - 1;
41  escan = atoi(argv[5]) - 1;
42  outFilename = argv[6];
43  } else {
44  printf("\nusage: l1bextract_meris infile [spixl epixl] sscan escan outfile\n");
45  printf(" where:\n");
46  printf(" infile - input envsat file\n");
47  printf(" spixl - start pixel (1-based)\n");
48  printf(" epixl - end pixel (1-based)\n");
49  printf(" sscan - start scan line (1-based)\n");
50  printf(" escan - end scan line (1-based)\n");
51  printf(" outfile - output file name\n");
52  exit(EXIT_FAILURE);
53  }
54 
55  if ((spixl > epixl) || (sscan > escan)) {
56  printf("Invalid range requested:\n");
57  int npixl = epixl - spixl + 1;
58  int nscan = escan - sscan + 1;
59  printf("spixl: %5d epixl: %5d npixl: %5d\n", spixl+1, epixl+1, npixl);
60  printf("sscan: %5d escan: %5d nscan: %5d\n", sscan+1, escan+1, nscan);
61  exit(BOUNDS_ERROR);
62  }
63 
64  EnvsatFile* envsatFile = new EnvsatFile(inFilename);
65  if (envsatFile->openFile()) {
66  printf("Error: Could not open file %s\n", inFilename.c_str());
67  exit(EXIT_FAILURE);
68  }
69  if (envsatFile->readHeader()) {
70  printf("Error: Could not read header for file %s\n", inFilename.c_str());
71  exit(EXIT_FAILURE);
72  }
73 
74  EnvsatMPH* mph = envsatFile->getMPH();
75  if (mph == NULL) {
76  printf("Error: Could not get MPH from file\n");
77  exit(EXIT_FAILURE);
78  }
79  EnvsatSPH* sph = envsatFile->getSPH();
80  if (sph == NULL) {
81  printf("Error: Could not get SPH from file\n");
82  exit(EXIT_FAILURE);
83  }
84 
85 #ifdef DEBUG
86  printf("Original File-------------------------------\n");
87  envsatFile->printRecursive();
88 #endif
89 
90  EnvsatFile* envsatFile2 = new EnvsatFile(*envsatFile);
91  envsatFile2->setFileName(outFilename);
92 
93  EnvsatMPH* mph2 = envsatFile2->getMPH();
94  if (mph2 == NULL) {
95  printf("Error: Could not get MPH from file2\n");
96  exit(EXIT_FAILURE);
97  }
98  EnvsatSPH* sph2 = envsatFile2->getSPH();
99  if (sph2 == NULL) {
100  printf("Error: Could not get SPH from file2\n");
101  exit(EXIT_FAILURE);
102  }
103 
104  // fix start / end line
105  int linesPerTiepoint = sph->getLinesPerTiepoint();
106  int origNumScans = envsatFile->getNumScans();
107  int origNumPixels = envsatFile->getNumPixels();
108  if (sscan < 0)
109  sscan = 0;
110  if (sscan >= origNumScans) {
111  printf("sscan %d >= scan count (%d)\n",
112  sscan, origNumScans);
113  exit(BOUNDS_ERROR);
114  }
115  if (escan < 0)
116  escan = origNumScans - 1;
117  if (escan >= origNumScans) {
118  printf("escan %d >= scan count (%d); adjusting.\n",
119  escan, origNumScans);
120  escan = origNumScans - 1;
121  }
122 
123  // calc start end tiepoint line
124  int sscanTiepoint = sscan / linesPerTiepoint;
125  int escanTiepoint = escan / linesPerTiepoint;
126  if (escan % linesPerTiepoint > 0)
127  escanTiepoint++;
128 
129  // make start scan be on the tiepoint boundary
130  sscan = sscanTiepoint * linesPerTiepoint;
131 
132  // calc quality ADS start and stop lines
133  int sscanQuality = sscanTiepoint / 8;
134  int escanQuality = escanTiepoint / 8;
135  if (escanTiepoint % 8 > 0)
136  escanQuality++;
137 
138  // calc the number of scans
139  int numScans = escan - sscan + 1;
140  int numScansTiepoint = escanTiepoint - sscanTiepoint + 1;
141  int numScansQuality = escanQuality - sscanQuality + 1;
142 
143  // fix the spixl and epixl
144  if (spixl < 0)
145  spixl = -1;
146  if (spixl >= origNumPixels) {
147  printf("spixl %d >= pixel count (%d)\n",
148  spixl, origNumPixels);
149  exit(BOUNDS_ERROR);
150  }
151  if (epixl < 0)
152  epixl = -1;
153  if (epixl >= origNumPixels) {
154  printf("epixl %d >= pixel count (%d); adjusting.\n",
155  epixl, origNumPixels);
156  epixl = -1;
157  // epixl = origNumPixels - 1;
158  }
159 
160  // since the epr api that l2gen uses to read the N1 file reverses
161  // the pixels on a line, we need to do the same for epixl and spixl
162  // so the command line for the extractor and l2gen match
163  int tmp_spixl = spixl;
164  if (epixl == -1)
165  spixl = -1;
166  else
167  spixl = origNumPixels - epixl - 1;
168 
169  if (tmp_spixl == -1)
170  epixl = -1;
171  else
172  epixl = origNumPixels - tmp_spixl - 1;
173 
174  // loop through all of the DSDs setting the offset and size
175  int64_t offset = mph2->getMPHSize() + mph2->getSPHSize();
176 
177  EnvsatDSD* dsd;
178  EnvsatDSD* dsd2;
179 
180  for (int i = 0; i < sph2->getNumDSDs(); i++) {
181  dsd2 = sph2->getDSD(i);
182  if (dsd2 != NULL && dsd2->isValid()) {
183 
184  if (dsd2->getName() == sph2->getQualityName()) {
185  // Quality ADS
186  dsd2->setDSOffset(offset);
187  dsd2->setNumDSRs(numScansQuality);
188  int64_t dsSize = numScansQuality * dsd2->getDSRSize();
189  dsd2->setDSSize(dsSize);
190  offset += dsSize;
191 
192  } else if (dsd2->getName() == sph2->getTiepointName()) {
193  // Tiepoint ADS
194  dsd2->setDSOffset(offset);
195  dsd2->setNumDSRs(numScansTiepoint);
196  int64_t dsSize = numScansTiepoint * dsd2->getDSRSize();
197  dsd2->setDSSize(dsSize);
198  offset += dsSize;
199 
200  } else if (dsd2->getType() == 'M') {
201  // Measurement DS
202  dsd2->setDSOffset(offset);
203  dsd2->setNumDSRs(numScans);
204  int64_t dsSize = numScans * dsd2->getDSRSize();
205  dsd2->setDSSize(dsSize);
206  offset += dsSize;
207 
208  } else if (dsd2->getType() == 'R') {
209  // Reference DS
210  // do nothing
211 
212  } else {
213  // everything else just set the offset to copy the whole data set
214  if (dsd2->getDSOffset() != 0) {
215  dsd2->setDSOffset(offset);
216  offset += dsd2->getDSSize();
217  }
218 
219  }
220 
221  } // dsd good
222 
223  } // loop DSDs
224 
225  // set the total file size
226  mph2->setTotalSize(offset);
227 
228  // get tie point for first line
229  dsd = sph->findDSD(sph->getTiepointName());
230  if (dsd == NULL) {
231  printf("Error: Could not find Tiepoint DSD\n");
232  exit(EXIT_FAILURE);
233  }
234 
235  TiepointDSR* tiepointDSR1 = new TiepointDSR(dsd->getDSRSize());
236  TiepointDSR* tiepointDSR2 = new TiepointDSR(dsd->getDSRSize());
237 
238  // set first line metadata
239  if (envsatFile->seekData(dsd, sscanTiepoint)) {
240  printf("Error: Could seek to DSR#%d of Tiepoint DSD\n", sscanTiepoint);
241  exit(EXIT_FAILURE);
242  }
243  if (envsatFile->readData(tiepointDSR1)) {
244  printf("Error: Could read data for DSR#%d of Tiepoint DSD\n", sscanTiepoint);
245  exit(EXIT_FAILURE);
246  }
247 
248 #ifdef DEBUG
249  printf("sscan tiepoint# = %d\n", sscanTiepoint);
250  tiepointDSR1->print();
251 #endif
252 
253  int samplesPerTiepoint = sph->getSamplesPerTiepoint();
254  int startTiepoint, midTiepoint, endTiepoint;
255 
256  if (spixl == -1) {
257  startTiepoint = 0;
258  } else {
259  startTiepoint = spixl / samplesPerTiepoint;
260  }
261  if (epixl == -1) {
262  endTiepoint = tiepointDSR1->getNumPixels() - 1;
263  } else {
264  endTiepoint = epixl / samplesPerTiepoint;
265  }
266  midTiepoint = (endTiepoint - startTiepoint) / 2 + startTiepoint;
267 
268  mph2->setSensingStart(tiepointDSR1->getStartTime());
269  sph2->setFirstLineTime(tiepointDSR1->getStartTime());
270  sph2->setFirstFirstLat(tiepointDSR1->getLat(startTiepoint));
271  sph2->setFirstFirstLon(tiepointDSR1->getLon(startTiepoint));
272  sph2->setFirstMidLat(tiepointDSR1->getLat(midTiepoint));
273  sph2->setFirstMidLon(tiepointDSR1->getLon(midTiepoint));
274  sph2->setFirstLastLat(tiepointDSR1->getLat(endTiepoint));
275  sph2->setFirstLastLon(tiepointDSR1->getLon(endTiepoint));
276 
277  // set last line metadata
278  int tiepointIndex1, tiepointIndex2;
279  int tiepointScan1, tiepointScan2;
280  tiepointIndex1 = escan / linesPerTiepoint;
281  if (tiepointIndex1 >= (dsd->getNumDSRs() - 1)) {
282  tiepointIndex1--;
283  }
284  tiepointIndex2 = tiepointIndex1 + 1;
285  tiepointScan1 = tiepointIndex1 * linesPerTiepoint;
286  tiepointScan2 = tiepointIndex2 * linesPerTiepoint;
287 
288  if (envsatFile->seekData(dsd, tiepointIndex1)) {
289  printf("Error: Could not seek file position for DSR#%d of DSD \"%s\"\n", tiepointIndex1, dsd->getName().c_str());
290  exit(EXIT_FAILURE);
291  }
292  if (envsatFile->readData(tiepointDSR1)) {
293  printf("Error: Could not read data for DSR#%d of DSD \"%s\"\n", tiepointIndex1, dsd->getName().c_str());
294  exit(EXIT_FAILURE);
295  }
296  if (envsatFile->readData(tiepointDSR2)) {
297  printf("Error: Could not read data for DSR#%d of DSD \"%s\"\n", tiepointIndex1 + 1, dsd->getName().c_str());
298  exit(EXIT_FAILURE);
299  }
300 
301 #ifdef DEBUG
302  printf("tiepointScan1 = %d\n", tiepointScan1);
303  printf("tiepointScan2 = %d\n", tiepointScan2);
304  tiepointDSR1->print();
305 #endif
306 
307  double x1, x2, y1, y2;
308  double y;
309  x1 = tiepointScan1;
310  x2 = tiepointScan2;
311  y1 = tiepointDSR1->getStartTime();
312  y2 = tiepointDSR2->getStartTime();
313  y = envsatInterp(x1, x2, y1, y2, escan);
314  mph2->setSensingStop(y);
315  sph2->setLastLineTime(y);
316 
317  y1 = tiepointDSR1->getLat(startTiepoint);
318  y2 = tiepointDSR2->getLat(startTiepoint);
319  sph2->setLastFirstLat(envsatInterp(x1, x2, y1, y2, escan));
320 
321  y1 = tiepointDSR1->getLon(startTiepoint);
322  y2 = tiepointDSR2->getLon(startTiepoint);
323  sph2->setLastFirstLon(envsatInterp(x1, x2, y1, y2, escan));
324 
325  y1 = tiepointDSR1->getLat(midTiepoint);
326  y2 = tiepointDSR2->getLat(midTiepoint);
327  sph2->setLastMidLat(envsatInterp(x1, x2, y1, y2, escan));
328 
329  y1 = tiepointDSR1->getLon(midTiepoint);
330  y2 = tiepointDSR2->getLon(midTiepoint);
331  sph2->setLastMidLon(envsatInterp(x1, x2, y1, y2, escan));
332 
333  y1 = tiepointDSR1->getLat(endTiepoint);
334  y2 = tiepointDSR2->getLat(endTiepoint);
335  sph2->setLastLastLat(envsatInterp(x1, x2, y1, y2, escan));
336 
337  y1 = tiepointDSR1->getLon(endTiepoint);
338  y2 = tiepointDSR2->getLon(endTiepoint);
339  sph2->setLastLastLon(envsatInterp(x1, x2, y1, y2, escan));
340 
341  envsatFile2->modifyProductName();
342 
343  // set the pixel extract information in the header
344  if (spixl == -1 && epixl == -1)
345  mph2->setObpgExtract(false);
346  else
347  mph2->setObpgExtract(true);
348  mph2->setStartPixel(spixl + 1); // if spixl=-1 then 0 also clears this field
349  mph2->setEndPixel(epixl + 1); // if epixl=-1 then 0 also clears this field
350 
351 #ifdef DEBUG
352  printf("\n\nModify File-------------------------------\n");
353  envsatFile2->printRecursive();
354 #endif
355 
356  // done with header info, write out the new file header
357  if (envsatFile2->openFile(true)) {
358  printf("Error: Could not open file \"%s\"\n", envsatFile2->getFileName().c_str());
359  exit(EXIT_FAILURE);
360  }
361  if (envsatFile2->writeHeader()) {
362  printf("Error: Could not write header for file \"%s\"\n", envsatFile2->getFileName().c_str());
363  exit(EXIT_FAILURE);
364  }
365 
366  // loop through data sets copying data and
367  for (int i = 0; i < sph->getNumDSDs(); i++) {
368 
369  // bool firstLine = true;
370 
371  EnvsatDSR* dsr;
372  EnvsatDSD* dsd = sph->getDSD(i);
373 
374 #ifdef DEBUG
375  printf("copying DSD %s\n", dsd->getName().c_str());
376 #endif
377 
378  if (dsd != NULL && dsd->isValid()) {
379  if (dsd->getNumDSRs() > 0) {
380  int start, end;
381  bool extractPixel = false;
382  int fillVal = 0;
383 
384  if (dsd->getName() == sph->getQualityName()) {
385  // Quality ADS
386  dsr = new EnvsatDSR(dsd->getDSRSize());
387  start = sscanQuality;
388  end = escanQuality;
389  } else if (dsd->getName() == sph->getTiepointName()) {
390  // Tiepoint ADS
391  dsr = new EnvsatDSR(dsd->getDSRSize());
392  start = sscanTiepoint;
393  end = escanTiepoint;
394  } else if (dsd->getType() == 'M') {
395  // Measurement DS
396  if (dsd->getName().substr(0, 12).compare("Radiance MDS") == 0)
397  dsr = new RadianceDSR(dsd->getDSRSize());
398  else if (dsd->getName().substr(0, 9).compare("Flags MDS") == 0) {
399  dsr = new FlagsDSR(dsd->getDSRSize());
400  fillVal = 128;
401  } else
402  dsr = new MeasurementDSR(dsd->getDSRSize());
403  start = sscan;
404  end = escan;
405  extractPixel = true;
406  } else {
407  // copy all DSRs
408  dsr = new EnvsatDSR(dsd->getDSRSize());
409  start = 0;
410  end = dsd->getNumDSRs() - 1;
411  }
412 
413  if (envsatFile->seekData(dsd, start)) {
414  printf("Error: Could not seek file position for DSR#%d of DSD \"%s\"\n", start, dsd->getName().c_str());
415  exit(EXIT_FAILURE);
416  }
417  for (int row = start; row <= end; row++) {
418  if (envsatFile->readData(dsr)) {
419  printf("Error: Could not read data for DSR#%d of DSD \"%s\"\n", row, dsd->getName().c_str());
420  exit(EXIT_FAILURE);
421  }
422 
423  if (extractPixel) {
424  if (spixl > 0)
425  dsr->setRange(0, spixl, fillVal);
426  if (epixl >= 0)
427  dsr->setRange(epixl + 1, dsr->getNumPixels() - epixl - 1, fillVal);
428  }
429 
430  if (envsatFile2->writeData(dsr)) {
431  printf("Error: Could not write data for DSR#%d of DSD \"%s\"\n", row, dsd->getName().c_str());
432  exit(EXIT_FAILURE);
433  }
434  }
435  free(dsr);
436  }
437  }
438  }
439 
440  // close the files
441  envsatFile2->closeFile();
442  envsatFile->closeFile();
443 
444  return EXIT_SUCCESS;
445 }
virtual int getMPHSize()
Definition: EnvsatMPH.h:42
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
virtual int writeData(EnvsatDSR *dsr)
Definition: EnvsatFile.cpp:185
virtual void setDSSize(int64_t size)
Definition: EnvsatDSD.cpp:66
virtual const std::string & getQualityName()=0
virtual void setFileName(const std::string &name)
Definition: EnvsatFile.cpp:116
virtual int64_t getDSSize()
Definition: EnvsatDSD.cpp:62
virtual int getNumPixels()
Definition: TiepointDSR.h:22
virtual EnvsatDSD * findDSD(const std::string &name)
Definition: EnvsatSPH.cpp:120
#define NULL
Definition: decode_rs.h:63
virtual int seekData(EnvsatDSD *dsd, int scanLine=0)
Definition: EnvsatFile.cpp:173
virtual int getNumDSDs()
Definition: EnvsatSPH.cpp:98
virtual void setFirstLastLat(double lat)=0
virtual void setLastLineTime(double unixTime)=0
virtual EnvsatMPH * getMPH()
Definition: EnvsatFile.cpp:120
virtual double getLon(int pixel)
Definition: TiepointDSR.cpp:50
virtual void setFirstFirstLat(double lat)=0
int32 nscan
Definition: l1_czcs_hdf.c:19
virtual void setLastLastLat(double lat)=0
virtual int getNumScans()
Definition: EnvsatFile.cpp:150
virtual void setLastFirstLon(double lon)=0
virtual void print()
Definition: TiepointDSR.cpp:34
virtual int getNumDSRs()
Definition: EnvsatDSD.cpp:70
virtual void setFirstFirstLon(double lon)=0
virtual double getLat(int pixel)
Definition: TiepointDSR.cpp:45
virtual int64_t getDSRSize()
Definition: EnvsatDSD.cpp:78
int main(int argc, char **argv)
virtual int readData(EnvsatDSR *dsr)
Definition: EnvsatFile.cpp:181
virtual int readHeader()
Definition: EnvsatFile.cpp:86
virtual int getNumPixels()
Definition: EnvsatDSR.h:28
virtual void setDSOffset(int64_t offset)
Definition: EnvsatDSD.cpp:58
virtual void setFirstLastLon(double lon)=0
virtual double getStartTime()
Definition: TiepointDSR.cpp:41
virtual void setEndPixel(int pix)
Definition: EnvsatMPH.cpp:176
virtual void setLastMidLat(double lat)=0
virtual void setFirstLineTime(double unixTime)=0
virtual std::string & getName()
Definition: EnvsatDSD.cpp:44
virtual void setNumDSRs(int size)
Definition: EnvsatDSD.cpp:74
virtual int getLinesPerTiepoint()=0
virtual int64_t getDSOffset()
Definition: EnvsatDSD.cpp:54
virtual void setTotalSize(int64_t size)
Definition: EnvsatMPH.cpp:214
virtual EnvsatSPH * getSPH()
Definition: EnvsatFile.cpp:124
virtual const std::string & getTiepointName()=0
virtual void setStartPixel(int pix)
Definition: EnvsatMPH.cpp:145
virtual char getType()
Definition: EnvsatDSD.cpp:50
virtual void setFirstMidLon(double lon)=0
virtual void modifyProductName()
Definition: EnvsatFile.cpp:189
#define BOUNDS_ERROR
virtual int writeHeader()
Definition: EnvsatFile.cpp:99
virtual void setObpgExtract(bool val)
Definition: EnvsatMPH.cpp:119
virtual void setLastMidLon(double lon)=0
virtual void printRecursive()
Definition: EnvsatFile.cpp:133
double envsatInterp(double x1, double x2, double y1, double y2, double xin)
Definition: EnvsatUtil.cpp:174
virtual int getSamplesPerTiepoint()=0
virtual void setSensingStop(double unixTime)
Definition: EnvsatMPH.cpp:205
virtual bool isValid()
Definition: EnvsatDSD.cpp:38
virtual int openFile(bool write=false)
Definition: EnvsatFile.cpp:74
virtual void setLastFirstLat(double lat)=0
virtual void setLastLastLon(double lon)=0
l2prod offset
virtual int getSPHSize()
Definition: EnvsatMPH.cpp:218
virtual void setFirstMidLat(double lat)=0
virtual const std::string & getFileName()
Definition: EnvsatFile.cpp:112
int i
Definition: decode_rs.h:71
virtual void closeFile()
Definition: EnvsatFile.cpp:143
virtual void setRange(int offset, int count, int val)
Definition: EnvsatDSR.cpp:78
virtual EnvsatDSD * getDSD(int index)
Definition: EnvsatSPH.cpp:102
virtual void setSensingStart(double unixTime)
Definition: EnvsatMPH.cpp:194
virtual int getNumPixels()
Definition: EnvsatFile.cpp:165