winrats inter-operation

Use this for suggestions about improvements in RATS
hardmann
Posts: 211
Joined: Sat Feb 26, 2011 9:49 pm

winrats inter-operation

Unread post 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
Last edited by hardmann on Tue Dec 12, 2023 6:32 pm, edited 1 time in total.
hardmann
Posts: 211
Joined: Sat Feb 26, 2011 9:49 pm

Re: winrats inter-opration

Unread post 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
TomDoan
Posts: 7702
Joined: Wed Nov 01, 2006 4:36 pm

Re: winrats inter-opration

Unread post 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.
PeterF
Posts: 55
Joined: Thu Apr 12, 2012 2:03 pm

Re: winrats inter-opration

Unread post 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
PeterF
Posts: 55
Joined: Thu Apr 12, 2012 2:03 pm

Re: winrats inter-opration

Unread post 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
TomDoan
Posts: 7702
Joined: Wed Nov 01, 2006 4:36 pm

Re: winrats inter-opration

Unread post 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.
PeterF
Posts: 55
Joined: Thu Apr 12, 2012 2:03 pm

Re: winrats inter-opration

Unread post 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
TomDoan
Posts: 7702
Joined: Wed Nov 01, 2006 4:36 pm

Re: winrats inter-opration

Unread post 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.
hardmann
Posts: 211
Joined: Sat Feb 26, 2011 9:49 pm

Re: winrats inter-opration

Unread post 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
TomDoan
Posts: 7702
Joined: Wed Nov 01, 2006 4:36 pm

Re: winrats inter-opration

Unread post 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.
Post Reply