Beginner Question: Using csv as an input

Problems with syntax of GAMS
Post Reply
leobarlach
User
User
Posts: 2
Joined: 4 months ago

Beginner Question: Using csv as an input

Post by leobarlach » 4 months ago

Hello,

I'm having some difficulties with inputting tables from csv.

I believe it has something to do with the headers of the csv, which I could remove manually, but would like to maintain because that's how they come out of R.

Whenever I try to read the table, I get the following error:
326 An empty table statement is not followed by a legal statement
Would appreciate any help.

My code:

Code: Select all

$Title Debugging model

$OnEmpty OnMulti

* definitions

sets
   y             /2015,2020/
   p             /1*4/
   sc            /sc1,sc2/
   g             /Coal,NaturalGas,Nuclear,FuelOil/
   nd            /A,B/

parameters
   pDemand     (y,sc,nd,p)
   pWindGen    (y,sc,nd,p)

   pGenCap     (y, g,nd  )


$ondelim
table
Hourly(y,sc,nd,h,*)
/
$include hourlyDataInput.csv
/
;
$offdelim

pDemand  (y,sc,nd,p) = Hourly (y,sc,nd,p,'Demand') ;

pWindGen (y,sc,nd,p) = Hourly (y,sc,nd,p,'WindGen');

parameter    pGenCap(y,   g,nd)  "Generation capacity per year and hub "
/
$ondelim
$include CapDataInput.csv
$offdelim
/
;
The CapDataInput.csv file:

Code: Select all

y,g,nd,GenCap
2015,Coal,A,20
2020,Coal,A,22
2015,NaturalGas,A,30
2020,NaturalGas,A,40
2015,Nuclear,A,5
2020,Nuclear,A,5
2015,FuelOil,A,2
2020,FuelOil,A,2
2015,Coal,B,10
2020,Coal,B,10
2015,NaturalGas,B,35
2020,NaturalGas,B,48
2015,Nuclear,B,0
2020,Nuclear,B,0
2015,FuelOil,B,3
2020,FuelOil,B,3
The hourlyDataInput.csv file:

Code: Select all

y,sc,nd,p,Demand,WindGen
2015,sc1,A,1,17.64,4.52
2015,sc1,A,2,21.13,3.33
2015,sc1,A,3,22.48,5.74
2015,sc1,A,4,22.01,4.81
2020,sc1,A,1,27.32,4.44
2020,sc1,A,2,19.94,2.5
2020,sc1,A,3,28.08,4.05
2020,sc1,A,4,19.77,4.2
2015,sc1,B,1,24.03,5.41
2015,sc1,B,2,15.89,3.03
2015,sc1,B,3,18.71,4.11
2015,sc1,B,4,23.36,3.67
2020,sc1,B,1,16.62,1.9
2020,sc1,B,2,24.22,4.91
2020,sc1,B,3,29.33,3.33
2020,sc1,B,4,27.38,2.89
2015,sc2,A,1,22.82,3.82
2015,sc2,A,2,25.19,3.2
2015,sc2,A,3,23.77,4.39
2015,sc2,A,4,20.67,4.21
2020,sc2,A,1,20.69,3.61
2020,sc2,A,2,23.15,2.35
2020,sc2,A,3,19.1,2.4
2020,sc2,A,4,25.26,3.19
2015,sc2,B,1,26.72,4.21
2015,sc2,B,2,28.73,3.18
2015,sc2,B,3,26.8,3.93
2015,sc2,B,4,23.23,4.05
2020,sc2,B,1,21.67,2.65
2020,sc2,B,2,27.35,5.91
2020,sc2,B,3,13.6,5.23
2020,sc2,B,4,17.97,3.87

User avatar
Renger
Posts: 127
Joined: 1 year ago

Re: Beginner Question: Using csv as an input

Post by Renger » 4 months ago

Hi

You made some mistakes in the syntax and the definition of the tables themselves (the second table was defined as a parameter at the beginning of your code and you don't have to use the forward slashes).
Here is the correct code:

Code: Select all

table Hourly(y,sc,nd,*,*)
$ondelim
$include hourlyInputData.csv
$offdelim
;
pDemand  (y,sc,nd,p) = Hourly (y,sc,nd,p,'Demand') ;

pWindGen (y,sc,nd,p) = Hourly (y,sc,nd,p,'WindGen');

table    pGenCap(y,g,nd,*)  "Generation capacity per year and hub "
$ondelim
$include CapDataInput.csv
$offdelim
;
CHeers
Renger

leobarlach
User
User
Posts: 2
Joined: 4 months ago

Re: Beginner Question: Using csv as an input

Post by leobarlach » 4 months ago

Thank you. As with everything with new languages, I though I had tried that before, but clearly was making some mistake.

For using csv for parameters, is there also a way I can use headers? Or is it better to assign a new table and then the parameter? (if we want to retain the csv with header for future work).

For example:

Code: Select all

parameter
	param(y) 	
	
parameter
param(y)
$ondelim
$include param.csv
$offdelim
With the param.csv file looking like:

Code: Select all

y,param
2015,0.5
2020,0.6

User avatar
Renger
Posts: 127
Joined: 1 year ago

Re: Beginner Question: Using csv as an input

Post by Renger » 4 months ago

Hi Leo

Why would you use a header? I would do it like this:

Code: Select all

sets
   y   /2015,2020/
;
parameter param(y) /
$ondelim
$include param.csv
$offdelim
/;

display param;
with param.csv

Code: Select all

2015,0.5
2020,0.6
Cheers
Renger

User avatar
dirkse
Moderator
Moderator
Posts: 21
Joined: 1 year ago
Location: Fairfax, VA

Re: Beginner Question: Using csv as an input

Post by dirkse » 4 months ago

If you want to use CSV files with headers and not change them at all, you could use the GAMS utility csv2gdx. The datalib model csv2gdx1 gives you a working example: there's a call to csv2gdx in that model, and also some error checking.

To read your file, I did:

Code: Select all

csv2gdx param.csv output=param.gdx ID=param useheader=T trace=3 index=(1) values=(2)

Post Reply