Q: My raw data has a 9*9 block format as follows saved as a .dat file, say, MyData.dat:
0 21 21 11 11 11 11 11 11 0 0 11 11 11 11 22 22 22 0 0 0 11 22 22 22 22 22 0 0 0 0 22 22 22 22 22 0 0 0 0 0 22 22 22 22 0 0 0 0 0 0 12 12 12 0 0 0 0 0 0 0 22 22 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0
Now I want GAMS automatically import that data formated like shown below:
Table X(i,i) C1 C2 C3 C4 C5 C6 C7 C8 C9 C1 0 21 21 11 11 11 11 11 11 C2 0 0 11 11 11 11 22 22 22 C3 0 0 0 11 22 22 22 22 22 ...
GAMS does not rely on positional information when reading data, it needs set elements. The Windows GAMS system comes with a couple of Posix/Unix text utilities that allow you to modify your input file in a structured way to meet the GAMS data input requirements. Please find below an example of using the “awk” utility to create row and column information for your table:
$set CMAX 9 Set i /C1 * C%CMAX%/ ; TABLE X(i,i) $onechoV > x.awk BEGIN { for(i=0; i<=C; i++) printf "C%d ",i; printf "\n"; } NR<=C { print "C" NR,$0 } $offecho $call awk -vC=%CMAX% -f x.awk MyData.dat > X.dat $ondelim $include X.dat $offdelim option decimals=0; display X;
---- 22 PARAMETER X C2 C3 C4 C5 C6 C7 C8 C9 C1 21 21 11 11 11 11 11 11 C2 11 11 11 11 22 22 22 C3 11 22 22 22 22 22 C4 22 22 22 22 22 C5 22 22 22 22 C6 12 12 12 C7 22 22 C8 11
Below is another example:
189 187 192 202 210 235 293 321 328 325 315 297 286 286 290 299 321 321 299 282 270 255 234 209 229 ...
Sets p / t1*t1000 /; $echo "{print "t"++t,$0}" > x.awk $call awk -f x.awk raw.dat > indexed.gms Parameter data(p) / $include indexed.gms /;
t1 189 t2 187 t3 192 t4 202 t5 210 t6 235 t7 293 t8 321 t9 328 t10 325 t11 315 t12 297 t13 286 t14 286 t15 290 t16 299 t17 321 t18 321 t19 299 t20 282 t21 270 t22 255 t23 234 t24 209 t25 229 ...