0

I have a software - FUNCOR2.exe - for calculating the autocorrelation function given a data file, i.e. test.txt.

First, I execute FUNCOR2.exe from Windows command line and then the program takes control and asks me for the input data file.

I want to automate this in Python, so I can use working:

os.system("FUNCOR2")

But then I am not able to type INTO the program the input file name.

So far I've tried:

PressKey(0x54) # T
PressKey(0x45) # E
PressKey(0x53) # S
PressKey(0x54) # T
PressKey(110) # .
PressKey(0x54) # T
PressKey(0x58) # X
PressKey(0x54) # T

which I took from Generate keyboard events, but it does not work, and also:

win32api.keybd_event(0x54, 0) 
win32api.keybd_event(0x45, 0)
win32api.keybd_event(0x53, 0)
win32api.keybd_event(0x54, 0)
win32api.keybd_event(110, 0)
win32api.keybd_event(0x54, 0)
win32api.keybd_event(0x58, 0)
win32api.keybd_event(0x54, 0)

it does not work either.

This program does not accept arguments, so I cannot use:

FUNCOR2.exe test.txt

I've found something similar in here: Writing in the cmd after executing an app.exe from java, but not at all.

Any ideas?

Community
  • 1
  • 1
  • 3
    Is it really an MS-DOS program, or Windows command-line program? MS-DOS does not support multitasking and you cannot do anything while another process is running. In the latter case, you can use Python’s [`subprocess`](https://docs.python.org/3/library/subprocess.html). – Melebius Nov 03 '16 at 11:57
  • I believe 99% it is an MS-DOS program, that cannot be launched from elsewhere but from the console. I just need a method for emulating the keyboard. – Web-GIS entrepreneur Nov 03 '16 at 12:05
  • 1
    If this is a program which performs standard statistical computations (and autocorrelation is pretty standard), its functionality is probably contained somewhere in padas/scipy/numpy. It might be a better use of time to figure out how to do it directly from Python data analysis libraries. – John Coleman Nov 03 '16 at 12:11
  • 1
    A VBScript program *can* accept command line arguments. If you really want to go this route, it might be easier to write a VBScript program which does what you are trying to do (using `SendKeys`) and then call it from Python. You'll encounter many of the same issues, but using a language which is closer to Windows. At the very least, you'll find more resources about running MS-DOS programs with `SendKeys` then you will find about using Python. – John Coleman Nov 03 '16 at 13:59
  • 1
    MS-DOS programs are console applications, but not all console applications are MD-DOS programs! Consider to read this article: [The Windows command prompt is *NOT* a DOS prompt!](https://scalibq.wordpress.com/2012/05/23/the-windows-command-prompt-is-not-a-dos-prompt/) – aschipfl Nov 03 '16 at 14:14

1 Answers1

3

The only thing that I've found (other than your question) which refers to FUNCOR2 is the paper "A library of computer programs for assisting teaching and research in cyclostratigraphic analysis" . Since this is from the Journal "Computers and Geosciences" and since you are in the GIS field, I assume that this is correct.

There are a couple of possibilities (beyond sending keystrokes, which is dicey at best):

1) The paper clearly gives the formula being used (and a quick glance at the actual Fortran code downloadable from the Journal's website confirms that nothing else is done) suggests that this is easily implemented in Python, either straight-Python or using pandas (which has functions for computing autocorrelations).

2) Modify the Fortran source and recompile with an open source Fortran compiler (I'm not a Fortran expert, but this seems to be Fortran 77). Towards the beginning of the code you see:

READ (5,100,ERR=1) CFIL1
OPEN (1,FILE=CFIL1)

The first line is how the code gets the file name from the user. Replace that line by a line which reads a command line argument and voila -- you have a version of FUNCOR2 which gets its input file from the command line, hence easily invoked from Python. It should be easy enough to find examples for getting a file name from a command line argument in Fortran. My guess is just 1 or 2 lines of code replacing that line would be enough. I am not interested enough to try, and doubt that it is worthwhile. This is because another line in the source is:

DIMENSION X(1024),V(1024),COR(200),NPA(200)

Oddly enough, the program would fail if your file has more than 1024 observations. Maybe something like that made sense in the late 90s when the paper was written, but there is almost certainly equivalent pandas code which will be able to handle millions of observations. Sometimes old code should be allowed to die.

John Coleman
  • 51,337
  • 7
  • 54
  • 119