Skip to content

Commit

Permalink
v0.2.6 update to tests and catch for over-range clues on conversion
Browse files Browse the repository at this point in the history
Roundtrip HMS/DMS <=> Deg/Rad at high precision. Note this is dependent on a change in TSOFA. Precision values set to higher than required level of 14 decimal places for Rad<=>Deg and 10dp for Sexagesimal conversions.

minor correction to tsconfig

correction to vv_ms test rig to manage acceptance of similar output (rather than exact character match)
  • Loading branch information
Republicof1 committed Sep 25, 2024
1 parent d3896c2 commit 7972240
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 35 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tsastro/astrolib",
"version": "0.2.4",
"version": "0.2.6",
"type": "module",
"description": "Astrometry library",
"files": [
Expand All @@ -21,7 +21,7 @@
"private": false,
"dependencies": {
"@buge/ts-units": "^1.2.3",
"@republicof1/tsofa": "^18.4.1",
"@republicof1/tsofa": "^18.5.0",
"common-js": "^0.3.8"
},
"devDependencies": {
Expand Down
48 changes: 31 additions & 17 deletions src/AsLi_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ const RAD_MIN = -1.57952297307;
const RAD_MAX = 6.291911955;
const DEG_MIN = -90.5;
const DEG_MAX = 360.5;
const CONV_PRECISION_RD = 10;
const CONV_PRECISION_SE = 6;
const CONV_PRECISION_RD = 14;
const CONV_PRECISION_SE = 10;

//deg in
export function DegToDms(value: number):string
{
return ConvertToDMS(ConvertDegToRad(value, CONV_PRECISION_RD), CONV_PRECISION_SE);
const rad = ConvertDegToRad(value, CONV_PRECISION_RD);
return ConvertToDMS(rad, CONV_PRECISION_SE);
}
export function DegToHms(value: number):string
{
return ConvertToHMS(ConvertDegToRad(value, CONV_PRECISION_RD), CONV_PRECISION_SE);
{
const rad = ConvertDegToRad(value, CONV_PRECISION_RD);
return ConvertToHMS(rad, CONV_PRECISION_SE);
}
export function DegToRad(value: number):number
{
Expand Down Expand Up @@ -59,11 +61,13 @@ export function HmsToRad(value: string):number
}
//low priority
export function RadToDms(value: number):string {
//console.log("R2DMS - in: " + value);
return ConvertToDMS(value, CONV_PRECISION_SE);
}

//low priority
export function RadToHms(value: number):string {
//console.log("R2HMS - in: " + value);
return ConvertToHMS(value, CONV_PRECISION_SE);
}

Expand Down Expand Up @@ -100,7 +104,6 @@ function XXmsToDeg(value: string, mode: string):number

//split on all non digits
let values: string[] = value.split(/\D/);

//handle negative symbol
//in the case of RA we elimitate it, for Dec we apply it
if(value[0] == '-' || value[0] == '+')
Expand All @@ -112,34 +115,49 @@ function XXmsToDeg(value: string, mode: string):number
//console.log("negative value");
}
}

//if we have 4 components at this stage, handle decimal
if(values.length == 4)
{
const decimal = values[3];
values = values.slice(0,3);
values[2] += "." + decimal;
}

//process values - by this point we should be returning good data
if(values.length == 3)
{

out = Number.parseInt(values[0]);
out += Number.parseInt(values[1]) / 60;
out += Number.parseFloat(values[2]) / 3600.0;



if(mode == "RA")
{
//if out of range
if(out < 0 || out > 24.0)
{
return -111111;
}
out *= 15;

if(negative)
{
console.log("negative RA detected! Resolving to positive value")
}
}
else if(mode == "Dec")
{
//if out of range
if(out < -90 || out > 90.0)
{
return -111111;
}

if(negative)
{
out *= -1;
}
}
//("gamma: " + out);
}
return out;
}
}
Expand All @@ -157,7 +175,8 @@ function ConvertDegToRad(value: number, precision: number):number
}
const pm = PrecMod(precision);
let retval = value * TSOFA.DD2R_$LI$();
retval = Math.round((retval + Number.EPSILON)* pm);

retval = Math.round((retval + Number.EPSILON) * pm);
retval /= pm;
return retval;
}
Expand Down Expand Up @@ -187,23 +206,18 @@ function ConvertRadToDeg(value: number, precision: number):number
return retval;
}


function ConvertToDMS(angleRad: number, decimalPrecision: number): string
{
let idmsf: number[] = new Array(4);
const sign: string = TSOFA.jauA2af(decimalPrecision, angleRad, idmsf);

let formated: string = sign + idmsf[0]+" "+idmsf[1]+" "+idmsf[2]+"."+idmsf[3];

return formated;
}

function ConvertToHMS(angleRad: number, decimalPrecision: number): string
{
let idmsf: number[] = new Array(4);
const sign: string = TSOFA.jauA2tf(decimalPrecision, angleRad, idmsf);

let formated: string = sign + idmsf[0]+" "+idmsf[1]+" "+idmsf[2]+"."+idmsf[3];

return formated;
}
47 changes: 32 additions & 15 deletions test/astroLib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import exp from "constants";
import { AstroLib } from "../src/AstroLib.js";
import { TSOFA } from "@republicof1/tsofa";
import { convertCompilerOptionsFromJson } from "typescript";
import { DegToDms } from "../src/AsLi_base.js";
import { DegToDms, DmsToDeg } from "../src/AsLi_base.js";

/* Validate a double result.
*
Expand Down Expand Up @@ -44,15 +44,17 @@ function vvf(testValue: number, expectedValue: number){
}
}


function vv_ms(testValue: string, expectedValue: string){

////////
// A test to compare sexegesimal components
//
// it breaks each HMS and DMS into H/D M & S and
function vv_ms(testValue: string, expectedValue: string, callingFn: string ){
let gvArr = expectedValue.split(" ");
let tvArr = testValue.split(" ");

expect(gvArr[0]).toBe(tvArr[0]);
expect(gvArr[1]).toBe(tvArr[1]);
expect(Number.parseFloat(gvArr[2])).toBeCloseTo(Number.parseFloat(tvArr[2]))
expect(Number.parseInt(gvArr[0])).toBe(Number.parseInt(tvArr[0]));
expect(Number.parseInt(gvArr[0])).toBe(Number.parseInt(tvArr[0]));
expect(Number.parseFloat(gvArr[2])).toBeCloseTo(Number.parseFloat(tvArr[2]));
}

//all values confirmed with SIMBAD
Expand Down Expand Up @@ -113,25 +115,40 @@ test("Radians to Degrees; pos(3); edge(2); neg(1)",()=> {
});

test("Degree to HourMinSec; pos(2)", () => {
vv_ms(AstroLib.DegToHms(141.0079023718000), "+9 24 1.8965692320");
vv_ms(AstroLib.DegToHms(221.7409285236400), "+14 46 57.8228456736");
vv_ms(AstroLib.DegToHms(141.0079023718000), "+9 24 1.8965692320", "DEG to HMS 1");
vv_ms(AstroLib.DegToHms(221.7409285236400), "+14 46 57.8228456736", "DEG to HMS 2");
});

test("Degree to DegreeMinSec; pos(2)", () => {
vv_ms(AstroLib.DegToDms(+89.26410897), "+89 15 50.79229");
vv_ms(AstroLib.DegToDms(-60.8656960707900), "-60 51 56.50585");
vv_ms(AstroLib.DegToDms(+89.26410897), "+89 15 50.79229", "DEG to DMS 1");
vv_ms(AstroLib.DegToDms(-60.8656960707900), "-60 51 56.50585", "DEG to DMS 2");
});

test("Radian to HourMinSec; pos(2)", () => {
vv_ms(AstroLib.RadToHms(2.461052167719), "+9 24 1.8965692320");
vv_ms(AstroLib.RadToHms(3.8701092891669), "+14 46 57.8228456736");
vv_ms(AstroLib.RadToHms(2.461052167719), "+9 24 1.8965692320", "RAD to HMS 1");
vv_ms(AstroLib.RadToHms(3.8701092891669), "+14 46 57.8228456736", "RAD to HMS 2");
});

test("Radian to DegreeMinSec; pos(2)", () => {
vv_ms(AstroLib.RadToDms(1.557952605), "+89 15 50.79229");
vv_ms(AstroLib.RadToDms(-1.062306797953), "-60 51 56.50585");
vv_ms(AstroLib.RadToDms(1.557952605), "+89 15 50.79229", "RAD to DMS 1");
vv_ms(AstroLib.RadToDms(-1.062306797953), "-60 51 56.50585", "RAD to DMS 2");
});

test("HMS Round trips", () => {
let testVals = ["23 59 49.2197", "11 48 18.2283623", "00 01 1.8965692320"]
for(let i = 0; i < testVals.length; ++i)
{
vv_ms(AstroLib.DegToHms(AstroLib.HmsToDeg(testVals[i])),testVals[i], "DEG to HMS " + i);
}
});

test("DMS Round trips", () => {
let testVals = ["-89 01 01.2197234", "0 01 18.2283623", "89 59 1.8965692320"];
for(let i = 0; i < testVals.length; ++i)
{
vv_ms(AstroLib.DegToDms(DmsToDeg(testVals[i])),testVals[i], "DEG to DMS " + i);
}
});


// test("Julian date; pos(0)", () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"esModuleInterop": true,
"types": ["node", "jest"],
},
"include": ["src/**/*", "test/tsofa.test.ts"]
"include": ["src/**/*", "test/astroLib.test.ts"]
}

0 comments on commit 7972240

Please sign in to comment.