Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Commit

Permalink
Enable platesolving from any image
Browse files Browse the repository at this point in the history
  • Loading branch information
GuLinux committed Jun 18, 2020
1 parent 064fd1a commit 6c8b180
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
44 changes: 40 additions & 4 deletions backend/platesolving/platesolving.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from static_settings import StaticSettings
import astropy.units as u
from astropy.coordinates import SkyCoord, Angle
from astropy.io import fits
from PIL import Image, UnidentifiedImageError
import numpy as np

class PlateSolving:

Expand Down Expand Up @@ -100,18 +103,51 @@ def is_close_enough():
finally:
this.__set_status('idle')

def __check_fits_file(self, file):
try:
with fits.open(file, mode='readonly'):
pass
except OSError:
return False
return True

def __data_to_fits(self, data, temp_path):
temp_upload_prefix = os.path.join(temp_path, 'solve_field_input')
fits_file_path = temp_upload_prefix + '.fits'
with open(temp_upload_prefix, 'wb') as file:
file.write(data)

if self.__check_fits_file(temp_upload_prefix):
shutil.move(temp_upload_prefix, fits_file_path)
else:
if not self.__img_to_fits(temp_upload_prefix, fits_file_path, remove=True):
raise BadRequestError('File is not a FITS nor a recognizable image format')
return fits_file_path

def __img_to_fits(self, temp_file, destination, remove=False):
try:
logger.debug('Converting image to FITS format')
img = Image.open(temp_file)
xsize, ysize = img.size
data = np.array(img.convert('L').getdata(), dtype=np.uint8).reshape(ysize, xsize)
fits_file = fits.PrimaryHDU(data=data)
fits_file.writeto(destination)
if remove:
os.remove(temp_file)
return True
except UnidentifiedImageError:
return False


def __start_solver(self, options):
temp_path = os.path.join(StaticSettings.ASTROMETRY_TEMP_PATH, 'solve_field_{}'.format(time.time()))
os.makedirs(temp_path, exist_ok=True)

fits_file_path = None
logger.debug('Solve field options: %s', str(['{}: {}'.format(key, '<blob>' if key == 'fileBuffer' else value) for key, value in options.items()]))
if 'fileBuffer' in options:
fits_file_path = os.path.join(temp_path, 'solve_field_input.fits')
data = base64.b64decode(options['fileBuffer'][options['fileBuffer'].find(PlateSolving.DATAURL_SEPARATOR) + len(PlateSolving.DATAURL_SEPARATOR):])
with open(fits_file_path, 'wb') as file:
file.write(data)

fits_file_path = self.__data_to_fits(data, temp_path)
elif 'filePath' in options and os.path.isfile(options['filePath']):
fits_file_path = options['filePath']
elif 'cameraOptions' in options:
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/PlateSolving/PlateSolving.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ export class PlateSolving extends React.Component {
<Grid.Column width={3} verticalAlign='middle'><Header size='tiny' content='Solve from'/></Grid.Column>
<Grid.Column width={13}>
{this.optionButton(Options.camera, true, {content: 'Camera shot'})}
{this.optionButton(Options.camera, false, {content: 'FITS file'})}
{this.optionButton(Options.camera, false, {content: 'Image file'})}
{ !options[Options.camera] &&
<UploadFileDialog
title='Upload FITS'
trigger={<Button disabled={loading} icon='upload' content='Upload FITS' primary size='mini'/>}
title='Upload Image'
trigger={<Button disabled={loading} icon='upload' content='Upload Image' primary size='mini'/>}
readAsDataURL={true}
onFileUploaded={this.onFileUploaded}
/>
Expand Down Expand Up @@ -329,8 +329,8 @@ export class PlateSolving extends React.Component {
</Segment>
<Segment>
<UploadFileDialog
title='Upload FITS'
trigger={<Button icon='upload' content='Upload existing FITS as target' primary size='mini'/>}
title='Upload Image'
trigger={<Button icon='upload' content='Upload existing image as target' primary size='mini'/>}
readAsDataURL={true}
onFileUploaded={this.onTargetFileUploaded}
/>
Expand Down

0 comments on commit 6c8b180

Please sign in to comment.