-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
img prep utils #2
base: master
Are you sure you want to change the base?
Changes from 2 commits
687e47d
c4580b8
0d8c67e
6be0e81
ee0fa08
69b8ede
ac265db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
''' | ||
fitstotext.py | ||
Purpose: Converts .fit files to .txt, replacing IRAF cl script. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would edit this to just |
||
Input: FITS image files. The string in line 16 can be modified to reflect your needs and file | ||
naming conventions. | ||
Output: .txt files with the pixel values for the corresponding image and the same basename. | ||
Author: Erik Monson | ||
|
||
Last modified May 29, 2015 | ||
''' | ||
|
||
import glob | ||
from pyraf import iraf | ||
|
||
#Get filenames of all the suitable fits files in working directory | ||
fits_list_all = glob.glob("*crop.fit") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about we just make this |
||
|
||
if (len(fits_list_all) == 0): | ||
print "ERROR: No fits files matching search string were found.\nCheck directory or search string." | ||
else: | ||
#Convert the .fit files to .txt | ||
for i in range(0,len(fits_list_all)): | ||
basename = fits_list_all[i][0:(len(fits_list_all[i])-4)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we should probably make the basename simply the entire filename minus |
||
iraf.wtextimage(fits_list_all[i],basename+".txt",header='no',pixels='yes') | ||
#End program |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
''' | ||
get_radius.py | ||
Purpose: Automates the process of finding the radii of simulation images. Uses numpy and astropy. | ||
Inputs: all_centers.txt (the output from get_center.py) and all FITS images in the working directory | ||
Outputs: r_max.txt, a list of the galaxies' radii in the same order as all_centers.txt | ||
Notes: Line 28 in the Radius function removes the scales superimposed on the image from the calculation. | ||
You may need to modify the slice indices, depending on whether or not your image has scales and | ||
where they are located. | ||
It is best to run this script from a directory containing a complete set of simulation images (0.000 - 3.000 Gyr). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make these instructions more general. Something like: While you can use |
||
Otherwise, the entries in r_max.txt will not line up with those in all_centers.txt | ||
It is not strictly necessary to loop over the entire image; assuming symmetry and looping over a smaller part | ||
would increase program speed. | ||
Author: Erik Monson | ||
|
||
Last modified July 10, 2015 | ||
''' | ||
#!/usr/bin/env/ python | ||
import numpy as np | ||
from astropy.io import fits | ||
import glob | ||
import sys | ||
|
||
# Begin subroutine Radius | ||
def Radius(filename, CENTER_X, CENTER_Y): | ||
img_data = fits.getdata(filename, ignore_missing_end = True) #Grab the pixel values from the image | ||
X_SIZE,Y_SIZE = img_data.shape | ||
img_data[0:80,0:600] = img_data[520:600,0:600] = 0 #Get rid of the scales at the top and bottom of the image | ||
radius = 1 | ||
for i in range(Y_SIZE): | ||
for j in range(X_SIZE): | ||
if img_data[i][j] != 0: #If the pixel at (j,i) isn't black ... | ||
distance = int(np.ceil(np.sqrt((j-CENTER_X)**2+(i-CENTER_Y)**2))) #find the distance from the center to the pixel at (j,i) ... | ||
if distance > radius: #and if it's greater than the current record ... | ||
radius = distance #set the distance as the new record. | ||
return radius | ||
# End subroutine | ||
|
||
# Begin main program | ||
imgs = glob.glob("*.fit") #Grab all the FITS files in the working directory | ||
if len(imgs) == 0: | ||
print "There are no FITS files (.fit) in this directory\n" | ||
sys.exit() | ||
names = np.loadtxt("all_centers.txt", dtype ='S', usecols = (0,)) #Grab the base names of the images to compare with the files in the directory | ||
centers = np.loadtxt("all_centers.txt",usecols = (1,2)) #Grab the x and y coords for the center from all_centers.txt | ||
outfile = open("r_max.txt","w") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may be able to save your output to a Numpy array first and then write all the output to a file at the very end of the process. It may not make much of a difference in performance, but keeping computational/output steps separate is a good idea in general. |
||
|
||
for i in range(len(names)): | ||
if (names[i]+".fit") in imgs: #Make sure that the image is in the working directory | ||
if len(names) > 1: | ||
center_x = centers[i][0] | ||
center_y = centers[i][1] | ||
else: | ||
center_x = centers[0] | ||
center_y = centers[1] | ||
radius = Radius(names[i]+".fit",center_x,center_y) | ||
outfile.write(str(radius)) | ||
outfile.write('\n') | ||
print names[i] + ' ' + str(radius) #This line can be commented | ||
outfile.close() | ||
# End main program |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change
fitstotext.py
tofits_to_text.py