// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright 2013 - UDS/CNRS
// The Aladin Lite program is distributed under the terms
// of the GNU Lesser General Public License version 3
// or (at your option) any later version.
//
// This file is part of Aladin Lite.
//
// Aladin Lite is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Aladin Lite is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Aladin Lite. If not, see <https://www.gnu.org/licenses/>.
//
/******************************************************************************
* Aladin Lite project
*
* File AladinUtils
*
* Author: Thomas Boch[CDS]
*
*****************************************************************************/
import { Aladin } from "./Aladin";
import { Sesame } from "./Sesame";
// wasm-bindgen truncates a nside to an unsigned 32-bit integer, so check it here
function checkNside(nside) {
if (!Number.isInteger(nside) || nside < 1 || nside > 2 ** 29 || (nside & (nside - 1)) !== 0) {
throw new Error('nside must be a power of two between 1 and 2^29, got ' + nside);
}
}
// Pass arrays and typed arrays as they are, wrap a single value
function toArray(v) {
return Array.isArray(v) || ArrayBuffer.isView(v) ? v : [v];
}
// The WASM HEALPix functions take u64 cell indices, i.e. BigInt values
function toBigIntArray(ipix) {
if (!Array.isArray(ipix) && !ArrayBuffer.isView(ipix)) {
ipix = [ipix];
}
return Array.from(ipix, (p) => BigInt(p));
}
/**
* @namespace AladinUtils
* @description Aladin Lite utils API namespace for basic functions
*/
export let AladinUtils = {
/**
* @namespace HEALPix
* @memberof AladinUtils
* @description Namespace for HEALPix-related utilities within the Aladin Lite API.
*/
HEALPix: {
/**
* Represents a geographical point with longitude and latitude coordinates.
*
* @typedef {Object} LonLat
* @property {number} lon - The longitude coordinate, in radians.
* @property {number} lat - The latitude coordinate, in radians.
*/
/**
* Represents the vertices of a HEALPix cell, where each vertex is a LonLat object.
*
* @typedef {Object} HpxCellVertices
* @property {LonLat} v1 - The first vertex.
* @property {LonLat} v2 - The second vertex.
* @property {LonLat} v3 - The third vertex.
* @property {LonLat} v4 - The fourth vertex.
*/
/**
* Computes HEALPix vertices for a given NSIDE and pixel index (ipix).
*
* @function
* @memberof AladinUtils.HEALPix
* @name vertices
*
* @param {number} nside - NSIDE parameter for the HEALPix grid.
* @param {number | number[]} ipix - Pixel index or an array of pixel indices.
* @throws {string} Throws an error if A.init is not called first.
* @returns {HpxCellVertices[]} vertices - An array representing HEALPix cell vertices. Each element has v1, v2, v3, v4 properties. Each vi is an object having a lon and a lat property.
*/
vertices: function(nside, ipix) {
let wasm = Aladin.wasmLibs.core;
if (!wasm) {
throw 'A.init must be called first'
}
// Cast to a 1d array of BigInt, as expected by the WASM
ipix = toBigIntArray(ipix);
checkNside(nside);
const vertices = wasm.HEALPixVertices(nside, ipix)
return vertices;
},
/**
* Computes HEALPix pixel indices from angular coordinates (longitude and latitude).
*
* @function
* @memberof AladinUtils.HEALPix
* @name ang2pix
*
* @param {number} nside - NSIDE parameter for the HEALPix grid.
* @param {number | number[]} lon - Longitude or an array of longitudes, in radians.
* @param {number | number[]} lat - Latitude or an array of latitudes, in radians, between -pi/2 and pi/2.
* @throws {string} Throws an error if A.init is not called first.
* @returns {BigUint64Array} ipix - The pixel indices, one per position.
*/
ang2pix: function(nside, lon, lat) {
let wasm = Aladin.wasmLibs.core;
if (!wasm) {
throw 'A.init must be called first'
}
checkNside(nside);
const ipix = wasm.HEALPixAng2Pix(nside, toArray(lon), toArray(lat))
return ipix;
},
/**
* Computes angular coordinates (longitude and latitude) from HEALPix pixel indices.
*
* @function
* @memberof AladinUtils.HEALPix
* @name pix2ang
*
* @param {number} nside - NSIDE parameter for the HEALPix grid.
* @param {number | number[]} ipix - Pixel index or an array of pixel indices.
*
* @throws {string} Throws an error if A.init is not called first.
* @returns {LonLat[]} lonlat - Longitude and latitude or an array of longitudes and latitudes.
*/
pix2ang: function(nside, ipix) {
let wasm = Aladin.wasmLibs.core;
if (!wasm) {
throw 'A.init must be called first'
}
// Cast to a 1d array of BigInt, as expected by the WASM
ipix = toBigIntArray(ipix);
checkNside(nside);
const lonlat = wasm.HEALPixPix2Ang(nside, ipix)
return lonlat;
}
},
/**
* @namespace Sesame
* @memberof AladinUtils
* @description Namespace for Sesame related service.
*/
Sesame: {
/**
* Find RA, DEC for any target (object name or position) <br/>
* if successful, callback is called with an object {ra: {@link number}, dec: {@link number}} <br/>
* if not successful, errorCallback is called
*
* @function
* @memberof AladinUtils.Sesame
* @name resolveAstronomicalName
*
* @param {string} target - object name or position
* @param {Function} callback - if successful, callback is called with an object {ra: {@link number}, dec: {@link number}}
* @param {Function} errorCallback - if not successful, errorCallback is called with the error
*/
resolveAstronomicalName: Sesame.getTargetRADec
},
/**
* @deprecated
*
* Converts celestial coordinates (ra, dec) to screen coordinates (x, y) in pixels within the view.
* Use {@link Aladin.world2pix} instead
*
*
* @function
* @memberof AladinUtils
* @name radecToViewXy
*
* @param {number} ra - Right Ascension (RA) coordinate in degrees.
* @param {number} dec - Declination (Dec) coordinate in degrees.
* @param {Aladin} aladin - Aladin Lite object containing the WebAssembly API.
* @returns {number[]} A 2 elements array representing the screen coordinates [X, Y] in pixels.
*/
radecToViewXy: function(ra, dec, aladin) {
return aladin.world2pix(ra, dec);
},
/**
* Convert a number in degrees into a string<br>
*
* @function
* @memberof AladinUtils
* @name degreesToString
*
* @param numberDegrees number in degrees (integer or decimal)
* @return the formatted string
*
* @example <caption> Result in degrees </caption>
* // returns "1°"
* Numbers.degreesToString(1)
* @example <caption> Result in arcminutes </caption>
* // returns "6 arcmin"
* Numbers.degreesToString(0.1);
* @example <caption> Result in arcseconds </caption>
* // returns "36 arcsec"
* Numbers.degreesToString(0.01);
*/
// FIXME: Same as in the libs/astro/angle.js
degreesToString: function(numberDegrees) {
let setPrecision = 3
let degrees = numberDegrees | 0;
let notDegrees = Math.abs(numberDegrees - degrees);
let minutes = notDegrees * 60 | 0;
let seconds = ((notDegrees* 60 - minutes) *60).toPrecision(setPrecision);
if (degrees!=0) {
return numberDegrees.toPrecision(setPrecision) + '°';
}
else if (minutes!=0){
return (notDegrees * 60).toPrecision(setPrecision) + " arcmin";
}
else if (seconds!=0){
return seconds + " arcsec";
}
else {
return "0°";
}
}
};