Skip to content

Commit

Permalink
ydoy2mjd
Browse files Browse the repository at this point in the history
  • Loading branch information
xanthospap committed Oct 23, 2024
1 parent e77677e commit ec5828d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ target_include_directories(mjd2ymd PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_executable(mjd2ydoy src/bin/mjd2ydoy.cpp)
target_link_libraries(mjd2ydoy PRIVATE datetime)
target_include_directories(mjd2ydoy PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_executable(ydoy2mjd src/bin/ydoy2mjd.cpp)
target_link_libraries(ydoy2mjd PRIVATE datetime)
target_include_directories(ydoy2mjd PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_executable(integral_seconds_limits src/bin/integral_datetime_limits.cpp)
target_link_libraries(integral_seconds_limits PRIVATE datetime)
target_include_directories(integral_seconds_limits PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
Expand Down
3 changes: 3 additions & 0 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ perform fundamental date transformations. These are:
* ymd2mjd
* mjd2ymd
* mjd2ydoy
* ydoy2mjd

They came with help, which can be triggered by the `-h` switch (e.g. `$> ymd2mjd -h`), listing
options and usage instructions. They all except input from STDIN and write results to
Expand Down Expand Up @@ -209,6 +210,8 @@ ERROR. Failed parsing/transforming line: 2014TT01:1
2014/001
2014/001
2014/001
$> echo "2008:32" | build/ydoy2mjd | build/mjd2ymd
2008/02/01
```

## For Developers
Expand Down
2 changes: 1 addition & 1 deletion src/bin/mjd2ydoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ constexpr const int MAX_ERRORS_ALLOWED = 10;
/* help message */
void prhelp() {
printf(
"jd2ydoy: Transform a date from Modified Julian Day to calendar date, "
"mjd2ydoy: Transform a date from Modified Julian Day to calendar date, "
"i.e.\n\"YYYYdDDD\". The program expects the read a Modified Julian "
"Day string\n(actually an integral value) from STDIN (or multiple MJDs, "
"seperated by\nnewlines) and will print results on STDOUT. The MJD string"
Expand Down
83 changes: 83 additions & 0 deletions src/bin/ydoy2mjd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "calendar.hpp"
#include <cstdio>
#include <cstring>
#include <exception>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#endif

constexpr const int MAX_ERRORS_ALLOWED = 10;

/* help message */
void prhelp() {
printf(
"ydoy2mjd: Transform a date from Year, Day-Of-Year to Modified Julian "
"Day.\n"
"The program expects the read a date compliant to the format "
"\n\"YYYYdDDD\" "
"where \"d\" is any non-numeric character from STDIN (or multiple "
"\ndates, "
"seperated by newlines) and will print results on STDOUT. \nThe date "
"string"
" can be followed by any number of remaining characters that \nwill be "
"ignored.\n\nOptions:\n[-h] "
"help message\n\tprint (this) message and exit.\n[-e] "
"MAX_ERRORS_ALLOWED\n\tMaximum number of errors allowed (i.e. date "
"strings that where not\n\tparsed correctly). Default values is %d\n\n"
"\n\nWarnings:\n\t* Command line options are only available on POSIX "
"systems.\n\nDionysos Satellite Observatory\nNational Technical "
"University of "
"Athens\nhttps://github.com/DSOlab/ggdatetime\n",
MAX_ERRORS_ALLOWED);
return;
}

int main(int argc, char *argv[]) {
char line[124];
int yr, dy;
char c;
int error = 0, cer = 0;
int max_errors_allowed = MAX_ERRORS_ALLOWED;

#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
/* parse command line arguments */
while ((c = getopt(argc, argv, "he:")) != -1) {
switch (c) {
case 'h':
prhelp();
return 0;
case 'e':
max_errors_allowed = atoi(optarg);
break;
default:
fprintf(stderr, "Usage: %s [-e MAX_ERRORS_ALLOWED]\n", argv[0]);
return 1;
} /* switch c */
}
#endif

while (fgets(line, sizeof(line), stdin) && (error < max_errors_allowed)) {
if (3 == sscanf(line, "%d%c%d", &yr, &c, &dy)) {
try {
const dso::modified_julian_day m{dso::year(yr), dso::day_of_year(dy)};
printf("%d\n", m.as_underlying_type());
} catch (std::exception &) {
++error;
}
} else {
++error;
}

if (error > cer) {
fprintf(stderr, "ERROR. Failed parsing/transforming line: %s\n", line);
++cer;
}
}

if (error >= max_errors_allowed) {
fprintf(stderr, "Too many errors, giving up!\n");
return 1;
}

return 0;
}

0 comments on commit ec5828d

Please sign in to comment.