Page 1 of 2
winrats inter-operation
Posted: Sat Dec 09, 2023 11:56 am
by hardmann
Dear Tom:
I use several econometric software, including Winrats and Oxmetrics, do repetitive adjustment work on a large number of time series and write them into MySQL database. I plan to use Python to call or control the entire process. I really want to know wether winrats inter-oprate or manipulate with other econometrics software, eg, python?
Best Regard
Re: winrats inter-opration
Posted: Tue Dec 12, 2023 9:39 am
by hardmann
Dear Tom:
I got it
Code: Select all
import os
import subprocess
rats_path = "C:\Program Files (x86)\Estima\WinRATS Pro 9.0"
command = f'{rats_path}/ratsv9_64.exe "c:\myprog\rats\batchjob.rpf" /run '
result = subprocess.check_output(command)
print("Winrats result:")
print(result.decode('utf-8'))
Best Regard
Hardmann
Re: winrats inter-opration
Posted: Tue Dec 12, 2023 1:22 pm
by TomDoan
Python to RATS basically requires running a full RATS program (as you are doing). Python to RATS (for doing something like fishing data off a web site) should be quite a bit simpler.
Re: winrats inter-opration
Posted: Tue Dec 12, 2023 1:41 pm
by PeterF
Dear Hardmann,
would it also be possible to work the other way round that python is called from Winrats to do a job and reports the results back to rats. I can obtain data via an API but have to provider a header with the number of the api-key in the request send to the server. This is possible with the request class in python. However, i would have to safe the data first in a format that Winrats can read, which I would like to avoid. I have not found a solution in the manual and help for Winrats.
Best regards
PeterF
Re: winrats inter-opration
Posted: Tue Dec 12, 2023 1:54 pm
by PeterF
TomDoan wrote:Python to RATS basically requires running a full RATS program (as you are doing). Python to RATS (for doing something like fishing data off a web site) should be quite a bit simpler.
Dear Tom,
could you please explain how RATS can instruct python to run a script to retrieve data and pass it to RATS. That would be very helpful and welcome.
Thank you very much in advance!
Best regards
PeterF
Re: winrats inter-opration
Posted: Mon Feb 19, 2024 8:46 am
by TomDoan
This is a Python program in a CGI script, which produces a plain text file:
Code: Select all
#!/usr/bin/python3
print("Content-type: text/plain\n");
print("a b c")
print("11 12 13")
print("22 24 26")
and this is the RATS program to process it:
Code: Select all
open data https://estima.com/cgi-bin/short.cgi
data(format=prn,org=columns)
A more complicated example (which passes arguments to the CGI script is)
Code: Select all
#!/usr/bin/python3
## import the required libraries
import os
import urllib.parse
## print a HTTP content header
print('Content-type: text/plain\r\n')
## get the query string. this gets passed to cgi scripts as the environment
## variable QUERY_STRING
query_string = os.environ['QUERY_STRING']
## convert the query string to a dictionary
arguments = urllib.parse.parse_qs(query_string)
## override column name if argument is included for a, b and c
## note that values in the dictionary are lists (in case of duplicated keys on the URL)
labelA = str(arguments["a"][0]) if "a" in arguments else "a"
labelB = str(arguments["b"][0]) if "b" in arguments else "b"
labelC = str(arguments["c"][0]) if "c" in arguments else "c"
print(f"{labelA} {labelB} {labelC}")
print("11 12 13")
print("22 24 26")
An example of this is:
Code: Select all
open data https://estima.com/cgi-bin/witharguments.cgi?a=ant&b=bear&c=coyote
data(format=prn,org=columns)
The A option overrides the name of column A, similarly for B and C.
Re: winrats inter-opration
Posted: Tue Feb 20, 2024 8:54 am
by PeterF
Dear Tom,
thank you very much for the two examples for exchanging data between RATS and Python. For your examples you use of course the Estima webserver. Howerver, not every user of RATS has access to a webserver for storing python scripts in the cgi-bin directory of the server. But this is not an unsurmountable obstacle as Python 3 provides an easy server, which could also execute cgi scripts. But a few adaptations are required.
I made to following steps to get the first example running correctly on my pc.
- 1. I made a new sub-directory at the C: drive, which could be either created with Windows explorer or the cmd commandline: md MyServer
- 2.now, I created a sub-directory of MyServer, which is called cgi-bin and contains the scripts for Python to run for sending data to RATS. The two command lines cd MyServer and md cgi-bin are performed
- 3. I copied the complete text of the first example starting with "#!/usr/bin/python3" and saved it in the cgi-bin as short.py. I had to change the file extension from cgi to py, as the first version led to errors when the script was excuted.
- 4. One small command line is sufficent to start an easy Python webserver. In the example I will use localhost as IP-address but if the pc is part of a home network it is probably better to get the IP-address with IPConfig in the command line window. To run the webserver, the following command within the quotation marks has to be used: "python3 -m http.server -b localhost -d /MyServer/ --cgi 8000". For not having to type this line for every run, i saved it in text file with the extension ".bat" in the MyServer directory. In order to start the execution of the batch file from C: directory I included as first line the command "cd.."
Running then the two lines of the RATS code reads the two data points from Python for the 3 series.
Of course, it will be also possible to include another line in the batch file to start and run also the RATS program. This would make it possible to run the applications automatically with support from the Windows task scheduler at a fixed time.
I hope these few lines reduce the time spend by RATS users for retrieving data with python and transfering it to RATS.
Best regards
PeterF
Re: winrats inter-opration
Posted: Wed Feb 21, 2024 1:51 pm
by TomDoan
There's an undocumented instruction
DOS command line
which shells to Window to run the command line. For instance, if you do
dos excel (or "excel"---though you only need "..." if there are embedded spaces)
RATS will run Excel until you quit Excel. You could shell to a python program that writes your data file locally and then follow with the usual data reading commands.
Re: winrats inter-opration
Posted: Fri Oct 18, 2024 11:45 pm
by hardmann
Dear Tom:
If I use python to call winrats to deal with massive time series, for example, to disaggregation. Could I take data file and calendar and startdate as arguements to pass to winrats. In other word, I use python identify the frequency and startdate of time series and then pass through Winrats. How can I do?
In other words, whether the code can be separated from the data, and more flexible batch mode can be processed through parameter calls.
Below is the hypothetical code I have devised.
Code: Select all
import os
import subprocess
rats_path = "C:\Program Files (x86)\Estima\WinRATS Pro 9.0"
command = f'{rats_path}/ratsv9_64.exe "c:\myprog\rats\batchjob.rpf" "myfile.xlsx" "quarter" "1947Q1" /run '
Best regard
Hardmann
Re: winrats inter-opration
Posted: Tue Oct 22, 2024 10:23 am
by TomDoan
You can use command line definitions. See the last paragraph on
https://estima.com/webhelp/topics/batchmode.html
Looks like something like /Dfname="myfile.xlsx" /Dfreq="quarter" /Dstart="1947Q1" would allow you to use FNAME, FREQ and START inside the RATS program. As you have that written, you would have to use string functions to hack the start date out of the START variable.
Re: winrats inter-operation
Posted: Thu Mar 13, 2025 9:42 am
by hardmann
Dear Tom:
I didn't succeed in my attempt. I have rewritten the MZN 2003 example in Winrats. I successfully called Winrats and RPF files using Python.
Code: Select all
import os
import subprocess
rats_path = "C:\Program Files (x86)\Estima\WinRATS Pro 10.0\\x64"
command = f'{rats_path}/ratsv10_64.exe C:\python_winrats\mnz_restat_2003.rpf /run '
result = subprocess.check_output(command)
print("Winrats result:")
But I commented out the read data in RPF,
Code: Select all
* open data lgdp.txt
calendar(q) 1947
data(format=free,org=columns) 1947:01 1998:02 lgdp
and when calling/DFname="lgdp. txt" in a Python script, a dialog box popped up requiring manual selection of the data file.
Code: Select all
import os
import subprocess
rats_path = "C:\Program Files (x86)\Estima\WinRATS Pro 10.0\\x64"
command = f'{rats_path}/ratsv10_64.exe C:\python_winrats\mnz_restat_2003_py.rpf /Dfname="lgdp.txt" /run '
result = subprocess.check_output(command)
print("Winrats result:")
Best Regard
Hardmann
Re: winrats inter-operation
Posted: Thu Mar 13, 2025 10:25 am
by TomDoan
All the command line definition does is to define the variable fname. You then have to tell the RATS program what to do with it. It looks like you want
open data &fname
calendar(q) 1947
data(format=free,org=columns) 1947:01 1998:02 lgdp
See
https://estima.com/webhelp/topics/type- ... #ampersand
Re: winrats inter-operation
Posted: Thu Mar 13, 2025 11:57 pm
by hardmann
Dear Tom:
It still doesn't work. Python respose as follows
open data &fname
calendar(q) 1947
data(format=free,org=columns) 1947:01 1998:02 lgdp
## IO3. Unable to open "C:\PYTHON_WINRATS\LGDP.TXT"
IF using &Dfname rather than &fname both in Python and Winrats. Python respose as follows
open data &Dfname
<<<<
## SX22. Expected Type STRING, Got ANY Instead
Best Regard
Hardmann
Re: winrats inter-operation
Posted: Fri Mar 14, 2025 8:01 am
by TomDoan
The /D is the switch for "define" so using dfname is definitely wrong. IO3 is a RATS error not a Python error. It certainly looks like it is properly substituting in the command line variable. Is C:\PYTHON_WINRATS\LGDP.TXT the correct path to the data file? If it is and that's a file being generated by the Python code, is it possible that it's still open for writing in Python?
Re: winrats inter-operation
Posted: Sat Mar 15, 2025 9:05 am
by hardmann
Dear Tom:
Yes. I tried the relative and absolute path to the data file.
How to pass parameters from Python to Winrats?
Best Regard
Hardmann