OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
EnvsatUtil.cpp
Go to the documentation of this file.
1 /*
2  * File: EnvsatUtil.cpp
3  * Author: dshea
4  *
5  * Created on November 28, 2012, 1:11 PM
6  */
7 
8 #include "EnvsatUtil.h"
9 
10 #include <stdio.h>
11 #include <math.h>
12 #include <string.h>
13 
14 #include <timeutils.h>
15 
16 using namespace std;
17 
18 const char* monthArray[12] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL",
19  "AUG", "SEP", "OCT", "NOV", "DEC"};
20 
21 string& trim_right(string& s, const string& delimiters) {
22  return s.erase(s.find_last_not_of(delimiters) + 1);
23 }
24 
25 string& trim_left(string& s, const string& delimiters) {
26  return s.erase(0, s.find_first_not_of(delimiters));
27 }
28 
29 string& trim(string& s, const string& delimiters) {
30  return trim_left(trim_right(s, delimiters), delimiters);
31 }
32 
33 void setString(const string &str, char* buffer, unsigned int offset, int size) {
34  int i;
35  for (i = 0; i < (int) str.size(); i++) {
36  if (i >= size)
37  break;
38  buffer[offset + i] = str[i];
39  }
40  for (; i < size; i++) {
41  buffer[offset + i] = ' ';
42  }
43 }
44 
45 void getString(const char* buffer, string &str, unsigned int offset, int size) {
46  str.assign(buffer + offset, size);
47  trim(str);
48 }
49 
50 int getInt(const char* buffer, unsigned int offset, int size) {
51  int val;
52  char formatStr[32];
53 
54  sprintf(formatStr, "%%%dd", size);
55  sscanf(buffer + offset, formatStr, &val);
56  return val;
57 }
58 
59 void setInt(int val, char* buffer, unsigned int offset, int size) {
60  char tmpBuff[size + 32];
61 
62  sprintf(tmpBuff, "%+0*d", size, val);
63  memcpy(buffer + offset, tmpBuff, size);
64 }
65 
66 int64_t getInt64(const char* buffer, unsigned int offset, int size) {
67  long long val;
68  char formatStr[32];
69 
70  sprintf(formatStr, "%%%dlld", size);
71  sscanf(buffer + offset, formatStr, &val);
72  return val;
73 }
74 
75 void setInt64(int64_t val, char* buffer, unsigned int offset, int size) {
76  char tmpBuff[size + 32];
77 
78  sprintf(tmpBuff, "%+0*lld", size, (long long) val);
79  memcpy(buffer + offset, tmpBuff, size);
80 }
81 
82 double getLatLon(const char* buffer, unsigned int offset) {
83  int64_t val = getInt64(buffer, offset, LAT_LON_LENGTH);
84  return ((double) val) * 1.0e-6;
85 }
86 
87 void setLatLon(double val, char* buffer, unsigned int offset) {
88  int64_t i = round(val * 1.0e6);
89  setInt64(i, buffer, offset, LAT_LON_LENGTH);
90 }
91 
92 int getRawInt16(const char* buffer, unsigned int offset) {
93  return (((buffer[offset + 0] & 0xff) << 8) | (buffer[offset + 1] & 0xff));
94 }
95 
96 void setRawInt16(int val, char* buffer, unsigned int offset) {
97  ((unsigned char*) buffer)[offset + 0] = (val >> 8) & 0xff;
98  ((unsigned char*) buffer)[offset + 1] = val & 0xff;
99 }
100 
101 int getRawInt32(const char* buffer, unsigned int offset) {
102  return (((buffer[offset + 0] & 0xff) << 24)
103  | ((buffer[offset + 1] & 0xff) << 16)
104  | ((buffer[offset + 2] & 0xff) << 8) | (buffer[offset + 3] & 0xff));
105 }
106 
107 void setRawInt32(int val, char* buffer, unsigned int offset) {
108  ((unsigned char*) buffer)[offset + 0] = (val >> 24) & 0xff;
109  ((unsigned char*) buffer)[offset + 1] = (val >> 16) & 0xff;
110  ((unsigned char*) buffer)[offset + 2] = (val >> 8) & 0xff;
111  ((unsigned char*) buffer)[offset + 3] = val & 0xff;
112 }
113 
114 double getMJD(const char* buffer, unsigned int offset) {
115  int day, sec, microsec;
116  struct tm trec;
117  time_t secSince;
118 
119  day = getRawInt32(buffer, offset);
120  sec = getRawInt32(buffer, offset + 4);
121  microsec = getRawInt32(buffer, offset + 8);
122 
123  trec.tm_year = 2000 - 1900;
124  trec.tm_mon = 0;
125  trec.tm_mday = day + 1;
126  trec.tm_hour = 0;
127  trec.tm_min = 0;
128  trec.tm_sec = 0;
129  trec.tm_isdst = 0;
130  secSince = mktime(&trec) - gmt_offset();
131 
132  /* */
133  /* Now we total the seconds */
134  /* */
135  return secSince + sec + microsec * 1.0e-6;
136 }
137 
138 void setMJD(int day, int sec, int microsec, char* buffer, unsigned int offset) {
139  setRawInt32(day, buffer, offset);
140  setRawInt32(sec, buffer, offset + 4);
141  setRawInt32(microsec, buffer, offset + 8);
142 }
143 
144 double merisTime2unix(const string& timeStr) {
145  // 02-AUG-2002 18:45:14.058298
146  int16_t year, month, day, hour, min;
147  char monthStr[5];
148  double sec;
149 
150  sscanf(timeStr.c_str(), "%2hd-%3s-%4hd %2hd:%2hd:%lf", &day, monthStr,
151  &year, &hour, &min, &sec);
152  for (month = 0; month < 12; month++) {
153  if (strcmp(monthStr, monthArray[month]) == 0)
154  break;
155  }
156  sec += hour * 3600.0 + min * 60.0;
157  return ymds2unix(year, month + 1, day, sec);
158 }
159 
160 const string& unix2merisTime(double unixTime) {
161  // 02-AUG-2002 18:45:14.058298
162  int16_t year, month, day, hour, min;
163  double sec;
164  char str[32];
165  static string timeStr;
166 
167  unix2ymdhms(unixTime, &year, &month, &day, &hour, &min, &sec);
168  sprintf(str, "%02d-%s-%04d %02d:%02d:%09.6f", day, monthArray[month - 1],
169  year, hour, min, sec);
170  timeStr.assign(str);
171  return timeStr;
172 }
173 
174 double envsatInterp(double x1, double x2, double y1, double y2, double xin) {
175  double m = (y2 - y1) / (x2 - x1);
176  double x = xin - x1;
177  return m * x + y1;
178 }
179 
string & trim_left(string &s, const string &delimiters)
Definition: EnvsatUtil.cpp:25
int32_t day
time_t gmt_offset(void)
Definition: gmt_offset.c:8
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
int64_t getInt64(const char *buffer, unsigned int offset, int size)
Definition: EnvsatUtil.cpp:66
int getRawInt32(const char *buffer, unsigned int offset)
Definition: EnvsatUtil.cpp:101
const char * monthArray[12]
Definition: EnvsatUtil.cpp:18
int getInt(const char *buffer, unsigned int offset, int size)
Definition: EnvsatUtil.cpp:50
void setRawInt16(int val, char *buffer, unsigned int offset)
Definition: EnvsatUtil.cpp:96
double getMJD(const char *buffer, unsigned int offset)
Definition: EnvsatUtil.cpp:114
float tm[MODELMAX]
void setMJD(int day, int sec, int microsec, char *buffer, unsigned int offset)
Definition: EnvsatUtil.cpp:138
void setInt64(int64_t val, char *buffer, unsigned int offset, int size)
Definition: EnvsatUtil.cpp:75
double merisTime2unix(const string &timeStr)
Definition: EnvsatUtil.cpp:144
int getRawInt16(const char *buffer, unsigned int offset)
Definition: EnvsatUtil.cpp:92
void setString(const string &str, char *buffer, unsigned int offset, int size)
Definition: EnvsatUtil.cpp:33
void getString(const char *buffer, string &str, unsigned int offset, int size)
Definition: EnvsatUtil.cpp:45
string & trim(string &s, const string &delimiters)
Definition: EnvsatUtil.cpp:29
void setRawInt32(int val, char *buffer, unsigned int offset)
Definition: EnvsatUtil.cpp:107
const char * str
Definition: l1c_msi.cpp:35
void setLatLon(double val, char *buffer, unsigned int offset)
Definition: EnvsatUtil.cpp:87
double envsatInterp(double x1, double x2, double y1, double y2, double xin)
Definition: EnvsatUtil.cpp:174
const string & unix2merisTime(double unixTime)
Definition: EnvsatUtil.cpp:160
data_t s[NROOTS]
Definition: decode_rs.h:75
double ymds2unix(short year, short month, short day, double secs)
string & trim_right(string &s, const string &delimiters)
Definition: EnvsatUtil.cpp:21
double getLatLon(const char *buffer, unsigned int offset)
Definition: EnvsatUtil.cpp:82
l2prod offset
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
void setInt(int val, char *buffer, unsigned int offset, int size)
Definition: EnvsatUtil.cpp:59
int i
Definition: decode_rs.h:71
msiBandIdx val
Definition: l1c_msi.cpp:34