NASA Logo
Ocean Color Science Software

ocssw V2022
udunits2unix.cpp
Go to the documentation of this file.
1 #include <timeutils.h>
2 #include <string>
3 #include <boost/algorithm/string.hpp>
4 #include <vector>
5 #include <sstream>
6 #include <iostream>
7 
8 using namespace std;
9 /* ---------------------------------------------------------------
10 
11  Given a string in the format:
12  1. yyyy-mm-dd HH:MM:SSZ
13  2. yyyy-mm-dd HH:MM:SS
14  3. yyyy-mm-dd HH:MM:SS.fffZ
15  4. yyyy-mm-dd HH:MM:SS.fff
16  5. or use a T between date and time
17 
18  covert it into a double.
19 ------------------------------------------------------------------*/
20 double udunits2unix(const string& str) {
21 
22  // split the string into 2, index 0 containing the date and index 1 with the time
23  vector<string> datetime, date, time;
24  boost::split(datetime, str, boost::is_any_of(" T"));
25  if (datetime.size() != 2) {
26  cout << "-E- Error: Udunits needs a space or 'T' between the date and time - " << str << endl;
27  exit(1);
28  }
29 
30  boost::split(date, datetime[0], boost::is_any_of("-"));
31  if (date.size() != 3) {
32  cout << "-E- Error: Udunits needs a '-' between the date fields - " << datetime[0] << endl;
33  exit(1);
34  }
35 
36  boost::split(time, datetime[1], boost::is_any_of(":"));
37  if (time.size() != 3) {
38  cout << "-E- Error: Udunits needs a ':' between the time fields - " << datetime[1] << endl;
39  exit(1);
40  }
41 
42  int year, month, day;
43  int hours, mins;
44  double secs;
45 
46  year = stoi(date[0]);
47  month = stoi(date[1]);
48  day = stoi(date[2]);
49 
50  hours = stoi(time[0]);
51  mins = stoi(time[1]);
52 
53  // seconds may have a Z at the end.
54  // remove the last char if there is a Z
55  if (time[2][time[2].size()-1] == 'Z') {
56  time[2] = time[2].substr(0, time[2].size()-1);
57  }
58  secs = stod(time[2]);
59 
60  double totalSecs = hours * 3600.0 + mins * 60.0 + secs;
61  return ymds2unix((int16_t)year, (int16_t)month, (int16_t)day, totalSecs);
62 }
63 
64 // given a number as a string, if it's a single digit, pad 0 to
65 // the left
66 string padZero(const string& str) {
67  string newStr = str;
68  if (str.size() == 1)
69  newStr = "0" + str;
70  return newStr;
71 }
72 
73 /* ---------------------------------------------------------------
74 
75  Given a unix time as a double, convert it into udunit string:
76  1. yyyy-mm-dd HH:MM:SSZ
77  2. yyyy-mm-dd HH:MM:SS
78 
79  @param time - unix time as a double
80  @param hasZ - inclue the Z or not at the end
81 ------------------------------------------------------------------*/
82 string unix2udunits(double unixtime, bool hasZ) {
83 
84  if (unixtime < 0) {
85  cout << "-E- Error: time can't be less than 0 to convert to udunits in unix2udunits_cpp().\n" << endl;
86  exit(1);
87  }
88 
89  // convert into individual units
90  int16_t year, month, day, hour, min;
91  double sec;
92  unix2ymdhms(unixtime, &year, &month, &day, &hour, &min, &sec);
93 
94  // format the month, day, and time to be 2 digits if it's 1
95  string monthStr = padZero(std::to_string(month));
96  string dayStr = padZero(std::to_string(day));
97  string hourStr = padZero(std::to_string(hour));
98  string minStr = padZero(std::to_string(min));
99  string secStr = padZero(std::to_string((int)sec));
100 
101  // format the time
102  ostringstream udunitStr;
103  udunitStr << year << "-"
104  << monthStr << "-"
105  << dayStr << " "
106  << hourStr << ":"
107  << minStr << ":"
108  << secStr;
109  if (hasZ) {
110  udunitStr << "Z";
111  }
112  return udunitStr.str();
113 }
114 
115 // use the C++ function to do the work for the C version
116 extern "C" double udunits2unix(const char* str) {
117  return udunits2unix((string) str);
118 }
119 
120 // use the C++ function to do the work for the C version
121 extern "C" const char* unix2udunits_c(double unixtime, int hasZ) {
122  bool wantZ;
123  if(hasZ)
124  wantZ = true;
125  else
126  wantZ = false;
127  static string tmpStr = unix2udunits(unixtime, wantZ);
128  return tmpStr.c_str();
129 }
int32_t day
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
const char * unix2udunits_c(double unixtime, int hasZ)
double udunits2unix(const string &str)
string unix2udunits(double unixtime, bool hasZ)
this program makes no use of any feature of the SDP Toolkit that could generate such a then geolocation is calculated at that and then aggregated up to Resolved feature request Bug by adding three new int8 SDSs for each high resolution offsets between the high resolution geolocation and a bi linear interpolation extrapolation of the positions This can be used to reconstruct the high resolution geolocation Resolved Bug by delaying cumulation of gflags until after validation of derived products Resolved Bug by setting Latitude and Longitude to the correct fill resolving to support Near Real Time because they may be unnecessary if use of entrained ephemeris and attitude data is turned resolving bug report Corrected to filter out Aqua attitude records with missing status helping resolve bug MOD_PR03 will still correctly write scan and pixel data that does not depend upon the start time
Definition: HISTORY.txt:248
string padZero(const string &str)
double ymds2unix(short year, short month, short day, double secs)
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
Definition: aerosol.c:136
#define str(s)