ascii_io¶
A single model that receives data from three input files and sends received messages to three output files. The first file is an unstructure ASCII text file, the second file is a table delimited ASCII table that is read row by row, and the third file is a table identical to the second that is read all at once.
Mixed Version¶
Model Code:
1#include <stdio.h>
2#include <stdlib.h>
3// Include interface methods
4#include "YggInterface.h"
5
6#define BSIZE 8192 // the max
7
8
9
10int main(int argc,char *argv[]){
11 int ret;
12 int error_code = 0;
13
14 // Input & output to an ASCII file line by line
15 yggAsciiFileInput_t FileInput = yggAsciiFileInput("inputC_file");
16 yggAsciiFileOutput_t FileOutput = yggAsciiFileOutput("outputC_file");
17 // Input & output from a table row by row
18 yggAsciiTableInput_t TableInput = yggAsciiTableInput("inputC_table");
19 yggAsciiTableOutput_t TableOutput = yggAsciiTableOutput("outputC_table",
20 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
21 // Input & output from a table as an array
22 yggAsciiArrayInput_t ArrayInput = yggAsciiArrayInput("inputC_array");
23 yggAsciiArrayOutput_t ArrayOutput = yggAsciiArrayOutput("outputC_array",
24 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
25
26 // Read lines from ASCII text file until end of file is reached.
27 // As each line is received, it is then sent to the output ASCII file.
28 printf("ascii_io(C): Receiving/sending ASCII file.\n");
29 size_t line_size = BSIZE;
30 char *line = (char*)malloc(line_size);
31 ret = 0;
32 while (ret >= 0) {
33 line_size = BSIZE; // Reset to size of buffer
34
35 // Receive a single line
36 ret = yggRecvRealloc(FileInput, &line, &line_size);
37 if (ret >= 0) {
38 // If the receive was succesful, send the line to output
39 printf("File: %s", line);
40 ret = yggSend(FileOutput, line, line_size);
41 if (ret < 0) {
42 printf("ascii_io(C): ERROR SENDING LINE\n");
43 error_code = -1;
44 break;
45 }
46 } else {
47 // If the receive was not succesful, send the end-of-file message to
48 // close the output file.
49 printf("End of file input (C)\n");
50 }
51 }
52 if (line) free(line);
53
54 // Read rows from ASCII table until end of file is reached.
55 // As each row is received, it is then sent to the output ASCII table
56 printf("ascii_io(C): Receiving/sending ASCII table.\n");
57 char name[BSIZE];
58 size_t name_siz = BSIZE;
59 long number;
60 double value;
61 complex_double comp;
62 ret = 0;
63 while (ret >= 0) {
64 name_siz = BSIZE; // Reset to size of the buffer
65
66 // Receive a single row with values stored in scalars declared locally
67 ret = yggRecv(TableInput, &name, &name_siz, &number, &value, &comp);
68
69 if (ret >= 0) {
70 // If the receive was succesful, send the values to output. Formatting
71 // is taken care of on the output driver side.
72 printf("Table: %.5s, %ld, %3.1f, %g%+gj\n",
73 name, number, value, creal(comp), cimag(comp));
74 ret = yggSend(TableOutput, name, name_siz, number, value, comp);
75 if (ret < 0) {
76 printf("ascii_io(C): ERROR SENDING ROW\n");
77 error_code = -1;
78 break;
79 }
80 } else {
81 // If the receive was not succesful, send the end-of-file message to
82 // close the output file.
83 printf("End of table input (C)\n");
84 }
85 }
86
87 // Read entire array from ASCII table into columns that are dynamically
88 // allocated. The returned values tells us the number of elements in the
89 // columns.
90 printf("Receiving/sending ASCII table as array.\n");
91 size_t nrows;
92 char *name_arr = NULL;
93 long *number_arr = NULL;
94 double *value_arr = NULL;
95 complex_double *comp_arr = NULL;
96 ret = 0;
97 while (ret >= 0) {
98 ret = yggRecvRealloc(ArrayInput, &nrows, &name_arr, &number_arr, &value_arr, &comp_arr);
99 if (ret >= 0) {
100 printf("Array: (%lu rows)\n", nrows);
101 // Print each line in the array
102 int i;
103 for (i = 0; i < nrows; i++)
104 printf("%.5s, %ld, %3.1f, %3.1lf%+3.1lfj\n", &name_arr[5*i], number_arr[i],
105 value_arr[i], creal(comp_arr[i]), cimag(comp_arr[i]));
106 // Send the columns in the array to output. Formatting is handled on the
107 // output driver side.
108 ret = yggSend(ArrayOutput, nrows, name_arr, number_arr, value_arr, comp_arr);
109 if (ret < 0) {
110 printf("ascii_io(C): ERROR SENDING ARRAY\n");
111 error_code = -1;
112 break;
113 }
114 } else {
115 printf("End of array input (C)\n");
116 }
117 }
118
119 // Free dynamically allocated columns
120 if (name_arr) free(name_arr);
121 if (number_arr) free(number_arr);
122 if (value_arr) free(value_arr);
123 if (comp_arr) free(comp_arr);
124
125 return error_code;
126}
1#include <stdio.h>
2#include <stdlib.h>
3// Include interface methods
4#include "YggInterface.hpp"
5
6#define BSIZE 8192 // the max
7
8
9int main(int argc,char *argv[]){
10 int ret;
11 int error_code = 0;
12
13 // Input & output to an ASCII file line by line
14 YggAsciiFileInput FileInput("inputCPP_file");
15 YggAsciiFileOutput FileOutput("outputCPP_file");
16 // Input & output from a table row by row
17 YggAsciiTableInput TableInput("inputCPP_table");
18 YggAsciiTableOutput TableOutput("outputCPP_table",
19 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
20 // Input & output from a table as an array
21 YggAsciiArrayInput ArrayInput("inputCPP_array");
22 YggAsciiArrayOutput ArrayOutput("outputCPP_array",
23 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
24
25 // Read lines from ASCII text file until end of file is reached.
26 // As each line is received, it is then sent to the output ASCII file.
27 printf("ascii_io(CPP): Receiving/sending ASCII file.\n");
28 char *line = (char*)malloc(BSIZE);
29 // char line[BSIZE];
30 ret = 0;
31 while (ret >= 0) {
32 // Receive a single line
33 ret = FileInput.recv_line(line, BSIZE);
34 if (ret >= 0) {
35 // If the receive was succesful, send the line to output
36 printf("File: %s", line);
37 ret = FileOutput.send_line(line);
38 if (ret < 0) {
39 printf("ascii_io(CPP): ERROR SENDING LINE\n");
40 error_code = -1;
41 break;
42 }
43 } else {
44 // If the receive was not succesful, send the end-of-file message to
45 // close the output file.
46 printf("End of file input (CPP)\n");
47 // FileOutput.send_eof();
48 }
49 }
50 free(line);
51
52 // Read rows from ASCII table until end of file is reached.
53 // As each row is received, it is then sent to the output ASCII table
54 printf("ascii_io(CPP): Receiving/sending ASCII table.\n");
55 size_t name_siz = BSIZE;
56 size_t * const p_name_siz = &name_siz; // Required in C++ to get name size
57 char name[BSIZE];
58 int64_t number;
59 double value;
60 std::complex<double> comp;
61 ret = 0;
62 while (ret >= 0) {
63 name_siz = BSIZE; // Reset to buffer size
64
65 // Receive a single row with values stored in scalars declared locally
66 ret = TableInput.recv(5, &name, &name_siz, &number, &value, &comp);
67 name_siz = strlen(name);
68 if (ret >= 0) {
69 // If the receive was succesful, send the values to output. Formatting
70 // is taken care of on the output driver side.
71 printf("Table: %.5s, %ld, %3.1f, %3.1lf%+3.1lfj\n", name, number, value,
72 std::real(comp), std::imag(comp));
73 ret = TableOutput.send(5, name, name_siz, number, value, comp);
74 if (ret < 0) {
75 printf("ascii_io(CPP): ERROR SENDING ROW\n");
76 error_code = -1;
77 break;
78 }
79 name_siz = BSIZE;
80 } else {
81 // If the receive was not succesful, send the end-of-file message to
82 // close the output file.
83 printf("End of table input (CPP)\n");
84 }
85 }
86
87 // Read entire array from ASCII table into columns that are dynamically
88 // allocated. The returned values tells us the number of elements in the
89 // columns.
90 printf("Receiving/sending ASCII table as array.\n");
91 size_t nrows;
92 char *name_arr = NULL;
93 long *number_arr = NULL;
94 double *value_arr = NULL;
95 std::complex<double> *comp_arr = NULL;
96 ret = 0;
97 while (ret >= 0) {
98 ret = ArrayInput.recvRealloc(5, &nrows, &name_arr, &number_arr, &value_arr, &comp_arr);
99 if (ret >= 0) {
100 printf("Array: (%lu rows)\n", nrows);
101 // Print each line in the array
102 for (size_t i = 0; i < nrows; i++)
103 printf("%.5s, %ld, %3.1f, %3.1lf%+3.1lfj\n", &name_arr[5*i], number_arr[i],
104 value_arr[i], std::real(comp_arr[i]), std::imag(comp_arr[i]));
105 // Send the columns in the array to output. Formatting is handled on the
106 // output driver side.
107 ret = ArrayOutput.send(5, nrows, name_arr, number_arr, value_arr, comp_arr);
108 if (ret < 0) {
109 printf("ascii_io(CPP): ERROR SENDING ARRAY\n");
110 error_code = -1;
111 break;
112 }
113 } else {
114 printf("End of array input (C++)\n");
115 }
116 }
117
118 // Free dynamically allocated columns
119 if (name_arr) free(name_arr);
120 if (number_arr) free(number_arr);
121 if (value_arr) free(value_arr);
122 if (comp_arr) free(comp_arr);
123
124 return error_code;
125}
1program main
2 ! Include interface methods
3 use fygg
4
5 integer, parameter :: BSIZE = 8192
6 logical :: ret
7 integer :: error_code = 0
8 type(yggcomm) :: file_input, file_output, &
9 table_input, table_output, array_input, array_output
10 integer(kind=c_size_t), target :: line_size = BSIZE
11 type(yggchar_r) :: line ! Wrapped to be reallocatable
12 character(len=BSIZE), target :: name
13 integer(kind=c_size_t), target :: name_siz = BSIZE
14 integer(kind=8) :: number
15 real(kind=8) :: value
16 complex(kind=8) :: comp
17 integer(kind=c_size_t), target :: nrows
18 type(character_1d), target :: name_arr
19 type(integer8_1d), target :: number_arr
20 type(real8_1d), target :: value_arr
21 type(complex8_1d), target :: comp_arr
22 integer(kind=c_size_t) :: i
23
24 ! Input & output to an ASCII file line by line
25 file_input = ygg_ascii_file_input("inputF_file")
26 file_output = ygg_ascii_file_output("outputF_file")
27 ! Input & output from a table row by row
28 table_input = ygg_ascii_table_input("inputF_table")
29 table_output = ygg_ascii_table_output("outputF_table", &
30 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
31 ! Input & output from a table as an array
32 array_input = ygg_ascii_array_input("inputF_array")
33 array_output = ygg_ascii_array_output("outputF_array", &
34 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
35
36 ! Read lines from ASCII text file until end of file is reached.
37 ! As each line is received, it is then sent to the output ASCII file.
38 write(*, '("ascii_io(F): Receiving/sending ASCII file.")')
39 ret = .true.
40 do while (ret)
41 line_size = size(line%x) ! Reset to size of buffer
42
43 ! Receive a single line
44 ret = ygg_recv_var_realloc(file_input, &
45 [yggarg(line), yggarg(line_size)])
46 if (ret) then
47 ! If the receive was succesful, send the line to output
48 write(*, '("File: ",80A)', advance="no") line%x
49 ret = ygg_send_var(file_output, &
50 [yggarg(line), yggarg(line_size)])
51 if (.not.ret) then
52 write(*, '("ascii_io(F): ERROR SENDING LINE")')
53 error_code = -1
54 exit
55 end if
56 else
57 ! If the receive was not succesful, send the end-of-file message to
58 ! close the output file.
59 write(*, '("End of file input (F)")')
60 end if
61 end do
62
63 ! Read rows from ASCII table until end of file is reached.
64 ! As each row is received, it is then sent to the output ASCII table
65 write(*, '("ascii_io(F): Receiving/sending ASCII table.")')
66 ret = .true.
67 do while (ret)
68 name_siz = BSIZE ! Reset to size of the buffer
69
70 ! Receive a single row with values stored in scalars declared locally
71 ret = ygg_recv_var(table_input, &
72 [yggarg(name), yggarg(name_siz), yggarg(number), &
73 yggarg(value), yggarg(comp)])
74
75 if (ret) then
76 ! If the receive was succesful, send the values to output. Formatting
77 ! is taken care of on the output driver side.
78 write (*, '("Table: ",A,", ",I7,", ",F10.5,", ",2F10.5)') &
79 trim(name), number, value, comp
80 ret = ygg_send_var(table_output, &
81 [yggarg(name), yggarg(name_siz), yggarg(number), &
82 yggarg(value), yggarg(comp)])
83 if (.not.ret) then
84 write(*, '("ascii_io(F): ERROR SENDING ROW")')
85 error_code = -1
86 exit
87 end if
88 else
89 ! If the receive was not succesful, send the end-of-file message to
90 ! close the output file.
91 write(*, '("End of table input (F)")')
92 end if
93 end do
94
95 ! Read entire array from ASCII table into columns that are dynamically
96 ! allocated. The returned values tells us the number of elements in the
97 ! columns.
98 write(*, '("Receiving/sending ASCII table as array.")')
99 ret = .true.
100 do while (ret)
101 ret = ygg_recv_var_realloc(array_input, [yggarg(nrows), &
102 yggarg(name_arr), yggarg(number_arr), &
103 yggarg(value_arr), yggarg(comp_arr)])
104 if (ret) then
105 write(*, '("Array: (",I7," rows)")') nrows
106 ! Print each line in the array
107 do i = 1, nrows
108 write(*, '(5A)', advance="no") name_arr%x(i)%x
109 write(*, '(", ",I7,", ",F10.5,", ",2F10.5)') &
110 number_arr%x(i), value_arr%x(i), comp_arr%x(i)
111 end do
112 ! Send the columns in the array to output. Formatting is handled on the
113 ! output driver side.
114 ret = ygg_send_var(array_output, [yggarg(nrows), &
115 yggarg(name_arr), yggarg(number_arr), &
116 yggarg(value_arr), yggarg(comp_arr)])
117 if (.not.ret) then
118 write(*, '("ascii_io(F): ERROR SENDING ARRAY")')
119 error_code = -1
120 exit
121 end if
122 else
123 write(*, '("End of array input (F)")')
124 end if
125 end do
126
127 if (error_code.lt.0) then
128 stop 1
129 end if
130
131end program main
1# Import library for input/output channels
2using Yggdrasil
3using Printf
4
5# Input & output to an ASCII file line by line
6in_file = Yggdrasil.YggInterface("YggAsciiFileInput", "inputJulia_file")
7out_file = Yggdrasil.YggInterface("YggAsciiFileOutput", "outputJulia_file")
8# Input & output from a table row by row
9in_table = Yggdrasil.YggInterface("YggAsciiTableInput", "inputJulia_table")
10out_table = Yggdrasil.YggInterface("YggAsciiTableOutput", "outputJulia_table",
11 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
12# Input & output from a table as an array
13in_array = Yggdrasil.YggInterface("YggAsciiArrayInput", "inputJulia_array")
14out_array = Yggdrasil.YggInterface("YggAsciiArrayOutput", "outputJulia_array",
15 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
16
17# Read lines from ASCII text file until end of file is reached.
18# As each line is received, it is then sent to the output ASCII file.
19println("ascii_io(Julia): Receiving/sending ASCII file.")
20ret = true
21while (ret)
22 # Receive a single line
23 global ret, line = in_file.recv()
24 if (ret)
25 # If the receive was succesful, send the line to output
26 @printf("File: %s\n", line)
27 global ret = out_file.send(line)
28 if (!ret)
29 error("ascii_io(Julia): ERROR SENDING LINE")
30 end
31 else
32 # If the receive was not succesful, send the end-of-file message to
33 # close the output file.
34 println("End of file input (Julia)")
35 out_file.send_eof()
36 end
37end
38
39# Read rows from ASCII table until end of file is reached.
40# As each row is received, it is then sent to the output ASCII table
41println("ascii_io(Julia): Receiving/sending ASCII table.")
42ret = true
43while (ret)
44 # Receive a single row
45 global ret, line = in_table.recv()
46 if (ret)
47 # If the receive was succesful, send the values to output.
48 # Formatting is taken care of on the output driver side.
49 println(line)
50 println(typeof(line))
51 @printf("Table: %s, %d, %3.1f, %s\n", line[1], line[2], line[3], line[4])
52 global ret = out_table.send(line[1], line[2], line[3], line[4])
53 if (!ret)
54 error("ascii_io(Julia): ERROR SENDING ROW")
55 end
56 else
57 # If the receive was not succesful, send the end-of-file message to
58 # close the output file.
59 println("End of table input (Julia)")
60 out_table.send_eof()
61 end
62end
63
64# Read entire array from ASCII table into numpy array
65ret = true
66while (ret)
67 global ret, arr = in_array.recv_array()
68 if (ret)
69 @printf("Array: (%d rows)\n", length(arr))
70 # Print each line in the array
71 for i = 1:length(arr)
72 @printf("%5s, %d, %3.1f, %s\n",
73 arr[i][1], arr[i][2], arr[i][3], arr[i][4])
74 end
75 # Send the array to output. Formatting is handled on the output driver side.
76 global ret = out_array.send_array(arr)
77 if (!ret)
78 error("ascii_io(Julia): ERROR SENDING ARRAY")
79 end
80 else
81 print("ascii_io(Julia): End of array input (Julia)")
82 end
83end
1% Input & output to an ASCII file line by line
2in_file = YggInterface('YggAsciiFileInput', 'inputM_file');
3out_file = YggInterface('YggAsciiFileOutput', 'outputM_file');
4% Input & output from a table row by row
5in_table = YggInterface('YggAsciiTableInput', 'inputM_table');
6out_table = YggInterface('YggAsciiTableOutput', 'outputM_table', ...
7 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n');
8% Input & output from a table as an array
9in_array = YggInterface('YggAsciiArrayInput', 'inputM_array');
10out_array = YggInterface('YggAsciiArrayOutput', 'outputM_array', ...
11 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n');
12
13% Read lines from ASCII text file until end of file is reached.
14% As each line is received, it is then sent to the output ASCII file.
15disp('ascii_io(M): Receiving/sending ASCII file.');
16flag = true;
17while flag
18 % Receive a single line
19 [flag, line] = in_file.recv();
20 if flag
21 % If the receive was succesful, send the line to output
22 fprintf('File: %s', char(line));
23 ret = out_file.send(line);
24 if (~ret);
25 error('ascii_io(M): ERROR SENDING LINE');
26 break;
27 end;
28 else
29 % If the receive was not succesful, send the end-of-file message to
30 % close the output file.
31 disp('End of file input (Matlab)');
32 out_file.send_eof();
33 end;
34end;
35
36% Read rows from ASCII table until end of file is reached.
37% As each row is received, it is then sent to the output ASCII table
38flag = true;
39while flag
40 % Receive a single row
41 [flag, line] = in_table.recv();
42 if flag
43 % If the receive was succesful, send the values to output.
44 % Formatting is taken care of on the output driver side.
45 fprintf('Table: %s, %d, %3.1f, %3.1f%+3.1fi\n', char(line{1}), ...
46 line{2}, line{3}, real(line{4}), imag(line{4}));
47 ret = out_table.send(line);
48 if (~ret);
49 error('ascii_io(M): ERROR SENDING ROW');
50 break;
51 end;
52 else
53 % If the receive was not succesful, send the end-of-file message to
54 % close the output file.
55 disp('End of table input (Matlab)');
56 out_table.send_eof();
57 end;
58end;
59
60% Read entire array from ASCII table into an array
61flag = true;
62while flag
63 [flag, arr] = in_array.recv_array();
64 if flag
65 nr = size(arr, 1);
66 fprintf('Array: (%d rows)\n', nr);
67 % Print each line in the array
68 for i = 1:nr
69 fprintf('%5s, %d, %3.1f, %3.1f%+3.1fi\n', ...
70 char(arr{i,1}), arr{i,2}, arr{i,3}, ...
71 real(arr{i,4}), imag(arr{i,4}));
72 end;
73 % Send the array to output. Formatting is handled on the output driver side.
74 ret = out_array.send_array(arr);
75 if (~ret);
76 error('ascii_io(M): ERROR SENDING ARRAY');
77 break;
78 end;
79 else
80 % If the receive was not succesful, send the end-of-file message to
81 % close the output file.
82 disp('End of array input (Matlab)');
83 end;
84end;
1from __future__ import print_function
2# Import necessary connection interfaces
3from yggdrasil.tools import print_encoded
4from yggdrasil.interface.YggInterface import (
5 YggAsciiFileInput, YggAsciiFileOutput,
6 YggAsciiTableInput, YggAsciiTableOutput,
7 YggAsciiArrayInput, YggAsciiArrayOutput)
8
9
10if __name__ == '__main__':
11
12 # Input & output to an ASCII file line by line
13 in_file = YggAsciiFileInput('inputPy_file')
14 out_file = YggAsciiFileOutput('outputPy_file')
15 # Input & output from a table row by row
16 in_table = YggAsciiTableInput('inputPy_table')
17 out_table = YggAsciiTableOutput('outputPy_table',
18 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
19 # Input & output from a table as an array
20 in_array = YggAsciiArrayInput('inputPy_array')
21 out_array = YggAsciiArrayOutput('outputPy_array',
22 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
23
24 # Read lines from ASCII text file until end of file is reached.
25 # As each line is received, it is then sent to the output ASCII file.
26 print('ascii_io(P): Receiving/sending ASCII file.')
27 ret = True
28 while ret:
29 # Receive a single line
30 (ret, line) = in_file.recv()
31 if ret:
32 # If the receive was succesful, send the line to output
33 print("File: %s" % line)
34 ret = out_file.send(line)
35 if not ret:
36 raise RuntimeError("ascii_io(P): ERROR SENDING LINE")
37 else:
38 # If the receive was not succesful, send the end-of-file message to
39 # close the output file.
40 print("End of file input (Python)")
41 out_file.send_eof()
42
43 # Read rows from ASCII table until end of file is reached.
44 # As each row is received, it is then sent to the output ASCII table
45 print('ascii_io(P): Receiving/sending ASCII table.')
46 ret = True
47 while ret:
48 # Receive a single row
49 (ret, line) = in_table.recv()
50 if ret:
51 # If the receive was succesful, send the values to output.
52 # Formatting is taken care of on the output driver side.
53 print_encoded("Table: %s, %d, %3.1f, %s" % tuple(line))
54 # print("Table: %s, %d, %3.1f, %s" % line)
55 ret = out_table.send(*line)
56 if not ret:
57 raise RuntimeError("ascii_io(P): ERROR SENDING ROW")
58 else:
59 # If the receive was not succesful, send the end-of-file message to
60 # close the output file.
61 print("End of table input (Python)")
62 out_table.send_eof()
63
64 # Read entire array from ASCII table into numpy array
65 ret = True
66 while ret:
67 ret, arr = in_array.recv_array()
68 if ret:
69 print("Array: (%d rows)" % len(arr))
70 # Print each line in the array
71 for i in range(len(arr)):
72 print("%5s, %d, %3.1f, %s" % tuple(arr[i]))
73 # Send the array to output. Formatting is handled on the output driver side.
74 ret = out_array.send_array(arr)
75 if not ret:
76 raise RuntimeError("ascii_io(P): ERROR SENDING ARRAY")
77 else:
78 print("ascii_io(P): End of array input (Python)")
1library(yggdrasil)
2
3# Input & output to an ASCII file line by line
4in_file <- YggInterface('YggAsciiFileInput', 'inputR_file')
5out_file <- YggInterface('YggAsciiFileOutput', 'outputR_file')
6# Input & output from a table row by row
7in_table <- YggInterface('YggAsciiTableInput', 'inputR_table')
8out_table <- YggInterface('YggAsciiTableOutput', 'outputR_table',
9 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
10# Input & output from a table as an array
11in_array <- YggInterface('YggAsciiArrayInput', 'inputR_array')
12out_array <- YggInterface('YggAsciiArrayOutput', 'outputR_array',
13 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
14
15# Read lines from ASCII text file until end of file is reached.
16# As each line is received, it is then sent to the output ASCII file.
17print('ascii_io(R): Receiving/sending ASCII file.')
18ret = TRUE
19while (ret) {
20 # Receive a single line
21 c(ret, line) %<-% in_file$recv()
22 if (ret) {
23 # If the receive was succesful, send the line to output
24 fprintf('File: %s', line)
25 ret <- out_file$send(line)
26 if (!ret) {
27 stop("ascii_io(R): ERROR SENDING LINE")
28 }
29 } else {
30 # If the receive was not succesful, send the end-of-file message to
31 # close the output file.
32 print("End of file input (R)")
33 out_file$send_eof()
34 }
35}
36
37# Read rows from ASCII table until end of file is reached.
38# As each row is received, it is then sent to the output ASCII table
39print('ascii_io(R): Receiving/sending ASCII table.')
40ret <- TRUE
41while (ret) {
42 # Receive a single row
43 c(ret, line) %<-% in_table$recv()
44 if (ret) {
45 # If the receive was succesful, send the values to output.
46 # Formatting is taken care of on the output driver side.
47 fprintf("Table: %s, %f, %3.1f, %s", line[[1]], line[[2]], line[[3]], line[[4]])
48 ret <- out_table$send(line[[1]], line[[2]], line[[3]], line[[4]])
49 if (!ret) {
50 stop("ascii_io(R): ERROR SENDING ROW")
51 }
52 } else {
53 # If the receive was not succesful, send the end-of-file message to
54 # close the output file.
55 print("End of table input (R)")
56 out_table$send_eof()
57 }
58}
59
60# Read entire array from ASCII table into numpy array
61ret <- TRUE
62while (ret) {
63 c(ret, arr) %<-% in_array$recv_array()
64 if (ret) {
65 fprintf("Array: (%d rows)", length(arr))
66 # Print each line in the array
67 for (i in 1:length(arr)) {
68 fprintf("%5s, %d, %3.1f, %s",
69 arr[[i]][[1]], arr[[i]][[2]], arr[[i]][[3]], arr[[i]][[4]])
70 }
71 # Send the array to output. Formatting is handled on the output driver side.
72 ret <- out_array$send_array(arr)
73 if (!ret) {
74 stop("ascii_io(R): ERROR SENDING ARRAY")
75 }
76 } else {
77 print("ascii_io(R): End of array input (R)")
78 }
79}
Model YAML:
1---
2
3models:
4 -
5 name: ascii_io_GCC
6 driver: GCCModelDriver
7 args: src/ascii_io.c
8
9 inputs:
10 - name: inputC_file
11 driver: AsciiFileInputDriver
12 args: ./Input/input_file.txt
13
14 - name: inputC_table
15 driver: AsciiTableInputDriver
16 args: ./Input/input_table.txt
17
18 - name: inputC_array
19 driver: AsciiTableInputDriver
20 args: ./Input/input_array.txt
21 as_array: True
22
23 outputs:
24 - name: outputC_file
25 driver: OutputDriver
26 args: outputC_file
27
28 - name: outputC_table
29 driver: OutputDriver
30 args: outputC_table
31 field_names: name,number,value,complex
32
33 - name: outputC_array
34 driver: OutputDriver
35 args: outputC_array
36 field_names: name,number,value,complex
37
38 -
39 name: ascii_io_Fortran
40 driver: FortranModelDriver
41 args: src/ascii_io.f90
42
43 inputs:
44 - name: inputF_file
45 driver: InputDriver
46 args: outputC_file
47
48 - name: inputF_table
49 driver: InputDriver
50 args: outputC_table
51
52 - name: inputF_array
53 driver: InputDriver
54 args: outputC_array
55
56 outputs:
57 - name: outputF_file
58 driver: OutputDriver
59 args: outputF_file
60
61 - name: outputF_table
62 driver: OutputDriver
63 args: outputF_table
64 field_names: name,number,value,complex
65
66 - name: outputF_array
67 driver: OutputDriver
68 args: outputF_array
69 field_names: name,number,value,complex
70
71 -
72 name: ascii_io_Python
73 driver: PythonModelDriver
74 args: src/ascii_io.py
75
76 inputs:
77 - name: inputPy_file
78 driver: InputDriver
79 args: outputF_file
80
81 - name: inputPy_table
82 driver: InputDriver
83 args: outputF_table
84
85 - name: inputPy_array
86 driver: InputDriver
87 args: outputF_array
88
89 outputs:
90 - name: outputPy_file
91 driver: OutputDriver
92 args: outputPy_file
93
94 - name: outputPy_table
95 driver: OutputDriver
96 args: outputPy_table
97 field_names: name,number,value,complex
98
99 - name: outputPy_array
100 driver: OutputDriver
101 args: outputPy_array
102 field_names: name,number,value,complex
103
104 -
105 name: ascii_io_CPP
106 driver: GCCModelDriver
107 args: src/ascii_io.cpp
108
109 inputs:
110 - name: inputCPP_file
111 driver: InputDriver
112 args: outputPy_file
113
114 - name: inputCPP_table
115 driver: InputDriver
116 args: outputPy_table
117
118 - name: inputCPP_array
119 driver: InputDriver
120 args: outputPy_array
121
122 outputs:
123 - name: outputCPP_file
124 driver: OutputDriver
125 args: outputCPP_file
126
127 - name: outputCPP_table
128 driver: OutputDriver
129 args: outputCPP_table
130 field_names: name,number,value,complex
131
132 - name: outputCPP_array
133 driver: OutputDriver
134 args: outputCPP_array
135 field_names: name,number,value,complex
136
137 -
138 name: ascii_io_Matlab
139 driver: MatlabModelDriver
140 args: src/ascii_io.m
141
142 inputs:
143 - name: inputM_file
144 driver: InputDriver
145 args: outputCPP_file
146
147 - name: inputM_table
148 driver: InputDriver
149 args: outputCPP_table
150
151 - name: inputM_array
152 driver: InputDriver
153 args: outputCPP_array
154
155 outputs:
156 - name: outputM_file
157 driver: AsciiFileOutputDriver
158 args: output_file.txt
159 in_temp: True
160
161 - name: outputM_table
162 driver: AsciiTableOutputDriver
163 args: output_table.txt
164 in_temp: True
165 field_names: name,number,value,complex
166
167 - name: outputM_array
168 driver: AsciiTableOutputDriver
169 args: output_array.txt
170 as_array: True
171 in_temp: True
172 field_names: name,number,value,complex
Mixed w/o Matlab Version¶
Model Code:
1#include <stdio.h>
2#include <stdlib.h>
3// Include interface methods
4#include "YggInterface.h"
5
6#define BSIZE 8192 // the max
7
8
9
10int main(int argc,char *argv[]){
11 int ret;
12 int error_code = 0;
13
14 // Input & output to an ASCII file line by line
15 yggAsciiFileInput_t FileInput = yggAsciiFileInput("inputC_file");
16 yggAsciiFileOutput_t FileOutput = yggAsciiFileOutput("outputC_file");
17 // Input & output from a table row by row
18 yggAsciiTableInput_t TableInput = yggAsciiTableInput("inputC_table");
19 yggAsciiTableOutput_t TableOutput = yggAsciiTableOutput("outputC_table",
20 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
21 // Input & output from a table as an array
22 yggAsciiArrayInput_t ArrayInput = yggAsciiArrayInput("inputC_array");
23 yggAsciiArrayOutput_t ArrayOutput = yggAsciiArrayOutput("outputC_array",
24 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
25
26 // Read lines from ASCII text file until end of file is reached.
27 // As each line is received, it is then sent to the output ASCII file.
28 printf("ascii_io(C): Receiving/sending ASCII file.\n");
29 size_t line_size = BSIZE;
30 char *line = (char*)malloc(line_size);
31 ret = 0;
32 while (ret >= 0) {
33 line_size = BSIZE; // Reset to size of buffer
34
35 // Receive a single line
36 ret = yggRecvRealloc(FileInput, &line, &line_size);
37 if (ret >= 0) {
38 // If the receive was succesful, send the line to output
39 printf("File: %s", line);
40 ret = yggSend(FileOutput, line, line_size);
41 if (ret < 0) {
42 printf("ascii_io(C): ERROR SENDING LINE\n");
43 error_code = -1;
44 break;
45 }
46 } else {
47 // If the receive was not succesful, send the end-of-file message to
48 // close the output file.
49 printf("End of file input (C)\n");
50 }
51 }
52 if (line) free(line);
53
54 // Read rows from ASCII table until end of file is reached.
55 // As each row is received, it is then sent to the output ASCII table
56 printf("ascii_io(C): Receiving/sending ASCII table.\n");
57 char name[BSIZE];
58 size_t name_siz = BSIZE;
59 long number;
60 double value;
61 complex_double comp;
62 ret = 0;
63 while (ret >= 0) {
64 name_siz = BSIZE; // Reset to size of the buffer
65
66 // Receive a single row with values stored in scalars declared locally
67 ret = yggRecv(TableInput, &name, &name_siz, &number, &value, &comp);
68
69 if (ret >= 0) {
70 // If the receive was succesful, send the values to output. Formatting
71 // is taken care of on the output driver side.
72 printf("Table: %.5s, %ld, %3.1f, %g%+gj\n",
73 name, number, value, creal(comp), cimag(comp));
74 ret = yggSend(TableOutput, name, name_siz, number, value, comp);
75 if (ret < 0) {
76 printf("ascii_io(C): ERROR SENDING ROW\n");
77 error_code = -1;
78 break;
79 }
80 } else {
81 // If the receive was not succesful, send the end-of-file message to
82 // close the output file.
83 printf("End of table input (C)\n");
84 }
85 }
86
87 // Read entire array from ASCII table into columns that are dynamically
88 // allocated. The returned values tells us the number of elements in the
89 // columns.
90 printf("Receiving/sending ASCII table as array.\n");
91 size_t nrows;
92 char *name_arr = NULL;
93 long *number_arr = NULL;
94 double *value_arr = NULL;
95 complex_double *comp_arr = NULL;
96 ret = 0;
97 while (ret >= 0) {
98 ret = yggRecvRealloc(ArrayInput, &nrows, &name_arr, &number_arr, &value_arr, &comp_arr);
99 if (ret >= 0) {
100 printf("Array: (%lu rows)\n", nrows);
101 // Print each line in the array
102 int i;
103 for (i = 0; i < nrows; i++)
104 printf("%.5s, %ld, %3.1f, %3.1lf%+3.1lfj\n", &name_arr[5*i], number_arr[i],
105 value_arr[i], creal(comp_arr[i]), cimag(comp_arr[i]));
106 // Send the columns in the array to output. Formatting is handled on the
107 // output driver side.
108 ret = yggSend(ArrayOutput, nrows, name_arr, number_arr, value_arr, comp_arr);
109 if (ret < 0) {
110 printf("ascii_io(C): ERROR SENDING ARRAY\n");
111 error_code = -1;
112 break;
113 }
114 } else {
115 printf("End of array input (C)\n");
116 }
117 }
118
119 // Free dynamically allocated columns
120 if (name_arr) free(name_arr);
121 if (number_arr) free(number_arr);
122 if (value_arr) free(value_arr);
123 if (comp_arr) free(comp_arr);
124
125 return error_code;
126}
1#include <stdio.h>
2#include <stdlib.h>
3// Include interface methods
4#include "YggInterface.hpp"
5
6#define BSIZE 8192 // the max
7
8
9int main(int argc,char *argv[]){
10 int ret;
11 int error_code = 0;
12
13 // Input & output to an ASCII file line by line
14 YggAsciiFileInput FileInput("inputCPP_file");
15 YggAsciiFileOutput FileOutput("outputCPP_file");
16 // Input & output from a table row by row
17 YggAsciiTableInput TableInput("inputCPP_table");
18 YggAsciiTableOutput TableOutput("outputCPP_table",
19 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
20 // Input & output from a table as an array
21 YggAsciiArrayInput ArrayInput("inputCPP_array");
22 YggAsciiArrayOutput ArrayOutput("outputCPP_array",
23 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
24
25 // Read lines from ASCII text file until end of file is reached.
26 // As each line is received, it is then sent to the output ASCII file.
27 printf("ascii_io(CPP): Receiving/sending ASCII file.\n");
28 char *line = (char*)malloc(BSIZE);
29 // char line[BSIZE];
30 ret = 0;
31 while (ret >= 0) {
32 // Receive a single line
33 ret = FileInput.recv_line(line, BSIZE);
34 if (ret >= 0) {
35 // If the receive was succesful, send the line to output
36 printf("File: %s", line);
37 ret = FileOutput.send_line(line);
38 if (ret < 0) {
39 printf("ascii_io(CPP): ERROR SENDING LINE\n");
40 error_code = -1;
41 break;
42 }
43 } else {
44 // If the receive was not succesful, send the end-of-file message to
45 // close the output file.
46 printf("End of file input (CPP)\n");
47 // FileOutput.send_eof();
48 }
49 }
50 free(line);
51
52 // Read rows from ASCII table until end of file is reached.
53 // As each row is received, it is then sent to the output ASCII table
54 printf("ascii_io(CPP): Receiving/sending ASCII table.\n");
55 size_t name_siz = BSIZE;
56 size_t * const p_name_siz = &name_siz; // Required in C++ to get name size
57 char name[BSIZE];
58 int64_t number;
59 double value;
60 std::complex<double> comp;
61 ret = 0;
62 while (ret >= 0) {
63 name_siz = BSIZE; // Reset to buffer size
64
65 // Receive a single row with values stored in scalars declared locally
66 ret = TableInput.recv(5, &name, &name_siz, &number, &value, &comp);
67 name_siz = strlen(name);
68 if (ret >= 0) {
69 // If the receive was succesful, send the values to output. Formatting
70 // is taken care of on the output driver side.
71 printf("Table: %.5s, %ld, %3.1f, %3.1lf%+3.1lfj\n", name, number, value,
72 std::real(comp), std::imag(comp));
73 ret = TableOutput.send(5, name, name_siz, number, value, comp);
74 if (ret < 0) {
75 printf("ascii_io(CPP): ERROR SENDING ROW\n");
76 error_code = -1;
77 break;
78 }
79 name_siz = BSIZE;
80 } else {
81 // If the receive was not succesful, send the end-of-file message to
82 // close the output file.
83 printf("End of table input (CPP)\n");
84 }
85 }
86
87 // Read entire array from ASCII table into columns that are dynamically
88 // allocated. The returned values tells us the number of elements in the
89 // columns.
90 printf("Receiving/sending ASCII table as array.\n");
91 size_t nrows;
92 char *name_arr = NULL;
93 long *number_arr = NULL;
94 double *value_arr = NULL;
95 std::complex<double> *comp_arr = NULL;
96 ret = 0;
97 while (ret >= 0) {
98 ret = ArrayInput.recvRealloc(5, &nrows, &name_arr, &number_arr, &value_arr, &comp_arr);
99 if (ret >= 0) {
100 printf("Array: (%lu rows)\n", nrows);
101 // Print each line in the array
102 for (size_t i = 0; i < nrows; i++)
103 printf("%.5s, %ld, %3.1f, %3.1lf%+3.1lfj\n", &name_arr[5*i], number_arr[i],
104 value_arr[i], std::real(comp_arr[i]), std::imag(comp_arr[i]));
105 // Send the columns in the array to output. Formatting is handled on the
106 // output driver side.
107 ret = ArrayOutput.send(5, nrows, name_arr, number_arr, value_arr, comp_arr);
108 if (ret < 0) {
109 printf("ascii_io(CPP): ERROR SENDING ARRAY\n");
110 error_code = -1;
111 break;
112 }
113 } else {
114 printf("End of array input (C++)\n");
115 }
116 }
117
118 // Free dynamically allocated columns
119 if (name_arr) free(name_arr);
120 if (number_arr) free(number_arr);
121 if (value_arr) free(value_arr);
122 if (comp_arr) free(comp_arr);
123
124 return error_code;
125}
1program main
2 ! Include interface methods
3 use fygg
4
5 integer, parameter :: BSIZE = 8192
6 logical :: ret
7 integer :: error_code = 0
8 type(yggcomm) :: file_input, file_output, &
9 table_input, table_output, array_input, array_output
10 integer(kind=c_size_t), target :: line_size = BSIZE
11 type(yggchar_r) :: line ! Wrapped to be reallocatable
12 character(len=BSIZE), target :: name
13 integer(kind=c_size_t), target :: name_siz = BSIZE
14 integer(kind=8) :: number
15 real(kind=8) :: value
16 complex(kind=8) :: comp
17 integer(kind=c_size_t), target :: nrows
18 type(character_1d), target :: name_arr
19 type(integer8_1d), target :: number_arr
20 type(real8_1d), target :: value_arr
21 type(complex8_1d), target :: comp_arr
22 integer(kind=c_size_t) :: i
23
24 ! Input & output to an ASCII file line by line
25 file_input = ygg_ascii_file_input("inputF_file")
26 file_output = ygg_ascii_file_output("outputF_file")
27 ! Input & output from a table row by row
28 table_input = ygg_ascii_table_input("inputF_table")
29 table_output = ygg_ascii_table_output("outputF_table", &
30 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
31 ! Input & output from a table as an array
32 array_input = ygg_ascii_array_input("inputF_array")
33 array_output = ygg_ascii_array_output("outputF_array", &
34 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
35
36 ! Read lines from ASCII text file until end of file is reached.
37 ! As each line is received, it is then sent to the output ASCII file.
38 write(*, '("ascii_io(F): Receiving/sending ASCII file.")')
39 ret = .true.
40 do while (ret)
41 line_size = size(line%x) ! Reset to size of buffer
42
43 ! Receive a single line
44 ret = ygg_recv_var_realloc(file_input, &
45 [yggarg(line), yggarg(line_size)])
46 if (ret) then
47 ! If the receive was succesful, send the line to output
48 write(*, '("File: ",80A)', advance="no") line%x
49 ret = ygg_send_var(file_output, &
50 [yggarg(line), yggarg(line_size)])
51 if (.not.ret) then
52 write(*, '("ascii_io(F): ERROR SENDING LINE")')
53 error_code = -1
54 exit
55 end if
56 else
57 ! If the receive was not succesful, send the end-of-file message to
58 ! close the output file.
59 write(*, '("End of file input (F)")')
60 end if
61 end do
62
63 ! Read rows from ASCII table until end of file is reached.
64 ! As each row is received, it is then sent to the output ASCII table
65 write(*, '("ascii_io(F): Receiving/sending ASCII table.")')
66 ret = .true.
67 do while (ret)
68 name_siz = BSIZE ! Reset to size of the buffer
69
70 ! Receive a single row with values stored in scalars declared locally
71 ret = ygg_recv_var(table_input, &
72 [yggarg(name), yggarg(name_siz), yggarg(number), &
73 yggarg(value), yggarg(comp)])
74
75 if (ret) then
76 ! If the receive was succesful, send the values to output. Formatting
77 ! is taken care of on the output driver side.
78 write (*, '("Table: ",A,", ",I7,", ",F10.5,", ",2F10.5)') &
79 trim(name), number, value, comp
80 ret = ygg_send_var(table_output, &
81 [yggarg(name), yggarg(name_siz), yggarg(number), &
82 yggarg(value), yggarg(comp)])
83 if (.not.ret) then
84 write(*, '("ascii_io(F): ERROR SENDING ROW")')
85 error_code = -1
86 exit
87 end if
88 else
89 ! If the receive was not succesful, send the end-of-file message to
90 ! close the output file.
91 write(*, '("End of table input (F)")')
92 end if
93 end do
94
95 ! Read entire array from ASCII table into columns that are dynamically
96 ! allocated. The returned values tells us the number of elements in the
97 ! columns.
98 write(*, '("Receiving/sending ASCII table as array.")')
99 ret = .true.
100 do while (ret)
101 ret = ygg_recv_var_realloc(array_input, [yggarg(nrows), &
102 yggarg(name_arr), yggarg(number_arr), &
103 yggarg(value_arr), yggarg(comp_arr)])
104 if (ret) then
105 write(*, '("Array: (",I7," rows)")') nrows
106 ! Print each line in the array
107 do i = 1, nrows
108 write(*, '(5A)', advance="no") name_arr%x(i)%x
109 write(*, '(", ",I7,", ",F10.5,", ",2F10.5)') &
110 number_arr%x(i), value_arr%x(i), comp_arr%x(i)
111 end do
112 ! Send the columns in the array to output. Formatting is handled on the
113 ! output driver side.
114 ret = ygg_send_var(array_output, [yggarg(nrows), &
115 yggarg(name_arr), yggarg(number_arr), &
116 yggarg(value_arr), yggarg(comp_arr)])
117 if (.not.ret) then
118 write(*, '("ascii_io(F): ERROR SENDING ARRAY")')
119 error_code = -1
120 exit
121 end if
122 else
123 write(*, '("End of array input (F)")')
124 end if
125 end do
126
127 if (error_code.lt.0) then
128 stop 1
129 end if
130
131end program main
1# Import library for input/output channels
2using Yggdrasil
3using Printf
4
5# Input & output to an ASCII file line by line
6in_file = Yggdrasil.YggInterface("YggAsciiFileInput", "inputJulia_file")
7out_file = Yggdrasil.YggInterface("YggAsciiFileOutput", "outputJulia_file")
8# Input & output from a table row by row
9in_table = Yggdrasil.YggInterface("YggAsciiTableInput", "inputJulia_table")
10out_table = Yggdrasil.YggInterface("YggAsciiTableOutput", "outputJulia_table",
11 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
12# Input & output from a table as an array
13in_array = Yggdrasil.YggInterface("YggAsciiArrayInput", "inputJulia_array")
14out_array = Yggdrasil.YggInterface("YggAsciiArrayOutput", "outputJulia_array",
15 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
16
17# Read lines from ASCII text file until end of file is reached.
18# As each line is received, it is then sent to the output ASCII file.
19println("ascii_io(Julia): Receiving/sending ASCII file.")
20ret = true
21while (ret)
22 # Receive a single line
23 global ret, line = in_file.recv()
24 if (ret)
25 # If the receive was succesful, send the line to output
26 @printf("File: %s\n", line)
27 global ret = out_file.send(line)
28 if (!ret)
29 error("ascii_io(Julia): ERROR SENDING LINE")
30 end
31 else
32 # If the receive was not succesful, send the end-of-file message to
33 # close the output file.
34 println("End of file input (Julia)")
35 out_file.send_eof()
36 end
37end
38
39# Read rows from ASCII table until end of file is reached.
40# As each row is received, it is then sent to the output ASCII table
41println("ascii_io(Julia): Receiving/sending ASCII table.")
42ret = true
43while (ret)
44 # Receive a single row
45 global ret, line = in_table.recv()
46 if (ret)
47 # If the receive was succesful, send the values to output.
48 # Formatting is taken care of on the output driver side.
49 println(line)
50 println(typeof(line))
51 @printf("Table: %s, %d, %3.1f, %s\n", line[1], line[2], line[3], line[4])
52 global ret = out_table.send(line[1], line[2], line[3], line[4])
53 if (!ret)
54 error("ascii_io(Julia): ERROR SENDING ROW")
55 end
56 else
57 # If the receive was not succesful, send the end-of-file message to
58 # close the output file.
59 println("End of table input (Julia)")
60 out_table.send_eof()
61 end
62end
63
64# Read entire array from ASCII table into numpy array
65ret = true
66while (ret)
67 global ret, arr = in_array.recv_array()
68 if (ret)
69 @printf("Array: (%d rows)\n", length(arr))
70 # Print each line in the array
71 for i = 1:length(arr)
72 @printf("%5s, %d, %3.1f, %s\n",
73 arr[i][1], arr[i][2], arr[i][3], arr[i][4])
74 end
75 # Send the array to output. Formatting is handled on the output driver side.
76 global ret = out_array.send_array(arr)
77 if (!ret)
78 error("ascii_io(Julia): ERROR SENDING ARRAY")
79 end
80 else
81 print("ascii_io(Julia): End of array input (Julia)")
82 end
83end
1from __future__ import print_function
2# Import necessary connection interfaces
3from yggdrasil.tools import print_encoded
4from yggdrasil.interface.YggInterface import (
5 YggAsciiFileInput, YggAsciiFileOutput,
6 YggAsciiTableInput, YggAsciiTableOutput,
7 YggAsciiArrayInput, YggAsciiArrayOutput)
8
9
10if __name__ == '__main__':
11
12 # Input & output to an ASCII file line by line
13 in_file = YggAsciiFileInput('inputPy_file')
14 out_file = YggAsciiFileOutput('outputPy_file')
15 # Input & output from a table row by row
16 in_table = YggAsciiTableInput('inputPy_table')
17 out_table = YggAsciiTableOutput('outputPy_table',
18 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
19 # Input & output from a table as an array
20 in_array = YggAsciiArrayInput('inputPy_array')
21 out_array = YggAsciiArrayOutput('outputPy_array',
22 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
23
24 # Read lines from ASCII text file until end of file is reached.
25 # As each line is received, it is then sent to the output ASCII file.
26 print('ascii_io(P): Receiving/sending ASCII file.')
27 ret = True
28 while ret:
29 # Receive a single line
30 (ret, line) = in_file.recv()
31 if ret:
32 # If the receive was succesful, send the line to output
33 print("File: %s" % line)
34 ret = out_file.send(line)
35 if not ret:
36 raise RuntimeError("ascii_io(P): ERROR SENDING LINE")
37 else:
38 # If the receive was not succesful, send the end-of-file message to
39 # close the output file.
40 print("End of file input (Python)")
41 out_file.send_eof()
42
43 # Read rows from ASCII table until end of file is reached.
44 # As each row is received, it is then sent to the output ASCII table
45 print('ascii_io(P): Receiving/sending ASCII table.')
46 ret = True
47 while ret:
48 # Receive a single row
49 (ret, line) = in_table.recv()
50 if ret:
51 # If the receive was succesful, send the values to output.
52 # Formatting is taken care of on the output driver side.
53 print_encoded("Table: %s, %d, %3.1f, %s" % tuple(line))
54 # print("Table: %s, %d, %3.1f, %s" % line)
55 ret = out_table.send(*line)
56 if not ret:
57 raise RuntimeError("ascii_io(P): ERROR SENDING ROW")
58 else:
59 # If the receive was not succesful, send the end-of-file message to
60 # close the output file.
61 print("End of table input (Python)")
62 out_table.send_eof()
63
64 # Read entire array from ASCII table into numpy array
65 ret = True
66 while ret:
67 ret, arr = in_array.recv_array()
68 if ret:
69 print("Array: (%d rows)" % len(arr))
70 # Print each line in the array
71 for i in range(len(arr)):
72 print("%5s, %d, %3.1f, %s" % tuple(arr[i]))
73 # Send the array to output. Formatting is handled on the output driver side.
74 ret = out_array.send_array(arr)
75 if not ret:
76 raise RuntimeError("ascii_io(P): ERROR SENDING ARRAY")
77 else:
78 print("ascii_io(P): End of array input (Python)")
1library(yggdrasil)
2
3# Input & output to an ASCII file line by line
4in_file <- YggInterface('YggAsciiFileInput', 'inputR_file')
5out_file <- YggInterface('YggAsciiFileOutput', 'outputR_file')
6# Input & output from a table row by row
7in_table <- YggInterface('YggAsciiTableInput', 'inputR_table')
8out_table <- YggInterface('YggAsciiTableOutput', 'outputR_table',
9 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
10# Input & output from a table as an array
11in_array <- YggInterface('YggAsciiArrayInput', 'inputR_array')
12out_array <- YggInterface('YggAsciiArrayOutput', 'outputR_array',
13 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
14
15# Read lines from ASCII text file until end of file is reached.
16# As each line is received, it is then sent to the output ASCII file.
17print('ascii_io(R): Receiving/sending ASCII file.')
18ret = TRUE
19while (ret) {
20 # Receive a single line
21 c(ret, line) %<-% in_file$recv()
22 if (ret) {
23 # If the receive was succesful, send the line to output
24 fprintf('File: %s', line)
25 ret <- out_file$send(line)
26 if (!ret) {
27 stop("ascii_io(R): ERROR SENDING LINE")
28 }
29 } else {
30 # If the receive was not succesful, send the end-of-file message to
31 # close the output file.
32 print("End of file input (R)")
33 out_file$send_eof()
34 }
35}
36
37# Read rows from ASCII table until end of file is reached.
38# As each row is received, it is then sent to the output ASCII table
39print('ascii_io(R): Receiving/sending ASCII table.')
40ret <- TRUE
41while (ret) {
42 # Receive a single row
43 c(ret, line) %<-% in_table$recv()
44 if (ret) {
45 # If the receive was succesful, send the values to output.
46 # Formatting is taken care of on the output driver side.
47 fprintf("Table: %s, %f, %3.1f, %s", line[[1]], line[[2]], line[[3]], line[[4]])
48 ret <- out_table$send(line[[1]], line[[2]], line[[3]], line[[4]])
49 if (!ret) {
50 stop("ascii_io(R): ERROR SENDING ROW")
51 }
52 } else {
53 # If the receive was not succesful, send the end-of-file message to
54 # close the output file.
55 print("End of table input (R)")
56 out_table$send_eof()
57 }
58}
59
60# Read entire array from ASCII table into numpy array
61ret <- TRUE
62while (ret) {
63 c(ret, arr) %<-% in_array$recv_array()
64 if (ret) {
65 fprintf("Array: (%d rows)", length(arr))
66 # Print each line in the array
67 for (i in 1:length(arr)) {
68 fprintf("%5s, %d, %3.1f, %s",
69 arr[[i]][[1]], arr[[i]][[2]], arr[[i]][[3]], arr[[i]][[4]])
70 }
71 # Send the array to output. Formatting is handled on the output driver side.
72 ret <- out_array$send_array(arr)
73 if (!ret) {
74 stop("ascii_io(R): ERROR SENDING ARRAY")
75 }
76 } else {
77 print("ascii_io(R): End of array input (R)")
78 }
79}
Model YAML:
1---
2
3models:
4 -
5 name: ascii_io_GCC
6 driver: GCCModelDriver
7 args: src/ascii_io.c
8
9 inputs:
10 - name: inputC_file
11 driver: AsciiFileInputDriver
12 args: ./Input/input_file.txt
13
14 - name: inputC_table
15 driver: AsciiTableInputDriver
16 args: ./Input/input_table.txt
17
18 - name: inputC_array
19 driver: AsciiTableInputDriver
20 args: ./Input/input_array.txt
21 as_array: True
22
23 outputs:
24 - name: outputC_file
25 driver: OutputDriver
26 args: outputC_file
27
28 - name: outputC_table
29 driver: OutputDriver
30 args: outputC_table
31 field_names: name,number,value,complex
32
33 - name: outputC_array
34 driver: OutputDriver
35 args: outputC_array
36 field_names: name,number,value,complex
37
38 -
39 name: ascii_io_Fortran
40 driver: FortranModelDriver
41 args: src/ascii_io.f90
42
43 inputs:
44 - name: inputF_file
45 driver: InputDriver
46 args: outputC_file
47
48 - name: inputF_table
49 driver: InputDriver
50 args: outputC_table
51
52 - name: inputF_array
53 driver: InputDriver
54 args: outputC_array
55
56 outputs:
57 - name: outputF_file
58 driver: OutputDriver
59 args: outputF_file
60
61 - name: outputF_table
62 driver: OutputDriver
63 args: outputF_table
64 field_names: name,number,value,complex
65
66 - name: outputF_array
67 driver: OutputDriver
68 args: outputF_array
69 field_names: name,number,value,complex
70
71 -
72 name: ascii_io_Python
73 driver: PythonModelDriver
74 args: src/ascii_io.py
75
76 inputs:
77 - name: inputPy_file
78 driver: InputDriver
79 args: outputF_file
80
81 - name: inputPy_table
82 driver: InputDriver
83 args: outputF_table
84
85 - name: inputPy_array
86 driver: InputDriver
87 args: outputF_array
88
89 outputs:
90 - name: outputPy_file
91 driver: OutputDriver
92 args: outputPy_file
93
94 - name: outputPy_table
95 driver: OutputDriver
96 args: outputPy_table
97 field_names: name,number,value,complex
98
99 - name: outputPy_array
100 driver: OutputDriver
101 args: outputPy_array
102 field_names: name,number,value,complex
103
104 -
105 name: ascii_io_CPP
106 driver: GCCModelDriver
107 args: src/ascii_io.cpp
108
109 inputs:
110 - name: inputCPP_file
111 driver: InputDriver
112 args: outputPy_file
113
114 - name: inputCPP_table
115 driver: InputDriver
116 args: outputPy_table
117
118 - name: inputCPP_array
119 driver: InputDriver
120 args: outputPy_array
121
122 outputs:
123 - name: outputCPP_file
124 driver: AsciiFileOutputDriver
125 args: output_file.txt
126 in_temp: True
127
128 - name: outputCPP_table
129 driver: AsciiTableOutputDriver
130 args: output_table.txt
131 in_temp: True
132 field_names: name,number,value,complex
133
134 - name: outputCPP_array
135 driver: AsciiTableOutputDriver
136 args: output_array.txt
137 as_array: True
138 in_temp: True
139 field_names: name,number,value,complex
C Version¶
Model Code:
1#include <stdio.h>
2#include <stdlib.h>
3// Include interface methods
4#include "YggInterface.h"
5
6#define BSIZE 8192 // the max
7
8
9
10int main(int argc,char *argv[]){
11 int ret;
12 int error_code = 0;
13
14 // Input & output to an ASCII file line by line
15 yggAsciiFileInput_t FileInput = yggAsciiFileInput("inputC_file");
16 yggAsciiFileOutput_t FileOutput = yggAsciiFileOutput("outputC_file");
17 // Input & output from a table row by row
18 yggAsciiTableInput_t TableInput = yggAsciiTableInput("inputC_table");
19 yggAsciiTableOutput_t TableOutput = yggAsciiTableOutput("outputC_table",
20 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
21 // Input & output from a table as an array
22 yggAsciiArrayInput_t ArrayInput = yggAsciiArrayInput("inputC_array");
23 yggAsciiArrayOutput_t ArrayOutput = yggAsciiArrayOutput("outputC_array",
24 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
25
26 // Read lines from ASCII text file until end of file is reached.
27 // As each line is received, it is then sent to the output ASCII file.
28 printf("ascii_io(C): Receiving/sending ASCII file.\n");
29 size_t line_size = BSIZE;
30 char *line = (char*)malloc(line_size);
31 ret = 0;
32 while (ret >= 0) {
33 line_size = BSIZE; // Reset to size of buffer
34
35 // Receive a single line
36 ret = yggRecvRealloc(FileInput, &line, &line_size);
37 if (ret >= 0) {
38 // If the receive was succesful, send the line to output
39 printf("File: %s", line);
40 ret = yggSend(FileOutput, line, line_size);
41 if (ret < 0) {
42 printf("ascii_io(C): ERROR SENDING LINE\n");
43 error_code = -1;
44 break;
45 }
46 } else {
47 // If the receive was not succesful, send the end-of-file message to
48 // close the output file.
49 printf("End of file input (C)\n");
50 }
51 }
52 if (line) free(line);
53
54 // Read rows from ASCII table until end of file is reached.
55 // As each row is received, it is then sent to the output ASCII table
56 printf("ascii_io(C): Receiving/sending ASCII table.\n");
57 char name[BSIZE];
58 size_t name_siz = BSIZE;
59 long number;
60 double value;
61 complex_double comp;
62 ret = 0;
63 while (ret >= 0) {
64 name_siz = BSIZE; // Reset to size of the buffer
65
66 // Receive a single row with values stored in scalars declared locally
67 ret = yggRecv(TableInput, &name, &name_siz, &number, &value, &comp);
68
69 if (ret >= 0) {
70 // If the receive was succesful, send the values to output. Formatting
71 // is taken care of on the output driver side.
72 printf("Table: %.5s, %ld, %3.1f, %g%+gj\n",
73 name, number, value, creal(comp), cimag(comp));
74 ret = yggSend(TableOutput, name, name_siz, number, value, comp);
75 if (ret < 0) {
76 printf("ascii_io(C): ERROR SENDING ROW\n");
77 error_code = -1;
78 break;
79 }
80 } else {
81 // If the receive was not succesful, send the end-of-file message to
82 // close the output file.
83 printf("End of table input (C)\n");
84 }
85 }
86
87 // Read entire array from ASCII table into columns that are dynamically
88 // allocated. The returned values tells us the number of elements in the
89 // columns.
90 printf("Receiving/sending ASCII table as array.\n");
91 size_t nrows;
92 char *name_arr = NULL;
93 long *number_arr = NULL;
94 double *value_arr = NULL;
95 complex_double *comp_arr = NULL;
96 ret = 0;
97 while (ret >= 0) {
98 ret = yggRecvRealloc(ArrayInput, &nrows, &name_arr, &number_arr, &value_arr, &comp_arr);
99 if (ret >= 0) {
100 printf("Array: (%lu rows)\n", nrows);
101 // Print each line in the array
102 int i;
103 for (i = 0; i < nrows; i++)
104 printf("%.5s, %ld, %3.1f, %3.1lf%+3.1lfj\n", &name_arr[5*i], number_arr[i],
105 value_arr[i], creal(comp_arr[i]), cimag(comp_arr[i]));
106 // Send the columns in the array to output. Formatting is handled on the
107 // output driver side.
108 ret = yggSend(ArrayOutput, nrows, name_arr, number_arr, value_arr, comp_arr);
109 if (ret < 0) {
110 printf("ascii_io(C): ERROR SENDING ARRAY\n");
111 error_code = -1;
112 break;
113 }
114 } else {
115 printf("End of array input (C)\n");
116 }
117 }
118
119 // Free dynamically allocated columns
120 if (name_arr) free(name_arr);
121 if (number_arr) free(number_arr);
122 if (value_arr) free(value_arr);
123 if (comp_arr) free(comp_arr);
124
125 return error_code;
126}
Model YAML:
1---
2
3model:
4 name: ascii_io_GCC
5 driver: GCCModelDriver
6 args: src/ascii_io.c
7
8 inputs:
9 - name: inputC_file
10 driver: AsciiFileInputDriver
11 args: ./Input/input_file.txt
12
13 - name: inputC_table
14 driver: AsciiTableInputDriver
15 args: ./Input/input_table.txt
16
17 - name: inputC_array
18 driver: AsciiTableInputDriver
19 args: ./Input/input_array.txt
20 as_array: True
21
22 outputs:
23 - name: outputC_file
24 driver: AsciiFileOutputDriver
25 args: output_file.txt
26 in_temp: True
27
28 - name: outputC_table
29 driver: AsciiTableOutputDriver
30 args: output_table.txt
31 in_temp: True
32 field_names: name,number,value,complex
33
34 - name: outputC_array
35 driver: AsciiTableOutputDriver
36 args: output_array.txt
37 as_array: True
38 in_temp: True
39 field_names: name,number,value,complex
C++ Version¶
Model Code:
1#include <stdio.h>
2#include <stdlib.h>
3// Include interface methods
4#include "YggInterface.hpp"
5
6#define BSIZE 8192 // the max
7
8
9int main(int argc,char *argv[]){
10 int ret;
11 int error_code = 0;
12
13 // Input & output to an ASCII file line by line
14 YggAsciiFileInput FileInput("inputCPP_file");
15 YggAsciiFileOutput FileOutput("outputCPP_file");
16 // Input & output from a table row by row
17 YggAsciiTableInput TableInput("inputCPP_table");
18 YggAsciiTableOutput TableOutput("outputCPP_table",
19 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
20 // Input & output from a table as an array
21 YggAsciiArrayInput ArrayInput("inputCPP_array");
22 YggAsciiArrayOutput ArrayOutput("outputCPP_array",
23 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n");
24
25 // Read lines from ASCII text file until end of file is reached.
26 // As each line is received, it is then sent to the output ASCII file.
27 printf("ascii_io(CPP): Receiving/sending ASCII file.\n");
28 char *line = (char*)malloc(BSIZE);
29 // char line[BSIZE];
30 ret = 0;
31 while (ret >= 0) {
32 // Receive a single line
33 ret = FileInput.recv_line(line, BSIZE);
34 if (ret >= 0) {
35 // If the receive was succesful, send the line to output
36 printf("File: %s", line);
37 ret = FileOutput.send_line(line);
38 if (ret < 0) {
39 printf("ascii_io(CPP): ERROR SENDING LINE\n");
40 error_code = -1;
41 break;
42 }
43 } else {
44 // If the receive was not succesful, send the end-of-file message to
45 // close the output file.
46 printf("End of file input (CPP)\n");
47 // FileOutput.send_eof();
48 }
49 }
50 free(line);
51
52 // Read rows from ASCII table until end of file is reached.
53 // As each row is received, it is then sent to the output ASCII table
54 printf("ascii_io(CPP): Receiving/sending ASCII table.\n");
55 size_t name_siz = BSIZE;
56 size_t * const p_name_siz = &name_siz; // Required in C++ to get name size
57 char name[BSIZE];
58 int64_t number;
59 double value;
60 std::complex<double> comp;
61 ret = 0;
62 while (ret >= 0) {
63 name_siz = BSIZE; // Reset to buffer size
64
65 // Receive a single row with values stored in scalars declared locally
66 ret = TableInput.recv(5, &name, &name_siz, &number, &value, &comp);
67 name_siz = strlen(name);
68 if (ret >= 0) {
69 // If the receive was succesful, send the values to output. Formatting
70 // is taken care of on the output driver side.
71 printf("Table: %.5s, %ld, %3.1f, %3.1lf%+3.1lfj\n", name, number, value,
72 std::real(comp), std::imag(comp));
73 ret = TableOutput.send(5, name, name_siz, number, value, comp);
74 if (ret < 0) {
75 printf("ascii_io(CPP): ERROR SENDING ROW\n");
76 error_code = -1;
77 break;
78 }
79 name_siz = BSIZE;
80 } else {
81 // If the receive was not succesful, send the end-of-file message to
82 // close the output file.
83 printf("End of table input (CPP)\n");
84 }
85 }
86
87 // Read entire array from ASCII table into columns that are dynamically
88 // allocated. The returned values tells us the number of elements in the
89 // columns.
90 printf("Receiving/sending ASCII table as array.\n");
91 size_t nrows;
92 char *name_arr = NULL;
93 long *number_arr = NULL;
94 double *value_arr = NULL;
95 std::complex<double> *comp_arr = NULL;
96 ret = 0;
97 while (ret >= 0) {
98 ret = ArrayInput.recvRealloc(5, &nrows, &name_arr, &number_arr, &value_arr, &comp_arr);
99 if (ret >= 0) {
100 printf("Array: (%lu rows)\n", nrows);
101 // Print each line in the array
102 for (size_t i = 0; i < nrows; i++)
103 printf("%.5s, %ld, %3.1f, %3.1lf%+3.1lfj\n", &name_arr[5*i], number_arr[i],
104 value_arr[i], std::real(comp_arr[i]), std::imag(comp_arr[i]));
105 // Send the columns in the array to output. Formatting is handled on the
106 // output driver side.
107 ret = ArrayOutput.send(5, nrows, name_arr, number_arr, value_arr, comp_arr);
108 if (ret < 0) {
109 printf("ascii_io(CPP): ERROR SENDING ARRAY\n");
110 error_code = -1;
111 break;
112 }
113 } else {
114 printf("End of array input (C++)\n");
115 }
116 }
117
118 // Free dynamically allocated columns
119 if (name_arr) free(name_arr);
120 if (number_arr) free(number_arr);
121 if (value_arr) free(value_arr);
122 if (comp_arr) free(comp_arr);
123
124 return error_code;
125}
Model YAML:
1---
2
3model:
4 name: ascii_io_cpp
5 driver: GCCModelDriver
6 args: src/ascii_io.cpp
7
8 inputs:
9 - name: inputCPP_file
10 driver: AsciiFileInputDriver
11 args: ./Input/input_file.txt
12
13 - name: inputCPP_table
14 driver: AsciiTableInputDriver
15 args: ./Input/input_table.txt
16
17 - name: inputCPP_array
18 driver: AsciiTableInputDriver
19 args: ./Input/input_array.txt
20 as_array: True
21
22 outputs:
23 - name: outputCPP_file
24 driver: AsciiFileOutputDriver
25 args: output_file.txt
26 in_temp: True
27
28 - name: outputCPP_table
29 driver: AsciiTableOutputDriver
30 args: output_table.txt
31 in_temp: True
32 field_names: name,number,value,complex
33
34 - name: outputCPP_array
35 driver: AsciiTableOutputDriver
36 args: output_array.txt
37 as_array: True
38 in_temp: True
39 field_names: name,number,value,complex
Fortran Version¶
Model Code:
1program main
2 ! Include interface methods
3 use fygg
4
5 integer, parameter :: BSIZE = 8192
6 logical :: ret
7 integer :: error_code = 0
8 type(yggcomm) :: file_input, file_output, &
9 table_input, table_output, array_input, array_output
10 integer(kind=c_size_t), target :: line_size = BSIZE
11 type(yggchar_r) :: line ! Wrapped to be reallocatable
12 character(len=BSIZE), target :: name
13 integer(kind=c_size_t), target :: name_siz = BSIZE
14 integer(kind=8) :: number
15 real(kind=8) :: value
16 complex(kind=8) :: comp
17 integer(kind=c_size_t), target :: nrows
18 type(character_1d), target :: name_arr
19 type(integer8_1d), target :: number_arr
20 type(real8_1d), target :: value_arr
21 type(complex8_1d), target :: comp_arr
22 integer(kind=c_size_t) :: i
23
24 ! Input & output to an ASCII file line by line
25 file_input = ygg_ascii_file_input("inputF_file")
26 file_output = ygg_ascii_file_output("outputF_file")
27 ! Input & output from a table row by row
28 table_input = ygg_ascii_table_input("inputF_table")
29 table_output = ygg_ascii_table_output("outputF_table", &
30 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
31 ! Input & output from a table as an array
32 array_input = ygg_ascii_array_input("inputF_array")
33 array_output = ygg_ascii_array_output("outputF_array", &
34 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
35
36 ! Read lines from ASCII text file until end of file is reached.
37 ! As each line is received, it is then sent to the output ASCII file.
38 write(*, '("ascii_io(F): Receiving/sending ASCII file.")')
39 ret = .true.
40 do while (ret)
41 line_size = size(line%x) ! Reset to size of buffer
42
43 ! Receive a single line
44 ret = ygg_recv_var_realloc(file_input, &
45 [yggarg(line), yggarg(line_size)])
46 if (ret) then
47 ! If the receive was succesful, send the line to output
48 write(*, '("File: ",80A)', advance="no") line%x
49 ret = ygg_send_var(file_output, &
50 [yggarg(line), yggarg(line_size)])
51 if (.not.ret) then
52 write(*, '("ascii_io(F): ERROR SENDING LINE")')
53 error_code = -1
54 exit
55 end if
56 else
57 ! If the receive was not succesful, send the end-of-file message to
58 ! close the output file.
59 write(*, '("End of file input (F)")')
60 end if
61 end do
62
63 ! Read rows from ASCII table until end of file is reached.
64 ! As each row is received, it is then sent to the output ASCII table
65 write(*, '("ascii_io(F): Receiving/sending ASCII table.")')
66 ret = .true.
67 do while (ret)
68 name_siz = BSIZE ! Reset to size of the buffer
69
70 ! Receive a single row with values stored in scalars declared locally
71 ret = ygg_recv_var(table_input, &
72 [yggarg(name), yggarg(name_siz), yggarg(number), &
73 yggarg(value), yggarg(comp)])
74
75 if (ret) then
76 ! If the receive was succesful, send the values to output. Formatting
77 ! is taken care of on the output driver side.
78 write (*, '("Table: ",A,", ",I7,", ",F10.5,", ",2F10.5)') &
79 trim(name), number, value, comp
80 ret = ygg_send_var(table_output, &
81 [yggarg(name), yggarg(name_siz), yggarg(number), &
82 yggarg(value), yggarg(comp)])
83 if (.not.ret) then
84 write(*, '("ascii_io(F): ERROR SENDING ROW")')
85 error_code = -1
86 exit
87 end if
88 else
89 ! If the receive was not succesful, send the end-of-file message to
90 ! close the output file.
91 write(*, '("End of table input (F)")')
92 end if
93 end do
94
95 ! Read entire array from ASCII table into columns that are dynamically
96 ! allocated. The returned values tells us the number of elements in the
97 ! columns.
98 write(*, '("Receiving/sending ASCII table as array.")')
99 ret = .true.
100 do while (ret)
101 ret = ygg_recv_var_realloc(array_input, [yggarg(nrows), &
102 yggarg(name_arr), yggarg(number_arr), &
103 yggarg(value_arr), yggarg(comp_arr)])
104 if (ret) then
105 write(*, '("Array: (",I7," rows)")') nrows
106 ! Print each line in the array
107 do i = 1, nrows
108 write(*, '(5A)', advance="no") name_arr%x(i)%x
109 write(*, '(", ",I7,", ",F10.5,", ",2F10.5)') &
110 number_arr%x(i), value_arr%x(i), comp_arr%x(i)
111 end do
112 ! Send the columns in the array to output. Formatting is handled on the
113 ! output driver side.
114 ret = ygg_send_var(array_output, [yggarg(nrows), &
115 yggarg(name_arr), yggarg(number_arr), &
116 yggarg(value_arr), yggarg(comp_arr)])
117 if (.not.ret) then
118 write(*, '("ascii_io(F): ERROR SENDING ARRAY")')
119 error_code = -1
120 exit
121 end if
122 else
123 write(*, '("End of array input (F)")')
124 end if
125 end do
126
127 if (error_code.lt.0) then
128 stop 1
129 end if
130
131end program main
Model YAML:
1---
2
3model:
4 name: ascii_io_Fortran
5 driver: FortranModelDriver
6 args: src/ascii_io.f90
7
8 inputs:
9 - name: inputF_file
10 driver: AsciiFileInputDriver
11 args: ./Input/input_file.txt
12
13 - name: inputF_table
14 driver: AsciiTableInputDriver
15 args: ./Input/input_table.txt
16
17 - name: inputF_array
18 driver: AsciiTableInputDriver
19 args: ./Input/input_array.txt
20 as_array: True
21
22 outputs:
23 - name: outputF_file
24 driver: AsciiFileOutputDriver
25 args: output_file.txt
26 in_temp: True
27
28 - name: outputF_table
29 driver: AsciiTableOutputDriver
30 args: output_table.txt
31 in_temp: True
32 field_names: name,number,value,complex
33
34 - name: outputF_array
35 driver: AsciiTableOutputDriver
36 args: output_array.txt
37 as_array: True
38 in_temp: True
39 field_names: name,number,value,complex
Julia Version¶
Model Code:
1# Import library for input/output channels
2using Yggdrasil
3using Printf
4
5# Input & output to an ASCII file line by line
6in_file = Yggdrasil.YggInterface("YggAsciiFileInput", "inputJulia_file")
7out_file = Yggdrasil.YggInterface("YggAsciiFileOutput", "outputJulia_file")
8# Input & output from a table row by row
9in_table = Yggdrasil.YggInterface("YggAsciiTableInput", "inputJulia_table")
10out_table = Yggdrasil.YggInterface("YggAsciiTableOutput", "outputJulia_table",
11 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
12# Input & output from a table as an array
13in_array = Yggdrasil.YggInterface("YggAsciiArrayInput", "inputJulia_array")
14out_array = Yggdrasil.YggInterface("YggAsciiArrayOutput", "outputJulia_array",
15 "%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n")
16
17# Read lines from ASCII text file until end of file is reached.
18# As each line is received, it is then sent to the output ASCII file.
19println("ascii_io(Julia): Receiving/sending ASCII file.")
20ret = true
21while (ret)
22 # Receive a single line
23 global ret, line = in_file.recv()
24 if (ret)
25 # If the receive was succesful, send the line to output
26 @printf("File: %s\n", line)
27 global ret = out_file.send(line)
28 if (!ret)
29 error("ascii_io(Julia): ERROR SENDING LINE")
30 end
31 else
32 # If the receive was not succesful, send the end-of-file message to
33 # close the output file.
34 println("End of file input (Julia)")
35 out_file.send_eof()
36 end
37end
38
39# Read rows from ASCII table until end of file is reached.
40# As each row is received, it is then sent to the output ASCII table
41println("ascii_io(Julia): Receiving/sending ASCII table.")
42ret = true
43while (ret)
44 # Receive a single row
45 global ret, line = in_table.recv()
46 if (ret)
47 # If the receive was succesful, send the values to output.
48 # Formatting is taken care of on the output driver side.
49 println(line)
50 println(typeof(line))
51 @printf("Table: %s, %d, %3.1f, %s\n", line[1], line[2], line[3], line[4])
52 global ret = out_table.send(line[1], line[2], line[3], line[4])
53 if (!ret)
54 error("ascii_io(Julia): ERROR SENDING ROW")
55 end
56 else
57 # If the receive was not succesful, send the end-of-file message to
58 # close the output file.
59 println("End of table input (Julia)")
60 out_table.send_eof()
61 end
62end
63
64# Read entire array from ASCII table into numpy array
65ret = true
66while (ret)
67 global ret, arr = in_array.recv_array()
68 if (ret)
69 @printf("Array: (%d rows)\n", length(arr))
70 # Print each line in the array
71 for i = 1:length(arr)
72 @printf("%5s, %d, %3.1f, %s\n",
73 arr[i][1], arr[i][2], arr[i][3], arr[i][4])
74 end
75 # Send the array to output. Formatting is handled on the output driver side.
76 global ret = out_array.send_array(arr)
77 if (!ret)
78 error("ascii_io(Julia): ERROR SENDING ARRAY")
79 end
80 else
81 print("ascii_io(Julia): End of array input (Julia)")
82 end
83end
Model YAML:
1---
2
3model:
4 name: ascii_io_julia
5 driver: JuliaModelDriver
6 args: src/ascii_io.jl
7
8 inputs:
9 - name: inputJulia_file
10 driver: AsciiFileInputDriver
11 args: ./Input/input_file.txt
12
13 - name: inputJulia_table
14 driver: AsciiTableInputDriver
15 args: ./Input/input_table.txt
16
17 - name: inputJulia_array
18 driver: AsciiTableInputDriver
19 args: ./Input/input_array.txt
20 as_array: True
21
22 outputs:
23 - name: outputJulia_file
24 driver: AsciiFileOutputDriver
25 args: output_file.txt
26 in_temp: True
27
28 - name: outputJulia_table
29 driver: AsciiTableOutputDriver
30 args: output_table.txt
31 in_temp: True
32 field_names: name,number,value,complex
33
34 - name: outputJulia_array
35 driver: AsciiTableOutputDriver
36 args: output_array.txt
37 as_array: True
38 in_temp: True
39 field_names: name,number,value,complex
Matlab Version¶
Model Code:
1% Input & output to an ASCII file line by line
2in_file = YggInterface('YggAsciiFileInput', 'inputM_file');
3out_file = YggInterface('YggAsciiFileOutput', 'outputM_file');
4% Input & output from a table row by row
5in_table = YggInterface('YggAsciiTableInput', 'inputM_table');
6out_table = YggInterface('YggAsciiTableOutput', 'outputM_table', ...
7 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n');
8% Input & output from a table as an array
9in_array = YggInterface('YggAsciiArrayInput', 'inputM_array');
10out_array = YggInterface('YggAsciiArrayOutput', 'outputM_array', ...
11 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n');
12
13% Read lines from ASCII text file until end of file is reached.
14% As each line is received, it is then sent to the output ASCII file.
15disp('ascii_io(M): Receiving/sending ASCII file.');
16flag = true;
17while flag
18 % Receive a single line
19 [flag, line] = in_file.recv();
20 if flag
21 % If the receive was succesful, send the line to output
22 fprintf('File: %s', char(line));
23 ret = out_file.send(line);
24 if (~ret);
25 error('ascii_io(M): ERROR SENDING LINE');
26 break;
27 end;
28 else
29 % If the receive was not succesful, send the end-of-file message to
30 % close the output file.
31 disp('End of file input (Matlab)');
32 out_file.send_eof();
33 end;
34end;
35
36% Read rows from ASCII table until end of file is reached.
37% As each row is received, it is then sent to the output ASCII table
38flag = true;
39while flag
40 % Receive a single row
41 [flag, line] = in_table.recv();
42 if flag
43 % If the receive was succesful, send the values to output.
44 % Formatting is taken care of on the output driver side.
45 fprintf('Table: %s, %d, %3.1f, %3.1f%+3.1fi\n', char(line{1}), ...
46 line{2}, line{3}, real(line{4}), imag(line{4}));
47 ret = out_table.send(line);
48 if (~ret);
49 error('ascii_io(M): ERROR SENDING ROW');
50 break;
51 end;
52 else
53 % If the receive was not succesful, send the end-of-file message to
54 % close the output file.
55 disp('End of table input (Matlab)');
56 out_table.send_eof();
57 end;
58end;
59
60% Read entire array from ASCII table into an array
61flag = true;
62while flag
63 [flag, arr] = in_array.recv_array();
64 if flag
65 nr = size(arr, 1);
66 fprintf('Array: (%d rows)\n', nr);
67 % Print each line in the array
68 for i = 1:nr
69 fprintf('%5s, %d, %3.1f, %3.1f%+3.1fi\n', ...
70 char(arr{i,1}), arr{i,2}, arr{i,3}, ...
71 real(arr{i,4}), imag(arr{i,4}));
72 end;
73 % Send the array to output. Formatting is handled on the output driver side.
74 ret = out_array.send_array(arr);
75 if (~ret);
76 error('ascii_io(M): ERROR SENDING ARRAY');
77 break;
78 end;
79 else
80 % If the receive was not succesful, send the end-of-file message to
81 % close the output file.
82 disp('End of array input (Matlab)');
83 end;
84end;
Model YAML:
1---
2
3model:
4 name: ascii_io_Matlab
5 driver: MatlabModelDriver
6 args: src/ascii_io.m
7
8 inputs:
9 - name: inputM_file
10 driver: AsciiFileInputDriver
11 args: ./Input/input_file.txt
12
13 - name: inputM_table
14 driver: AsciiTableInputDriver
15 args: ./Input/input_table.txt
16
17 - name: inputM_array
18 driver: AsciiTableInputDriver
19 args: ./Input/input_array.txt
20 as_array: True
21
22 outputs:
23 - name: outputM_file
24 driver: AsciiFileOutputDriver
25 args: output_file.txt
26 in_temp: True
27
28 - name: outputM_table
29 driver: AsciiTableOutputDriver
30 args: output_table.txt
31 in_temp: True
32 field_names: name,number,value,complex
33
34 - name: outputM_array
35 driver: AsciiTableOutputDriver
36 args: output_array.txt
37 as_array: True
38 in_temp: True
39 field_names: name,number,value,complex
Python Version¶
Model Code:
1from __future__ import print_function
2# Import necessary connection interfaces
3from yggdrasil.tools import print_encoded
4from yggdrasil.interface.YggInterface import (
5 YggAsciiFileInput, YggAsciiFileOutput,
6 YggAsciiTableInput, YggAsciiTableOutput,
7 YggAsciiArrayInput, YggAsciiArrayOutput)
8
9
10if __name__ == '__main__':
11
12 # Input & output to an ASCII file line by line
13 in_file = YggAsciiFileInput('inputPy_file')
14 out_file = YggAsciiFileOutput('outputPy_file')
15 # Input & output from a table row by row
16 in_table = YggAsciiTableInput('inputPy_table')
17 out_table = YggAsciiTableOutput('outputPy_table',
18 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
19 # Input & output from a table as an array
20 in_array = YggAsciiArrayInput('inputPy_array')
21 out_array = YggAsciiArrayOutput('outputPy_array',
22 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
23
24 # Read lines from ASCII text file until end of file is reached.
25 # As each line is received, it is then sent to the output ASCII file.
26 print('ascii_io(P): Receiving/sending ASCII file.')
27 ret = True
28 while ret:
29 # Receive a single line
30 (ret, line) = in_file.recv()
31 if ret:
32 # If the receive was succesful, send the line to output
33 print("File: %s" % line)
34 ret = out_file.send(line)
35 if not ret:
36 raise RuntimeError("ascii_io(P): ERROR SENDING LINE")
37 else:
38 # If the receive was not succesful, send the end-of-file message to
39 # close the output file.
40 print("End of file input (Python)")
41 out_file.send_eof()
42
43 # Read rows from ASCII table until end of file is reached.
44 # As each row is received, it is then sent to the output ASCII table
45 print('ascii_io(P): Receiving/sending ASCII table.')
46 ret = True
47 while ret:
48 # Receive a single row
49 (ret, line) = in_table.recv()
50 if ret:
51 # If the receive was succesful, send the values to output.
52 # Formatting is taken care of on the output driver side.
53 print_encoded("Table: %s, %d, %3.1f, %s" % tuple(line))
54 # print("Table: %s, %d, %3.1f, %s" % line)
55 ret = out_table.send(*line)
56 if not ret:
57 raise RuntimeError("ascii_io(P): ERROR SENDING ROW")
58 else:
59 # If the receive was not succesful, send the end-of-file message to
60 # close the output file.
61 print("End of table input (Python)")
62 out_table.send_eof()
63
64 # Read entire array from ASCII table into numpy array
65 ret = True
66 while ret:
67 ret, arr = in_array.recv_array()
68 if ret:
69 print("Array: (%d rows)" % len(arr))
70 # Print each line in the array
71 for i in range(len(arr)):
72 print("%5s, %d, %3.1f, %s" % tuple(arr[i]))
73 # Send the array to output. Formatting is handled on the output driver side.
74 ret = out_array.send_array(arr)
75 if not ret:
76 raise RuntimeError("ascii_io(P): ERROR SENDING ARRAY")
77 else:
78 print("ascii_io(P): End of array input (Python)")
Model YAML:
1---
2
3model:
4 name: ascii_io_Python
5 driver: PythonModelDriver
6 args: src/ascii_io.py
7
8 inputs:
9 - name: inputPy_file
10 driver: AsciiFileInputDriver
11 args: ./Input/input_file.txt
12
13 - name: inputPy_table
14 driver: AsciiTableInputDriver
15 args: ./Input/input_table.txt
16
17 - name: inputPy_array
18 driver: AsciiTableInputDriver
19 args: ./Input/input_array.txt
20 as_array: True
21
22 outputs:
23 - name: outputPy_file
24 driver: AsciiFileOutputDriver
25 args: output_file.txt
26 in_temp: True
27
28 - name: outputPy_table
29 driver: AsciiTableOutputDriver
30 args: output_table.txt
31 in_temp: True
32 field_names: name,number,value,complex
33
34 - name: outputPy_array
35 driver: AsciiTableOutputDriver
36 args: output_array.txt
37 as_array: True
38 in_temp: True
39 field_names: name,number,value,complex
R Version¶
Model Code:
1library(yggdrasil)
2
3# Input & output to an ASCII file line by line
4in_file <- YggInterface('YggAsciiFileInput', 'inputR_file')
5out_file <- YggInterface('YggAsciiFileOutput', 'outputR_file')
6# Input & output from a table row by row
7in_table <- YggInterface('YggAsciiTableInput', 'inputR_table')
8out_table <- YggInterface('YggAsciiTableOutput', 'outputR_table',
9 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
10# Input & output from a table as an array
11in_array <- YggInterface('YggAsciiArrayInput', 'inputR_array')
12out_array <- YggInterface('YggAsciiArrayOutput', 'outputR_array',
13 '%5s\t%ld\t%3.1f\t%3.1lf%+3.1lfj\n')
14
15# Read lines from ASCII text file until end of file is reached.
16# As each line is received, it is then sent to the output ASCII file.
17print('ascii_io(R): Receiving/sending ASCII file.')
18ret = TRUE
19while (ret) {
20 # Receive a single line
21 c(ret, line) %<-% in_file$recv()
22 if (ret) {
23 # If the receive was succesful, send the line to output
24 fprintf('File: %s', line)
25 ret <- out_file$send(line)
26 if (!ret) {
27 stop("ascii_io(R): ERROR SENDING LINE")
28 }
29 } else {
30 # If the receive was not succesful, send the end-of-file message to
31 # close the output file.
32 print("End of file input (R)")
33 out_file$send_eof()
34 }
35}
36
37# Read rows from ASCII table until end of file is reached.
38# As each row is received, it is then sent to the output ASCII table
39print('ascii_io(R): Receiving/sending ASCII table.')
40ret <- TRUE
41while (ret) {
42 # Receive a single row
43 c(ret, line) %<-% in_table$recv()
44 if (ret) {
45 # If the receive was succesful, send the values to output.
46 # Formatting is taken care of on the output driver side.
47 fprintf("Table: %s, %f, %3.1f, %s", line[[1]], line[[2]], line[[3]], line[[4]])
48 ret <- out_table$send(line[[1]], line[[2]], line[[3]], line[[4]])
49 if (!ret) {
50 stop("ascii_io(R): ERROR SENDING ROW")
51 }
52 } else {
53 # If the receive was not succesful, send the end-of-file message to
54 # close the output file.
55 print("End of table input (R)")
56 out_table$send_eof()
57 }
58}
59
60# Read entire array from ASCII table into numpy array
61ret <- TRUE
62while (ret) {
63 c(ret, arr) %<-% in_array$recv_array()
64 if (ret) {
65 fprintf("Array: (%d rows)", length(arr))
66 # Print each line in the array
67 for (i in 1:length(arr)) {
68 fprintf("%5s, %d, %3.1f, %s",
69 arr[[i]][[1]], arr[[i]][[2]], arr[[i]][[3]], arr[[i]][[4]])
70 }
71 # Send the array to output. Formatting is handled on the output driver side.
72 ret <- out_array$send_array(arr)
73 if (!ret) {
74 stop("ascii_io(R): ERROR SENDING ARRAY")
75 }
76 } else {
77 print("ascii_io(R): End of array input (R)")
78 }
79}
Model YAML:
1---
2
3model:
4 name: ascii_io_R
5 driver: RModelDriver
6 args: src/ascii_io.R
7
8 inputs:
9 - name: inputR_file
10 driver: AsciiFileInputDriver
11 args: ./Input/input_file.txt
12
13 - name: inputR_table
14 driver: AsciiTableInputDriver
15 args: ./Input/input_table.txt
16
17 - name: inputR_array
18 driver: AsciiTableInputDriver
19 args: ./Input/input_array.txt
20 as_array: True
21
22 outputs:
23 - name: outputR_file
24 driver: AsciiFileOutputDriver
25 args: output_file.txt
26 in_temp: True
27
28 - name: outputR_table
29 driver: AsciiTableOutputDriver
30 args: output_table.txt
31 in_temp: True
32 field_names: name,number,value,complex
33
34 - name: outputR_array
35 driver: AsciiTableOutputDriver
36 args: output_array.txt
37 as_array: True
38 in_temp: True
39 field_names: name,number,value,complex