From a4ea061f389e8e1dfdd61e1e668f6b8a6d2dae4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P=2E?= Date: Tue, 24 Sep 2024 23:03:36 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 + composer.json | 25 +++++++++ src/Exceptions/InvalidFitsException.php | 10 ++++ src/Exceptions/InvalidPathException.php | 12 ++++ src/Fits.php | 73 +++++++++++++++++++++++++ src/FitsHeader.php | 10 ++++ 6 files changed, 132 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Exceptions/InvalidFitsException.php create mode 100644 src/Exceptions/InvalidPathException.php create mode 100644 src/Fits.php create mode 100644 src/FitsHeader.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d8a7996 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +composer.lock +vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c9ed08c --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "dumbastro/fits-php", + "description": "A crappy and dumb FITS library for PHP to (possibly?) interact with astronomical images", + "type": "library", + "version": "0.1", + "require": { + "psr/log": "^3.0" + }, + "require-dev": { + "phpstan/phpstan": "^1.12", + "friendsofphp/php-cs-fixer": "^3.64", + "symfony/var-dumper": "^7.1", + "phpunit/phpunit": "^11.3" + }, + "scripts": { + "run-pstan": "vendor/bin/phpstan analyze src/ --level 6" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "Dumbastro\\FitsPhp\\": "src/" + } + }, + "minimum-stability": "beta" +} diff --git a/src/Exceptions/InvalidFitsException.php b/src/Exceptions/InvalidFitsException.php new file mode 100644 index 0000000..55a287b --- /dev/null +++ b/src/Exceptions/InvalidFitsException.php @@ -0,0 +1,10 @@ +byteStream = fopen($path, 'rb'); + $this->path = $path; + $this->size = filesize($this->path); + + if (! $this->validate()) { + throw new InvalidFitsException('The opened file is not a valid FITS image (invalid block size)'); + } + } + /** + * Validate the given FITS file based on block sizes + * + * From the FITS standard spec: + * > "Each FITS structure shall consist of an integral number of + * FITS blocks, which are each 2880 bytes (23040 bits) in length." + * (_Definition of the Flexible Image Transport System (FITS)_, ch. 3, par. 3.1) + */ + public function validate(): bool + { + if ($this->size % 2880 !== 0) { + return false; + } + + return true; + } + /** + * @todo Return FitsHeader object + * @return string + */ + #[\ReturnTypeWillChange] + public function header(): string + { + $contents = fread($this->byteStream, $this->size); + $end = strpos($contents, 'END'); + // Determine minimum integer number of blocks including 'END' position + $headerEnd = (($end - ($end % 2880)) / 2880 + 1) * 2880; + + return substr($contents, 0, $headerEnd); + } + + public function fromPath(string $path): void + { + $this->byteStream = fopen($path, 'rb'); + } +} diff --git a/src/FitsHeader.php b/src/FitsHeader.php new file mode 100644 index 0000000..068256f --- /dev/null +++ b/src/FitsHeader.php @@ -0,0 +1,10 @@ +