-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from tsastro/1-exchange-of-formats
Principle work to handle conversion between unit Deg/Rad/(H|D)MS
- Loading branch information
Showing
11 changed files
with
1,023 additions
and
588 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/node_modules/ | ||
/.idea/ | ||
/dist/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createDefaultEsmPreset, type JestConfigWithTsJest } from 'ts-jest' | ||
|
||
const defaultEsmPreset = createDefaultEsmPreset( | ||
|
||
) | ||
|
||
const jestConfig: JestConfigWithTsJest = { | ||
// [...] | ||
...defaultEsmPreset, | ||
transform: { | ||
"^.+.(t|j)sx?$": ["ts-jest",{ | ||
useESM: true, | ||
tsconfig: "./tsconfig-jest.json" | ||
}], | ||
}, | ||
moduleNameMapper: { | ||
'^(\\.{1,2}/.*)\\.js$': '$1', | ||
} | ||
} | ||
|
||
export default jestConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
|
||
import { constrainedMemory, loadEnvFile } from "process"; | ||
import {TSOFA} from "@tsastro/tsofa"; | ||
|
||
// not sure what the best library structure is https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html | ||
|
||
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; | ||
|
||
//deg in | ||
export function DegToDms(value: number):string | ||
{ | ||
const rad = ConvertDegToRad(value, CONV_PRECISION_RD); | ||
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); | ||
} | ||
export function DegToRad(value: number):number | ||
{ | ||
return ConvertDegToRad(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(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(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); | ||
} | ||
|
||
|
||
////////////////////////////////////////////////////////////////////////// | ||
// Internal Functions | ||
////////////////////////////////////////////////////////////////////////// | ||
|
||
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
// This does the heavy lifting of conversion from | ||
// Sexegesimal (RA & Dec) to decimal form | ||
// | ||
function XXmsToDeg(value: string, mode: string):number | ||
{ | ||
let out: number; | ||
let conforms: boolean = false; | ||
let negative: boolean = false; | ||
const regexp: RegExp = new RegExp(/(\d{1,2})\D(\d{1,2})\D(\d{1,2}(\.\d+)[sS]*)/); | ||
|
||
//first filter to ensure input is at least in the ball park of sensible | ||
conforms = regexp.test(value); | ||
if(conforms) | ||
{ | ||
//handle white space | ||
value = value.trim(); | ||
//handle trailing 'S' or 's' | ||
value = value.replace(/[sS]/, ''); | ||
|
||
//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] == '+') | ||
{ | ||
values = values.slice(1, values.length); | ||
if(value[0] == '-') | ||
{ | ||
negative = true; | ||
//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; | ||
} | ||
} | ||
return out; | ||
} | ||
} | ||
//console.log("non-conformant pattern found"); | ||
return -111111; | ||
} | ||
|
||
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
// Wrapper for Deg >> Rad with precision modifier | ||
// | ||
function ConvertDegToRad(value: number, precision: number):number | ||
{ | ||
if(value < DEG_MIN || value > DEG_MAX){ | ||
return -111111; | ||
} | ||
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 | ||
// | ||
function ConvertRadToDeg(value: number, precision: number):number | ||
{ | ||
if(value < RAD_MIN || value > RAD_MAX){ | ||
return -111111; | ||
} | ||
const pm = PrecMod(precision); | ||
let retval = value * TSOFA.DR2D_$LI$(); | ||
retval = Math.round((retval + Number.EPSILON)* pm); | ||
retval /= pm; | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as AstroLib from "./AsLi_base.js"; |
Oops, something went wrong.