0

这是一个我想读入的名为“galfit.365”的文件。我需要通过python访问这个文件,在“输入菜单文件:”后面找到数字“10”,在“”后面找到数字“39” w。” 之后,我想将这些数字保存到目录中。

我知道一般方法;读入文件,找到相关的子字符串,保存该字符串,然后用 asciitable 写出来。但是,我有点像 Python 新手,所以我不知道从哪里开始。首先,我如何“读入文件”?之后,如何找到我感兴趣的特定字符串值?

#  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)

================================================================================
4

2 回答 2

2

显然有更优雅的方法可以做到这一点,但这将完成工作,这是你的 python 旅行和冒险的一个很好的起点。神速。

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]
于 2013-11-12T23:33:22.827 回答
1

要“读取文件”,如果它是一个文本文件(人类可读的大约 80 个字符的行),您通常会逐行执行。看起来像这样:

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

但是在您的情况下,您只想阅读第一行,而不关心其他任何内容,对吗?所以:

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

现在,你如何处理那条线?很多人会建议使用正则表达式,并且\s(\d+)f\d+w(\d+)\.应该这样做,但您不太可能理解这一点。

一种方法是找到“拆分”生产线的地方以获得您想要的零件。

#  Input menu file: 10f160w39.feedme

如果你用空格隔开,你关心的一切都在最后一个词中,10f160w39.feedme. (最后也可能有不可见的空格——事实上,至少一个换行符。我会先把strip它们扔掉。)所以,让我们开始吧:

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

现在,the 之前的所有内容f都是一个数字,而 thew和 the之间的所有内容都是另一个数字,.对吧?所以:

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

我们完成了。

于 2013-11-12T23:31:45.097 回答