From 31d27cd6c033676ba231c4b3b40cd129788bce0b Mon Sep 17 00:00:00 2001 From: Ali Camarata Date: Tue, 14 Nov 2023 11:52:49 +0700 Subject: [PATCH] initial commit --- .gitignore | 5 +++++ CHANGELOG.md | 7 +++++++ LICENSE | 7 +++++++ README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ cycleMonth.js | 30 +++++++++++++++++++++++++++++ cycleYear.js | 30 +++++++++++++++++++++++++++++ index.d.ts | 12 ++++++++++++ index.js | 7 +++++++ package.json | 25 +++++++++++++++++++++++++ test.js | 12 ++++++++++++ 10 files changed, 187 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 cycleMonth.js create mode 100644 cycleYear.js create mode 100644 index.d.ts create mode 100644 index.js create mode 100644 package.json create mode 100644 test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..c99d0de2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +node_modules/ +dist/ +.env +.DS_Store +.vscode diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..7f460416 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +## [1.0.0] - 2023-11-14 + +- Initial release diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..fdb56f32 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +ISC License + +Copyright (c) 2023, [Your Full Name] + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 00000000..061ccd2a --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ + +# moon-cycle + +Simple functions to map a Date() to the current lunar phase and map that to hourly photos of the moon during that phase from NASA. + +Yearly takes the full photos for all of 2023 (8760 images) while Monthly take only the cycle starting from November 13 (708 images). + +The images have been organized, named, converted to webp, and optimized according to the following structure: +- "mm" (moon monthly) vs "my" (moon yearly) +- "256" (256x256 size) vs "512" (512x512 size) +- "75" (lower quality) vs "85" (higher quality) + +## Installation + +```bash +npm install praycalc +``` + +## Usage + +Copy any of the mm or my folders (or both) to your projects "public" directory. Then use as follows: + +```js +const { cycleMonth, cycleYear } = require('moon-cycle'); + +const date = new Date(); + +// Get results +const mm = cycleMonth(date) +const my = cycleYear(date) + +// Print results +console.log(`\nDate = ${date.toISOString()}\nMatch Moon Image URLs to Date..\n`) +console.log(`MM = /mm/${mm}`); +console.log(`MY = /my/${my}`); + + +``` + +Exported functions include cycleMonth and cycleYear + +### Parameters: + +- result: name of the monthly or yearly webp file + +## Contributing + +Contributions are welcome! + +## License + +ISC License diff --git a/cycleMonth.js b/cycleMonth.js new file mode 100644 index 00000000..31eabdce --- /dev/null +++ b/cycleMonth.js @@ -0,0 +1,30 @@ +function cycleMonth(date = new Date()) { + /** + * There are 708 images from 001.webp to 708.webp in + * the moon monthly (mm) folder. These are hourly + * photos from NASA's 2023 collection which directly + * relate to one per hour. The below function takes + * seconds since the last known new moon the figures + * number of Synodic months since then to find the + * current "hourly phase" for the current cycle of + * the moon. This relates R as always 1-708. + */ + + const startDate = new Date("2023-11-13T09:27:00Z"); + const synDays = 29.53058821398858; + const synSecs = synDays * 24 * 60 * 60; + + // X = number of seconds since 2023-11-13T09:27:00Z + const X = Math.floor((date.getTime() - startDate.getTime()) / 1000); + + // Y = get the number of months since + const Y = Math.floor(X / synSecs); + + // R = decimal time (minus years) * number of images + const R = Math.floor(((X / synSecs) - Y) * 708); + + let result = R.toString().padStart(3, '0')+'.webp'; + return result; +} + +module.exports = { cycleMonth }; diff --git a/cycleYear.js b/cycleYear.js new file mode 100644 index 00000000..d756fd22 --- /dev/null +++ b/cycleYear.js @@ -0,0 +1,30 @@ +function cycleYear(date = new Date()) { + /** + * There are 8760 images from 0001.webp to 8760.webp + * in the moon yearly (my) folder. These are hourly + * photos from NASA's 2023 collection which directly + * relate to one per hour. The below function takes + * hours (seconds / 3600) and maps directly to these + * 8760 images in the folders. The last calculation + * below rounds the cycle so R is always 1-8760. + */ + + const startDate = new Date("2023-01-01T00:00:00Z"); + + // X = number of seconds since 2023-01-01T00:00:00Z + const X = Math.floor((date.getTime() - startDate.getTime()) / 1000); + + // Y = convert to number of hours since + const Y = Math.floor(X / 3600); + + // Z = get the number of years since + const Z = Math.floor(Y / 8760); + + // R = decimal time (minus years) * number of images + const R = Math.floor(((Y / 8760) - Z) * 8760); + + let result = R.toString().padStart(4, '0')+'.webp'; + return result; +} + +module.exports = { cycleYear }; diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 00000000..e87db49f --- /dev/null +++ b/index.d.ts @@ -0,0 +1,12 @@ +// index.d.ts +declare module 'moon-cycle' { + export function cycleMonth(date: Date): MonthResult; + export function cycleYear(date: Date): YearResult; + + interface MonthResult { + result: string; + } + interface YearResult { + result: string; + } +} diff --git a/index.js b/index.js new file mode 100644 index 00000000..08ce34d6 --- /dev/null +++ b/index.js @@ -0,0 +1,7 @@ +const { cycleMonth } = require('./cycleMonth'); +const { cycleYear } = require('./cycleYear'); + +module.exports = { + cycleMonth, + cycleYear +}; diff --git a/package.json b/package.json new file mode 100644 index 00000000..6d00db1a --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "moon-cycle", + "version": "1.0.0", + "description": "NREL SPA native implementation in JS", + "main": "index.js", + "scripts": { + "test": "node test.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ussunnah/moon-cycle.git" + }, + "keywords": [ + "moon", + "cycle", + "images", + "nasa" + ], + "author": "USF", + "license": "ISC", + "bugs": { + "url": "https://github.com/ussunnah/moon-cycle/issues" + }, + "homepage": "https://github.com/ussunnah/moon-cycle#readme" +} diff --git a/test.js b/test.js new file mode 100644 index 00000000..7d9145ef --- /dev/null +++ b/test.js @@ -0,0 +1,12 @@ +const { cycleMonth, cycleYear } = require('./index'); + +const date = new Date(); + +// Get results +const mm = cycleMonth(date) +const my = cycleYear(date) + +// Print results +console.log(`\nDate = ${date.toISOString()}\nMatch Moon Image URLs to Date..\n`) +console.log(`MM = /mm/${mm}`); +console.log(`MY = /my/${my}`);