Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 2dfft_utils/misc/fits_to_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
fitstotext.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change fitstotext.py to fits_to_text.py

Purpose: Converts .fit files to .txt, replacing IRAF cl script.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would edit this to just Purpose: Converts .fit files to .txt.

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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we just make this "*fit"? Better yet, check for any *fits as well (since some people will have FITS files with an s in the extension).


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)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should probably make the basename simply the entire filename minus .fit or .fits. It's less convenient for my naming scheme, but it's better for users to modify their filenames separately so that this script is as general as possible.

iraf.wtextimage(fits_list_all[i],basename+".txt",header='no',pixels='yes')
#End program
60 changes: 60 additions & 0 deletions 2dfft_utils/misc/get_radius.py
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).
Copy link
Contributor

Choose a reason for hiding this comment

The 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 get_radius.py in isolation, if you use it in conjunction with the other scripts here, you can't add/remove images in between steps (eg, you must use the same images & corresponding script output from beginning to end).

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")
Copy link
Contributor

Choose a reason for hiding this comment

The 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