Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Principle work to handle conversion between unit Deg/Rad/(H|D)MS #1

Merged
merged 9 commits into from
Sep 26, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules/
/.idea/
/dist/
7 changes: 0 additions & 7 deletions jest.config.js

This file was deleted.

21 changes: 21 additions & 0 deletions jest.config.ts
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
23 changes: 15 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
{
"name": "@tsastro/astrolib",
"version": "0.1.0",
"version": "0.2.0",
"type": "module",
"description": "Astrometry library",
"files": [
"dist/"
],
"scripts": {
"build": "tsc",
"test": "jest --watch",
"test": "jest",
"testconf": "jest --showConfig",
"doc": "typedoc src/AstroLib.ts",
"eslint": "eslint ."
"eslint": "eslint .",
"start": "node astrolib.ts",
"start:dev": "node -r ts-node/register astrolib.ts"
},
"main": "dist/AstroLib.ts",
"types": "dist/Astrolib.d.ts",
"exports": "./dist/AstroLib.js",
"types": "./dist/Astrolib.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/tsastro/astrolib.git"
},
"private": false,
"dependencies": {
"@buge/ts-units": "^1.2.3",
"@tsastro/tsofa": "^18.0.0"
"@tsastro/tsofa": "^18.1.0",
"common-js": "^0.3.8"
},
"devDependencies": {
"@eslint/js": "^9.9.1",
"@eslint/js": "^8.57.0",
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.7.1",
"eslint": "^9.9.1",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jsdoc": "^46.8.1",
"eslint-plugin-prefer-arrow": "^1.2.3",
Expand Down
7 changes: 0 additions & 7 deletions src/AstroLib.ts

This file was deleted.

223 changes: 223 additions & 0 deletions src/main/AsLi_base.ts
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;
}
1 change: 1 addition & 0 deletions src/main/AstroLib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as AstroLib from "./AsLi_base.js";
Loading
Loading