-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfits.py
810 lines (657 loc) · 27.2 KB
/
fits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
import os
import re
import shutil
import subprocess
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from typing import Pattern, Union
from warnings import warn
from astropy import units as u
from astropy.io import fits
from astropy.time import Time
from astropy.wcs import WCS
from dateutil.parser import parse as parse_date
from dateutil.tz import UTC
from loguru import logger
from panoptes.utils import error
from panoptes.utils.time import flatten_time
PATH_MATCHER: Pattern[str] = re.compile(r"""^
(?P<pre_info>.*)? # Anything before unit_id
(?P<unit_id>PAN\d{3}) # unit_id - PAN + 3 digits
/?(?P<field_name>.*)? # Legacy field name - any
/(?P<camera_id>[a-gA-G0-9]{6}) # camera_id - 6 digits
/(?P<sequence_time>[0-9]{8}T[0-9]{6}) # Observation start time
/(?P<image_time>[0-9]{8}T[0-9]{6}) # Image start time
(?P<post_info>.*)? # Anything after (file ext)
$""",
re.VERBOSE)
@dataclass
class ObservationPathInfo:
"""Parse the location path for an image.
This is a small dataclass that offers some convenience methods for dealing
with a path based on the image id.
This would usually be instantiated via `path`:
>>> from panoptes.utils.images.fits import ObservationPathInfo # noqa
>>> bucket_path = 'gs://panoptes-images-background/PAN012/Hd189733/358d0f/20180824T035917/20180824T040118.fits'
>>> path_info = ObservationPathInfo(path=bucket_path)
>>> path_info.id
'PAN012_358d0f_20180824T035917_20180824T040118'
>>> path_info.unit_id
'PAN012'
>>> path_info.sequence_id
'PAN012_358d0f_20180824T035917'
>>> path_info.image_id
'PAN012_358d0f_20180824T040118'
>>> path_info.as_path(base='/tmp', ext='jpg')
PosixPath('/tmp/PAN012/358d0f/20180824T035917/20180824T040118.jpg')
>>> ObservationPathInfo(path='foobar')
Traceback (most recent call last):
...
ValueError: Invalid path received: self.path='foobar'
>>> # Works from a fits file directly, which reads header.
>>> fits_fn = getfixture('unsolved_fits_file')
>>> path_info = ObservationPathInfo.from_fits(fits_fn)
>>> path_info.unit_id
'PAN001'
"""
unit_id: str = None
camera_id: str = None
field_name: str = None
sequence_time: Union[str, datetime, Time] = None
image_time: Union[str, datetime, Time] = None
path: Union[str, Path] = None
def __post_init__(self):
"""Parse the path when provided upon initialization."""
if self.path is not None:
path_match = PATH_MATCHER.match(self.path)
if path_match is None:
raise ValueError(f'Invalid path received: {self.path}')
self.unit_id = path_match.group('unit_id')
self.camera_id = path_match.group('camera_id')
self.field_name = path_match.group('field_name')
self.sequence_time = Time(parse_date(path_match.group('sequence_time')))
self.image_time = Time(parse_date(path_match.group('image_time')))
@property
def id(self):
"""Full path info joined with underscores"""
return self.get_full_id()
@property
def sequence_id(self) -> str:
"""The sequence id."""
return f'{self.unit_id}_{self.camera_id}_{flatten_time(self.sequence_time)}'
@property
def image_id(self) -> str:
"""The matched image id."""
return f'{self.unit_id}_{self.camera_id}_{flatten_time(self.image_time)}'
def as_path(self, base: Union[Path, str] = None, ext: str = None) -> Path:
"""Return a Path object."""
image_str = flatten_time(self.image_time)
if ext is not None:
image_str = f'{image_str}.{ext}'
full_path = Path(self.unit_id, self.camera_id, flatten_time(self.sequence_time), image_str)
if base is not None:
full_path = base / full_path
return full_path
def get_full_id(self, sep='_') -> str:
"""Returns the full path id with the given separator."""
return f'{sep}'.join([
self.unit_id,
self.camera_id,
flatten_time(self.sequence_time),
flatten_time(self.image_time)
])
@classmethod
def from_fits_header(cls, header):
try:
new_instance = cls(path=header['FILENAME'])
except ValueError:
sequence_id = header['SEQID']
image_id = header['IMAGEID']
unit_id, camera_id, sequence_time = sequence_id.split('_')
_, _, image_time = image_id.split('_')
new_instance = cls(unit_id=unit_id,
camera_id=camera_id,
sequence_time=Time(parse_date(sequence_time)),
image_time=Time(parse_date(image_time)))
return new_instance
@classmethod
def from_fits(cls, fits_file):
return cls.from_fits_header(getheader(fits_file))
def solve_field(fname, timeout=15, solve_opts=None, *args, **kwargs):
""" Plate solves an image.
Note: This is a low-level wrapper around the underlying `solve-field`
program. See `get_solve_field` for more typical usage and examples.
Args:
fname(str, required): Filename to solve in .fits extension.
timeout(int, optional): Timeout for the solve-field command,
defaults to 60 seconds.
solve_opts(list, optional): List of options for solve-field.
"""
solve_field_script = shutil.which('solve-field')
if solve_field_script is None: # pragma: no cover
raise error.InvalidSystemCommand("Can't find solve-field, is astrometry.net installed?")
# Add the options for solving the field
if solve_opts is not None:
options = solve_opts
else:
# Default options
options = [
'--guess-scale',
'--cpulimit', str(timeout),
'--no-verify',
'--crpix-center',
'--temp-axy',
'--index-xyls', 'none',
'--solved', 'none',
'--match', 'none',
'--rdls', 'none',
'--corr', 'none',
'--downsample', '4',
'--no-plots',
]
if 'ra' in kwargs:
options.append('--ra')
options.append(str(kwargs.get('ra')))
if 'dec' in kwargs:
options.append('--dec')
options.append(str(kwargs.get('dec')))
if 'radius' in kwargs:
options.append('--radius')
options.append(str(kwargs.get('radius')))
# Gather all the kwargs that start with `--` and are not already present.
logger.debug(f'Adding kwargs: {kwargs!r}')
def _modify_opt(opt, val):
if isinstance(val, bool):
opt_string = str(opt)
else:
opt_string = f'{opt}={val}'
return opt_string
options.extend([_modify_opt(opt, val)
for opt, val
in kwargs.items()
if opt.startswith('--') and opt not in options])
cmd = [solve_field_script] + options + [fname]
logger.debug(f'Solving with: {cmd}')
try:
proc = subprocess.Popen(cmd,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except Exception as e:
raise error.PanError(f"Problem plate-solving in solve_field: {e!r}")
return proc
def get_solve_field(fname, replace=True, overwrite=True, timeout=30, **kwargs):
"""Convenience function to wait for `solve_field` to finish.
This function merely passes the `fname` of the image to be solved along to `solve_field`,
which returns a subprocess.Popen object. This function then waits for that command
to complete, populates a dictonary with the EXIF informaiton and returns. This is often
more useful than the raw `solve_field` function.
Example:
>>> from panoptes.utils.images import fits as fits_utils
>>> # Get our fits filename.
>>> fits_fn = getfixture('unsolved_fits_file')
>>> # Perform the solve.
>>> solve_info = fits_utils.get_solve_field(fits_fn) # doctest: +SKIP
>>> # Show solved filename.
>>> solve_info['solved_fits_file'] # doctest: +SKIP
'.../unsolved.fits'
>>> # Pass a suggested location.
>>> ra = 15.23
>>> dec = 90
>>> radius = 5 # deg
>>> solve_info = fits_utils.solve_field(fits_fn, ra=ra, dec=dec, radius=radius) # doctest: +SKIP
>>> # Pass kwargs to `solve-field` program.
>>> solve_kwargs = {'--pnm': '/tmp/awesome.bmp'}
>>> solve_info = fits_utils.get_solve_field(fits_fn, skip_solved=False, **solve_kwargs) # doctest: +SKIP
>>> assert os.path.exists('/tmp/awesome.bmp') # doctest: +SKIP
Args:
fname ({str}): Name of FITS file to be solved.
replace (bool, optional): Saves the WCS back to the original file,
otherwise output base filename with `.new` extension. Default True.
overwrite (bool, optional): Clobber file, default True. Required if `replace=True`.
timeout (int, optional): The timeout for solving, default 30 seconds.
**kwargs ({dict}): Options to pass to `solve_field` should start with `--`.
Returns:
dict: Keyword information from the solved field.
"""
skip_solved = kwargs.get('skip_solved', True)
out_dict = {}
header = getheader(fname)
wcs = WCS(header)
# Check for solved file
if skip_solved and wcs.is_celestial:
logger.info(f"Skipping solved file (use skip_solved=False to solve again): {fname}")
out_dict.update(header)
out_dict['solved_fits_file'] = fname
return out_dict
# Set a default radius of 15
if overwrite:
kwargs['--overwrite'] = True
# Use unpacked version of file.
was_compressed = False
if fname.endswith('.fz'):
logger.debug(f'Uncompressing {fname}')
fname = funpack(fname)
logger.debug(f'Using {fname} for solving')
was_compressed = True
logger.debug(f'Use solve arguments: {kwargs!r}')
proc = solve_field(fname, timeout=timeout, **kwargs)
try:
# Timeout plus a small buffer.
output, errs = proc.communicate(timeout=(timeout))
except subprocess.TimeoutExpired:
proc.kill()
output, errs = proc.communicate()
raise error.Timeout(f'Timeout while solving: {output!r} {errs!r}')
else:
if proc.returncode != 0:
logger.debug(f'Returncode: {proc.returncode}')
for log in [output, errs]:
if log and log > '':
logger.debug(f'Output on {fname}: {log}')
if proc.returncode == 3:
raise error.SolveError(f'solve-field not found: {output}')
new_fname = fname.replace('.fits', '.new')
if replace:
logger.debug(f'Overwriting original {fname}')
os.replace(new_fname, fname)
else:
fname = new_fname
try:
header = getheader(fname)
header.remove('COMMENT', ignore_missing=True, remove_all=True)
header.remove('HISTORY', ignore_missing=True, remove_all=True)
out_dict.update(header)
except OSError:
logger.warning(f"Can't read fits header for: {fname}")
# Check it was solved.
if WCS(header).is_celestial is False:
raise error.SolveError('File not properly solved, no WCS header present.')
# Remove WCS file.
os.remove(fname.replace('.fits', '.wcs'))
if was_compressed and replace:
logger.debug(f'Compressing plate-solved {fname}')
fname = fpack(fname)
out_dict['solved_fits_file'] = fname
return out_dict
def get_wcsinfo(fits_fname, **kwargs):
"""Returns the WCS information for a FITS file.
Uses the `wcsinfo` astrometry.net utility script to get the WCS information
from a plate-solved file.
Args:
fits_fname ({str}): Name of a FITS file that contains a WCS.
**kwargs: Args that can be passed to wcsinfo.
Returns:
dict: Output as returned from `wcsinfo`.
Raises:
error.InvalidCommand: Raised if `wcsinfo` is not found (part of astrometry.net)
"""
assert os.path.exists(fits_fname), warn(f"No file exists at: {fits_fname}")
wcsinfo = shutil.which('wcsinfo')
if wcsinfo is None:
raise error.InvalidCommand('wcsinfo not found')
run_cmd = [wcsinfo, fits_fname]
if fits_fname.endswith('.fz'):
run_cmd.append('-e')
run_cmd.append('1')
proc = subprocess.Popen(run_cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, universal_newlines=True)
try:
output, errs = proc.communicate(timeout=5)
except subprocess.TimeoutExpired: # pragma: no cover
proc.kill()
output, errs = proc.communicate()
unit_lookup = {
'crpix0': u.pixel,
'crpix1': u.pixel,
'crval0': u.degree,
'crval1': u.degree,
'cd11': (u.deg / u.pixel),
'cd12': (u.deg / u.pixel),
'cd21': (u.deg / u.pixel),
'cd22': (u.deg / u.pixel),
'imagew': u.pixel,
'imageh': u.pixel,
'pixscale': (u.arcsec / u.pixel),
'orientation': u.degree,
'ra_center': u.degree,
'dec_center': u.degree,
'orientation_center': u.degree,
'ra_center_h': u.hourangle,
'ra_center_m': u.minute,
'ra_center_s': u.second,
'dec_center_d': u.degree,
'dec_center_m': u.minute,
'dec_center_s': u.second,
'fieldarea': (u.degree * u.degree),
'fieldw': u.degree,
'fieldh': u.degree,
'decmin': u.degree,
'decmax': u.degree,
'ramin': u.degree,
'ramax': u.degree,
'ra_min_merc': u.degree,
'ra_max_merc': u.degree,
'dec_min_merc': u.degree,
'dec_max_merc': u.degree,
'merc_diff': u.degree,
}
wcs_info = {}
for line in output.split('\n'):
try:
k, v = line.split(' ')
try:
v = float(v)
except Exception:
pass
wcs_info[k] = float(v) * unit_lookup.get(k, 1)
except ValueError:
pass
# print("Error on line: {}".format(line))
wcs_info['wcs_file'] = fits_fname
return wcs_info
def fpack(fits_fname, unpack=False, overwrite=True):
"""Compress/Decompress a FITS file
Uses `fpack` (or `funpack` if `unpack=True`) to compress a FITS file
Args:
fits_fname ({str}): Name of a FITS file that contains a WCS.
unpack ({bool}, optional): file should decompressed instead of compressed, default False.
Returns:
str: Filename of compressed/decompressed file.
"""
assert os.path.exists(fits_fname), warn(
"No file exists at: {}".format(fits_fname))
if unpack:
fpack = shutil.which('funpack')
run_cmd = [fpack, '-D', fits_fname]
out_file = fits_fname.replace('.fz', '')
else:
fpack = shutil.which('fpack')
run_cmd = [fpack, '-D', '-Y', fits_fname]
out_file = fits_fname.replace('.fits', '.fits.fz')
if os.path.exists(out_file):
if overwrite is False:
raise FileExistsError('Destination file already exists at location and overwrite=False')
else:
os.remove(out_file)
try:
assert fpack is not None
except AssertionError:
warn("fpack not found (try installing cfitsio). File has not been changed")
return fits_fname
logger.debug("fpack command: {}".format(run_cmd))
proc = subprocess.Popen(run_cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, universal_newlines=True)
try:
output, errs = proc.communicate(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
output, errs = proc.communicate()
return out_file
def funpack(*args, **kwargs):
"""Unpack a FITS file.
Note:
This is a thin-wrapper around the ~fpack function
with the `unpack=True` option specified. See ~fpack
documentation for details.
Args:
*args: Arguments passed to ~fpack.
**kwargs: Keyword arguments passed to ~fpack.
Returns:
str: Path to uncompressed FITS file.
"""
return fpack(*args, unpack=True, **kwargs)
def write_fits(data, header, filename, exposure_event=None, compress=False, **kwargs):
"""Write FITS file to requested location.
>>> from panoptes.utils.images import fits as fits_utils
>>> data = np.random.normal(size=100)
>>> header = { 'FILE': 'delete_me', 'TEST': True }
>>> filename = str(getfixture('tmpdir').join('temp.fits'))
>>> fits_utils.write_fits(data, header, filename)
>>> assert os.path.exists(filename)
>>> fits_utils.getval(filename, 'FILE')
'delete_me'
>>> data2 = fits_utils.getdata(filename)
>>> assert np.array_equal(data, data2)
Args:
data (array_like): The data to be written.
header (dict): Dictionary of items to be saved in header.
filename (str): Path to filename for output.
exposure_event (None|`threading.Event`, optional): A `threading.Event` that
can be triggered when the image is written.
kwargs (dict): Options that are passed to the `astropy.io.fits.PrimaryHDU.writeto`
method.
"""
if not isinstance(header, fits.Header):
header = fits.Header(header)
if compress:
hdu = fits.CompImageHDU(data, header=header,
compression_type='GZIP_1')
else:
hdu = fits.PrimaryHDU(data, header=header)
# Create directories if required.
if os.path.dirname(filename):
os.makedirs(os.path.dirname(filename), mode=0o775, exist_ok=True)
try:
hdu.writeto(filename, **kwargs)
except OSError as err:
logger.error(f'Error writing image to {filename}: {err!r}')
else:
logger.debug(f'Image written to {filename}')
finally:
if exposure_event:
exposure_event.set()
def update_observation_headers(file_path, info):
"""Update FITS headers with items from the Observation status.
>>> # Check the headers
>>> from panoptes.utils.images import fits as fits_utils
>>> fits_fn = getfixture('unsolved_fits_file')
>>> # Show original value
>>> fits_utils.getval(fits_fn, 'FIELD')
'KIC 8462852'
>>> info = {'field_name': 'Tabbys Star'}
>>> update_observation_headers(fits_fn, info)
>>> # Show new value
>>> fits_utils.getval(fits_fn, 'FIELD')
'Tabbys Star'
Args:
file_path (str): Path to a FITS file.
info (dict): The return dict from `pocs.observatory.Observation.status`,
which includes basic information about the observation.
"""
with fits.open(file_path, 'update') as f:
hdu = f[0]
hdu.header.set('IMAGEID', info.get('image_id', ''))
hdu.header.set('SEQID', info.get('sequence_id', ''))
hdu.header.set('FIELD', info.get('field_name', ''))
hdu.header.set('RA-MNT', info.get('ra_mnt', ''), 'Degrees')
hdu.header.set('HA-MNT', info.get('ha_mnt', ''), 'Degrees')
hdu.header.set('DEC-MNT', info.get('dec_mnt', ''), 'Degrees')
hdu.header.set('EQUINOX', info.get('equinox', 2000.)) # Assume J2000
hdu.header.set('AIRMASS', info.get('airmass', ''), 'Sec(z)')
hdu.header.set('FILTER', info.get('filter', ''))
hdu.header.set('LAT-OBS', info.get('latitude', ''), 'Degrees')
hdu.header.set('LONG-OBS', info.get('longitude', ''), 'Degrees')
hdu.header.set('ELEV-OBS', info.get('elevation', ''), 'Meters')
hdu.header.set('MOONSEP', info.get('moon_separation', ''), 'Degrees')
hdu.header.set('MOONFRAC', info.get('moon_fraction', ''))
hdu.header.set('CREATOR', info.get('creator', ''), 'POCS Software version')
hdu.header.set('INSTRUME', info.get('camera_uid', ''), 'Camera ID')
hdu.header.set('OBSERVER', info.get('observer', ''), 'PANOPTES Unit ID')
hdu.header.set('ORIGIN', info.get('origin', ''))
hdu.header.set('RA-RATE', info.get('tracking_rate_ra', ''), 'RA Tracking Rate')
def extract_metadata(header: fits.Header) -> dict:
"""Get the metadata from a FITS image.
This function parses some of the more common headers (some from the
`update_observation_headers` but others as well) and puts them into a dict
with the obvious data types converted into objects (e.g. dates and times).
>>> # Check the headers
>>> from panoptes.utils.images import fits as fits_utils
>>> fits_fn = getfixture('unsolved_fits_file')
>>> header = fits_utils.getheader(fits_fn)
>>> metadata = extract_metadata(header)
>>> metadata['unit']['name']
'PAN001'
Args:
header (astropy.io.fits.Header): The Header object from a FITS file.
"""
path_info = ObservationPathInfo.from_fits_header(header)
try:
# Add a units doc if it doesn't exist.
unit_info = dict(
name=header.get('OBSERVER'),
latitude=header.get('LAT-OBS'),
longitude=header.get('LONG-OBS'),
elevation=float(header.get('ELEV-OBS')),
)
sequence_info = dict(
time=path_info.sequence_time.to_datetime(timezone=UTC),
exptime=float(header.get('EXPTIME')),
software_version=header.get('CREATOR', ''),
field_name=header.get('FIELD', ''),
iso=header.get('ISO'),
ra=header.get('CRVAL1'),
dec=header.get('CRVAL2'),
camera_id=path_info.camera_id,
camera_serial_number=str(header.get('CAMSN')),
lens_serial_number=header.get('INTSN'),
)
measured_rggb = header.get('MEASRGGB', '0 0 0 0').split(' ')
if 'DATE' in header:
file_date = parse_date(header.get('DATE')).replace(tzinfo=UTC)
else:
file_date = path_info.image_time.to_datetime(timezone=UTC)
camera_date = parse_date(header.get('DATE-OBS', path_info.image_time)).replace(tzinfo=UTC)
image_info = dict(
airmass=header.get('AIRMASS'),
camera=dict(
dateobs=camera_date,
blue_balance=float(header.get('BLUEBAL')),
circconf=float(header.get('CIRCCONF', '0.').split(' ')[0]),
colortemp=float(header.get('COLORTMP')),
measured_ev=float(header.get('MEASEV')),
measured_ev2=float(header.get('MEASEV2')),
measured_r=float(measured_rggb[0]),
measured_g1=float(measured_rggb[1]),
measured_g2=float(measured_rggb[2]),
measured_b=float(measured_rggb[3]),
red_balance=float(header.get('REDBAL')),
temperature=float(header.get('CAMTEMP', 0).split(' ')[0]),
white_lvln=header.get('WHTLVLN'),
white_lvls=header.get('WHTLVLS'),
),
exptime=float(header.get('EXPTIME')),
file_creation_date=file_date,
moonfrac=float(header.get('MOONFRAC')),
moonsep=float(header.get('MOONSEP')),
mount_dec=header.get('DEC-MNT'),
mount_ha=header.get('HA-MNT'),
mount_ra=header.get('RA-MNT'),
time=path_info.image_time.to_datetime(timezone=UTC),
)
metadata = dict(
unit=unit_info,
sequence=sequence_info,
image=image_info,
)
except Exception as e:
raise error.PanError(f'Error in extracting metadata: {e!r}')
logger.success('Metadata extracted from header')
return metadata
def getdata(fn, *args, **kwargs):
"""Get the FITS data.
Small wrapper around `astropy.io.fits.getdata` to auto-determine
the FITS extension. This will return the data associated with the
image.
>>> fits_fn = getfixture('solved_fits_file')
>>> d0 = getdata(fits_fn)
>>> d0
array([[2215, 2169, 2200, ..., 2169, 2235, 2168],
[2123, 2191, 2133, ..., 2212, 2127, 2217],
[2208, 2154, 2209, ..., 2159, 2233, 2187],
...,
[2120, 2201, 2120, ..., 2209, 2126, 2195],
[2219, 2151, 2199, ..., 2173, 2214, 2166],
[2114, 2194, 2122, ..., 2202, 2125, 2204]], dtype=uint16)
>>> d1, h1 = getdata(fits_fn, header=True)
>>> (d0 == d1).all()
True
>>> h1['FIELD']
'KIC 8462852'
Args:
fn (str): Path to FITS file.
*args: Passed to `astropy.io.fits.getdata`.
**kwargs: Passed to `astropy.io.fits.getdata`.
Returns:
`np.ndarray`: The FITS data.
"""
return fits.getdata(fn, *args, **kwargs)
def getheader(fn, *args, **kwargs):
"""Get the FITS header.
Small wrapper around `astropy.io.fits.getheader` to auto-determine
the FITS extension. This will return the header associated with the
image. If you need the compression header information use the astropy
module directly.
>>> fits_fn = getfixture('tiny_fits_file')
>>> os.path.basename(fits_fn)
'tiny.fits'
>>> header = getheader(fits_fn)
>>> header['IMAGEID']
'PAN001_XXXXXX_20160909T081152'
>>> # Works with fpacked files
>>> fits_fn = getfixture('solved_fits_file')
>>> os.path.basename(fits_fn)
'solved.fits.fz'
>>> header = getheader(fits_fn)
>>> header['IMAGEID']
'PAN001_XXXXXX_20160909T081152'
Args:
fn (str): Path to FITS file.
*args: Passed to `astropy.io.fits.getheader`.
**kwargs: Passed to `astropy.io.fits.getheader`.
Returns:
`astropy.io.fits.header.Header`: The FITS header for the data.
"""
ext = 0
if fn.endswith('.fz'):
ext = 1
return fits.getheader(fn, ext=ext)
def getwcs(fn, *args, **kwargs):
"""Get the WCS for the FITS file.
Small wrapper around `astropy.wcs.WCS`.
>>> from panoptes.utils.images import fits as fits_utils
>>> fits_fn = getfixture('solved_fits_file')
>>> wcs = fits_utils.getwcs(fits_fn)
>>> wcs.is_celestial
True
>>> fits_fn = getfixture('unsolved_fits_file')
>>> wcs = fits_utils.getwcs(fits_fn)
>>> wcs.is_celestial
False
Args:
fn (str): Path to FITS file.
*args: Passed to `astropy.io.fits.getheader`.
**kwargs: Passed to `astropy.io.fits.getheader`.
Returns:
`astropy.wcs.WCS`: The World Coordinate System information.
"""
return WCS(getheader(fn, *args, **kwargs), *args, **kwargs)
def getval(fn, *args, **kwargs):
"""Get a value from the FITS header.
Small wrapper around `astropy.io.fits.getval` to auto-determine
the FITS extension. This will return the value from the header
associated with the image (not the compression header). If you need
the compression header information use the astropy module directly.
>>> fits_fn = getfixture('tiny_fits_file')
>>> getval(fits_fn, 'IMAGEID')
'PAN001_XXXXXX_20160909T081152'
Args:
fn (str): Path to FITS file.
Returns:
str or float: Value from header (with no type conversion).
"""
ext = 0
if fn.endswith('.fz'):
ext = 1
return fits.getval(fn, *args, ext=ext, **kwargs)