Initial commit
This commit is contained in:
commit
a4ea061f38
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
composer.lock
|
||||
vendor/
|
25
composer.json
Normal file
25
composer.json
Normal file
@ -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"
|
||||
}
|
10
src/Exceptions/InvalidFitsException.php
Normal file
10
src/Exceptions/InvalidFitsException.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dumbastro\FitsPhp\Exceptions;
|
||||
|
||||
final class InvalidFitsException extends \Exception implements \Throwable
|
||||
{
|
||||
}
|
||||
|
12
src/Exceptions/InvalidPathException.php
Normal file
12
src/Exceptions/InvalidPathException.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dumbastro\FitsPhp\Exceptions;
|
||||
|
||||
/**
|
||||
* @todo Different exception if path is not writable?
|
||||
*/
|
||||
final class InvalidPathException extends \Exception
|
||||
{
|
||||
}
|
73
src/Fits.php
Normal file
73
src/Fits.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dumbastro\FitsPhp;
|
||||
|
||||
use Dumbastro\FitsPhp\Exceptions\{
|
||||
InvalidFitsException,
|
||||
InvalidPathException,
|
||||
};
|
||||
|
||||
class Fits
|
||||
{
|
||||
/**
|
||||
* @var resource $byteStream
|
||||
*/
|
||||
private $byteStream;
|
||||
private string $path;
|
||||
public readonly int $size;
|
||||
|
||||
/**
|
||||
* @throws InvalidFitsException, InvalidPathException
|
||||
* @todo Check path for reading/writing errors
|
||||
*/
|
||||
public function __construct(string $path)
|
||||
{
|
||||
if (! is_readable($path) || ! is_file($path)) {
|
||||
throw new InvalidPathException("The path '$path' is not readable or is not a file.");
|
||||
}
|
||||
$this->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');
|
||||
}
|
||||
}
|
10
src/FitsHeader.php
Normal file
10
src/FitsHeader.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dumbastro\FitsPhp;
|
||||
|
||||
class FitsHeader
|
||||
{
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user