-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen_fits.py
65 lines (54 loc) · 2.56 KB
/
open_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
###########################################
# Display .FITS file #
# Matheus J. Castro #
# Version 2.1 #
# Last Modification: 05/04/2020 #
###########################################
# learn more at: http://learn.astropy.org/FITS-images.html
# example for ZScale: https://qiita.com/phyblas/items/87667c250b29e195f7fb
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from matplotlib.colors import LogNorm
from astropy.visualization import ZScaleInterval, ImageNormalize, MinMaxInterval, PowerStretch
image = 'm31_proc.fits' # name for your fits file
image_file = fits.open(image)
image_data = image_file[0].data # get the extension 0 of your fits
# normally, the image is on first extension
if len(image_file) > 1:
image_mask = image_file[1].data # get the extension 1, on my case, the mask of pixel for data reduction
mask = 3
else:
mask = 2
head = image_file[0].header["DATE"] # example of print one data from fits header
print(head)
head = np.array(repr(image_file[0].header)) # example of print all header
print(head)
print(type(head))
np.savetxt("Header_{}.txt".format(image[:-5]), [head], fmt="%s") # save header on a .txt file
print(image_file.info()) # print some useful information about your fits
image_file.close()
print('Min:', np.min(image_data)) # min of image
print('Max:', np.max(image_data)) # max of image
print('Mean:', np.mean(image_data)) # mean of image
print('Stdev:', np.std(image_data)) # standard deviation of image
# from numpy
plt.figure(figsize=(12, 4))
plt.subplot(1, mask, 1)
plt.imshow(image_data, cmap='gray', origin='lower') # the primary image, by default, is set no MinMaxInterval
plt.title("Primary")
# plt.colorbar() # if you want to add a color bar on your subplot
# OPTIONS FOR DISPLAY IMAGES
# norm=LogNorm() --> display in a logarithmic color scale
# norm=ImageNormalize(image_data, interval=ZScaleInterval()) --> display in zscale
plt.subplot(1, mask, 2)
plt.imshow(image_data, cmap='gray', origin='lower', norm=ImageNormalize(image_data, interval=ZScaleInterval()))#
# , stretch=PowerStretch(5))) # add other type of stretch on the image
# (need to be inside of ImageNormalize() function)
# print the zscale image
plt.title("Zscale")
if mask == 3:
plt.subplot(133)
plt.imshow(image_mask, cmap='gray', origin='lower') # print the pixel mask
plt.title("Masked Pixels")
plt.show()