简体   繁体   中英

How can I automatically give a running linux program commands with python?

I have already done a lot of Fire Dynamic Simulation (FDS) calculations on a Linux OS and now I have thousands of folders. Each of these folders contains some .sf data saved in specific binary format. NIST (Who developed FDS) provides a program called fds2ascii, which can be used to decode these .sf datas to .csv format. This fds2ascii program can only decode these datas one at a time. I am now trying to write a python script to decode all these datas automatically.

The problem is that after you launch the fds2ascii program, it won't start doing its job at once. Instead, you need to answer some questions asked by the program like this:

>> fds2ascii
  Enter Job ID string (CHID):
bucket_test_1
  What type of file to parse?
  PL3D file? Enter 1
  SLCF file? Enter 2
  BNDF file? Enter 3
3
  Enter Sampling Factor for Data?
  (1 for all data, 2 for every other point, etc.)
1
  Limit the domain size? (y or n)
y
  Enter min/max x, y and z
-5 5 -5 5 0 1
  1   MESH  1, WALL TEMPERATURE
  Enter starting and ending time for averaging (s)
35 36
  Enter orientation: (plus or minus 1, 2 or 3)
3
  Enter number of variables
1
 Enter boundary file index for variable 1
1
 Enter output file name:
bucket_test_1_fds2ascii.csv
  Writing to file...      bucket_test_1_fds2ascii.csv

The answers for all the datas are the same.

I know how to program my python script to run fds2ascii program for each of these datas one after another. But I also need to know how to let python answer these questions asked by the fds2ascii program in order to make the script work. Note that these questions should be answered WHILE the fds2ascii program is running.

Is there any way to implement this?

My understanding is that fds2ascii takes an input file. Now if you loop through you files and folders and generate a list of options from that. You should be able to construct an options_list like I have below (untested):

from subprocess import call

options_list = ['bucket_test_1', 3, 1, 'y', 1, '-5 5 -5 5 0 1', '35 36', 3, 1, 1, 'output.csv']

def fds2ascii_with_options(options_list)
    '''Takes a list of options that will be parsed to an input.txt file.
    This file will be used as input for fds2ascii.'''
    with open('input.txt', 'w') as fds_input:
        for e in options_list:
            fds_input.write(e)

    call('fds2ascii < input.txt')
    print(f'Output csv created: {options_list[-1]}')

I have not tested the options_list but this should get you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM