OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
EnvsatFile.cpp
Go to the documentation of this file.
1 /*
2  * File: EnvsatFile.cpp
3  * Author: dshea
4  *
5  * Created on November 28, 2012, 1:07 PM
6  */
7 
8 #include "EnvsatFile.h"
9 
10 #include <iomanip>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 
18 #include <timeutils.h>
19 
20 using namespace std;
21 
22 //EnvsatFile::EnvsatFile() {
23 // init("");
24 //}
25 
27  init(orig.filename);
28  mph = new EnvsatMPH(*orig.mph);
29  sph = orig.mph->createSPH();
30  *sph = *(orig.sph);
31 }
32 
33 EnvsatFile::EnvsatFile(const string& filenameStr) {
34  init(filenameStr);
35  mph = new EnvsatMPH(this);
36 }
37 
39  if (fd != -1)
40  close(fd);
41  if (sph)
42  delete sph;
43  if (mph)
44  delete mph;
45 }
46 
47 void EnvsatFile::init(const string& name) {
48  filename = name;
49  fd = -1;
50  mph = NULL;
51  sph = NULL;
52 }
53 
55  filename = src.filename;
56  fd = -1;
57  if (mph)
58  *mph = *(src.mph);
59  else
60  mph = new EnvsatMPH(*(src.mph));
61 
62  if (sph) {
63  free(sph);
64  sph = NULL;
65  }
66 
67  if (src.sph) {
68  sph = mph->createSPH();
69  *sph = *(src.sph);
70  }
71  return *this;
72 }
73 
74 int EnvsatFile::openFile(bool write) {
75  if (fd != -1)
76  close(fd);
77  if (write)
78  fd = creat(filename.c_str(), 0666);
79  else
80  fd = open(filename.c_str(), O_RDONLY);
81  if (fd == -1)
82  return -1;
83  return 0;
84 }
85 
87  int result = mph->readHeader(fd);
88  if (result != 0)
89  return result;
90  sph = mph->createSPH();
91  if (sph == NULL) {
92  printf("Error %s:%s:%d Could not create SPH\n", __FILE__, __func__,
93  __LINE__);
94  exit(1);
95  }
96  return sph->readHeader(fd);
97 }
98 
100  int result = mph->writeHeader(fd);
101  if (result != 0) {
102  return result;
103  }
104  if (sph == NULL) {
105  printf("Error %s:%s:%d Could not create SPH\n", __FILE__, __func__,
106  __LINE__);
107  exit(1);
108  }
109  return sph->writeHeader(fd);
110 }
111 
112 const string& EnvsatFile::getFileName() {
113  return filename;
114 }
115 
116 void EnvsatFile::setFileName(const string& name) {
117  filename = name;
118 }
119 
121  return mph;
122 }
123 
125  return sph;
126 }
127 
129  printf("EnvsatFile-----\n");
130  printf("File Name = %s\n", getFileName().c_str());
131 }
132 
134  print();
135  printf("\n");
136  mph->printRecursive();
137  if (sph != NULL) {
138  printf("\n");
139  sph->printRecursive();
140  }
141 }
142 
144  if (fd != -1) {
145  close(fd);
146  fd = -1;
147  }
148 }
149 
151  if (sph == NULL) {
152  printf("Error %s:%s:%d SPH is NULL\n", __FILE__, __func__, __LINE__);
153  exit(1);
154  }
155 
156  EnvsatDSD* dsd = sph->findFirstDSD('M');
157  if (dsd == NULL) {
158  printf("Error %s:%s:%d Could not find first Measurement DSD\n",
159  __FILE__, __func__, __LINE__);
160  exit(1);
161  }
162  return dsd->getNumDSRs();
163 }
164 
166  if (sph == NULL) {
167  printf("Error %s:%s:%d SPH is NULL\n", __FILE__, __func__, __LINE__);
168  exit(1);
169  }
170  return sph->getSamplesPerLine();
171 }
172 
173 int EnvsatFile::seekData(EnvsatDSD* dsd, int scanLine) {
174  off_t offset = dsd->getDSOffset() + dsd->getDSRSize() * scanLine;
175  off_t result = lseek(fd, offset, SEEK_SET);
176  if (result == -1)
177  return -1;
178  return 0;
179 }
180 
182  return dsr->readData(fd);
183 }
184 
186  return dsr->writeData(fd);
187 }
188 
190  if (mph == NULL) {
191  printf("Error %s:%s:%d mph == NULL\n", __FILE__, __func__, __LINE__);
192  exit(1);
193  }
194  if (sph == NULL) {
195  printf("Error %s:%s:%d sph == NULL\n", __FILE__, __func__, __LINE__);
196  exit(1);
197  }
198 
199  char str[32];
200  int16_t year, month, day, hour, min;
201  double sec;
202 
203  string oldName = mph->getProductName();
204  string newName = oldName.substr(0, 14);
205  unix2ymdhms((double) sph->getFirstLineTime(), &year, &month, &day, &hour, &min,
206  &sec);
207  sprintf(str, "%04d%02d%02d_%02d%02d%02d_", year, month, day, hour, min,
208  (int) sec);
209  newName += str;
210  int deltaT = abs((int) (sph->getLastLineTime() - sph->getFirstLineTime()));
211  sprintf(str, "%08d", deltaT);
212  newName += str;
213  newName += oldName.substr(38, 17);
214  newName += "0000.N1";
215  mph->setProductName(newName);
216 }
virtual EnvsatFile & operator=(const EnvsatFile &src)
Definition: EnvsatFile.cpp:54
virtual int writeData(EnvsatDSR *dsr)
Definition: EnvsatFile.cpp:185
int32_t day
virtual void setFileName(const std::string &name)
Definition: EnvsatFile.cpp:116
These are used to scale the SD before writing it to the HDF4 file The default is and which means the product is not scaled at all Since the product is usually stored as a float inside of this is a way to write the float out as a integer l2prod min
#define NULL
Definition: decode_rs.h:63
virtual int seekData(EnvsatDSD *dsd, int scanLine=0)
Definition: EnvsatFile.cpp:173
virtual int readData(int fin)
Definition: EnvsatDSR.cpp:50
virtual EnvsatSPH * createSPH()
Definition: EnvsatMPH.cpp:84
virtual EnvsatMPH * getMPH()
Definition: EnvsatFile.cpp:120
virtual ~EnvsatFile()
Definition: EnvsatFile.cpp:38
virtual int getNumScans()
Definition: EnvsatFile.cpp:150
virtual int getNumDSRs()
Definition: EnvsatDSD.cpp:70
virtual int64_t getDSRSize()
Definition: EnvsatDSD.cpp:78
virtual void print()
Definition: EnvsatFile.cpp:128
int init(int32_t ipr, int32_t jpr, char *efile, char *pfile)
Definition: proj_report.c:51
virtual int readData(EnvsatDSR *dsr)
Definition: EnvsatFile.cpp:181
virtual int readHeader()
Definition: EnvsatFile.cpp:86
virtual int64_t getDSOffset()
Definition: EnvsatDSD.cpp:54
virtual EnvsatSPH * getSPH()
Definition: EnvsatFile.cpp:124
char filename[FILENAME_MAX]
Definition: atrem_corl1.h:122
virtual void modifyProductName()
Definition: EnvsatFile.cpp:189
virtual int writeData(int fout)
Definition: EnvsatDSR.cpp:64
const char * str
Definition: l1c_msi.cpp:35
virtual int writeHeader()
Definition: EnvsatFile.cpp:99
virtual void printRecursive()
Definition: EnvsatFile.cpp:133
virtual int openFile(bool write=false)
Definition: EnvsatFile.cpp:74
l2prod offset
EnvsatFile(const EnvsatFile &orig)
Definition: EnvsatFile.cpp:26
virtual const std::string & getFileName()
Definition: EnvsatFile.cpp:112
void unix2ymdhms(double usec, int16_t *year, int16_t *mon, int16_t *day, int16_t *hour, int16_t *min, double *sec)
Definition: unix2ymdhms.c:8
#define abs(a)
Definition: misc.h:90
virtual void closeFile()
Definition: EnvsatFile.cpp:143
virtual int getNumPixels()
Definition: EnvsatFile.cpp:165