Skip to content

Commit

Permalink
v0.3.0 DMS/HMS format correction
Browse files Browse the repository at this point in the history
DMS and HMS fixed to pad zeros
Library structure changed for improved readability
  • Loading branch information
Republicof1 committed Oct 2, 2024
1 parent be55e19 commit 03f912b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 51 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tsastro/astrolib",
"version": "0.2.0",
"version": "0.3.0",
"type": "module",
"description": "Astrometry library",
"files": [
Expand Down
134 changes: 86 additions & 48 deletions src/main/AsLi_base.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,86 @@

import { constrainedMemory, loadEnvFile } from "process";
import {TSOFA} from "@tsastro/tsofa";
import { setMaxIdleHTTPParsers } from "http";

// not sure what the best library structure is https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html

//////////////////////////////
// constants
//
const RAD_MIN = -1.57952297307;
const RAD_MAX = 6.291911955;
const DEG_MIN = -90.5;
const DEG_MAX = 360.5;
const CONV_PRECISION_RD = 14;
const CONV_PRECISION_SE = 10;
const CONV_PRECISION_SE = 9;

//deg in
///////////////////////////////
// degrees in
//
export function DegToDms(value: number):string
{
const rad = ConvertDegToRad(value, CONV_PRECISION_RD);
return ConvertToDMS(rad, CONV_PRECISION_SE);
return ConvertToDms(rad, CONV_PRECISION_SE);
}
export function DegToHms(value: number):string
{
const rad = ConvertDegToRad(value, CONV_PRECISION_RD);
return ConvertToHMS(rad, CONV_PRECISION_SE);
return ConvertToHms(rad, CONV_PRECISION_SE);
}
export function DegToRad(value: number):number
{
return ConvertDegToRad(value, CONV_PRECISION_RD);
}

//sexegesemal in
///////////////////////////////
// radians in
//
export function RadToDms(value: number):string {
//console.log("R2DMS - in: " + value);
return ConvertToDms(value, CONV_PRECISION_SE);
}
export function RadToHms(value: number):string {
//console.log("R2HMS - in: " + value);
return ConvertToHms(value, CONV_PRECISION_SE);
}
export function RadToDeg(value: number):number
{
return ConvertRadToDeg(value, CONV_PRECISION_RD);
}

///////////////////////////////
// sexegesemal in
//
export function DmsToDeg(value: string):number
{
//DMS Degrees, Minutes, Seconds
return XXmsToDeg(value, "Dec");
}

export function DmsToRad(value: string):number
{
const deg = XXmsToDeg(value, "Dec");
//if an error has occurred, bail out
if(deg == -111111)
{
return deg;
}
return ConvertDegToRad(deg, CONV_PRECISION_RD);
}

export function HmsToDeg(value: string):number
{
//HMS Hours, Minutes, Seconds
return XXmsToDeg(value, "RA");
}

export function HmsToRad(value: string):number
{
const deg = XXmsToDeg(value, "RA");
//if an error has occurred, bail out
if(deg == -111111)
{
return deg;
}
return ConvertDegToRad(deg, CONV_PRECISION_RD);
}
//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);
}

//low priority
export function RadToDeg(value: number):number
{
return ConvertRadToDeg(value, CONV_PRECISION_RD);
}


//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -164,7 +169,16 @@ function XXmsToDeg(value: string, mode: string):number
//console.log("non-conformant pattern found");
return -111111;
}
function PrecMod(degree: number): number
{
let output = 1;
for(let i = 0; i < degree; ++i)
{
output *= 10;
}

return output;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Wrapper for Deg >> Rad with precision modifier
//
Expand All @@ -175,22 +189,10 @@ 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 /= pm;
return retval;
}

function PrecMod(degree: number): number
{
let output = 1;
for(let i = 0; i < degree; ++i)
{
output *= 10;
}

return output;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Wrapper for Rad >> Deg with precision modifier
//
Expand All @@ -206,18 +208,54 @@ function ConvertRadToDeg(value: number, precision: number):number
return retval;
}

function ConvertToDMS(angleRad: number, decimalPrecision: number): string
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Wrapper for dms calculation and string construction
//
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;
return ConvertToXXms(angleRad, decimalPrecision, TSOFA.jauA2af);
}

function ConvertToHMS(angleRad: number, decimalPrecision: number): string
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Wrapper for hms calculation and string construction
//
function ConvertToHms(angleRad: number, decimalPrecision: number): string
{
return ConvertToXXms(angleRad, decimalPrecision, TSOFA.jauA2tf);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// (*)ms calculation and string construction
//
function ConvertToXXms(angleRad: number, decimalPrecision: number, fn: Function): 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];
//int representaiton A2tf output array
let idmsf = new Array(4);
//string representation array output
let sdmsf = new Array(4);
//get answer components as number array
const sign: string = fn(decimalPrecision, angleRad, idmsf);
//convert and pad each component:
sdmsf[0] = idmsf[0].toString();
if(sdmsf[0].length == 1)
{
sdmsf[0] = "0" + sdmsf[0];
}
sdmsf[1] = idmsf[1].toString();
if(sdmsf[1].length == 1)
{
sdmsf[1] = "0" + sdmsf[1];
}
sdmsf[2] = idmsf[2].toString();
if(sdmsf[2].length == 1)
{
sdmsf[2] = "0" + sdmsf[2];
}
sdmsf[3] = idmsf[3].toString();
while(sdmsf[3].length != CONV_PRECISION_SE)
{
//lhs padding of precision - extant numbers
sdmsf[3] = "0" + sdmsf[3];
}
//compose and return the string
let formated: string = sign + sdmsf[0]+" "+sdmsf[1]+" "+sdmsf[2]+"."+sdmsf[3];
return formated;
}
6 changes: 4 additions & 2 deletions src/test/astroLib.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from '@jest/globals';
import { AstroLib } from "../main/AstroLib.js";
import { DegToDms, DmsToDeg } from "../main/AsLi_base.js";
//import { DegToDms, DmsToDeg } from "../main/AsLi_base.js";

/* Validate a double result.
*
Expand Down Expand Up @@ -122,6 +122,8 @@ test("Degree to DegreeMinSec; pos(2)", () => {
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");
vv_ms(AstroLib.DegToDms(45.0), "45 00 0.0", "DEG to DMS 2");
vv_ms(AstroLib.DegToDms(45.000000020304001), "45 00 00.00007309", "DEG to DMS 2");
vv_ms(AstroLib.DegToDms(45.020304000000001), "45 01 13.094399999", "DEG to DMS 2");
});

test("Radian to HourMinSec; pos(2)", () => {
Expand All @@ -148,7 +150,7 @@ 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);
vv_ms(AstroLib.DegToDms(AstroLib.DmsToDeg(testVals[i])),testVals[i], "DEG to DMS " + i);
}
});

Expand Down

0 comments on commit 03f912b

Please sign in to comment.