<jupyter_start><jupyter_text>SAMPLE 06. Make a class. create module 'fitsfile' => It is perform the preceding tasks (sample1 ~ 4) at once.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

class FitsFile:
	def __init__(self):
		# Initialize variables.
		self.file_path = ""
		self.header = ""
		self.data = ""

		self.bitpix = 0
		self.naxis = 0
		self.naxis1 = 0
		self.naxis2 = 0
		self.bzero = 0
		self.bscale = 1.0

    
	def get_value(self, keyword):
		value = ""
		records = int(len(self.header) / 80)

		for i in range(records):
			record = self.header[i*80:(i+1)*80]
			if(record.startswith(keyword)):
				begin = 9;
				end = record.find('/', begin, (i+1)*80)
				if(end == -1):
					end = (i+1)*80
				value = record[begin:end].strip()
				break
			if(record.startswith("END")):
				break

		return value


	def load_file(self, file_path):
		# Open a file.
		self.file_path = file_path
		file = open(self.file_path, "rb")


		# Read a header from the file.
		end_found = False
		while(end_found == False):
			buffer = file.read(2880)

			if(buffer == ""):
				break

			buffer = buffer.decode("ascii")
			self.header = self.header + buffer

			for i in range(80):
				record = buffer[i*80:(i+1)*80]
				if(record.startswith('END')):
					end_found = True
					break

		if (end_found == False):
			print("The file is not correct FITS format.")

      
		# Decode the header.
		value = self.get_value("BITPIX")
		if(value != ""):	self.bitpix = int(value)

		value = self.get_value("NAXIS")
		if(value != ""):	self.naxis = int(value)

		value = self.get_value("NAXIS1")
		if(value != ""):	self.naxis1 = int(value)

		value = self.get_value("NAXIS2")
		if(value != ""):	self.naxis2 = int(value)

		value = self.get_value("BZERO")
		if(value != ""):	self.bzero = int(value)

		value = self.get_value("BSCALE")
		if(value != ""):	self.bscale = float(value)


		# Read a data from the file.

		# binary data (byte)
		self.data = file.read(self.naxis1 * self.naxis2 * int(self.bitpix/8))

		# 1-D array (signed short)
		self.data = np.frombuffer(self.data, dtype=np.dtype(">i2"))

		# 2-D array (signed short)
		self.data = self.data.reshape((self.naxis2, self.naxis1))

		# physical data (float64)
		self.data = (self.data + self.bzero) / self.bscale


		# Close the file.
		file.close()

from google.colab import files
files.upload()
import matplotlib.pyplot as plt
import matplotlib.cm as cm

import fitsfile

fits = fitsfile.FitsFile()
fits.load_file("HorseHead.fits")


# Display an image.
plt.imshow(fits.data, cmap=cm.gray)
plt.show()
# Display an image.
plt.imshow(fits.data, cmap=cm.gray)
plt.grid(None)
plt.show()
# Display an image.
plt.imshow(fits.data, cmap=cm.bone)
plt.grid(None)
plt.show()
# Display an image.
plt.imshow(fits.data, cmap=cm.gist_heat)
plt.grid(None)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15596650383810187, 'fraction_numerical': 0.01849267271458479, 'mean_word_length': 3.4175654853620956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '612498', 'license': 'MIT', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1095, 'n_words': 247, 'path': 'horsehead_fitsfile.ipynb', 'repo_name': 'Hyenni/analysis_Astronomy_Data', 'size': '2866'}