This repository has been archived by the owner on Apr 26, 2018. It is now read-only.
/
loadFITS.py
141 lines (132 loc) · 6.8 KB
/
loadFITS.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
'''
@title loadFITS
@author: Rebecca Coles
Updated on Apr 6, 2016
Makes a directory for output products and move output products into it
Loads the flats fits files
Saves data to a fits file
'''
# Import
import glob, os, time, datetime
from astropy.io import fits
from loadConfig import loadConfig
from numpy import zeros, empty, append, array
from sys import exit, stdout
def outputsFolder(files_path, *default_parameters, **keyword_parameters):
#make a directory for output products and move output products into it
####################################################################################
#Optional parameters:
# 'newFolder': indicates that you want to create a new output product folder
# (in the same directory as the fits files folder)
# 'moveFileName' = filename: indicates that you want to move the named file into
# the output product folder
#Note: if you are making a new dir (newFolder): files_path is the fits files location.
# if you are moving files to the output folder (moveFileName): files_path is the
# location of the output folder.
####################################################################################
#'newFolder'
#Create folder for output products named with the date
if ('newFolder' in keyword_parameters):
today = datetime.date.today() # get today's date as a datetime type
output_path = os.path.dirname(os.path.dirname(files_path)) #Here files_path is fits files location
output_folder_name = 'LINT_Output_' + today.isoformat()
#Make output dir. Append # to dir name if dir already exists
if not os.path.exists(os.path.join(output_path, output_folder_name)): #No LINT output files exist for today's date
os.mkdir(os.path.join(output_path, output_folder_name))
new_folder_path = os.path.join(output_path, output_folder_name)
print('New folder created for output products: ', new_folder_path)
return new_folder_path
else: #Folder does exist
folder_exist = True
iterator = 1
while folder_exist == True:
output_product_folder_name = output_folder_name + '_Group-' + str(iterator)
if not os.path.exists(os.path.join(output_path, output_product_folder_name)):
os.mkdir(os.path.join(output_path, output_product_folder_name))
new_folder_path = os.path.join(output_path, output_product_folder_name)
folder_exist = False
iterator += 1
print('New folder created for output products (in directory that contains fits files directory): ', new_folder_path)
return new_folder_path
#'moveFileName'
#Just moving files, not making a dir. Here, files_path is where you want to move the files to
elif ('moveFileName' in keyword_parameters):
if os.path.isfile(os.path.join(os.getcwd(), keyword_parameters['moveFileName'])):
os.rename(os.path.join(os.getcwd(), keyword_parameters['moveFileName']), os.path.join(files_path, keyword_parameters['moveFileName']))
def makeList(fileDir):
#make a list of the files in the directory
#setting up to list files
filepathsAndFileNames = glob.glob(fileDir + '*')
return filepathsAndFileNames
def openFiles(filepathsAndFileNames, ext, rows, columns):
#load the fits file image data into a list (3D array)
#load files
print('Opening FITS files...')
fitsImages = [fits.getdata(image, ext) for image in filepathsAndFileNames]
#convert to 3D numpy array
numpyArray = array(fitsImages)
return numpyArray
def saveFITS(fitsPath, invertedImage, outputFITS):
#save an numpy array as a fits file
#create output dir
output_folder_path = outputsFolder(fitsPath, newFolder=True)
#name for output fits file
outputName = outputFITS + '_scaled-stacked-inverted.fits'
print( 'Scaled/Stacked/Inverted file: ', outputName)
#save fits
hdu = fits.PrimaryHDU(invertedImage)
hdu.writeto(output_folder_path + '/' + outputName, overwrite=True)
f = open(output_folder_path + '/' + outputName, 'r')
return f.name, output_folder_path
def imageDimensionTest(rows, columns, filepathsAndFileNames, ext):
#Scan the images in the array to insure that they are all the same size dimensionally.
#If an image is not the proper size, it will be removed from the array.
#The first image in the array will be used to set the standard size.
#get dimensions from first image
dimensions = [rows, columns]
badSize = []
print('LINT will now make sure that all of the images have the same dimensions.')
print('The preferred dimensions of the images (as specified in the LINT.config file): ', rows, 'rows x ', columns, 'columns')
# Initial call to print 0% progress
tt = 0
print_progress(tt, len(filepathsAndFileNames), prefix = 'Progress:', suffix = 'Complete', decimals=2, bar_length=50)
#check dimensions
for ii in range(len(filepathsAndFileNames)):
testImage = fits.getdata(filepathsAndFileNames[ii], ext)
#test rows
if testImage.shape[0] != rows:
badSize.append(filepathsAndFileNames[ii])
continue
#test columns
if testImage.shape[1] != columns:
badSize.append(filepathsAndFileNames[ii])
testImage = None
tt += 1
print_progress(tt, len(filepathsAndFileNames), prefix = 'Progress:', suffix = 'Complete', decimals=2, bar_length=50)
#If there are any images with inappropriate sizes, LINT will list them and the program will exit.
if not badSize:
print('All images have passed the dimensions test')
else:
print('Some images do not have the proper dimensions. Please remove these', len(badSize),' images and restart LINT:')
print("\n".join(badSize))
exit('Error: Images present that do not fit the row and column dimensions specified in LINT.config')
# Print iterations progress
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
bar_length - Optional : character length of bar (Int)
"""
str_format = "{0:." + str(decimals) + "f}"
percents = str_format.format(100 * (iteration / float(total)))
filled_length = int(round(bar_length * iteration / float(total)))
bar = '█' * filled_length + '-' * (bar_length - filled_length)
stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
if iteration == total:
stdout.write('\n')
stdout.flush()