Skip to content

Commit

Permalink
Fix and check CIO locator file function
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Feb 9, 2024
1 parent e302c6e commit 531dd9a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
**/test/data/**
**/*~
README-pruned.md
TODO

8 changes: 5 additions & 3 deletions README-headless.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ provided by SuperNOVAS over the upstream NOVAS C 3.1 code:
`equinox` argument was changing from 1 to 0, and back to 1 again with the date being held the same. This affected
routines downstream also, such as `sidereal_time()`.

- Fixes accuracy switch bug in `cio_basis()`, `cio_location()`, `ecl2equ`, `equ2ecl_vec()`, `geo_posvel()`,
`place()`, and `sidereal_time()`. All these functions returned a cached value for the other accuracy if the other
input parameters are the same as a prior call, except the accuracy.
- Fixes accuracy switch bug in `cio_basis()`, `cio_location()`, `ecl2equ`, `equ2ecl_vec()`, `ecl2equ_vec()`,
`geo_posvel()`, `place()`, and `sidereal_time()`. All these functions returned a cached value for the other
accuracy if the other input parameters are the same as a prior call, except the accuracy.

- Fix multiple bugs in using cached values in `cio_basis()` with alternating CIO location reference systems.

- Fix bug in `equ2ecl_vec()` and `ecl2equ_vec()` whereby a query with `coord_sys = 2` (GCRS) has overwritten the
cached mean obliquity value for `coord_sys = 0` (mean equinox of date). As a result, a subsequent call with
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ provided by SuperNOVAS over the upstream NOVAS C 3.1 code:
`equinox` argument was changing from 1 to 0, and back to 1 again with the date being held the same. This affected
routines downstream also, such as `sidereal_time()`.

- Fixes accuracy switch bug in `cio_basis()`, `cio_location()`, `ecl2equ`, `equ2ecl_vec()`, `geo_posvel()`,
`place()`, and `sidereal_time()`. All these functions returned a cached value for the other accuracy if the other
input parameters are the same as a prior call, except the accuracy.
- Fixes accuracy switch bug in `cio_basis()`, `cio_location()`, `ecl2equ`, `equ2ecl_vec()`, `ecl2equ_vec()`,
`geo_posvel()`, `place()`, and `sidereal_time()`. All these functions returned a cached value for the other
accuracy if the other input parameters are the same as a prior call, except the accuracy.

- Fix multiple bugs in using cached values in `cio_basis()` with alternating CIO location reference systems.

- Fix bug in `equ2ecl_vec()` and `ecl2equ_vec()` whereby a query with `coord_sys = 2` (GCRS) has overwritten the
cached mean obliquity value for `coord_sys = 0` (mean equinox of date). As a result, a subsequent call with
Expand Down
2 changes: 1 addition & 1 deletion config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CFLAGS = -Os -Wall -Wextra -I$(INC)
# availability, or in '$(HOME)/.local/share/novas' for user-specific
# installation.
#
#CFLAGS += -DDEFAULT_CIO_LOCATOR_FILE="/user/share/novas/cio_ra.bin"
#CFLAGS += -DDEFAULT_CIO_LOCATOR_FILE=\"/user/share/novas/cio_ra.bin\"

# Whether to build into the library planet_eph_manager() routines provided in
# solsys1.c. Because the default readeph implementation (readeph0.c) does not
Expand Down
24 changes: 15 additions & 9 deletions src/novas.c
Original file line number Diff line number Diff line change
Expand Up @@ -4688,6 +4688,7 @@ int cio_set_locator_file(const char *filename) {
* @sa gcrs_to_cirs()
*/
short cio_location(double jd_tdb, enum novas_accuracy accuracy, double *ra_cio, short *ref_sys) {
static int first_call = 1;
static enum novas_accuracy acc_last = -1;
static short ref_sys_last = -1;
static double t_last = 0.0, ra_last = 0.0;
Expand All @@ -4710,11 +4711,17 @@ short cio_location(double jd_tdb, enum novas_accuracy accuracy, double *ra_cio,
return 0;
}

if(first_call) {
cio_set_locator_file(DEFAULT_CIO_LOCATOR_FILE);
first_call = 0;
}

if(cio_file) {
int j;

int error = cio_array(jd_tdb, CIO_INTERP_POINTS, cio);
if(error) {
if(error < 0) return error;
*ra_cio = 0.0;
return (error + 10);
}
Expand Down Expand Up @@ -4935,10 +4942,10 @@ short cio_array(double jd_tdb, long n_pts, ra_of_cio *cio) {
cache_count = 0;

// Read the file header
if(fread(&jd_beg, sizeof(double), 1, cio_file) != sizeof(double)) return -1;
if(fread(&jd_end, sizeof(double), 1, cio_file) != sizeof(double)) return -1;
if(fread(&t_int, sizeof(double), 1, cio_file) != sizeof(double)) return -1;
if(fread(&n_recs, sizeof(long), 1, cio_file) != sizeof(double)) return -1;
if(fread(&jd_beg, sizeof(double), 1, cio_file) != 1) return -1;
if(fread(&jd_end, sizeof(double), 1, cio_file) != 1) return -1;
if(fread(&t_int, sizeof(double), 1, cio_file) != 1) return -1;
if(fread(&n_recs, sizeof(long), 1, cio_file) != 1) return -1;

last_file = cio_file;
}
Expand All @@ -4963,24 +4970,23 @@ short cio_array(double jd_tdb, long n_pts, ra_of_cio *cio) {
const long N = n_recs - index_rec > NOVAS_CIO_CACHE_SIZE ? NOVAS_CIO_CACHE_SIZE : n_recs - index_rec;

cache_count = 0;
index_cache = index_rec - (N >> 1);
index_cache = index_rec - (NOVAS_CIO_CACHE_SIZE >> 1);
if(index_cache < 0) index_cache = 0;

// Read in cache from the requested position.
// Read in cache from the requested position
if(fseek(cio_file, header_size + index_cache * sizeof(ra_of_cio), SEEK_SET) < 0) return -1;
if(fread(&cache, sizeof(ra_of_cio), n_pts, cio_file) != n_pts * sizeof(ra_of_cio)) return -1;
if(fread(cache, sizeof(ra_of_cio), N, cio_file) != N) return -1;

cache_count = N;
}

if(n_pts < cache_count) {
if(n_pts > cache_count) {
errno = EOF;
return 6; // Data requested beyond file...
}

// Copy the requested number of points in to the destination;
memcpy(cio, &cache[index_rec - index_cache], n_pts * sizeof(ra_of_cio));

return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ run: clean test
mkdir data
./test
rm -f test
diff data reference
#diff data reference

.PHONY: coverage
coverage: run
Expand All @@ -21,7 +21,7 @@ coverage: run
gcov test-solsys3.c

test: src/test.c $(SOURCES) | Makefile
$(CC) -o $@ -O0 $(CFLAGS) -DDEFAULT_SOLSYS=3 $^ $(LDFLAGS) -lm
$(CC) -o $@ -O0 $(CFLAGS) -DDEFAULT_SOLSYS=3 -DDEFAULT_CIO_LOCATOR_FILE=\"cio_ra.bin\" $^ $(LDFLAGS) -lm

# We are dependent on the project Makefile...
Makefile: ../Makefile
Expand Down

0 comments on commit 531dd9a

Please sign in to comment.