Python:在数组中单独文件的数据末尾添加文件中的日期两次



所以我需要一些帮助来解决某种特定问题。我有 .composite (例如:RR0063_0011.composite(文件,其中第二列(强度(被读取到数组中,但我需要在每行末尾添加单独文件第二列的日期(修改的儒略日期(两次在数组转置和保存之前。示例输入文件:

数据(.composite(文件:

宾#.....强度

1. -0.234987
2. 0.87734
...
512. -0.65523

修改的儒略日期文件:

从中抓取 MJD 的文件.....马吉德

RR0063_0011.profs   55105.07946
RR0023_0061.profs   53495.367377
RR0022_0041.profs   53492.307631

这是用于将数据读入数组并生成 mjd.txt 文件的代码。到目前为止,所有这些都有效,我只需要将 MJD 添加到相应 .composite 行的末尾两次。现在,我对Python知之甚少,但这是我目前拥有的代码。

#!/usr/bin/python
import sys
import glob
import numpy as np
import os
psrname = sys.argv[1]
file_list = glob.glob('*.composite')
cols = [1] 
data = []
for f in file_list:
# Split the filename from the extension to use later    
filename = os.path.splitext('{0}'.format(f))
data.append(np.loadtxt(f, usecols=cols))
print data
# Run 'vap' (a PSRCHIVE command) to grap the MJD from the .profs file for each observation and write out to a file called 'mjd.txt'
os.system('vap -nc mjd ../{0}/{0}.profs >> mjd.txt' .format(filename[0]))
# Put the MJDs only (from 'mjd.txt') in an array called mjd_array
mjd_array = np.genfromtxt('mjd.txt', dtype=[('filename_2','S40'),('mjd','f8')])
# Check if working
print mjd_array['mjd'][7]
arr = np.vstack(data)
transposed_arr = np.transpose(arr)
print transposed_arr
fout = np.savetxt(psrname + '.total', transposed_arr, delimiter='   ')

MJD 与 .composite 文件不符,最后我需要在保存之前按 MJD 对所有列进行排序。

感谢您的任何帮助!

期望输出:

强度

.....

强度

马吉德

马吉德

-0.234987
2. 0.87734
...
-0.65523
55105.07946
55105.07946

假设您不需要示例输出中的其他2.(这可能是示例输入中的复制和粘贴错误(,您可以首先从日期文件中读取日期并将其用作一种查找表:

import os
import numpy as np

# function to read dates from generated mjd file
# and create look-up table (is a list of lists)
def read_mjd_file():
with open('mjd.txt') as f:
lines = f.read().splitlines()
lines = [line.split() for line in lines]        
return lines

# function for date look-up
# filename is first list element, date is second
def get_date(base_name):
for l in lines:
if l[0].startswith(base_name):
return l[1]

# function to read data from data file
def extract_data(file_name):
with open(file_name) as f:
data = np.loadtxt(f, usecols=[1])
return data

# generate mjd file at first
# os.system(...)

# generate look-up table from mjd file
lines = read_mjd_file()

# walk through all files given in directory and filter for desired file type
for file_name in os.listdir():
if file_name.endswith('.composite'):
base_name = file_name.split('.')[0]
date = get_date(base_name)
data = extract_data(file_name)
out = np.append(data, 2*[date])

print(out)

您可以根据自己的特定需求调整此方法,因为这只是一个概念证明,以便为您提供提示。就个人而言,我更喜欢os.listdir()而不是glob.glob().此外,我认为您不需要使用numpy来完成这项相当简单的任务。Python的标准csv模块也应该完成这项工作。然而,numpy的功能要舒适得多。因此,如果您需要numpy来完成进一步的任务,您可以保留它。如果没有,使用csv模块重写代码片段应该没什么大不了的。

mjd.txt看起来像:

RR0063_0011.profs   55105.07946
RR0023_0061.profs   53495.367377
RR0022_0041.profs   53492.307631

RR0023_0061.composite看起来像:

1. -0.234987
2. 0.87734
512. -0.65523

输出(变量out(是一个np.array

['-0.234987' '0.87734' '-0.65523' '53495.367377' '53495.367377']

最新更新