Documentation:Tutorial:APIs:Fortran - MdsWiki
Navigation
Personal tools

From MdsWiki

Jump to: navigation, search

Reading and Writing MDSplus data in Fortran

An interface somewhat similar to that in C is available for the Fortran programming language. Still in this case it is necessary to specify the kind of variable which is going to receive the results of a read operation, or which contains the data which will replace the $n symbols when writing a TDI expression. In this case, however, the variable is not directly "attached" to the descriptor, which will only contain the information about the type and size of the variable, and which will be followed by the variable in the argument list.
The steps required for accessing data are the same as in C: connecting to a mdsip server (if doing remote data access); opening the tree; reading/writing data; closing the tree.

The following example illustrates how to read the content of node :SIGNAL1 in my_tree.

PROGRAM read_test
Include 'mdslib.inc'
REAL*4 current(20000)
REAL*4 times(20000)
INTEGER*4 samples_returned
INTEGER*4 status
INTEGER*4 socket

socket = MdsConnect('150.178.3.101'//char(0))
if(socket .eq. -1) then
write(6,*) 'Error connecting to mdsip server'
stop
endif
status = MdsOpen('my_tree'//char(0), -1)
if(mod(status,2) .eq. 0) then
write(6,*) 'Error opening tree'
stop
endif
status = MdsValue2(':SIGNAL1'//char(0),
+ descr2(IDTYPE_FLOAT,20000,0),
+ current, 0, samples_returned)
if(mod(status,2) .eq. 0) then
write(6,*) 'Error reading data'
stop
endif
write(6,*) samples_returned
status = MdsClose('my_tree'//char(0), -1)

stop
end

Being not possible in Fortran to dynamically allocate arrays, it is necessary to declare in advance enough room for the variables which will contain the Y and X values of :SIGNAL1.
Another difference is that in the string arguments for the IP address, the TDI expression and the node name, we need to add explicitly the terminator character, not supported by default in Fortran. Moreover, the names of the MdsValue, MdsPut, and descr are now changed to MdsValue2, MdsPut2, descr2 (other functions are available, but these are the suggested ones).
The arguments passed to function MdsValue2 differ somewhat from the C interface: in the descriptor, created by function descr2 (specifying in this case that the associated variable if of type REAL*4 and is an array with 20000 elements) is followed by the variable itself. Still in this case a null argument terminates the list of (descriptor, variable) pairs (mostly a single pair), and the last argument will contain the number of samples actually read (possibly less than the dimension of the passed array).

Still in this case the control of the returned status is done by checking whether it is odd or even.

In the following example, a new waveform is written in node :SIGNAL1

PROGRAM write_test
Include 'mdslib.inc'
REAL*4 samples(10000)
REAL*4 times(10000)
INTEGER*4 samples_returned
INTEGER*4 status
INTEGER*4 socket

C Build sample Y and X values
do 10 i = 1, 10000
samples(i) = sin(exp(i/3000.))
times(i) = i/10000.
10 continue
socket = MdsConnect('150.178.3.101'//char(0))
if(socket .eq. -1) then
write(6,*) 'Error connecting to mdsip server'
stop
endif
status = MdsOpen('my_tree'//char(0), -1)
if(mod(status,2) .eq. 0) then
write(6,*) 'Error opening tree'
stop
endif
status = MdsPut2(':SIGNAL1'//char(0),
+ ' BUILD_SIGNAL($1,$2)'//char(0),
+ descr2(IDTYPE_FLOAT,10000,0), samples,
+ descr2(IDTYPE_FLOAT,10000,0), times, 0)
if(mod(status,2) .eq. 0) then
write(6,*) 'Error writing data'
stop
endif
status = MdsClose('my_tree'//char(0), -1)
stop
end

In this example, two array are defined for the Y and X axis of the signal to write in the tree. The two variables are then passed to function MdsPut2, together with the corresponding descriptor, specifying their number of elements and type.

To compile and link a Fortran program (e.g. using g77 on a Linux system in which MDSplus is at /usr/local/mdsplus) give the following command:

 g77 -o prova prova.f -I/usr/local/mdsplus/include -L/usr/local/mdsplus/lib -lMdsLib

as in the cc command, flags -I and -L specify the libraries where the include file (mdslib.inc) and the shared libraries reside.