COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
Data Management function declarations
Collaboration diagram for Data Management function declarations:

Classes

struct  filestruc
 

Macros

#define DATA_LOG_TYPE_SOH   0
 
#define DATA_LOG_TYPE_EVENT   1
 
#define DATA_LOG_TYPE_BEACON   2
 
#define DATA_LOG_TYPE_PROGRAM   3
 

Functions

void log_write (string node, string agent, double utc, string extra, string type, string record, string location)
 Write log entry - full. More...
 
void log_write (string node, string agent, double utc, string type, const char *record)
 Write log entry - fixed location. More...
 
void log_write (string node, int type, double utc, const char *record, string directory)
 Write log entry - fixed location, no extra, integer type and agent. More...
 
void log_move (string oldpath, string newpath, bool compress)
 Move log file - path version. More...
 
void log_move (string node, string agent, string srclocation, string dstlocation, bool compress)
 Move log file - full version. More...
 
void log_move (string node, string agent)
 Move log file - short version. More...
 
vector< double > data_list_archive_days (string node, string agent)
 Get a list of days in a Node archive. More...
 
string log_read (gzFile &file, int num)
 log_reads Loop until the newline character is seen. Append it to a string and return the line once the newline is met. More...
 
vector< filestrucdata_list_archive (string node, string agent, double utc, string type)
 Get a list of files in a Node archive. More...
 
vector< filestrucdata_list_archive (string node, string agent, double utc)
 
vector< filestrucdata_list_files (string directory)
 Get list of files in a directory, directly. More...
 
size_t data_list_files (string directory, vector< filestruc > &files)
 Get list of files in a directory, indirectly. More...
 
vector< filestrucdata_list_files (string node, string location, string agent)
 Get list of files in a Node, directly. More...
 
size_t data_list_files (string node, string location, string agent, vector< filestruc > &files)
 Get list of files in a Node, indirectly. More...
 
vector< string > data_list_nodes ()
 Get list of Nodes, directly. More...
 
int32_t data_list_nodes (vector< string > &nodes)
 Get list of Nodes, indirectly. More...
 
int32_t data_get_nodes (vector< cosmosstruc > &node)
 Get vector of Node structures. More...
 
string data_name (string node, double mjd, string extra, string type)
 Create data file name. More...
 
string data_name (string node, double mjd, string type)
 
int32_t data_name_date (string node, string filename, uint16_t &year, uint16_t &jday, uint32_t &seconds)
 Get date from file name. More...
 
int32_t data_name_date (string node, string filename, double &utc)
 Get date from file name. More...
 
string data_base_path (string node, string location, string agent, string filename)
 Create data file path. More...
 
string data_base_path (string node, string location, string agent)
 
string data_base_path (string node, string location)
 
string data_base_path (string node)
 
string data_archive_path (string node, string agent, double mjd)
 
string data_type_path (string node, string location, string agent, double mjd, string type)
 Create data file path. More...
 
string data_type_path (string node, string location, string agent, double mjd, string extra, string type)
 Create data file path. More...
 
string data_name_path (string node, string location, string agent, double mjd, string name)
 Create data file path. More...
 
string data_resource_path (string name)
 Create resource file path. More...
 
bool data_exists (string &path)
 Check existence of path. More...
 
FILE * data_open (string path, const char *mode)
 Open file from path. More...
 
int32_t set_cosmosroot (string name, bool create_flag)
 Set Root Directory. More...
 
int32_t set_cosmosroot (bool create_flag)
 Find Root Directory. More...
 
string get_cosmosroot (bool create_flag)
 Return COSMOS Root Directory. More...
 
int32_t get_cosmosroot (string &result, bool create_flag)
 Return COSMOS Root Directory. More...
 
int32_t set_cosmosresources (string name, bool create_flag)
 Set Resources Directory. More...
 
int32_t set_cosmosresources (bool create_flag)
 Find Resources Directory. More...
 
string get_cosmosresources (bool create_flag)
 Return COSMOS Resources Directory. More...
 
int32_t get_cosmosresources (string &result, bool create_flag)
 Return COSMOS Resources Directory. More...
 
int32_t setEnvCosmosResources (string path)
 Set Environment Variable for COSMOS resources. More...
 
int32_t setEnvCosmosNodes (string path)
 Set Environment Variable for COSMOS nodes. More...
 
int32_t setEnv (string var, string path)
 Set Environment Variable for COSMOS. More...
 
int32_t setEnvCosmos (string path)
 
int32_t set_cosmosnodes (string name, bool create_flag)
 Set Nodes Directory. More...
 
int32_t set_cosmosnodes (bool create_flag)
 Find COSMOS Nodes Directory on Windows, Linux or MacOS. More...
 
string get_cosmosnodes (bool create_flag)
 Return COSMOS Nodes Directory. More...
 
int32_t get_cosmosnodes (string &result, bool create_flag)
 Get COSMOS Nodes Directory. More...
 
string get_nodedir (string node, bool create_flag)
 Get Current Node Directory. More...
 
int32_t data_load_archive (string node, string agent, double utcbegin, double utcend, string type, vector< string > &result)
 Load data from archive. More...
 
int32_t data_load_archive (string node, string agent, double mjd, string type, vector< string > &result)
 
int32_t data_load_archive (double mjd, vector< string > &telem, vector< string > &event, cosmosstruc *cinfo)
 
double findlastday (string name)
 Find last day in archive. More...
 
double findfirstday (string name)
 Find first day in archive. More...
 
int32_t kml_write (cosmosstruc *cinfo)
 Add to KML path. More...
 
bool data_isdir (string path)
 
bool data_ischardev (string path)
 
bool data_isfile (string path, off_t size)
 
double data_ctime (string path)
 
off_t data_size (string path)
 
int32_t data_execute (string cmd, string &result, string shell)
 
void log_reopen ()
 
int check_events (eventstruc *events, int max, cosmosstruc *data)
 

Detailed Description

Macro Definition Documentation

#define DATA_LOG_TYPE_SOH   0
#define DATA_LOG_TYPE_EVENT   1
#define DATA_LOG_TYPE_BEACON   2
#define DATA_LOG_TYPE_PROGRAM   3

Function Documentation

void log_write ( string  node,
string  agent,
double  utc,
string  extra,
string  type,
string  record,
string  location 
)

Write log entry - full.

Append the provided string to a file in the {node}/{location}/{agent} directory. The file name is created as {node}_yyyyjjjsssss_{extra}.{type}

Parameters
nodeNode name.
locationLocation name.
agentAgent name.
utcUTC to be converted to year (yyyy), julian day (jjj) and seconds (sssss).
extraExtra part of name.
typeType part of name.
recordString to be appended to file.
76 {
77  FILE *fout;
78  string path;
79 
80  if (utc == 0.)
81  return;
82 
83  if (extra.empty())
84  {
85  path = data_type_path(node, location, agent, utc, type);
86  }
87  else
88  {
89  path = data_type_path(node, location, agent, utc, extra, type);
90  }
91 
92  if (location == "immediate")
93  {
94  fout = data_open(path, const_cast<char *>("w"));
95  }
96  else
97  {
98  fout = data_open(path, const_cast<char *>("a+"));
99  }
100  if (fout != nullptr)
101  {
102  fprintf(fout,"%s\n",record.c_str());
103  fclose(fout);
104  }
105 }
FILE * data_open(string path, const char *mode)
Open file from path.
Definition: datalib.cpp:1019
string data_type_path(string node, string location, string agent, double mjd, string type)
Create data file path.
Definition: datalib.cpp:910
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
static string node
Definition: agent_monitor.cpp:126
void log_write ( string  node,
string  agent,
double  utc,
string  type,
const char *  record 
)

Write log entry - fixed location.

Append the provided string to a file in the {node}/temp/{agent} directory. The file name is created as {node}_yyyyjjjsssss_{extra}.{type}

Parameters
nodeNode name.
agentAgent name.
utcUTC to be converted to year (yyyy), julian day (jjj) and seconds (sssss).
extraExtra part of name.
typeType part of name.
recordString to be appended to file.Write log entry - fixed location, no extra

Append the provided string to a file in the {node}/temp/{agent} directory. The file name is created as {node}_yyyyjjjsssss.{type}

Parameters
nodeNode name.
agentAgent name.
utcUTC to be converted to year (yyyy), julian day (jjj) and seconds (sssss).
typeType part of name.
recordString to be appended to file.
132 {
133  log_write(node, agent, utc, "", type, record);
134  // FILE *fout;
135  // string path;
136 
137  // if (utc == 0.)
138  // return;
139 
140  // path = data_type_path(node, "temp", agent, utc, type);
141 
142  // if ((fout = data_open(path, (char *)"a+")) != nullptr)
143  // {
144  // fprintf(fout,"%s\n",record);
145  // fclose(fout);
146  // }
147 }
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
void log_write(string node, string agent, double utc, string extra, string type, string record, string location)
Write log entry - full.
Definition: datalib.cpp:75
static string node
Definition: agent_monitor.cpp:126
void log_write ( string  node,
int  type,
double  utc,
const char *  record,
string  directory 
)

Write log entry - fixed location, no extra, integer type and agent.

Append the provided string to a file in the {node}/temp/{agent_name} directory. The file name is created as {node}_yyyyjjjsssss.{type_name}

Parameters
nodeNode name.
typeInteger specifying what type and agent of file.
utcUTC to be converted to year (yyyy), julian day (jjj) and seconds (sssss).
recordString to be appended to file.
158 {
159  // FILE *fout;
160  // string path;
161 
162  // if (utc == 0.)
163  // return;
164 
165  switch (type)
166  {
167  case DATA_LOG_TYPE_SOH:
168  log_write(node, "soh", utc, "", "telemetry", record);
169  // path = data_type_path(node, "temp", "soh", utc, "telemetry");
170  break;
171  case DATA_LOG_TYPE_EVENT:
172  log_write(node, "soh", utc, "", "event", record);
173  // path = data_type_path(node, "temp", "soh", utc, "event");
174  break;
176  log_write(node, "beacon", utc, "", "beacon", record);
177  // path = data_type_path(node, "temp", "beacon", utc, "beacon");
178  break;
179  default:
180  log_write(node, "soh", utc, "", "log", record);
181  // path = data_type_path(node, "temp", "soh", utc, "log");
182  break;
183  }
184 
185  // if ((fout = data_open(path, (char *)"a+")) != nullptr)
186  // {
187  // fprintf(fout,"%s\n",record);
188  // fclose(fout);
189  // }
190 }
#define DATA_LOG_TYPE_EVENT
Definition: datalib.h:109
#define DATA_LOG_TYPE_BEACON
Definition: datalib.h:110
void log_write(string node, string agent, double utc, string extra, string type, string record, string location)
Write log entry - full.
Definition: datalib.cpp:75
#define DATA_LOG_TYPE_SOH
Definition: datalib.h:108
static string node
Definition: agent_monitor.cpp:126
void log_move ( string  oldpath,
string  newpath,
bool  compress 
)

Move log file - path version.

Move files previously created with log_write to their final location, optionally compressing with gzip. The path version accepts only specific paths, from and to, and whether compression should be used.

Parameters
oldpathPath to move from.
newpathPath to move to.
compressWether or not to compress with gzip.
201 {
202  if (compress)
203  {
204  char buffer[8192];
205  string temppath = oldpath + ".gz";
206  newpath += ".gz";
207  FILE *fin = data_open(oldpath, "rb");
208  FILE *fout = data_open(temppath, "wb");
209  gzFile gzfout;
210  gzfout = gzdopen(fileno(fout), "a");
211 
212  do
213  {
214  unsigned nbytes = (unsigned)fread(buffer, 1, 8192, fin);
215  if (nbytes)
216  {
217  gzwrite(gzfout, buffer, nbytes);
218  }
219  } while (!feof(fin));
220 
221  fclose(fin);
222  gzclose_w(gzfout);
223  rename(temppath.c_str(), newpath.c_str());
224  remove(temppath.c_str());
225  }
226  else
227  {
228  rename(oldpath.c_str(), newpath.c_str());
229  }
230  remove(oldpath.c_str());
231 }
FILE * data_open(string path, const char *mode)
Open file from path.
Definition: datalib.cpp:1019
int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
Definition: gzwrite.c:165
static char buffer[255]
Definition: propagator_simple.cpp:60
gzFile ZEXPORT gzdopen(int fd, const char *mode)
Definition: gzlib.c:284
int ZEXPORT gzclose_w(gzFile file)
Definition: gzwrite.c:539
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:62
Definition: zlib.h:1670
void log_move ( string  node,
string  agent,
string  srclocation,
string  dstlocation,
bool  compress 
)

Move log file - full version.

Move files previously created with log_write to their final location, optionally compressing with gzip. The full version allows for specification of the source and destination locations, and whether compression should be used. The routine will find all files currently in {node}/{srclocation}/{agent} and move them to {node}/{dstlocation}/{agent}.

Parameters
nodeNode name.
agentAgent name.
srclocationSource location name.
dstlocationDestination location name.
compressWether or not to compress with gzip.
245 {
246 // char buffer[8192];
247  vector<filestruc> oldfiles;
248  data_list_files(node, srclocation, agent, oldfiles);
249  for (auto oldfile: oldfiles)
250  {
251  log_move(oldfile.path, data_base_path(node, dstlocation, agent, oldfile.name), compress);
252 // string oldpath = oldfile.path;
253 
254 // if (compress)
255 // {
256 // string temppath = oldfile.path + ".gz";
257 // string newpath = data_base_path(node, dstlocation, agent, oldfile.name + ".gz");
258 // FILE *fin = data_open(oldpath, "rb");
259 // FILE *fout = data_open(temppath, "wb");
260 // gzFile gzfout;
261 // gzfout = gzdopen(fileno(fout), "a");
262 
263 // do
264 // {
265 // unsigned nbytes = (unsigned)fread(buffer, 1, 8192, fin);
266 // if (nbytes)
267 // {
268 // gzwrite(gzfout, buffer, nbytes);
269 // }
270 // } while (!feof(fin));
271 
272 // fclose(fin);
273 // gzclose_w(gzfout);
274 // rename(temppath.c_str(), newpath.c_str());
275 // remove(temppath.c_str());
276 // }
277 // else
278 // {
279 // string newpath = data_base_path(node, dstlocation, agent, oldfile.name);
280 // rename(oldpath.c_str(), newpath.c_str());
281 // }
282 // remove(oldpath.c_str());
283  }
284 }
void log_move(string oldpath, string newpath, bool compress)
Move log file - path version.
Definition: datalib.cpp:200
vector< filestruc > data_list_files(string directory)
Get list of files in a directory, directly.
Definition: datalib.cpp:461
string data_base_path(string node, string location, string agent, string filename)
Create data file path.
Definition: datalib.cpp:767
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:62
static string node
Definition: agent_monitor.cpp:126
void log_move ( string  node,
string  agent 
)

Move log file - short version.

Move files previously created with log_write to their final location. The short version assumes a source location of "temp" and a destination locations of "outgoing". The routine will find all files currently in {node}/temp/{agent} and move them to {node}/outgoing/{agent}.

Parameters
nodeNode name.
agentAgent name.
295 {
296  log_move(node, agent, "temp", "outgoing", true);
297 }
void log_move(string oldpath, string newpath, bool compress)
Move log file - path version.
Definition: datalib.cpp:200
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
static string node
Definition: agent_monitor.cpp:126
vector< double > data_list_archive_days ( string  node,
string  agent 
)

Get a list of days in a Node archive.

Generate a list of days available in the archive of the indicated Node and Agent. The result is returned as a vector of Modified Julian Days.

304 {
305  vector <double> days;
306 
307  // Check Base Path
308  string bpath = data_base_path(node, "data", agent);
309  DIR *jdp;
310  if ((jdp=opendir(bpath.c_str())) != nullptr)
311  {
312  struct dirent *td;
313  while ((td=readdir(jdp)) != nullptr)
314  {
315  // Check Year Path
316  if (td->d_name[0] != '.' && atof(td->d_name) > 1900 && atof(td->d_name) < 3000)
317  {
318  string ypath = (bpath + "/") + td->d_name;
319  DIR *jdp;
320  if ((jdp=opendir(ypath.c_str())) != nullptr)
321  {
322  double year = atof(td->d_name);
323  struct dirent *td;
324  while ((td=readdir(jdp)) != nullptr)
325  {
326  // Check Day Path
327  if (td->d_name[0] != '.' && atof(td->d_name) > 0 && atof(td->d_name) < 367)
328  {
329  string dpath = (ypath + "/") + td->d_name;
330  // struct stat st;
331  // stat(dpath.c_str(), &st);
332  // if (S_ISDIR(st.st_mode))
333  if (data_isdir(dpath))
334  {
335  double jday = atof(td->d_name);
336  double mjd = cal2mjd((int)year, 1, 0.) + jday;
337  days.push_back(mjd);
338  }
339  }
340  }
341  closedir(jdp);
342  }
343  }
344  }
345  closedir(jdp);
346  }
347  sort(days.begin(), days.end());
348  return days;
349 }
int closedir(DIR *dir)
Definition: dirent.c:74
Definition: dirent.c:24
string data_base_path(string node, string location, string agent, string filename)
Create data file path.
Definition: datalib.cpp:767
Definition: dirent.h:21
DIR * opendir(const char *name)
Definition: dirent.c:32
struct dirent * readdir(DIR *dir)
Definition: dirent.c:97
char * d_name
Definition: dirent.h:23
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
bool data_isdir(string path)
Definition: datalib.cpp:1865
double cal2mjd(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, int32_t second, int32_t nsecond)
Calendar representation to Modified Julian Day - full.
Definition: timelib.cpp:294
double mjd
Definition: udp_send.cpp:41
static string node
Definition: agent_monitor.cpp:126
string log_read ( gzFile file,
int  num 
)

log_reads Loop until the newline character is seen. Append it to a string and return the line once the newline is met.

Use this function to loop through the gzfile until eof is reached. It returns each string line by line in the log file.

Parameters
fileThe file to be read.
numThe maximum number of characters to be written to the buffer.
Returns
Each string in the gzfile.
357  {
358  string buffer;
359  buffer.resize(num);
360  string line;
361 
362  while (!(line.back() == '\n')) {
363  gzgets(file, const_cast <char *>(buffer.data()), num);
364  line.append(buffer);
365  }
366 
367  if (!gzeof(file)) {
368  return line;
369  }
370 
371  gzclose(file);
372 
373  return "";
374 }
int ZEXPORT gzeof(gzFile file)
Definition: gzlib.c:511
int ZEXPORT gzclose(gzFile file)
Definition: gzclose.c:11
static char buffer[255]
Definition: propagator_simple.cpp:60
char *ZEXPORT gzgets(gzFile file, char *buf, int len)
Definition: gzread.c:483
vector< filestruc > data_list_archive ( string  node,
string  agent,
double  utc,
string  type 
)

Get a list of files in a Node archive.

Generate a list of archived files for the indicated Node, Agent, and UTC. The result is returned as a vector of filestruc, one entry for each file found.

Parameters
nodeNode to search.
agentSubdirectory of location to search.
utcDay in archive as MJD.
typeFile extension.
Returns
A C++ vector of filestruc. Zero size if no files are found.
386 {
387  vector<filestruc> files;
388 
389  string dtemp;
390  DIR *jdp;
391  struct dirent *td;
392  filestruc tf;
393 
394  tf.node = node;
395  tf.agent = agent;
396  dtemp = data_archive_path(node, agent, utc);
397  if ((jdp=opendir(dtemp.c_str())) != nullptr)
398  {
399  while ((td=readdir(jdp)) != nullptr)
400  {
401  if (td->d_name[0] != '.')
402  {
403  tf.name = td->d_name;
404  tf.path = dtemp + "/" + tf.name;
405  struct stat st;
406  stat(tf.path.c_str(), &st);
407  tf.size = st.st_size;
408  // Not looking for a specific type, or specifically looking for directory
409  if (S_ISDIR(st.st_mode) && (type.empty() || type == "directory"))
410  {
411  tf.type = "directory";
412  }
413  else
414  {
415  for (size_t i=strlen(td->d_name)-1; i<strlen(td->d_name); --i)
416  {
417  if (td->d_name[i] == '.')
418  {
419  tf.type = &td->d_name[i+1];
420  break;
421  }
422  }
423  }
424  // Not looking for a specific type, or found type we were looking for
425  if (type.empty() || tf.type == type)
426  {
427  int32_t iretn = data_name_date(tf.node, tf.name, tf.year, tf.jday, tf.seconds);
428  if (iretn == 0)
429  {
430  tf.utc = cal2mjd(tf.year, 1, tf.seconds/86400.) + tf.jday;
431  files.push_back(tf);
432  for (size_t i=files.size()-1; i>0; --i)
433  {
434  if (files[i].utc < files[i-1].utc)
435  {
436  tf = files[i-1];
437  files[i-1] = files[i];
438  files[i] = tf;
439  }
440  }
441  }
442  }
443  }
444  }
445  closedir(jdp);
446  }
447  return files;
448 }
string name
Definition: datalib.h:117
off_t size
Definition: datalib.h:120
uint16_t year
Definition: datalib.h:121
uint32_t seconds
Definition: datalib.h:123
int closedir(DIR *dir)
Definition: dirent.c:74
int i
Definition: rw_test.cpp:37
string node
Definition: datalib.h:115
int iretn
Definition: rw_test.cpp:37
Definition: dirent.c:24
double utc
Definition: datalib.h:124
Definition: dirent.h:21
DIR * opendir(const char *name)
Definition: dirent.c:32
struct dirent * readdir(DIR *dir)
Definition: dirent.c:97
Definition: datalib.h:113
string path
Definition: datalib.h:119
char * d_name
Definition: dirent.h:23
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
double cal2mjd(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, int32_t second, int32_t nsecond)
Calendar representation to Modified Julian Day - full.
Definition: timelib.cpp:294
#define S_ISDIR(ST_MODE)
Definition: datalib.cpp:56
uint16_t jday
Definition: datalib.h:122
string data_archive_path(string node, string agent, double mjd)
Definition: datalib.cpp:871
string agent
Definition: datalib.h:116
static string node
Definition: agent_monitor.cpp:126
int32_t data_name_date(string node, string filename, uint16_t &year, uint16_t &jday, uint32_t &seconds)
Get date from file name.
Definition: datalib.cpp:722
string type
Definition: datalib.h:118
vector< filestruc > data_list_archive ( string  node,
string  agent,
double  utc 
)
451 {
452  return data_list_archive(node, agent, utc, "");
453 }
vector< filestruc > data_list_archive(string node, string agent, double utc, string type)
Get a list of files in a Node archive.
Definition: datalib.cpp:385
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
static string node
Definition: agent_monitor.cpp:126
vector< filestruc > data_list_files ( string  directory)

Get list of files in a directory, directly.

Generate a list of files for the indicated directory. The result is returned as a vector of filestruc, one entry for each file found.

Parameters
directoryDirectory to search.
Returns
A C++ vector of filestruc. Zero size if no files are found.
462 {
463  vector<filestruc> files;
464 
465  data_list_files(directory, files);
466 
467  return files;
468 }
vector< filestruc > data_list_files(string directory)
Get list of files in a directory, directly.
Definition: datalib.cpp:461
size_t data_list_files ( string  directory,
vector< filestruc > &  files 
)

Get list of files in a directory, indirectly.

Generate a list of files for the indicated directory. The result is returned as a vector of filestruc, one entry for each file found. Repeated calls to this function will append entries.

Parameters
directoryDirectory to search.
Returns
Number of files found, otherwise negative error.
478 {
479  DIR *jdp;
480  struct dirent *td;
481  filestruc tf;
482 
483  tf.node = "";
484  tf.agent = "";
485  if ((jdp=opendir(directory.c_str())) != nullptr)
486  {
487  while ((td=readdir(jdp)) != nullptr)
488  {
489  if (td->d_name[0] != '.')
490  {
491  tf.name = td->d_name;
492  tf.path = directory + "/" + tf.name;
493  vector <string> parts = string_split(directory, "/");
494  if (parts.size() > 2)
495  {
496  tf.agent = parts[parts.size()-1];
497  tf.node = parts[parts.size()-3];
498  }
499  struct stat st;
500  stat(tf.path.c_str(), &st);
501  tf.size = st.st_size;
502  tf.utc = unix2utc((double)st.st_ctime);
503  if (S_ISDIR(st.st_mode))
504  {
505  tf.type = "directory";
506  }
507  else
508  {
509  for (size_t i=strlen(td->d_name)-1; i<strlen(td->d_name); --i)
510  {
511  if (td->d_name[i] == '.')
512  {
513  tf.type = &td->d_name[i+1];
514  break;
515  }
516  }
517  }
518  files.push_back(tf);
519  for (size_t i=files.size()-1; i>1; --i)
520  {
521  if (files[i].name < files[i-1].name)
522  {
523  filestruc tfile = files[i-1];
524  files[i-1] = files[i];
525  files[i] = tfile;
526  }
527  }
528  }
529  }
530  closedir(jdp);
531  }
532 
533  return files.size();
534 }
string name
Definition: datalib.h:117
off_t size
Definition: datalib.h:120
int closedir(DIR *dir)
Definition: dirent.c:74
int i
Definition: rw_test.cpp:37
string node
Definition: datalib.h:115
Definition: dirent.c:24
double unix2utc(struct timeval unixtime)
Unix time to UTC.
Definition: timelib.cpp:120
double utc
Definition: datalib.h:124
Definition: dirent.h:21
DIR * opendir(const char *name)
Definition: dirent.c:32
struct dirent * readdir(DIR *dir)
Definition: dirent.c:97
Definition: datalib.h:113
string path
Definition: datalib.h:119
char * d_name
Definition: dirent.h:23
string name
Definition: cubesat2obj.cpp:6
#define S_ISDIR(ST_MODE)
Definition: datalib.cpp:56
string agent
Definition: datalib.h:116
string type
Definition: datalib.h:118
vector< string > string_split(string in, string delimeters)
Parse a string.
Definition: stringlib.cpp:47
vector< filestruc > data_list_files ( string  node,
string  location,
string  agent 
)

Get list of files in a Node, directly.

Generate a list of files for the indicated Node, location (eg. incoming, outgoing, ...), and Agent. The result is returned as a vector of filestruc, one entry for each file found.

Parameters
nodeNode to search.
locationSubdirectory of Node to search.
agentSubdirectory of location to search.
Returns
A C++ vector of filestruc. Zero size if no files are found.
545 {
546  vector<filestruc> files;
547 
548  data_list_files(node, location, agent, files);
549 
550  return files;
551 }
vector< filestruc > data_list_files(string directory)
Get list of files in a directory, directly.
Definition: datalib.cpp:461
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
static string node
Definition: agent_monitor.cpp:126
size_t data_list_files ( string  node,
string  location,
string  agent,
vector< filestruc > &  files 
)

Get list of files in a Node, indirectly.

Generate a list of files for the indicated Node, location (eg. incoming, outgoing, ...), and Agent. The result is returned as a vector of filestruc, one entry for each file found. Repeated calls to this function will append entries.

Parameters
nodeNode to search.
locationSubdirectory of Node to search.
agentSubdirectory of location to search.
filesList of filestruc.
Returns
Number of files found, otherwise negative error.
564 {
565  string dtemp;
566  dtemp = data_base_path(node, location, agent);
567 // size_t fcnt = files.size();
568  data_list_files(dtemp, files);
569 // for (size_t i=fcnt; i<files.size(); ++i)
570 // {
571 // files[i].agent = agent;
572 // files[i].node = node;
573 // }
574 
575  return (files.size());
576 }
vector< filestruc > data_list_files(string directory)
Get list of files in a directory, directly.
Definition: datalib.cpp:461
string data_base_path(string node, string location, string agent, string filename)
Create data file path.
Definition: datalib.cpp:767
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
static string node
Definition: agent_monitor.cpp:126
vector< string > data_list_nodes ( )

Get list of Nodes, directly.

Scan the COSMOS root directory and return the name of each Node that is found.

Returns
Any Nodes that are found.
584 {
585  vector<string> nodes;
586 
587  data_list_nodes(nodes);
588 
589  return nodes;
590 }
vector< string > data_list_nodes()
Get list of Nodes, directly.
Definition: datalib.cpp:583
std::vector< cosmosstruc > nodes
Definition: agent_data-2-0.cpp:57
int32_t data_list_nodes ( vector< string > &  nodes)

Get list of Nodes, indirectly.

Scan the COSMOS root directory and return the name of each Node that is found. Repeated calls to this function will append entries.

Parameters
nodesVector of strings with Node names.
Returns
Zero or negative error.
599 {
600  DIR *jdp;
601  string dtemp;
602  string rootd;
603  struct dirent *td;
604  string tnode;
605  // struct stat statbuf;
606 
607  int32_t iretn = get_cosmosnodes(rootd);
608  if (iretn < 0)
609  {
610  return iretn;
611  }
612 
613  dtemp = rootd;
614  if ((jdp=opendir(dtemp.c_str())) != nullptr)
615  {
616  while ((td=readdir(jdp)) != nullptr)
617  {
618  // if (td->d_name[0] != '.' && !stat(((dtemp+"/")+td->d_name).c_str(), &statbuf) && S_ISDIR(statbuf.st_mode))
619  if (td->d_name[0] != '.' && data_isdir((dtemp+"/")+td->d_name))
620  {
621  tnode = td->d_name;
622  nodes.push_back(tnode);
623  }
624  }
625  closedir(jdp);
626  }
627  return 0;
628 }
int closedir(DIR *dir)
Definition: dirent.c:74
int iretn
Definition: rw_test.cpp:37
Definition: dirent.c:24
Definition: dirent.h:21
DIR * opendir(const char *name)
Definition: dirent.c:32
struct dirent * readdir(DIR *dir)
Definition: dirent.c:97
string get_cosmosnodes(bool create_flag)
Return COSMOS Nodes Directory.
Definition: datalib.cpp:1532
char * d_name
Definition: dirent.h:23
std::vector< cosmosstruc > nodes
Definition: agent_data-2-0.cpp:57
bool data_isdir(string path)
Definition: datalib.cpp:1865
int32_t data_get_nodes ( vector< cosmosstruc > &  node)

Get vector of Node structures.

Scan the COSMOS root directory and return a cosmosstruc for each Node that is found.

Parameters
nodeVector of cosmosstruc for each Node.
Returns
Zero or negative error.
637 {
638  DIR *jdp;
639  string dtemp;
640  string rootd;
641  struct dirent *td;
642  cosmosstruc *tnode;
643 
644  int32_t iretn = get_cosmosnodes(rootd);
645  if (iretn < 0)
646  {
647  return iretn;
648  }
649 
650  if ((tnode=json_init()) == nullptr)
651  {
652  return (NODE_ERROR_NODE);
653  }
654 
655  dtemp = rootd;
656  if ((jdp=opendir(dtemp.c_str())) != nullptr)
657  {
658  while ((td=readdir(jdp)) != nullptr)
659  {
660  if (td->d_name[0] != '.')
661  {
662  string nodepath = td->d_name;
663  if (!json_setup_node(nodepath, tnode))
664  {
665  node.push_back(*tnode);
666  }
667  }
668  }
669  closedir(jdp);
670  }
671  return 0;
672 }
int closedir(DIR *dir)
Definition: dirent.c:74
int32_t json_setup_node(jsonnode json, cosmosstruc *cinfo, bool create_flag)
Setup JSON Namespace using Node description JSON.
Definition: jsonlib.cpp:6869
int iretn
Definition: rw_test.cpp:37
Definition: dirent.c:24
cosmosstruc * json_init()
Initialize JSON pointer map.
Definition: jsonlib.cpp:454
Definition: dirent.h:21
DIR * opendir(const char *name)
Definition: dirent.c:32
struct dirent * readdir(DIR *dir)
Definition: dirent.c:97
string get_cosmosnodes(bool create_flag)
Return COSMOS Nodes Directory.
Definition: datalib.cpp:1532
char * d_name
Definition: dirent.h:23
static string node
Definition: agent_monitor.cpp:126
Definition: jsondef.h:4199
#define NODE_ERROR_NODE
Definition: cosmos-errno.h:143
string data_name ( string  node,
double  mjd,
string  extra,
string  type 
)

Create data file name.

Builds a filename up from the date of creation and its type. Format is: yyyyjjjsssss_extra.type, where yyyy is the Year, jjj is the Julian Day, sssss is the Seconds, and type is any accepted COSMOS file type (eg. log, event, telemetry, message, command.)

Parameters
nodeNode name.
mjdUTC of creation date in Modified Julian Day
extraExtra part of file name.
typeAny valid extension type
Returns
Filename string, otherwise nullptr
686 {
687  string name;
688  char ntemp[100];
689 
690  int year, month, seconds;
691  double jday, day;
692 
693  mjd2ymd(mjd,year,month,day,jday);
694  seconds = (int)(86400.*(jday-(int)jday));
695  sprintf(ntemp,"_%04d%03d%05d",year,(int32_t)jday,seconds);
696  if (extra.empty())
697  {
698  name = node + ntemp + "." + type;
699  }
700  else
701  {
702  name = node + ntemp + "_" + extra + "." + type;
703  }
704  return (name);
705 }
int32_t mjd2ymd(double mjd, int32_t &year, int32_t &month, double &day)
MJD to Year, Month, and Decimal Day (overloaded)
Definition: timelib.cpp:217
string name
Definition: cubesat2obj.cpp:6
double mjd
Definition: udp_send.cpp:41
static string node
Definition: agent_monitor.cpp:126
string data_name ( string  node,
double  mjd,
string  type 
)
708 {
709  return data_name(node, mjd, "", type);
710 }
string data_name(string node, double mjd, string extra, string type)
Create data file name.
Definition: datalib.cpp:685
double mjd
Definition: udp_send.cpp:41
static string node
Definition: agent_monitor.cpp:126
int32_t data_name_date ( string  node,
string  filename,
uint16_t &  year,
uint16_t &  jday,
uint32_t &  seconds 
)

Get date from file name.

Assuming the COSMOS standard filename format from data_name, extract the date portion and return it as year, julian day and seconds.

Parameters
nodeName of Node.
filenameName of File.
yearHolder for integer year.
jdayHolder for integer julian day.
secondsHolder for integer julian seconds.
Returns
0 or negative error.
723 {
724  if (sscanf(filename.substr(node.size()+1).c_str(), "%4" SCNu16 "%3" SCNu16 "%5" SCNu32 "", &year, &jday, &seconds) == 3 && seconds < 86400 && jday < 367)
725  {
726  return 0;
727  }
728  else
729  {
730  return DATA_ERROR_FORMAT;
731  }
732 }
#define DATA_ERROR_FORMAT
Definition: cosmos-errno.h:152
static string node
Definition: agent_monitor.cpp:126
int32_t data_name_date ( string  node,
string  filename,
double &  utc 
)

Get date from file name.

Assuming the COSMOS standard filename format from data_name, extract the date portion and return it as a Modified Julian Day.

Parameters
nodeName of Node.
filenameName of File.
utcHolder for returned utc.
Returns
0 or negative error.
743 {
744  uint16_t year;
745  uint16_t jday;
746  uint32_t seconds;
747 
748  int32_t iretn = data_name_date(node, filename, year, jday, seconds);
749 
750  if (!iretn)
751  {
752  utc = cal2mjd(year, 1, seconds/86400.) + jday;
753  return 0;
754  }
755  else
756  {
757  return iretn;
758  }
759 }
int iretn
Definition: rw_test.cpp:37
double cal2mjd(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, int32_t second, int32_t nsecond)
Calendar representation to Modified Julian Day - full.
Definition: timelib.cpp:294
static string node
Definition: agent_monitor.cpp:126
int32_t data_name_date(string node, string filename, uint16_t &year, uint16_t &jday, uint32_t &seconds)
Get date from file name.
Definition: datalib.cpp:722
string data_base_path ( string  node,
string  location,
string  agent,
string  filename 
)

Create data file path.

Create a full path to the named file based on the provided information. The path will be of the form {cosmosnodes}/{node}/{location}/{agent}/{filename}, or in the case of a "data" location, {cosmosnodes}/{node}/{location}/{agent}/{yyyy}/{jjj}/{filename}. If {cosmosnodes} is not defined, or cannot be found, it will be left blank.

768 {
769  string path;
770  string tpath;
771 
772  tpath = data_base_path(node, location, agent);
773 
774  if (!tpath.empty())
775  {
776  if (location == "data")
777  {
778  uint16_t year;
779  uint16_t jday;
780  uint32_t seconds;
781  int32_t iretn = data_name_date(node, filename, year, jday, seconds);
782  if (!iretn)
783  {
784  char tbuf[10];
785  sprintf(tbuf, "/%04u", year);
786  tpath += tbuf;
787  if (COSMOS_MKDIR(tpath.c_str(),00777) == 0 || errno == EEXIST)
788  {
789  sprintf(tbuf, "/%03u", jday);
790  tpath += tbuf;
791  if (COSMOS_MKDIR(tpath.c_str(),00777) == 0 || errno == EEXIST)
792  {
793  path = tpath;
794  }
795  }
796  }
797  }
798  else
799  {
800  path = tpath;
801  }
802 
803  path += "/" + filename;
804  }
805  return path;
806 }
int iretn
Definition: rw_test.cpp:37
string data_base_path(string node, string location, string agent, string filename)
Create data file path.
Definition: datalib.cpp:767
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
static string node
Definition: agent_monitor.cpp:126
int32_t data_name_date(string node, string filename, uint16_t &year, uint16_t &jday, uint32_t &seconds)
Get date from file name.
Definition: datalib.cpp:722
string data_base_path ( string  node,
string  location,
string  agent 
)
809 {
810  string tpath;
811  string path;
812 
813  tpath = data_base_path(node, location);
814  if (!tpath.empty())
815  {
816  if (agent.empty())
817  {
818  path = tpath;
819  }
820  else
821  {
822  tpath += "/" + agent;
823  if (COSMOS_MKDIR(tpath.c_str(),00777) == 0 || errno == EEXIST)
824  {
825  path = tpath;
826  }
827  }
828  }
829  return path;
830 
831 }
string data_base_path(string node, string location, string agent, string filename)
Create data file path.
Definition: datalib.cpp:767
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
static string node
Definition: agent_monitor.cpp:126
string data_base_path ( string  node,
string  location 
)
834 {
835  string tpath;
836  string path;
837 
838  tpath = data_base_path(node);
839  if (!tpath.empty())
840  {
841  tpath += "/" + location;
842  if (COSMOS_MKDIR(tpath.c_str(),00777) == 0 || errno == EEXIST)
843  {
844  path = tpath;
845  }
846  }
847  return path;
848 
849 }
string data_base_path(string node, string location, string agent, string filename)
Create data file path.
Definition: datalib.cpp:767
static string node
Definition: agent_monitor.cpp:126
string data_base_path ( string  node)
852 {
853  string tpath;
854  string path="";
855 
856  int32_t iretn = get_cosmosnodes(tpath);
857  if (iretn >= 0)
858  {
859  tpath += "/" + node;
860 
861  if (COSMOS_MKDIR(tpath.c_str(),00777) == 0 || errno == EEXIST)
862  {
863 
864  path = tpath;
865  }
866  }
867  return path;
868 
869 }
int iretn
Definition: rw_test.cpp:37
string get_cosmosnodes(bool create_flag)
Return COSMOS Nodes Directory.
Definition: datalib.cpp:1532
static string node
Definition: agent_monitor.cpp:126
string data_archive_path ( string  node,
string  agent,
double  mjd 
)
872 {
873  string tpath;
874  char ntemp[COSMOS_MAX_NAME+1];
875  string path;
876 
877  tpath = data_base_path(node, "data", agent);
878  if (!tpath.empty())
879  {
880  int year, month;
881  double jday, day;
882  mjd2ymd(mjd,year,month,day,jday);
883  sprintf(ntemp, "/%04d", year);
884  tpath += ntemp;
885  if (COSMOS_MKDIR(tpath.c_str(),00777) == 0 || errno == EEXIST)
886  {
887  sprintf(ntemp, "/%03d", (int32_t)jday);
888  tpath += ntemp;
889  if (COSMOS_MKDIR(tpath.c_str(),00777) == 0 || errno == EEXIST)
890  {
891  path = tpath;
892  }
893  }
894  }
895 
896  return path;
897 
898 }
#define COSMOS_MAX_NAME
Largest JSON name.
Definition: cosmos-defs.h:55
string data_base_path(string node, string location, string agent, string filename)
Create data file path.
Definition: datalib.cpp:767
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
int32_t mjd2ymd(double mjd, int32_t &year, int32_t &month, double &day)
MJD to Year, Month, and Decimal Day (overloaded)
Definition: timelib.cpp:217
double mjd
Definition: udp_send.cpp:41
static string node
Definition: agent_monitor.cpp:126
string data_type_path ( string  node,
string  location,
string  agent,
double  mjd,
string  type 
)

Create data file path.

Build a path to a data file using its filename and the current Node directory.

Parameters
nodeNode directory in cosmosroot.
locationSubfolder in Node directory (outgoing, incoming, data, temp).
agentTask specific subfolder of location, if relevant
mjdUTC of creation date in Modified Julian Day
typeAny valid extension type
Returns
File path string, otherwise nullptr
911 {
912  string path;
913 
914  path = data_type_path(node, location, agent, mjd, "", type);
915 
916  return (path);
917 }
string data_type_path(string node, string location, string agent, double mjd, string type)
Create data file path.
Definition: datalib.cpp:910
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
double mjd
Definition: udp_send.cpp:41
static string node
Definition: agent_monitor.cpp:126
string data_type_path ( string  node,
string  location,
string  agent,
double  mjd,
string  extra,
string  type 
)

Create data file path.

Build a path to a data file using its filename and the current Node directory.

Parameters
nodeNode directory in cosmosroot.
locationSubfolder in Node directory (outgoing, incoming, data, temp).
agentTask specific subfolder of location, if relevant
mjdUTC of creation date in Modified Julian Day
extraExtra text to add to the full name.
typeAny valid extension type
Returns
File path string, otherwise nullptr
931 {
932  string path;
933  string tpath;
934 
935  tpath = data_name_path(node, location, agent, mjd, data_name(node, mjd, extra, type));
936 
937  if (!tpath.empty())
938  {
939  path = tpath;
940  }
941  return path;
942 }
string data_name(string node, double mjd, string extra, string type)
Create data file name.
Definition: datalib.cpp:685
string data_name_path(string node, string location, string agent, double mjd, string name)
Create data file path.
Definition: datalib.cpp:954
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
double mjd
Definition: udp_send.cpp:41
static string node
Definition: agent_monitor.cpp:126
string data_name_path ( string  node,
string  location,
string  agent,
double  mjd,
string  name 
)

Create data file path.

Build a path to a data file using its filename and the current Node directory.

Parameters
nodeNode directory in cosmosroot.
locationSubfolder in Node directory (outgoing, incoming, data, temp).
agentTask specific subfolder of location, if relevant
mjdUTC of creation date in Modified Julian Day
nameFile name.
Returns
File path string, otherwise nullptr
955 {
956  string path;
957  string tpath;
958 
959  if (location == "data")
960  {
961  tpath = data_archive_path(node, agent, mjd);
962  }
963  else
964  {
965  tpath = data_base_path(node, location, agent);
966  }
967 
968  if (!tpath.empty())
969  {
970  tpath += "/" + name;
971  path = tpath;
972  }
973  return path;
974 
975 }
string data_base_path(string node, string location, string agent, string filename)
Create data file path.
Definition: datalib.cpp:767
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
string name
Definition: cubesat2obj.cpp:6
double mjd
Definition: udp_send.cpp:41
string data_archive_path(string node, string agent, double mjd)
Definition: datalib.cpp:871
static string node
Definition: agent_monitor.cpp:126
string data_resource_path ( string  name)

Create resource file path.

Build a path to a resource file using its filename and the current resource directory.

Parameters
nameFile name.
Returns
File path string, otherwise nullptr
984 {
985  string path;
986 
987  path = get_cosmosresources() + "/" + name;
988  if (data_isfile(path))
989  {
990  return path;
991  }
992  else
993  {
994  return "";
995  }
996 }
string get_cosmosresources(bool create_flag)
Return COSMOS Resources Directory.
Definition: datalib.cpp:1337
bool data_isfile(string path, off_t size)
Definition: datalib.cpp:1895
string name
Definition: cubesat2obj.cpp:6
bool data_exists ( string &  path)

Check existence of path.

Check whether a path exists, within the limits of permissions.

Parameters
pathstring containing full path.
Returns
TRUE or FALSE
1004 {
1005  struct stat buffer;
1006  return (stat (path.c_str(), &buffer) == 0);
1007 }
static char buffer[255]
Definition: propagator_simple.cpp:60
FILE * data_open ( string  path,
const char *  mode 
)

Open file from path.

Attempt to open a file with an optional path. If a path is included, each directory element will be created if it is missing. The final file will be opened with the requested mode.

Parameters
pathFull path to file, absolute or relative.
modefopen style mode.
Returns
fopen style file handle, or nullptr if either a directory element can not be created, or the file can not be opened.
1020 {
1021  char dtemp[1024];
1022  uint32_t index, dindex, length;
1023  FILE *tfd;
1024 
1025  length = (uint32_t)path.size();
1026  for (dindex=length-1; dindex<length; --dindex)
1027  {
1028  if (path[dindex] == '/')
1029  break;
1030  }
1031 
1032  if (dindex > 0 && dindex < length)
1033  {
1034  for (index=1; index<=dindex; ++index)
1035  {
1036  if (path[index] == '/')
1037  {
1038  strncpy(dtemp, path.c_str(), index+1);
1039  dtemp[index+1] = 0;
1040  if (COSMOS_MKDIR(dtemp,00777))
1041  {
1042  if (errno != EEXIST)
1043  return (nullptr);
1044  }
1045  }
1046  }
1047  }
1048 
1049  if ((tfd = fopen(path.c_str(),mode)) != nullptr)
1050  {
1051  return (tfd);
1052  }
1053 
1054  return (nullptr);
1055 }
png_uint_32 length
Definition: png.c:2173
uint32_t dindex
Definition: agent_node.cpp:68
int32_t set_cosmosroot ( string  name,
bool  create_flag 
)

Set Root Directory.

Set the internal variable that points to where all COSMOS files are stored.

Parameters
nameAbsolute or relative pathname of directory.
create_flagCreate directory if not already present.
Returns
Zero, or negative error.
1065 {
1066  cosmosroot.clear();
1067  for (size_t i=0; i<name.length(); ++i)
1068  {
1069  if (name[i] == '\\')
1070  {
1071  name.replace(i, 1, "/");
1072  }
1073  }
1074  if (data_isdir(name))
1075  {
1076  cosmosroot = name;
1077  return 0;
1078  }
1079  else
1080  {
1081  if (create_flag)
1082  {
1083  if (COSMOS_MKDIR(name.c_str(), 00777) == 0 || errno == EEXIST)
1084  {
1085  cosmosroot = name;
1086  return 0;
1087  }
1088  else
1089  {
1090  return -errno;
1091  }
1092  }
1093  else
1094  {
1096  }
1097  }
1098 }
int i
Definition: rw_test.cpp:37
#define DATA_ERROR_RESOURCES_FOLDER
Definition: cosmos-errno.h:153
static string cosmosroot
Path to COSMOS root directory.
Definition: datalib.cpp:44
bool data_isdir(string path)
Definition: datalib.cpp:1865
string name
Definition: cubesat2obj.cpp:6
int32_t set_cosmosroot ( bool  create_flag)

Find Root Directory.

Set the internal variable that points to where all COSMOS resource files are stored. This checks, in succession:

  • a folder named "root" in a path specified by the COSMOS environment variable
  • the path specified by the COSMOSRESOURCES environment variable
  • "~/cosmos/root" (Unix); "c:\cosmos\root" (Windows)
  • up to 6 levels above the current directory, first in "cosmosroot", and then in "root".
    Returns
    Zero, or negative error.
1110 {
1111  string croot;
1112  char *troot;
1113  int32_t iretn;
1114 
1115  if (cosmosroot.empty())
1116  {
1117  // if the COSMOS environment variable was set, use it
1118  if ((troot = getenv("COSMOS")) != nullptr)
1119  {
1120  croot = troot;
1121  if (!set_cosmosroot(croot, create_flag))
1122  {
1123  return 0;
1124  }
1125  }
1126 
1127  // No environment variables set. Look in standard location.
1128 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_MAC_OS)
1129  // default path on macOS and linux is home folder for running user
1130 
1131  if ((troot = getenv("HOME")) != nullptr)
1132  {
1133  croot = troot + (string)"/cosmos";
1134  }
1135  else
1136  {
1137  croot = "";
1138  return (DATA_ERROR_ROOT_FOLDER);
1139  }
1140 #endif
1141 
1142 #ifdef COSMOS_WIN_OS
1143  croot = "c:/cosmos";
1144 #endif
1145  if (!set_cosmosroot(croot, false))
1146  {
1147  return 0;
1148  }
1149 
1150  // No standard location. Search upward for "cosmosroot"
1151  croot = "cosmos";
1152  for (size_t i=0; i<6; i++)
1153  {
1154  if (!set_cosmosroot(croot, false))
1155  {
1156  return 0;
1157  }
1158  croot = "../" + croot;
1159  }
1160  }
1161 
1162  // if cosmosroot is still empty then try to create standard location, otherwise fail
1163  if (cosmosroot.empty())
1164  {
1165  if (create_flag)
1166  {
1167 #ifdef COSMOS_LINUX_OS
1168  if ((troot = getenv("HOME")) != nullptr)
1169  {
1170  croot = troot + (string)"/cosmos";
1171  }
1172  else
1173  {
1174  croot = "";
1175  return (DATA_ERROR_ROOT_FOLDER);
1176  }
1177 #endif
1178 
1179 #ifdef COSMOS_MAC_OS
1180  croot = "/Applications/cosmos";
1181 #endif
1182 
1183 #ifdef COSMOS_WIN_OS
1184  croot = "c:/cosmos";
1185 #endif
1186  if ((iretn=set_cosmosroot(croot, create_flag)) == 0)
1187  {
1188  return 0;
1189  }
1190  else
1191  {
1192  return iretn;
1193  }
1194  }
1195  else
1196  {
1197  return (DATA_ERROR_ROOT_FOLDER);
1198  }
1199  }
1200  return 0;
1201 }
int32_t set_cosmosroot(string name, bool create_flag)
Set Root Directory.
Definition: datalib.cpp:1064
int i
Definition: rw_test.cpp:37
int iretn
Definition: rw_test.cpp:37
static string cosmosroot
Path to COSMOS root directory.
Definition: datalib.cpp:44
#define DATA_ERROR_ROOT_FOLDER
Definition: cosmos-errno.h:155
string get_cosmosroot ( bool  create_flag)

Return COSMOS Root Directory.

Get the internal variable that points to where all COSMOS Resource files are stored. Initialize variable if this is the first call to the function.

Parameters
create_flagCreate directory if not already present.
Returns
Length of string, otherwise negative error.
1210 {
1211  string result;
1212  get_cosmosroot(result, create_flag);
1213  return result;
1214 }
string get_cosmosroot(bool create_flag)
Return COSMOS Root Directory.
Definition: datalib.cpp:1209
int32_t get_cosmosroot ( string &  result,
bool  create_flag 
)

Return COSMOS Root Directory.

Get the internal variable that points to where all COSMOS Resource files are stored. Initialize variable if this is the first call to the function.

Parameters
resultFull path to Root directory.
create_flagCreate directory if not already present.
Returns
Length of string, otherwise negative error.
1224 {
1225  int32_t iretn;
1226 
1227  result.clear();
1228  if (cosmosroot.empty())
1229  {
1230  iretn = set_cosmosroot(create_flag);
1231  if (iretn < 0)
1232  {
1233  return iretn;
1234  }
1235  }
1236  result = cosmosroot;
1237  return 0;
1238 }
int32_t set_cosmosroot(string name, bool create_flag)
Set Root Directory.
Definition: datalib.cpp:1064
int iretn
Definition: rw_test.cpp:37
static string cosmosroot
Path to COSMOS root directory.
Definition: datalib.cpp:44
int32_t set_cosmosresources ( string  name,
bool  create_flag 
)

Set Resources Directory.

Set the internal variable that points to where all COSMOS resource files are stored.

Parameters
nameAbsolute or relative pathname of directory.
create_flagCreate directory if not already present.
Returns
Zero, or negative error.
1249 {
1250  cosmosresources.clear();
1251  for (size_t i=0; i<name.length(); ++i)
1252  {
1253  if (name[i] == '\\')
1254  {
1255  name.replace(i, 1, "/");
1256  }
1257  }
1258  if (data_isdir(name))
1259  {
1261  return 0;
1262  }
1263  else
1264  {
1265  if (create_flag)
1266  {
1267  if (COSMOS_MKDIR(name.c_str(), 00777) == 0 || errno == EEXIST)
1268  {
1270  return 0;
1271  }
1272  else
1273  {
1274  return -errno;
1275  }
1276  }
1277  else
1278  {
1280  }
1281  }
1282 }
int i
Definition: rw_test.cpp:37
#define DATA_ERROR_RESOURCES_FOLDER
Definition: cosmos-errno.h:153
bool data_isdir(string path)
Definition: datalib.cpp:1865
string name
Definition: cubesat2obj.cpp:6
static string cosmosresources
Path to COSMOS Resources directory.
Definition: datalib.cpp:48
int32_t set_cosmosresources ( bool  create_flag)

Find Resources Directory.

Set the internal variable that points to where all COSMOS resource files are stored. This checks, in succession:

  • a folder named "resources" in a path specified by the COSMOS environment variable
  • the path specified by the COSMOSRESOURCES environment variable
  • "~/cosmos/resources" (Unix); "c:\cosmos\resources" (Windows)
  • up to 6 levels above the current directory, first in "cosmosresources", and then in "resources".
    Returns
    Zero, or negative error.
1294 {
1295  string croot;
1296  char *troot;
1297  int32_t iretn;
1298 
1299  if (cosmosresources.empty())
1300  {
1301  if ((troot = getenv("COSMOSRESOURCES")) != nullptr)
1302  {
1303  croot = troot;
1304  if ((iretn=set_cosmosresources(croot, create_flag)) == 0)
1305  {
1306  return 0;
1307  }
1308  else
1309  {
1310  return iretn;
1311  }
1312  }
1313  else
1314  {
1315  if ((iretn = set_cosmosroot(create_flag)) == 0)
1316  {
1317  croot = cosmosroot + "/resources";
1318  iretn = set_cosmosresources(croot, create_flag);
1319  return iretn;
1320  }
1321  else
1322  {
1323  return iretn;
1324  }
1325  }
1326  }
1327 
1328  return 0;
1329 }
int32_t set_cosmosroot(string name, bool create_flag)
Set Root Directory.
Definition: datalib.cpp:1064
int iretn
Definition: rw_test.cpp:37
static string cosmosroot
Path to COSMOS root directory.
Definition: datalib.cpp:44
int32_t set_cosmosresources(string name, bool create_flag)
Set Resources Directory.
Definition: datalib.cpp:1248
static string cosmosresources
Path to COSMOS Resources directory.
Definition: datalib.cpp:48
string get_cosmosresources ( bool  create_flag)

Return COSMOS Resources Directory.

Get the internal variable that points to where all COSMOS Resource files are stored. Initialize variable if this is the first call to the function.

Parameters
create_flagCreate directory if not already present.
Returns
Length of string, otherwise negative error.
1338 {
1339  string result="";
1340  get_cosmosresources(result, create_flag);
1341  return result;
1342 }
string get_cosmosresources(bool create_flag)
Return COSMOS Resources Directory.
Definition: datalib.cpp:1337
int32_t get_cosmosresources ( string &  result,
bool  create_flag 
)

Return COSMOS Resources Directory.

Get the internal variable that points to where all COSMOS Resource files are stored. Initialize variable if this is the first call to the function.

Parameters
resultFull path to Resources directory.
create_flagCreate directory if not already present.
Returns
Length of string, otherwise negative error.
1352 {
1353  int32_t iretn;
1354 
1355  result.clear();
1356  if (cosmosresources.empty())
1357  {
1358  iretn = set_cosmosresources(create_flag);
1359  if (iretn < 0)
1360  {
1361  return iretn;
1362  }
1363  }
1364 
1365  result = cosmosresources;
1366  return 0;
1367 }
int iretn
Definition: rw_test.cpp:37
int32_t set_cosmosresources(string name, bool create_flag)
Set Resources Directory.
Definition: datalib.cpp:1248
static string cosmosresources
Path to COSMOS Resources directory.
Definition: datalib.cpp:48
int32_t setEnvCosmosResources ( string  path)

Set Environment Variable for COSMOS resources.

Parameters
pathFull path of the COSMOS resources folder.
Returns
Zero, or negative error.
1373  {
1374 
1375  return setEnv("COSMOSRESOURCES", path);
1376 }
int32_t setEnv(string var, string path)
Set Environment Variable for COSMOS.
Definition: datalib.cpp:1393
int32_t setEnvCosmosNodes ( string  path)

Set Environment Variable for COSMOS nodes.

Parameters
pathFull path of the COSMOS nodes folder.
Returns
Zero, or negative error.
1383  {
1384 
1385  return setEnv("COSMOSNODES", path);
1386 }
int32_t setEnv(string var, string path)
Set Environment Variable for COSMOS.
Definition: datalib.cpp:1393
int32_t setEnv ( string  var,
string  path 
)

Set Environment Variable for COSMOS.

Parameters
varenvironment variable to set (ex. COSMOSRESOURCES)
pathFull path of the COSMOS variable folder.
Returns
Zero, or negative error.
1393  {
1394 
1395  uint32_t iretn;
1396 
1397 #ifdef COSMOS_WIN_OS
1398  // windows
1399  iretn = _putenv_s(var.c_str(),path.c_str());
1400 #else
1401  // mac, linux
1402  iretn = setenv(var.c_str(),
1403  path.c_str(),1);
1404 #endif
1405  char *pathReturned = getenv(var.c_str());
1406  if (pathReturned == nullptr)
1407  {
1409  }
1410 
1411  // if (pathReturned!=NULL){
1412  // std::cout << var << " set to " << pathReturned << std::endl;
1413  // } else {
1414  // std::cout << var << " not set " << std::endl;
1415  // return DATA_ERROR_RESOURCES_FOLDER;
1416  // }
1417 
1418  return iretn;
1419 }
#define DATA_ERROR_RESOURCES_FOLDER
Definition: cosmos-errno.h:153
int iretn
Definition: rw_test.cpp:37
int32_t setEnvCosmos ( string  path)

Set Environment Variable for COSMOS Automatically These variables are just temporarily created while the program runs.

Parameters
pathfull path of the COSMOS variable folder.
Returns
Zero, or negative error.
1427  {
1428 
1429  uint32_t iretn;
1430 
1431  iretn = setEnv("COSMOSRESOURCES", path + "resources");
1432  iretn = setEnv("COSMOSNODES", path + "nodes");
1433 
1434  return iretn;
1435 }
int iretn
Definition: rw_test.cpp:37
int32_t setEnv(string var, string path)
Set Environment Variable for COSMOS.
Definition: datalib.cpp:1393
int32_t set_cosmosnodes ( string  name,
bool  create_flag 
)

Set Nodes Directory.

Set the internal variable that points to where all COSMOS resource files are stored.

Parameters
nameAbsolute or relative pathname of directory.
create_flagCreate directory if not already present.
Returns
Zero, or negative error.
1446 {
1447  cosmosnodes.clear();
1448  for (size_t i=0; i<name.length(); ++i)
1449  {
1450  if (name[i] == '\\')
1451  {
1452  name.replace(i, 1, "/");
1453  }
1454  }
1455  if (data_isdir(name))
1456  {
1457  cosmosnodes = name;
1458  return 0;
1459  }
1460  else
1461  {
1462  if (create_flag)
1463  {
1464  if (COSMOS_MKDIR(name.c_str(), 00777) == 0 || errno == EEXIST)
1465  {
1466  cosmosnodes = name;
1467  return 0;
1468  }
1469  else
1470  {
1471  return -errno;
1472  }
1473  }
1474  else
1475  {
1476  return DATA_ERROR_NODES_FOLDER;
1477  }
1478  }
1479 }
#define DATA_ERROR_NODES_FOLDER
Definition: cosmos-errno.h:154
int i
Definition: rw_test.cpp:37
bool data_isdir(string path)
Definition: datalib.cpp:1865
string name
Definition: cubesat2obj.cpp:6
static string cosmosnodes
Path to COSMOS Nodes directory.
Definition: datalib.cpp:46
int32_t set_cosmosnodes ( bool  create_flag)

Find COSMOS Nodes Directory on Windows, Linux or MacOS.

Set the internal variable that points to where all COSMOS node files are stored. This either uses the value in COSMOSNODES, or looks for the directory up to 6 levels above the current directory, first in "cosmosnodes", and then in "nodes".

Returns
Zero, or negative error.
1488 {
1489  string croot;
1490  char *troot;
1491  int32_t iretn;
1492 
1493  if (cosmosnodes.empty())
1494  {
1495  // find environment variable COSMOSNODES
1496  if ((troot = getenv("COSMOSNODES")) != nullptr)
1497  {
1498  croot = troot;
1499  if ((iretn=set_cosmosnodes(croot, create_flag)) == 0)
1500  {
1501  return 0;
1502  }
1503  else
1504  {
1505  return iretn;
1506  }
1507  }
1508  else // look for the default path: $COSMOS/nodes or $HOME/cosmos/nodes
1509  {
1510  if ((iretn = set_cosmosroot(create_flag)) == 0)
1511  {
1512  croot = cosmosroot + "/nodes";
1513  iretn = set_cosmosnodes(croot, create_flag);
1514  return iretn;
1515  }
1516  else
1517  {
1518  return iretn;
1519  }
1520  }
1521  }
1522 
1523  return 0;
1524 }
int32_t set_cosmosroot(string name, bool create_flag)
Set Root Directory.
Definition: datalib.cpp:1064
int32_t set_cosmosnodes(string name, bool create_flag)
Set Nodes Directory.
Definition: datalib.cpp:1445
int iretn
Definition: rw_test.cpp:37
static string cosmosroot
Path to COSMOS root directory.
Definition: datalib.cpp:44
static string cosmosnodes
Path to COSMOS Nodes directory.
Definition: datalib.cpp:46
string get_cosmosnodes ( bool  create_flag)

Return COSMOS Nodes Directory.

Get the internal variable that points to where all COSMOS Node files are stored. Initialize variable if this is the first call to the function.

Parameters
create_flagCreate directory if not already present.
Returns
Length of string, otherwise negative error.
1533 {
1534  string result="";
1535  get_cosmosnodes(result, create_flag);
1536  return result;
1537 }
string get_cosmosnodes(bool create_flag)
Return COSMOS Nodes Directory.
Definition: datalib.cpp:1532
int32_t get_cosmosnodes ( string &  result,
bool  create_flag 
)

Get COSMOS Nodes Directory.

Get the internal variable that points to where all COSMOS files are stored.

Parameters
resultString to place path in.
create_flagCreate directory if not already present.
Returns
Zero, or negative error.
1547 {
1548  int32_t iretn;
1549 
1550  result.clear();
1551  if (cosmosnodes.empty())
1552  {
1553  iretn = set_cosmosnodes(create_flag);
1554  if (iretn < 0)
1555  {
1556  // std::cerr << "error " << DATA_ERROR_NODES_FOLDER << ": could not find cosmos/nodes folder" << std::endl;
1557  return iretn;
1558  }
1559  }
1560 
1561  result = cosmosnodes;
1562  return 0;
1563 }
int32_t set_cosmosnodes(string name, bool create_flag)
Set Nodes Directory.
Definition: datalib.cpp:1445
int iretn
Definition: rw_test.cpp:37
static string cosmosnodes
Path to COSMOS Nodes directory.
Definition: datalib.cpp:46
string get_nodedir ( string  node,
bool  create_flag 
)

Get Current Node Directory.

Get the internal variable that points to where node files are stored for the current Node.

Parameters
nodeName of current Node
create_flagWhether or not to create node directory if it doesn't already exist.
Returns
Pointer to character string containing path to Node, otherwise nullptr.
1573 {
1574  nodedir.clear();
1575  if (!set_cosmosnodes(create_flag))
1576  {
1577  nodedir = cosmosnodes + "/" + node;
1578 
1579  // if the node folder does not exist
1580  if (!data_isdir(nodedir))
1581  {
1582  // if the create folder flag is not on then
1583  // exit this function without a nodedir
1584  if (!create_flag)
1585  {
1586  nodedir.clear();
1587  }
1588  else // let's create the node directory (good for on the fly nodes)
1589  {
1590  if (COSMOS_MKDIR(nodedir.c_str(),00777) != 0)
1591  {
1592  nodedir.clear();
1593  }
1594  }
1595  } else {
1596  // node folder exists
1597  }
1598  }
1599 
1600  // if the node folder exists or was created let's return the path
1601  return (nodedir);
1602 }
int32_t set_cosmosnodes(string name, bool create_flag)
Set Nodes Directory.
Definition: datalib.cpp:1445
bool data_isdir(string path)
Definition: datalib.cpp:1865
static string nodedir
Path to current COSMOS Node directory.
Definition: datalib.cpp:50
static string node
Definition: agent_monitor.cpp:126
static string cosmosnodes
Path to COSMOS Nodes directory.
Definition: datalib.cpp:46
int32_t data_load_archive ( string  node,
string  agent,
double  utcbegin,
double  utcend,
string  type,
vector< string > &  result 
)

Load data from archive.

Load JSON entries of specified type from data archive for specified Node and Agent. Will return all data that is available within specified date range, in files {COSMOSNODES}/{Node}/date/{Agent}/{yyyy}/{ddd}/{*}.type.

Parameters
nodeName of Node.
agentName of Agent.
utcbeginStarting UTC.
utcendEnding UTC.
typeType extension.
resultVector of JSON strings.
Returns
0 or negative error.
1617 {
1618  std::ifstream tfd;
1619  string tstring;
1620  vector <filestruc> files;
1621 
1622 
1623  result.clear();
1624 
1625  for (double mjd = floor(utcbegin); mjd <= floor(utcend); ++mjd)
1626  {
1627  files = data_list_archive(node, agent, mjd, type);
1628  for (size_t i=0; i<files.size(); ++i)
1629  {
1630  if (mjd == floor(utcbegin) && i < files.size()-2 && files[i+1].utc < utcbegin)
1631  {
1632  continue;
1633  }
1634  else if (mjd == floor(utcend) && files[i].utc > utcend)
1635  {
1636  continue;
1637  }
1638 
1639  tfd.open(files[i].path);
1640  if (tfd.is_open())
1641  {
1642  while (std::getline(tfd,tstring))
1643  {
1644  result.push_back(tstring);
1645  }
1646  tfd.close();
1647  }
1648  }
1649  }
1650  return 0;
1651 }
int i
Definition: rw_test.cpp:37
double utcend
Definition: fast_contacts.cpp:65
vector< filestruc > data_list_archive(string node, string agent, double utc, string type)
Get a list of files in a Node archive.
Definition: datalib.cpp:385
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
double mjd
Definition: udp_send.cpp:41
static string node
Definition: agent_monitor.cpp:126
int32_t data_load_archive ( string  node,
string  agent,
double  mjd,
string  type,
vector< string > &  result 
)
1654 {
1655  int32_t iretn;
1656  iretn = data_load_archive(node, agent, floor(mjd), floor(mjd)+.999999, type, result);
1657  return iretn;
1658 }
int iretn
Definition: rw_test.cpp:37
int32_t data_load_archive(string node, string agent, double utcbegin, double utcend, string type, vector< string > &result)
Load data from archive.
Definition: datalib.cpp:1616
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
double mjd
Definition: udp_send.cpp:41
static string node
Definition: agent_monitor.cpp:126
int32_t data_load_archive ( double  mjd,
vector< string > &  telem,
vector< string > &  event,
cosmosstruc cinfo 
)
1661 {
1662  int32_t iretn;
1663 
1664  iretn = data_load_archive(cinfo->node.name, "soh", mjd, "telemetry", telem);
1665  if (iretn < 0)
1666  {
1667  return iretn;
1668  }
1669  iretn = data_load_archive(cinfo->node.name, "soh", mjd, "event", event);
1670 
1671  return iretn;
1672 }
int iretn
Definition: rw_test.cpp:37
int32_t data_load_archive(string node, string agent, double utcbegin, double utcend, string type, vector< string > &result)
Load data from archive.
Definition: datalib.cpp:1616
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
char name[40+1]
Node Name.
Definition: jsondef.h:3556
double mjd
Definition: udp_send.cpp:41
double findlastday ( string  name)

Find last day in archive.

Searches through data archives for this Node to find most recent day for which data is available. This is then stored in lastday.

Returns
MJD of last day in archive, or zero.
1680 {
1681  DIR *jdp;
1682  struct dirent *td;
1683  int year, jday;
1684  char dtemp[356];
1685  struct tm mytm;
1686  time_t mytime;
1687 
1688  year = jday = 0;
1689  if (get_nodedir(name).size())
1690  {
1691  sprintf(dtemp,"%s/data/soh", nodedir.c_str());
1692  if ((jdp=opendir(dtemp))!=nullptr)
1693  {
1694  while ((td=readdir(jdp))!=nullptr)
1695  {
1696  if (td->d_name[0] != '.')
1697  {
1698  if (atol(td->d_name) > year)
1699  year = atol(td->d_name);
1700  }
1701  }
1702  closedir(jdp);
1703  sprintf(dtemp,"%s/data/soh/%04d",nodedir.c_str(),year);
1704  if ((jdp=opendir(dtemp))!=nullptr)
1705  {
1706  while ((td=readdir(jdp))!=nullptr)
1707  {
1708  if (td->d_name[0] != '.')
1709  {
1710  if (atol(td->d_name) > jday)
1711  jday = atol(td->d_name);
1712  }
1713  }
1714  closedir(jdp);
1715  }
1716  }
1717 
1718  if (year == 0. || jday == 0.)
1719  {
1720  return (0.);
1721  }
1722 
1723  mytm.tm_year = year - 1900;
1724  mytm.tm_hour = mytm.tm_min = mytm.tm_sec = 0;
1725  mytm.tm_mon = mytm.tm_mday = mytm.tm_wday = 0;
1726  mytm.tm_mday = 1;
1727  mytm.tm_mon = 0;
1728  mytm.tm_isdst = 0;
1729  mytime = mktime(&mytm);
1730  mytime += static_cast<int>(((jday-1) * 86400.));
1731 #ifdef COSMOS_WIN_OS
1732  struct tm *temptm;
1733  temptm = localtime(&mytime);
1734  if(temptm!=nullptr)
1735  {
1736  mytm = *temptm;
1737  }
1738 #else
1739  localtime_r(&mytime,&mytm);
1740 #endif
1741  return cal2mjd(year,mytm.tm_mon+1,mytm.tm_mday);
1742  }
1743  else
1744  {
1745  return 0.;
1746  }
1747 }
int closedir(DIR *dir)
Definition: dirent.c:74
Definition: dirent.c:24
Definition: dirent.h:21
DIR * opendir(const char *name)
Definition: dirent.c:32
struct dirent * readdir(DIR *dir)
Definition: dirent.c:97
char * d_name
Definition: dirent.h:23
string name
Definition: cubesat2obj.cpp:6
double cal2mjd(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, int32_t second, int32_t nsecond)
Calendar representation to Modified Julian Day - full.
Definition: timelib.cpp:294
string get_nodedir(string node, bool create_flag)
Get Current Node Directory.
Definition: datalib.cpp:1572
static string nodedir
Path to current COSMOS Node directory.
Definition: datalib.cpp:50
double findfirstday ( string  name)

Find first day in archive.

Searches through data archives for this Node to find oldest day for which data is available. This is then stored in firstday.

Returns
MJD of last day in archive.
1755 {
1756  DIR *jdp;
1757  struct dirent *td;
1758  int year, jday;
1759  char dtemp[356];
1760  struct tm mytm;
1761  time_t mytime;
1762 
1763  if (get_nodedir(name).size())
1764  {
1765  year = jday = 9000;
1766  sprintf(dtemp,"%s/data/soh", nodedir.c_str());
1767  if ((jdp=opendir(dtemp))!=nullptr)
1768  {
1769  while ((td=readdir(jdp))!=nullptr)
1770  {
1771  if (td->d_name[0] != '.')
1772  {
1773  if (atol(td->d_name) < year)
1774  year = atol(td->d_name);
1775  }
1776  }
1777  closedir(jdp);
1778  sprintf(dtemp,"%s/data/soh/%04d",nodedir.c_str(),year);
1779  if ((jdp=opendir(dtemp))!=nullptr)
1780  {
1781  while ((td=readdir(jdp))!=nullptr)
1782  {
1783  if (td->d_name[0] != '.')
1784  {
1785  if (atol(td->d_name) < jday)
1786  jday = atol(td->d_name);
1787  }
1788  }
1789  closedir(jdp);
1790  }
1791  }
1792 
1793  if (year == 9000. || jday == 9000.)
1794  {
1795  return (0.);
1796  }
1797 
1798  mytm.tm_year = year - 1900;
1799  mytm.tm_hour = mytm.tm_min = mytm.tm_sec = 0;
1800  mytm.tm_mon = mytm.tm_mday = mytm.tm_wday = 0;
1801  mytm.tm_mday = 1;
1802  mytm.tm_mon = 0;
1803  mytm.tm_isdst = 0;
1804  mytime = mktime(&mytm);
1805  mytime += (int)((jday-1) * 86400.);
1806 #ifdef COSMOS_WIN_OS
1807  struct tm *temptm;
1808  temptm = localtime(&mytime);
1809  mytm = *temptm;
1810 #else
1811  localtime_r(&mytime,&mytm);
1812 #endif
1813 
1814  return (cal2mjd(year,mytm.tm_mon+1,mytm.tm_mday));
1815  }
1816  else
1817  {
1818  return 0.;
1819  }
1820 }
int closedir(DIR *dir)
Definition: dirent.c:74
Definition: dirent.c:24
Definition: dirent.h:21
DIR * opendir(const char *name)
Definition: dirent.c:32
struct dirent * readdir(DIR *dir)
Definition: dirent.c:97
char * d_name
Definition: dirent.h:23
string name
Definition: cubesat2obj.cpp:6
double cal2mjd(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, int32_t second, int32_t nsecond)
Calendar representation to Modified Julian Day - full.
Definition: timelib.cpp:294
string get_nodedir(string node, bool create_flag)
Get Current Node Directory.
Definition: datalib.cpp:1572
static string nodedir
Path to current COSMOS Node directory.
Definition: datalib.cpp:50
int32_t kml_write ( cosmosstruc cinfo)

Add to KML path.

Write a KML file to keep track of the path the node is following. Create the file if it doesn't alreay exist. Append to it if it already exists.

Parameters
cinfoPointer to cosmosstruc to use.
Returns
0, otherwise negative error.
1829 {
1830  char buf[500];
1831  FILE *fin, *fout;
1832  double utc;
1833 
1834  utc = floor(cinfo->node.loc.utc);
1835 
1836  string path = data_type_path((string)cinfo->node.name, "outgoing", "google", utc, "points");
1837  fin = data_open(path, (char *)"a+");
1838  fprintf(fin,"%.5f,%.5f,%.5f\n",DEGOF(cinfo->node.loc.pos.geod.s.lon),DEGOF(cinfo->node.loc.pos.geod.s.lat),cinfo->node.loc.pos.geod.s.h);
1839 
1840  path = data_type_path(cinfo->node.name,(char *)"outgoing",(char *)"google", utc,(char *)"kml");
1841  fout = data_open(path, (char *)"w");
1842  fprintf(fout,"<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n");
1843  fprintf(fout,"<Document>\n");
1844  fprintf(fout,"<name>%s JD%5.0f</name>\n",cinfo->node.name,utc);
1845  fprintf(fout,"<description>Track of node.</description>\n");
1846  fprintf(fout,"<Style id=\"yellowLineGreenPoly\">\n<LineStyle>\n<color>7f00ffff</color>\n<width>4</width>\n</LineStyle>\n");
1847  fprintf(fout,"<PolyStyle>\n<color>7f00ff00</color>\n</PolyStyle>\n</Style>\n");
1848  fprintf(fout,"<Placemark>\n<name>Node Path</name>\n<description>%s JD%5.0f</description>\n",cinfo->node.name,utc);
1849  fprintf(fout,"<styleUrl>#yellowLineGreenPoly</styleUrl>\n<LineString>\n<extrude>1</extrude>\n<tessellate>1</tessellate>\n<altitudeMode>absolute</altitudeMode>\n");
1850  fprintf(fout,"<coordinates>\n");
1851 
1852  rewind (fin);
1853  while (fgets(buf, 500, fin) != nullptr)
1854  {
1855  fputs(buf, fout);
1856  }
1857  fclose(fin);
1858 
1859  fprintf(fout,"</coordinates>\n</LineString>\n</Placemark>\n</Document>\n</kml>\n");
1860  fclose(fout);
1861 
1862  return 0;
1863 }
FILE * data_open(string path, const char *mode)
Open file from path.
Definition: datalib.cpp:1019
double utc
Master time for location, in Modified Julian Day.
Definition: convertdef.h:879
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
string data_type_path(string node, string location, string agent, double mjd, string type)
Create data file path.
Definition: datalib.cpp:910
char name[40+1]
Node Name.
Definition: jsondef.h:3556
locstruc loc
Location structure.
Definition: jsondef.h:3596
#define DEGOF(rad)
Degrees of a Radian value.
Definition: math/constants.h:33
double h
Height in meters.
Definition: vector.h:229
double lon
Longitude in radians.
Definition: vector.h:227
posstruc pos
posstruc for this time.
Definition: convertdef.h:881
gvector s
Position vector.
Definition: convertdef.h:263
double lat
Latitude in radians.
Definition: vector.h:225
geoidpos geod
Definition: convertdef.h:741
char buf[128]
Definition: rw_test.cpp:40
bool data_isdir ( string  path)
1866 {
1867  struct stat st;
1868 
1869  if (!stat(path.c_str(), &st) && S_ISDIR(st.st_mode))
1870  {
1871  return true;
1872  }
1873  else
1874  {
1875  return false;
1876  }
1877 
1878 }
#define S_ISDIR(ST_MODE)
Definition: datalib.cpp:56
bool data_ischardev ( string  path)
1881 {
1882  struct stat st;
1883 
1884  if (!stat(path.c_str(), &st) && S_ISCHR(st.st_mode))
1885  {
1886  return true;
1887  }
1888  else
1889  {
1890  return false;
1891  }
1892 
1893 }
bool data_isfile ( string  path,
off_t  size 
)
1896 {
1897  struct stat st;
1898 
1899  if (!stat(path.c_str(), &st) && S_ISREG(st.st_mode) && (!size || (size == st.st_size)))
1900  {
1901  return true;
1902  }
1903  else
1904  {
1905  return false;
1906  }
1907 
1908 }
double data_ctime ( string  path)
1911 {
1912  struct stat st;
1913 
1914  if (!stat(path.c_str(), &st))
1915  {
1916  struct timeval unixtime;
1917 #ifdef COSMOS_WIN_OS
1918  unixtime.tv_sec = st.st_ctime;
1919  unixtime.tv_usec = 0;
1920 #elif defined(COSMOS_LINUX_OS)
1921  unixtime.tv_sec = st.st_ctim.tv_sec;
1922  unixtime.tv_usec = st.st_ctim.tv_nsec / 1000;
1923 #endif
1924  return unix2utc(unixtime);
1925  }
1926  else
1927  {
1928  return 0.;
1929  }
1930 }
double unix2utc(struct timeval unixtime)
Unix time to UTC.
Definition: timelib.cpp:120
off_t data_size ( string  path)
1933 {
1934  struct stat stat_buf;
1935 
1936  if ((stat(path.c_str(), &stat_buf)) == 0)
1937  {
1938  return stat_buf.st_size;
1939  }
1940  else
1941  {
1942  return 0;
1943  }
1944 
1945 }
int32_t data_execute ( string  cmd,
string &  result,
string  shell 
)
1948 {
1949 #if defined(COSMOS_WIN_OS)
1950  if (!data_isfile(cmd))
1951  {
1952  return GENERAL_ERROR_UNDEFINED;
1953  }
1954 
1955  /* child process's STDIN is the user input or data that you enter into the child process - READ */
1956  void * g_hChildStd_IN_Rd = NULL;
1957  /* child process's STDIN is the user input or data that you enter into the child process - WRITE */
1958  void * g_hChildStd_IN_Wr = NULL;
1959  /* child process's STDOUT is the program output or data that child process returns - READ */
1960  void * g_hChildStd_OUT_Rd = NULL;
1961  /* child process's STDOUT is the program output or data that child process returns - WRITE */
1962  void * g_hChildStd_OUT_Wr = NULL;
1963  SECURITY_ATTRIBUTES saAttr;
1964 
1965  cmd.insert(0, "cmd.exe /c ");
1966 
1967  // Set the bInheritHandle flag so pipe handles are inherited.
1968  saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1969  saAttr.bInheritHandle = TRUE;
1970  saAttr.lpSecurityDescriptor = NULL;
1971 
1972  //child process's STDOUT is the program output or data that child process returns
1973  // Create a pipe for the child process's STDOUT.
1974  if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
1975  {
1976  return -1;
1977  }
1978 
1979  // Ensure the read handle to the pipe for STDOUT is not inherited.
1980  if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
1981  {
1982  return -1;
1983  }
1984 
1985  //child process's STDIN is the user input or data that you enter into the child process
1986  // Create a pipe for the child process's STDIN.
1987  if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
1988  {
1989  return -1;
1990  }
1991 
1992  // Ensure the write handle to the pipe for STDIN is not inherited.
1993  if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
1994  {
1995  return -1;
1996  }
1997 
1998  // Create child process
1999  PROCESS_INFORMATION piProcInfo;
2000  STARTUPINFO siStartInfo;
2001  BOOL bSuccess = FALSE;
2002 
2003  // Set up members of the PROCESS_INFORMATION structure.
2004 
2005  ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
2006 
2007  // Set up members of the STARTUPINFO structure.
2008  // This structure specifies the STDIN and STDOUT handles for redirection.
2009 
2010  ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
2011  siStartInfo.cb = sizeof(STARTUPINFO);
2012  siStartInfo.hStdError = g_hChildStd_OUT_Wr;
2013  siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
2014  siStartInfo.hStdInput = g_hChildStd_IN_Rd;
2015  siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
2016 
2017  // Create the child process.
2018 
2019  bSuccess = CreateProcess(NULL,
2020  (LPSTR)cmd.c_str(), // command line
2021  NULL, // process security attributes
2022  NULL, // primary thread security attributes
2023  TRUE, // handles are inherited
2024  CREATE_NO_WINDOW, // creation flags
2025  NULL, // use parent's environment
2026  NULL, // use parent's current directory
2027  &siStartInfo, // STARTUPINFO pointer
2028  &piProcInfo); // receives PROCESS_INFORMATION
2029 
2030  // If an error occurs, exit the application.
2031  if (bSuccess)
2032  {
2033  // Close handles to the child process and its primary thread.
2034  CloseHandle(piProcInfo.hProcess);
2035  CloseHandle(piProcInfo.hThread);
2036  CloseHandle(g_hChildStd_OUT_Wr);
2037 
2038  COMMTIMEOUTS ct;
2039  int size_recv = 0;
2040  unsigned long dwRead;
2041  BOOL bSuccess = TRUE;
2042  string accum;
2043 
2044  //Set timeouts for stream
2045  ct.ReadIntervalTimeout = 0;
2046  ct.ReadTotalTimeoutMultiplier = 0;
2047  ct.ReadTotalTimeoutConstant = 10;
2048  ct.WriteTotalTimeoutConstant = 0;
2049  ct.WriteTotalTimeoutMultiplier = 0;
2050  SetCommTimeouts(g_hChildStd_OUT_Rd, &ct);
2051 
2052 
2053  //This accumulates each read into one buffer,
2054  //and copies back into rsp before leaving
2055  size_t size = result.size();
2056  accum.resize(size);
2057  result.clear();
2058 
2059  do
2060  {
2061  //Reads stream from child stdout
2062  bSuccess = ReadFile(g_hChildStd_OUT_Rd, (LPVOID)&accum[0], size-1, &dwRead, NULL);
2063  if (!bSuccess || dwRead == 0)
2064  {
2065  return 0;//successful - reading is done
2066  }
2067 
2068  accum[dwRead] = 0;
2069  size_recv = accum.length();
2070 
2071 
2072  if(size_recv == 0)
2073  {
2074  //should not get here for streaming
2075  return result.size();
2076  }
2077  else
2078  {
2079  //New Chunk:
2080  accum[size_recv]=0;
2081  result += accum;
2082  }
2083 
2084 
2085  }while(1);
2086  }
2087 
2088 
2089 #else
2090  FILE * stream;
2091  char *buffer = new char[198];
2092  result.clear();
2093 
2094  vector<string> cmds = string_split(cmd, " ");
2095  if (data_isfile(cmds[0]))
2096  {
2097 
2098  }
2099  else if (data_isfile("/bin/" + cmds[0]))
2100  {
2101  cmd.insert(0, "/bin/");
2102  }
2103  else if (data_isfile("/usr/bin/" + cmds[0]))
2104  {
2105  cmd.insert(0, "/usr/bin/");
2106  }
2107  else if (data_isfile("/sbin/" + cmds[0]))
2108  {
2109  cmd.insert(0, "/sbin/");
2110  }
2111  else if (data_isfile("/usr/sbin/" + cmds[0]))
2112  {
2113  cmd.insert(0, "/usr/sbin/");
2114  }
2115  else if (data_isfile(get_cosmosroot() + "/scripts/" + cmds[0]))
2116  {
2117  cmd.insert(0, get_cosmosroot() + "/scripts/");
2118  }
2119  else if (data_isfile(get_cosmosroot() + "/bin/" + cmds[0]))
2120  {
2121  cmd.insert(0, get_cosmosroot() + "/bin/");
2122  }
2123  else {
2124  return GENERAL_ERROR_UNDEFINED;
2125  }
2126 
2127  if (shell.empty())
2128  {
2129  char *eshell = getenv("SHELL");
2130  if ((eshell) != nullptr)
2131  {
2132  shell = eshell;
2133  }
2134  }
2135  else if (data_isfile(shell))
2136  {
2137  cmd.insert(0, shell + " -c ");
2138  }
2139 
2140  if (shell.find("csh") != string::npos)
2141  {
2142  cmd.append(" |& cat");
2143  }
2144  else
2145  {
2146  cmd.append(" 2>&1");
2147  }
2148 
2149  stream = popen(cmd.c_str(), "r");
2150  if (stream)
2151  {
2152  while (!feof(stream))
2153  {
2154  if (fgets(buffer, 198, stream) != nullptr)
2155  {
2156  result.append(buffer);
2157  }
2158  }
2159  pclose(stream);
2160  }
2161 #endif
2162 
2163  return result.size();
2164 
2165 }
#define FALSE
Definition: jpleph.cpp:69
static char buffer[255]
Definition: propagator_simple.cpp:60
bool data_isfile(string path, off_t size)
Definition: datalib.cpp:1895
#define TRUE
Definition: jpleph.cpp:68
#define GENERAL_ERROR_UNDEFINED
Definition: cosmos-errno.h:295
string get_cosmosroot(bool create_flag)
Return COSMOS Root Directory.
Definition: datalib.cpp:1209
char cmds[15][50]
Definition: rw_test.cpp:39
vector< string > string_split(string in, string delimeters)
Parse a string.
Definition: stringlib.cpp:47
void log_reopen ( )
int check_events ( eventstruc events,
int  max,
cosmosstruc data 
)