Python: Adding date from file twice at end of data from separate file in array -
so need assistance kind of specific problem. have .composite (ex: rr0063_0011.composite) files in second columns (intensities) read array, need add date (modified julian date) second column of separate file twice @ end of each row before array transposed , saved. example input files:
data (.composite) file:
bin#.....intensity
1. -0.234987 2. 0.87734 ... 512. -0.65523
modified julian date file:
file mjd grabbed.....mjd
rr0063_0011.profs 55105.07946 rr0023_0061.profs 53495.367377 rr0022_0041.profs 53492.307631
this code reading data array , making mjd.txt file. works far, need add mjd twice end of corresponding .composite row. now, have little knowledge of python, code have currently.
#!/usr/bin/python import sys import glob import numpy np import os psrname = sys.argv[1] file_list = glob.glob('*.composite') cols = [1] data = [] f in file_list: # split filename extension use later filename = os.path.splitext('{0}'.format(f)) data.append(np.loadtxt(f, usecols=cols)) print data # run 'vap' (a psrchive command) grap mjd .profs file each observation , write out file called 'mjd.txt' os.system('vap -nc mjd ../{0}/{0}.profs >> mjd.txt' .format(filename[0])) # put mjds (from 'mjd.txt') in 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=' ')
the mjds aren't in order .composite files, , @ end need sort columns mjd before saving.
thank help!
desired output:
intensity
.....
intensity
mjd
mjd
-0.234987 2. 0.87734 ... -0.65523 55105.07946 55105.07946
assuming not need additional 2.
in sample output (which might copy-and-paste-error sample input), can first read dates dates file , use kind of look-up table:
import os import numpy np # function read dates generated mjd file # , create look-up table (is list of lists) def read_mjd_file(): open('mjd.txt') f: lines = f.read().splitlines() lines = [line.split() line in lines] return lines # function date look-up # filename first list element, date second def get_date(base_name): l in lines: if l[0].startswith(base_name): return l[1] # function read data data file def extract_data(file_name): open(file_name) f: data = np.loadtxt(f, usecols=[1]) return data # generate mjd file @ first # os.system(...) # generate look-up table mjd file lines = read_mjd_file() # walk through files given in directory , filter desired file type 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)
you might adapt approach specific needs since proof of concept in order give hint. personally, prefer os.listdir()
glob.glob()
. in addition, don't think need use numpy
rather easy task. python's standard csv
module should job. however, numpy
's functions far more comfortable. so, if need numpy
further tasks might keep it. if not, rewriting snippet using csv
module should not big deal.
mjd.txt
looks like:
rr0063_0011.profs 55105.07946 rr0023_0061.profs 53495.367377 rr0022_0041.profs 53492.307631
rr0023_0061.composite
looks like:
1. -0.234987 2. 0.87734 512. -0.65523
output (variable out
) np.array
:
['-0.234987' '0.87734' '-0.65523' '53495.367377' '53495.367377']
Comments
Post a Comment