Question

This is a file, entitled "galfit.365" that I would like to read in. I need to access this file through python, find the number "10" after "Input menu file:" and find the number "39" after "w." After that, I would like to save those numbers into a catalog.

I know the general approach; read the file in, find the relevant substring, save that string, and write it out with asciitable. However, I'm sort of a Python newbie, so I'm not sure where to start. First of all, how do I "read the file in?" After that, how do I find the particular string values that I am interested in?

#  Input menu file: 10f160w39.feedme

#  Chi^2/nu = 0.003,  Chi^2 = 45.157,  Ndof = 16768

================================================================================
# IMAGE and GALFIT CONTROL PARAMETERS
A) NOISE10thousandthsciPHOTOF160w39.fits      # Input data image (FITS file)
B) NOISE10thousandthgalsciPHOTOF160w39.fits      # Output data image block
C) NOISE10thousandthsigmaPHOTOF160w39.fits      # Sigma image name (made from data if blank or "none") 
D) arjen_psf_f160w.fits          # Input PSF image and (optional) diffusion kernel
E) 1                   # PSF fine sampling factor relative to data 
F) maskNOISE10thousandthsciPHOTOF160w39.fits      # Bad pixel mask (FITS image or ASCII coord list)
G) none                # File with parameter constraints (ASCII file) 
H) 1    130  1    130  # Image region to fit (xmin xmax ymin ymax)
I) 200    200          # Size of the convolution box (x y)
J) 26.563              # Magnitude photometric zeropoint 
K) 0.038  0.038        # Plate scale (dx dy)   [arcsec per pixel]
O) regular             # Display type (regular, curses, both)
P) 0                   # Choose: 0=optimize, 1=model, 2=imgblock, 3=subcomps

# INITIAL FITTING PARAMETERS
#
#   For component type, the allowed functions are: 
#       sersic, expdisk, edgedisk, devauc, king, nuker, psf, 
#       gaussian, moffat, ferrer, and sky. 
#  
#   Hidden parameters will only appear when they're specified:
#       Bn (n=integer, Bending Modes).
#       C0 (diskyness/boxyness), 
#       Fn (n=integer, Azimuthal Fourier Modes).
#       R0-R10 (coordinate rotation, for creating spiral structures).
#       To, Ti, T0-T10 (truncation function).
# 
# ------------------------------------------------------------------------------
#   par)    par value(s)    fit toggle(s)    # parameter description 
# ------------------------------------------------------------------------------

# Component number: 1
 0) sersic                 #  Component type
 1) 65.6794  64.2952  1 1  #  Position x, y
 3) 23.7585     1          #  Integrated magnitude 
 4) 0.0100      1          #  R_e (effective radius)   [pix]
 5) 1.6931      1          #  Sersic index n (de Vaucouleurs n=4) 
 6) 0.0000      0          #     ----- 
 7) 0.0000      0          #     ----- 
 8) 0.0000      0          #     ----- 
 9) 0.6379      1          #  Axis ratio (b/a)  
10) -2.3601     1          #  Position angle (PA) [deg: Up=0, Left=90]
 Z) 0                      #  Skip this model in output image?  (yes=1, no=0)

# Component number: 2
 0) sky                    #  Component type
 1) 6.853e-05      1       #  Sky background at center of fitting region [ADUs]
 2) 0.000e+00      0       #  dsky/dx (sky gradient in x)     [ADUs/pix]
 3) 0.000e+00      0       #  dsky/dy (sky gradient in y)     [ADUs/pix]
 Z) 0                      #  Skip this model in output image?  (yes=1, no=0)

================================================================================
Was it helpful?

Solution 2

To "read a file in", if it's a text file (human-readable lines of around 80 characters), you normally do it line by line. That looks like this:

with open(filename) as file:
    for line in file:
        # whatever you wanted to do with each line

But in your case, you only want to read the very first line, and don't care about anything else, right? So:

with open(filename) as file:
    first_line = next(file)
    # whatever you wanted to do with first_line

Now, how do you deal with that line? A lot of people would suggest a regular expression, and \s(\d+)f\d+w(\d+)\. should do it, but you're not likely to understand that.

One way to do it is to find places to "split" the line to get the parts you want.

#  Input menu file: 10f160w39.feedme

If you separate that by spaces, everything you care about is in the last word, 10f160w39.feedme. (There may also be invisible whitespace on the end—in fact, there is at least a newline character. I'll use strip to throw them away first.) So, let's start with:

menufile = line.strip().rsplit()[-1]

Now, everything before the f is one number, and everything between the w and the . is another right? So:

f = menufile.partition('f')[0]
w = menufile.rpartition('.')[0].rpartition('w')[-1]

And we're done.

OTHER TIPS

Obviously there are more elegant ways of doing this, but this will get job done and it's a nice starting point for your python travels and adventures. God speed.

with open("galfit.365") as file:
     lines = file.readlines() # all your lines are here

     # should print "#  Input menu file: 10f160w39.feedme"
     # since it is the first line in the file
     print lines[0]

     index_of_w = lines[0].index('w')

     # this should give you 39
     number_after_w = lines[0][index_of_w+1] + lines[0][index_of_w+2]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top