6 thoughts on “Working with NetCDF files in an agent-based model: Skinning the model input data cat (UPDATED)”

  1. Thank you so much for the very clear tutorial. I am interested in using the same concepts to import a time series of vegetation index rasters into NetLogo. I am using MODIS 16 day data. That means if each tick in my simulation is 1 day, then I am interested in importing a raster after every 16 ticks. I know how to import the rasters (as a list of files) using raster package in R (r-extension), but need your guidance to efficiently call each raster in the go procedure). Thank you

    1. Hi Francis! If you can get the raster data into NetLogo it shouldn’t be too much trouble to iterate them, but I don’t know how you’re accessing the rasters in your list, whether it’s from R or from NetLogo. Here’s a few ideas:

      First, I’d create a global variable called “raster-counter” to keep track of which raster I was on, and set this up with a value of 0.

      If you had a set of raster datasets already stored in a list in NetLogo (say in a list call ‘raster-list’), you could do something like this:

      to go
      tick
      if ticks mod 16 = 0 [
      set raster-counter raster-counter + 1
      set vegraster item raster-counter raster-list
      ]
      end

      The ‘mod’ here uses the modulo to determine whether or not 16 ticks has passed, and then you just iterate through the items in the list this way.

      If the data isn’t kept in NetLogo, but is stored as a set of ASCII raster files on your computer, you could use the ‘word’ primitive to iterate through a set of conventionally-named files (like ‘raster1.asc’, ‘raster2.asc’ and so on), and then import them using the GIS extension. That would work like this:

      to go
      tick
      if ticks mod 16 = 0 [
      set raster-counter raster-counter + 1
      set vegraster gis:load-dataset (word "raster" raster-counter ".asc")
      ]
      end

      The ‘word’ primitive concatenates strings, numbers, and variable values into a single string. So if the value of ‘raster-counter’ was 1, the expression above would create a string reading “raster1.asc”. This could be used to iterate through a set of file names stored on the computer. You could use this similarly to iterate through a set of named R objects e.g. r:get (word “rasterObject” raster-counter).

      If you are getting the raster data straight from a NetCDF in R like I did with the snowcover data, you could just wrap the expression in the raster counting bit, like so:

      to go
      tick
      if ticks mod 16 = 0 [
      r:eval (word "snow2<-as.data.frame(t(snow[,," ticks "]))")
      set s r:get "snow2"
      ask patches [ get-snow ]
      ]
      if ticks = 297 [ stop ]
      end

      Hope that helps! Do let me know if you run into trouble.

      Cheers,
      Ben

      1. Thank you for the response, what I did and which works although inefficiently is to have a sorted list of the raster (ASCII) files, and read through the list at the intended tick….a snippet is here. My data was scaled by a factor 10000. The code works but it is in-efficient for visualization because it takes a second too long when the rasters have to be changed.

        to update-vegetation-index
        ifelse ticks mod 16 = 0 [
        let vi-data file-read-line
        set veg-data gis:load-dataset vi-data
        ask patches [
        set vi gis:raster-sample veg-data self]
        ]
        [
        ask patches [set vi vi]
        ]
        ask patches [
        if (vi = 0)[
        set pcolor scale-color green vi 0 10000
        ]
        ]
        end

      2. Glad that helps. To speed things up, you might want to try combining these into a single ask, like:

        to update-vegetation-index
        ifelse ticks mod 16 = 0 [
        let vi-data file-read-line
        set veg-data gis:load-dataset vi-data
        ask patches [
        set vi gis:raster-sample veg-data self
        set pcolor scale-color green vi 0 10000
        ]
        ]
        end

        And of course you could use the counter option as well. Good luck!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s