Daniel Svozil
Daniel Svozil

Reputation: 85

Read csv file repeatedly during model run in netlogo

I have a model in NetLogo where my turtles begin to move only when a variable (mean-daily-temp procedure) reaches 18. The values for this variable are coming from a csv file read in at the setup (see code below).

As it stands, the model runs the same number of ticks as there are data points (365 = # days per year). I wrote the model to only run this long by specifying if file-at-end? [stop], as I wanted to test if the go procedures worked (and they do). Now, I want the model to return to the first value in the data file and continue reading it, after reaching the end of the file. No previous questions on StackOverflow, or the google group for NetLogo, despite extensive googling of the keywords, NetLogo primitives or procedures have yielded satisfactory solutions. Possibly, I may not have understood them.

What can I replace [stop] to achieve this? Or does something else need to change?

I can extend the length of the data file, but I feel that the approach I want to use may be simpler or perhaps more elegant.

My code:

    `extensions
     [array
     csv]

     globals[
     river-patches ;;; water where the fish live
     bank-patches  ;;; patches were the fish cannot go
     initial-temp  ;;; temperature set at tick 0
     temp-list     ;;; variable containing temperature from .csv file
     mintemp       ;;; minimum temperature possible on any river-patch
     maxtemp]      ;;; maximum temperature possible on any river-patch

    patches-own
    [mean-daily-temp] ;;; variable for patch temperature

  to setup
     clear-all
     file-close-all               ;;; close any files
     file-open "temperature.csv"  ;;; contains the data for minimum and 
                                      maximum temperatures possible in each 
                                      tick
     setup-constants              ;;; set up constants
     setup-river-patches          ;;; set up river-patches
     setup-bank-patches           ;;; set up patches not in the river
     setup-turtles                ;;; set up turtles
     reset-ticks                  ;;; set ticks to 0
  end


   to setup-constants 
       set initial-temp (22.06)   ;;; initial temperature for each patch
   end


    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;;;;;;;;;;;;;;;;; SETUP PATCHES ;;;;;;;;;;;;;;;;;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

   ;;; creates patches that make up the river
   to setup-river-patches
     set river-patches patches with [pycor > -27 and pycor < 27]
     ask river-patches
       [set pcolor blue
        set mean-daily-temp (initial-temp)]

   ;;; creates bank patches that are green
     set bank-patches patches with [pycor <= -27 or pycor >= 27]
     ask bank-patches
       [set pcolor green]
   end

   ;;; creates the 'fish' turtles
   to setup-turtles
      create-turtles 20
      ask turtles [
        set color white
        set shape "fish"
        set size 1
        move-to one-of river-patches]
    end

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;;;;;;;;;; TO GO ;;;;;;;;;;;;;;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    to go 
       update-daily-temp ;;; this reads data from csv and creates the 
                             'mintemp' and 'maxtemp' variables used in 
                             'daily-river-temp'
       daily-river-temp  ;;; in each tick, this procedures sets the 
                             individual patch temperature

       move-turtles      ;;; procedure asking turtles to move in response to 
                             temperature
       tick
    end

       ;;;;;; HOW DO I ASK THE PROCEDURE update-daily-temp TO RETURN TO 
       ;;;;;;  START OF FILE, AFTER REACHING FINAL DATA POINT  
       ;;;;;;  SO THE MODEL CAN RUN FOR MORE TICKS THAN THERE 
       ;;;;;;  ARE DATA POINTS IN THE FILE? (i.e. UNTIL I CLICK GO AGAIN)

    to update-daily-temp
       if  file-at-end? [stop] 
       set temp-list csv:from-row file-read-line
       set mintemp item 0 temp-list
       set maxtemp item 1 temp-list
    end

    to daily-river-temp ;;; in each tick, set the individual patch 
                        ;;; temperature
       ask patches[
       set mean-daily-temp random-float (maxtemp - mintemp) + mintemp
       ]
    end

    to move-turtles
       ask turtles 
        [ifelse [mean-daily-temp] of patch-here > 18 and [pcolor] of patch 
        ahead 1 = blue
          [forward 1 set heading random 360 ]
          [set heading random 180]
        if xcor > 119 and xcor < 119
          [set heading random 360] pen-down]
   end

Thank you in anticipation!

Upvotes: 1

Views: 241

Answers (2)

Nicolas Payette
Nicolas Payette

Reputation: 14972

You can simply close and re-open your file instead of stopping:

if file-at-end? [
  file-close
  file-open "temperature.csv"
]

Another approach would be to load the entire file into memory using csv:from-file and then use modulo arithmetic to access the temperatures for that day. Supposing you have the entire file contents stored in a temperatures variable, your update-daily-temp procedure would become:

to update-daily-temp
   let temp-list item (ticks mod 365) temperatures
   set mintemp item 0 temp-list
   set maxtemp item 1 temp-list
end

Notice that I have used let instead of set for temp-list: you don't seem to be using that variable anywhere else then in update-daily-temp, so it should really be a local variable instead of a global variable. In general, you should avoid globals if you can.

Upvotes: 2

Jordan Chetcuti
Jordan Chetcuti

Reputation: 100

My instinct is that you should set up a counter such as:

ifelse tick <= 365[
  set InYearCounter tick
  set Year 1
]
[
  set InYearCounter (tick - (Year * 365))
  if InYearCounter = 365[
     set Year (Year + 1)
  ]
 ]

Then get the row in the csv table using the "InYearCounter".

to setup
   set temp-table csv:from-file "temperature.csv"
end

to update-daily-temp

   set temp-list item InYearCounter temp-table
   set mintemp item 0 temp-list
   set maxtemp item 1 temp-list
end

In this way the simulation won't end at the end of a year, but will be continued into a new year.

Upvotes: 0

Related Questions