NASA Logo
Ocean Color Science Software

ocssw V2022
l0stream.cpp
Go to the documentation of this file.
1 
9 #include "l0stream.hpp"
10 #include <cstring>
11 
12 #define PACE_OCI_HDR_LEN 64
13 #define EXP_CONTENT_TYPE "cFE1"
14 #define EXP_CONTENT_SUBTYPE " SCI"
15 #define EXP_SPACECRAFT_ID "PACE"
16 #define EXP_PROCESSOR_ID " OCI"
17 #define CURR_L0_FILE this->l0Files.at(idxCurrStream)
18 
19 using namespace std;
20 
21 L0Stream::L0Stream(vector<string> &l0FileNames) {
22  for (auto filename : l0FileNames) {
23  idxCurrStream = 0;
24 
25  L0File *l0File = new L0File;
26 
27  l0File->fileStream = new fstream(filename, fstream::in | fstream::binary);
28  l0File->fileSize = getFileSize(*l0File->fileStream);
29  l0File->hasHeader = hasHeader(*l0File->fileStream);
30  l0File->bytesLeft = l0File->fileSize;
31 
32  l0Files.push_back(l0File);
33  }
34 }
35 
37  for (auto l0File : l0Files) {
38  l0File->fileStream->close();
39  delete(l0File->fileStream);
40  delete(l0File);
41  }
42 }
43 
45  cerr << "-E- " << message << endl;
46  exit(EXIT_FAILURE);
47 }
48 
49 void L0Stream::strip_oci_header(fstream &fileStream) {
58  char hdr[PACE_OCI_HDR_LEN]; // Full .oci file header
59  fileStream.read(hdr, PACE_OCI_HDR_LEN);
60 
61  if (string(hdr + 0x00, 4) != EXP_CONTENT_TYPE || string(hdr + 0x04, 4) != EXP_CONTENT_SUBTYPE ||
62  string(hdr + 0x0c, 4) != EXP_SPACECRAFT_ID || string(hdr + 0x14, 4) != EXP_PROCESSOR_ID)
63  fileStream.seekg(0, fileStream.beg);
64 }
65 
66 bool L0Stream::hasHeader(fstream &fileStream) {
67  L0Stream::strip_oci_header(fileStream);
68  return fileStream.tellg() == 0 ? false : true;
69 }
70 
71 size_t L0Stream::getFileSize(std::fstream &fileStream) {
72  fileStream.seekg(0, fileStream.end);
73  size_t fileSize = fileStream.tellg();
74 
75  fileStream.seekg(0, fileStream.beg);
76  return fileSize;
77 }
78 
80  return CURR_L0_FILE->fileSize - CURR_L0_FILE->currPos;
81 }
82 
83 void L0Stream::read(char *buffer, size_t numBytes) {
84  fstream *currStream = CURR_L0_FILE->fileStream;
85  size_t bytesLeft = CURR_L0_FILE->bytesLeft;
86 
87  if (bytesLeft <= numBytes) {
88  int numBytesAfterCarryover = numBytes - bytesLeft;
89 
90  char *carryover = (char *)calloc(bytesLeft, sizeof(char));
91  currStream->read(carryover, bytesLeft);
92 
93  memcpy(buffer, carryover, bytesLeft);
94  free(carryover);
95 
96  idxCurrStream++; // Switch streams
97 
98  if (idxCurrStream >= l0Files.size()) // No more files to read
99  return;
100 
101  currStream->read(buffer + bytesLeft, numBytesAfterCarryover); // Read remaining bytes
102  CURR_L0_FILE->currPos = CURR_L0_FILE->fileStream->tellg();
103  CURR_L0_FILE->bytesLeft = getBytesLeft();
104  return;
105  }
106 
107  CURR_L0_FILE->fileStream->read(buffer, numBytes);
108  CURR_L0_FILE->currPos = CURR_L0_FILE->fileStream->tellg();
109  CURR_L0_FILE->bytesLeft = getBytesLeft();
110 
111  if (CURR_L0_FILE->currPos == CURR_L0_FILE->fileSize)
112  idxCurrStream++;
113 }
114 
115 void L0Stream::seekg(std::streamoff num, std::ios_base::seekdir dir) {
116  int currPos = CURR_L0_FILE->currPos;
117  int abs_num = abs(num); // Absolute value of num
118 
119  if (num < 0 && currPos < abs_num) { // Seeking toward beginning, into the previous file
120 
121  if (idxCurrStream <= 0)
122  exit_unhappy("Tried to seek into a file that doesn't exist");
123 
124  CURR_L0_FILE->fileStream->seekg(0); // Reset pos of this stream to beginning
125  idxCurrStream--;
126  CURR_L0_FILE->fileStream->seekg(abs_num - currPos, CURR_L0_FILE->fileStream->end);
127  } else {
128  CURR_L0_FILE->fileStream->seekg(num, dir);
129  }
130 }
131 
133  if (idxCurrStream < l0Files.size())
134  return CURR_L0_FILE->fileStream->fail();
135  else
136  return true;
137 }
138 
140  if (idxCurrStream < l0Files.size())
141  return CURR_L0_FILE->fileStream->eof();
142  else
143  return true;
144 }
145 
147  if (idxCurrStream < l0Files.size())
148  return CURR_L0_FILE->fileStream->tellg();
149  else
150  return -1;
151 }
int tellg()
Obtain the position of the read/write head from the beginning of the current filestream.
Definition: l0stream.cpp:146
#define EXP_CONTENT_SUBTYPE
Definition: l0stream.cpp:14
size_t getFileSize(std::fstream &fileStream)
Obtain the size in bytes of fileStream. This method does not guarantee that the read/write head retur...
Definition: l0stream.cpp:71
#define EXP_PROCESSOR_ID
Definition: l0stream.cpp:16
std::fstream * fileStream
Definition: l0stream.hpp:19
#define EXP_SPACECRAFT_ID
Definition: l0stream.cpp:15
size_t bytesLeft
Definition: l0stream.hpp:23
void seekg(std::streamoff num, std::ios_base::seekdir dir)
Move the read/write head a number of bytes in a particular direction, switching streams as necessary.
Definition: l0stream.cpp:115
#define EXP_CONTENT_TYPE
Definition: l0stream.cpp:13
bool fail()
Indicate if the fstream::badbit or fstream::failbit is set.
Definition: l0stream.cpp:132
#define CURR_L0_FILE
Definition: l0stream.cpp:17
~L0Stream()
Definition: l0stream.cpp:36
size_t getBytesLeft()
Obtain the number of bytes between the read/write head and the end of the current stream.
Definition: l0stream.cpp:79
void read(char *buffer, size_t num)
Read from the current stream, switching streams as necessary.
Definition: l0stream.cpp:83
instead the metadata field ProcessingEnvinronment is filled in from the output of a call to the POSIX compliant function uname from within the L1B code A small bug in L1B_Tables an incorrect comparison of RVS coefficients for TEBs to RVS coefficients for RSBs was being made This was replaced with a comparison between TEB coefficients This error never resulted in an incorrect RVS correction but did lead to recalculating the coefficients for each detector in a thermal band even if the coefficients were the same for all detectors To reduce to overall size of the reflective LUT HDF fill values were eliminated from all LUTs previously dimensioned where and where NUM_TIMES is the number of time dependent table pieces In Preprocess a small error where the trailing dropped scan counter was incremented when the leading dropped scan counter should have been was fixed This counter is internal only and is not yet used for any chiefly to casting of were added to make it LINUX compatible Output of code run on LINUX machines displays differences of at most scaled sector incalculable values of the Emissive calibration factor and incalculable values of SV or BB averages was moved outside the loop over frames in Emissive_Cal c since none of these quantities are frame dependent Initialization of b1 and XMS values in Preprocess c routine Process_OBCENG_Emiss was moved inside the detector loops The code was altered so that if up to five scans are dropped between the leading middle or middle trailing the leading or trailing granule will still be used in emissive calibration to form a cross granule average QA bits and are set for a gap between the leading middle and middle trailing granules respectively This may in rare instances lead to a change in emissive calibration coefficients for scans at the beginning or end of a granule A small bug in the Band correction algorithm was corrected an uncertainty value was being checked against an upper bound whereas the proper quantity to be checked was the corresponding which is the product of the Band radiance times the ratio of the Band to Band scaling factors times the LUT correction value for that detector In addition a new LUT which allows for a frame offset with regard to the Band radiance was added A LUT which switches the correction off or on was also added Changes which do not affect scientific output of the the pixel is flagged with the newly created flag and the number of pixels for which this occurs is counted in the QA_common table The array of b1s in Preprocess c was being initialized to outside the loop over which meant that if b1 could not be the value of b1 from the previous band for that scan detector combination was used The initialization was moved inside the band loop Minor code changes were made to eliminate compiler warnings when the code is compiled in bit mode Temperature equations were upgraded to be MODIS AQUA or MODIS TERRA specific and temperature conversion coefficients for AQUA were MOD_PR02 will not cease execution if the value of this parameter is not but will print a message
Definition: HISTORY.txt:644
static void strip_oci_header(std::fstream &fileStream)
Definition: l0stream.cpp:49
char filename[FILENAME_MAX]
Definition: atrem_corl1.h:122
L0Stream(std::vector< std::string > &l0FileNames)
Definition: l0stream.cpp:21
bool hasHeader(std::fstream &fileStream)
Strips the PACE OCI header off of fileStream if it exists, and returns whether it does so or not.
Definition: l0stream.cpp:66
bool eof()
Indicate if fstream::eof is true.
Definition: l0stream.cpp:139
void exit_unhappy(std::string message)
For use when the program has reached a bad state.
Definition: l0stream.cpp:44
#define abs(a)
Definition: misc.h:90
#define PACE_OCI_HDR_LEN
The implementation of a class to simulate a single stream object over multiple OCI L0 files.
Definition: l0stream.cpp:12