CSVWrite

Table of Contents

Note
This tool is part of the GAMS Tools Library. Please inspect the general information about GAMS Tools.

This tool writes a symbol from a GDX file to a CSV file. If gdxIn is not specified, a symbol can be written directly from the GAMS database as well. See the example below.

This tool utilizes the GAMS Connect Projection agent to convert variables and equations to parameters and the CSVWriter agent to write the symbol to a CSV file. Therefore, many of its parameters are detailed in the agent's chapter as well. Further, it also utilizes GDXDUMP to handle different numerical formats.

Usage

Command line:

gamstool [data.]csvwrite gdxIn=fileIn.gdx id=symbolName file=fileOut.csv

Compile time:

$callTool [data.]csvwrite [gdxIn=fileIn.gdx] id=symbolName file=fileOut.csv

Execution time:

executeTool '[data.]csvwrite [gdxIn=fileIn.gdx] id=symbolName file=fileOut.csv';

The following named parameters are available:

Parameter Type Default Description
allFields boolean N Specify whether all the attributes (level, marginal, lower, upper, and scale) of a variable or an equation are written to the CSV. By default only the level will be written. (Y for Yes, N for No)
decimalSep string period Specify a decimal separator. (period, comma)
dFormat string normal Specify the numerical format in the output file. (normal, hexponential, hexbytes) See Tool GDXDUMP option dFormat for more details.
fieldSep string comma Specify a field separator. (comma, semicolon, tab)
file string None Specify the name for the CSV file. If omitted and gdxIn is specified, the GDX file name will be used.
gdxIn string None Specify the input GDX file.
header boolean Y Specify if the header will be written. (Y for Yes, N for No)
id string None Specify the name of the symbol in the GDX file or GAMS database.
quoting integer 0 Control field quoting behavior. (0,1,2,3) See Connect agent CSVWriter option quoting for more details.
setHeader string None Specify a string that will be used as the header.
skipText boolean N Specify if the set element text will be skipped. (Y for Yes, N for No)
trace integer 0 Specify the trace level for debugging output. (0,1,2,3) See Connect agent CSVWriter option trace for more details.
unstack boolean N Specify if the last dimension will be unstacked to the header row. (Y for Yes, N for No)
Note
If dFormat is not normal, the tool utilizes GDXDUMP in the background instead of the CSVWriter. Since GDXDUMP does not allow to control the field quoting behavior, setting quoting will have no effect. The GDXDUMP quoting behavior corresponds to quoting=2.

Example

Set a /i1*i4/, b /j1*j2/;

Parameter c(a, b);

c(a,b)=UniformInt(1,10);

executeTool 'csvwrite id=c quoting=2 file=c_out.csv';

The above example generates the following CSV file named, c_out.csv.

"a","b","value"
"i1","j1",2.0
"i1","j2",9.0
"i2","j1",6.0
"i2","j2",4.0
"i3","j1",3.0
"i3","j2",3.0
"i4","j1",4.0
"i4","j2",9.0

The tool exported symbol c to the CSV file during execution-time. Further, the following example shows how GDXDUMP is utilized in the background to store the same data but in hexponential numeric format using option dFormat.

Set a /i1*i4/, b /j1*j2/;

Parameter c(a, b);

c(a,b)=UniformInt(1,10);

executeTool 'csvwrite id=c file=c_out_exp.csv dFormat=hexponential';

The above example generates the following CSV file named, c_out_exp.csv.

"a","b","Val"
"i1","j1",0x1.0p1
"i1","j2",0x1.2p3
"i2","j1",0x1.8p2
"i2","j2",0x1.0p2
"i3","j1",0x1.8p1
"i3","j2",0x1.8p1
"i4","j1",0x1.0p2
"i4","j2",0x1.2p3