Paulo Sergio
Paulo Sergio

Reputation: 39

Is it possible to import different files?

First, I'm sorry if this is a dumb question as I am new to Netlogo.

I've done the butterfly model from Steven Railsback and Volker Grimm book and there is an exercise to import a file that has coordinates X Y and a variable which is elevation in that example. So far, OK. But i'm currently trying to implement a model in which we have 4 different files each one containing the same coordinates but different variables, my question is: Is it possible to import those 4 files and have 4 different variables in our 'environment'?

Upvotes: 1

Views: 61

Answers (1)

Luke C
Luke C

Reputation: 10316

Just to make sure- you are working with the "ElevationData.txt" file found here, correct? So your file reading code should look more or less exactly as shown in the Railsback and Grimm book:

file-open "ElevationData.txt"                          
  while [not file-at-end?] [
    let next-X file-read                                 
    let next-Y file-read
    let next-elevation file-read                         
    ask patch next-X next-Y [set elevation next-elevation]
  ]
  file-close

So if the other files you want to import are mostly identical to the "ElevationData.txt" file, but with a different value in the third column, you can absolutely just modify that chunk of code accordingly. If we are dealing with vegetation cover as an example, you'd need a patches-own variable for that, along with the elevation variable that already exists:

patches-own [ elevation veg-cover ]

Now pretend that you have a "VegetationData.txt" file that looks something like:

0 0 0.86
1 0 0.15
2 0 0.42
3 0 0.44
4 0 0.43
5 0 0.33
...

After you have run your elevation import, you could do the exact same thing but with vegetation:

  file-open "VegetationData.txt"                          
  while [not file-at-end?] [
    let next-X file-read                                 
    let next-Y file-read
    let next-veg-cover file-read                         
    ask patch next-X next-Y [set veg-cover next-veg-cover]
  ]
  file-close

Now your patches will have a value assigned to both their elevation and veg-cover variables.

That said, it would be better if possible to combine your input files and load them all together. If you have a combined Elevation and Vegetation text file that looks like:

0 0 532.4 0.86
1 0 529.3 0.15
2 0 526 0.42
3 0 520 0.44
4 0 519.5 0.43
5 0 519.3 0.33
...

In the combined dataset ("ComboData.txt"), you still have the x and y columns, but now column three is elevation and column four is vegetation cover. Now you can load everything at the same time by slightly modifying your import code:

  file-open "ComboData.txt"                          
  while [not file-at-end?] [
    let next-X file-read                                 
    let next-Y file-read
    let next-elev file-read
    let next-veg-cover file-read                         
    ask patch next-X next-Y [
      set elevation next-elev
      set veg-cover next-veg-cover
    ]
  ]
  file-close

Upvotes: 2

Related Questions