From 687e47d32e1c5f8b3a51794af0a4c82d3ebc97e3 Mon Sep 17 00:00:00 2001 From: Erik Monson Date: Wed, 5 Aug 2015 13:28:54 -0500 Subject: [PATCH 1/5] Create fits_to_text.py Converts FITS images to text using PyRaf rather than an IRAF .cl script. --- 2dfft_utils/misc/fits_to_text.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2dfft_utils/misc/fits_to_text.py diff --git a/2dfft_utils/misc/fits_to_text.py b/2dfft_utils/misc/fits_to_text.py new file mode 100644 index 0000000..47421b0 --- /dev/null +++ b/2dfft_utils/misc/fits_to_text.py @@ -0,0 +1,25 @@ +''' + fitstotext.py + Purpose: Converts .fit files to .txt, replacing IRAF cl script. + 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") + +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)] + iraf.wtextimage(fits_list_all[i],basename+".txt",header='no',pixels='yes') +#End program From c4580b8ed26b9aa559d2bd3dbf0b2242ea0b437e Mon Sep 17 00:00:00 2001 From: Erik Monson Date: Wed, 5 Aug 2015 13:30:30 -0500 Subject: [PATCH 2/5] Create get_radius.py Automates the process of measuring a simulation image's radius. --- 2dfft_utils/misc/get_radius.py | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 2dfft_utils/misc/get_radius.py diff --git a/2dfft_utils/misc/get_radius.py b/2dfft_utils/misc/get_radius.py new file mode 100644 index 0000000..a5bd252 --- /dev/null +++ b/2dfft_utils/misc/get_radius.py @@ -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). + 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") + +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 From 6be0e81066eb5e00869ceb928b9c958757b0c41c Mon Sep 17 00:00:00 2001 From: Erik Monson Date: Sun, 4 Oct 2015 15:52:15 -0500 Subject: [PATCH 3/5] Update fits_to_text.py, addressing comments. --- 2dfft_utils/misc/fits_to_text.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/2dfft_utils/misc/fits_to_text.py b/2dfft_utils/misc/fits_to_text.py index 47421b0..fc3838b 100644 --- a/2dfft_utils/misc/fits_to_text.py +++ b/2dfft_utils/misc/fits_to_text.py @@ -1,25 +1,25 @@ ''' - fitstotext.py - Purpose: Converts .fit files to .txt, replacing IRAF cl script. - Input: FITS image files. The string in line 16 can be modified to reflect your needs and file + fits_to_text.py + Purpose: Converts FITS files to .txt. + Input: FITS image files. The string in line 13 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") +fits_list_all = glob.glob("*.fit") + glob.glob("*.fits") 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)] + if ".fits" in fits_list_all[i]: + basename = fits_list_all[i][0:(len(fits_list_all[i])-5)] + else: + basename = fits_list_all[i][0:(len(fits_list_all[i])-4)] iraf.wtextimage(fits_list_all[i],basename+".txt",header='no',pixels='yes') #End program From ee0fa08fea98cbaab750022807b7f3ebf76a4e21 Mon Sep 17 00:00:00 2001 From: Erik Monson Date: Sun, 4 Oct 2015 15:53:58 -0500 Subject: [PATCH 4/5] Update get_radius.py, addressing 1 of 2 comments --- 2dfft_utils/misc/get_radius.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/2dfft_utils/misc/get_radius.py b/2dfft_utils/misc/get_radius.py index a5bd252..49ba5f0 100644 --- a/2dfft_utils/misc/get_radius.py +++ b/2dfft_utils/misc/get_radius.py @@ -3,16 +3,15 @@ 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. + Notes: Line 26 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). - 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 + It is reccomended to use this script in conjunction with the get_center.py, since this script takes an input file + formatted in the same way as the output from get_center.py. + 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 (i.e., you must use the same images & corresponding script output from beginning to end). + 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 ac265db50952c18a992f455d2fe9f88b2fddb3e0 Mon Sep 17 00:00:00 2001 From: Erik Monson Date: Sun, 4 Oct 2015 16:03:42 -0500 Subject: [PATCH 5/5] Update user_guide.rst Added information on get_radius.py and fits_to_text.py --- docs/user_guide.rst | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/docs/user_guide.rst b/docs/user_guide.rst index f615bc3..ffea229 100644 --- a/docs/user_guide.rst +++ b/docs/user_guide.rst @@ -365,8 +365,29 @@ Automated Method ---------------- .. note:: + Script located in ``2dfft_utils/misc/get_radius.py`` - Script not yet in code base. +---- + +1. Open the terminal and cd to the directory containing your fits files. + +2. Place ``get_center.py`` in this directory and run it to generate a text file called ``all_centers.txt``. + +.. note:: + + If you are not using ``get_center.py`` to find the image centers but wish to use this script, you will + need to place your coordinates in a file formatted as follows.:: + + + + + and so on, where the "BaseName" refers to the filename without extension. + +3. Place ``get_radius.py`` in the directory and run the script.:: + + python get_radius.py + +4. The script will generate a file called ``r_max.txt``, which contains the radii of the images in the same order that they appear in ``all_centers.txt``. ---- @@ -500,9 +521,9 @@ Automated Method .. note:: - Script is located at ``2dfft_utils/fit2txt_all.cl``. + IRAF cl script is located at ``2dfft_utils/misc/fit2txt_all.cl``. - Script will be updated to one in Python. + Python script is located at ``2dfft_utils/misc/fits_to_text.py``. .. _running-2dfft: