【问题标题】:Use loadtxt to read files recursively使用 loadtxt 递归读取文件
【发布时间】:2014-07-15 11:25:47
【问题描述】:

我有大量 .asc 文件,其中包含两个给定卫星的 (x,y) 坐标。每个卫星大约有 3,000 个单独的文件(例如 Satellite1 = [file1,file2,..., file3000] 和 Satellite2= [file1,file2,..., file3000])。

我正在尝试用 Python(版本 2.7.8 |Anaconda 2.0。)编写一些代码,以查找地球表面上两个卫星轨道交叉的多个点。 我编写了一些基本代码,使用 loadtxt 将两个文件作为输入(即一个来自 Sat1,一个来自 Sat2)。简而言之,代码如下所示:

sat1_in = loadtxt("sat1_file1.asc", usecols = (1,2), comments = "#") 
sat2_in = loadtxt("sat2_file1.asc", usecols = (1,2), comments = "#") 

def main():        
    xover_search() # Returns True or False whether a crossover is found.
    xover_final()  # Returns the (x,y) coordinates of the crossover.
    write_output() # Appends this coordinates to a txt file for later display.

if __name__ == "__main__":
main()

我想将此代码实现到整个数据集,使用一个函数为卫星 1 和卫星 2 之间所有可能的文件组合输出“sat1_in”和“sat2_in”。到目前为止,这些是我的想法:

#Create two empty lists to store all the files to process for Sat1 and Sat2:
sat1_files = []
sat2_files = []

#Use os.walk to fill each list with the respective file paths:
for root, dirs, filenames in os.walk('.'):
    for filename in fnmatch.filter(filenames, 'sat1*.asc'):
        sat1_files.append(os.path.join(root, filename))

for root, dirs, filenames in os.walk('.'):
    for filename in fnmatch.filter(filenames, 'sat2*.asc'):
        sat2_files.append(os.path.join(root, filename))  

#Calculate all possible combinations between both lists using itertools.product:
iter_file = list(itertools.product(sat1_files, sat2_files)) 

#Extract two lists of files for sat1 and sat2 to be compared each iteration:
sat1_ordered = [seq[0] for seq in iter_file] 
sat2_ordered = [seq[1] for seq in iter_file]

这就是我卡住的地方。如何使用 loadtxt 遍历“sat1_ordered”和“sat2_ordered”以提取每个文件的坐标列表?我唯一尝试过的是:

for file in sat1_ordered:
    sat1_in = np.loadtxt(file, usecols = (1,2),comments = "#")

但这将创建一个包含卫星 1 的所有测量值的巨大列表。

有人能给我一些关于如何解决这个问题的想法吗?

【问题讨论】:

    标签: python numpy iteration


    【解决方案1】:

    也许您正在搜索类似的内容:

    for file1, file2 in iter_file:
      sat1_in = np.loadtxt(file1, usecols = (1,2),comments = "#")
      sat2_in = np.loadtxt(file2, usecols = (1,2),comments = "#")
      ....
    

    【讨论】:

    • 感谢您的回答罗曼。据我所见,这会生成两个数组,其中包含 sat_1 和 sat_2 的 (x,y) 坐标,并按正确的顺序进行比较。但是如何遍历这两个数组,一次为每个卫星提取一个文件呢?
    • 即sat1_file1 sat2_file1 ----> 用作 main() 的输入 sat1_file1 sat2_file2 ----> 用作 main() 等的输入
    • 对不起,我听不懂你的意思。请提供更多信息。是否要遍历 sat1_in 和 sat2_in,如果找到交叉点将其打印到输出文件?
    • 我编写的程序只将每个卫星的一个文件作为输入。您建议的想法返回两个数组,其中包含每个卫星的所有文件。我想知道是否有一种方法可以遍历“iter_file”以一次提取每个卫星的一个文件,并将此信息输入我的程序中。
    • 以下是我的数据的一些示例: iter_file = 要比较的文件列表 [('./c2p0001c054.asc', './j2p0001c218.asc'), ('./c2p0001c054.asc' , './j2p0002c218.asc'), ('./c2p0001c054.asc', './j2p0003c218.asc'), ('./c2p0001c054.asc', './j2p0004c218.asc'), ('./ c2p0001c054.asc', './j2p0005c218.asc'), ('./c2p0001c054.asc', './j2p0006c218.asc'),........] sat1_in = 经纬度数组. [[ 69.075923 78.650553] [ 69.019283 78.630961] ..., [-87.981672 -25.027398] [-87.983415 -26.63799 ]]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 2016-09-04
    • 1970-01-01
    • 2019-07-15
    • 2011-01-13
    • 2017-03-09
    相关资源
    最近更新 更多