OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
getDOI.cpp
Go to the documentation of this file.
1 #include <productInfo.h>
2 
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <genutils.h>
6 #include <string>
7 #include <ctype.h>
8 #include <algorithm>
9 
10 #include <pugixml.hpp>
11 
12 using namespace std;
13 using namespace pugi;
14 
15 
16 extern "C" const char* getDOI(const char* platform, const char* sensor,
17  const char* level, const char* suite, const char* version) {
18 
19  static string doiStr;
20 
21  xml_document rootNode;
22  xml_node dacNode;
23  string dacStr;
24 
25  char *dataRoot;
26  if ((dataRoot = getenv("OCDATAROOT")) == NULL) {
27  printf("-E- OCDATAROOT environment variable is not defined.\n");
28  exit(EXIT_FAILURE);
29  }
30  string doiXMLFileName = (string) dataRoot + "/common/doi.xml";
31 
32  xml_parse_result xmlResult = rootNode.load_file(doiXMLFileName.c_str());
33 
34  if (!xmlResult) {
35  printf("-E- %s Line %d: Can not load %s. %s\n", __FILE__, __LINE__,
36  doiXMLFileName.c_str(), xmlResult.description());
37  exit(EXIT_FAILURE);
38  }
39 
40  dacNode = rootNode.child("EID");
41  if (!dacNode) {
42  printf("-E- %s Line %d: could not find EID tag in XML file = %s\n",
43  __FILE__, __LINE__, doiXMLFileName.c_str());
44  exit(EXIT_FAILURE);
45  }
46 
47  dacStr = dacNode.attribute("name").value();
48 
49  string platformStr = platform;
50  transform(platformStr.begin(), platformStr.end(), platformStr.begin(), ::toupper);
51 
52  // OK time to add the exceptions
53  if (platformStr == "ADEOS")
54  platformStr = "ADEOS-I";
55  else if (platformStr == "SUOMI-NPP")
56  platformStr = "NPP";
57 
58  // search for the requested platform name
59  xml_node platformNode = dacNode.find_child_by_attribute("platform", "name", platformStr.c_str());
60  if (!platformNode) // see if platform name is found
61  return NULL;
62 
63  // fix sensor name
64  string sensorStr = sensor;
65  transform(sensorStr.begin(), sensorStr.end(), sensorStr.begin(), ::toupper);
66 
67  // search for the requested sensor name
68  xml_node sensorNode = platformNode.find_child_by_attribute("sensor", "name", sensorStr.c_str());
69  if (!sensorNode)
70  return NULL;
71 
72  // fix the level name
73  string levelStr = level;
74  transform(levelStr.begin(), levelStr.end(), levelStr.begin(), ::toupper);
75 
76  // search for the requested sensor name
77  xml_node levelNode = sensorNode.find_child_by_attribute("level", "name", levelStr.c_str());
78  if (!levelNode)
79  return NULL;
80 
81  // fix the suite name
82  string suiteStr = suite;
83  transform(suiteStr.begin(), suiteStr.end(), suiteStr.begin(), ::toupper);
84 
85  // search for the requested sensor name
86  xml_node suiteNode = levelNode.find_child_by_attribute("suite", "name", suiteStr.c_str());
87  if (!suiteNode)
88  return NULL;
89 
90  // fix the version name
91  string versionStr = version;
92  // delete any minor versions (2022.1 -> 2022)
93  size_t pos = versionStr.find_first_of('.');
94  if (pos != string::npos)
95  versionStr.erase(pos);
96  // delete the first character if it is not a number (R2022 -> 2022)
97  if(!isdigit(versionStr[0]))
98  versionStr.erase(0,1);
99 
100  // search for the requested version name
101  xml_node versionNode = suiteNode.find_child_by_attribute("version", "name", versionStr.c_str());
102  if (!versionNode)
103  return NULL;
104 
105  // see if there is an alias
106  xml_node aliasNode = versionNode.child("alias");
107  if (aliasNode) {
108  doiStr = aliasNode.child_value();
109  } else {
110  doiStr = dacStr + "/" + platformStr + "/" + sensorStr + "/" + levelStr +
111  "/" + suiteStr + "/" + versionStr;
112  }
113 
114  return doiStr.c_str();
115 }
#define NULL
Definition: decode_rs.h:63
float32 * pos
Definition: l1_czcs_hdf.c:35
@ string
#define isdigit(c)
level
Definition: mapgen.py:186
const char * getDOI(const char *platform, const char *sensor, const char *level, const char *suite, const char *version)
Definition: getDOI.cpp:16
version
Definition: setup.py:15