Help: how to pull various files in NetLogo, without it is named in sequence

20 views
Skip to first unread message

Rafaela Lorena

unread,
Sep 29, 2021, 1:28:47 PM9/29/21
to netlogo-users
Hello,

I have a problem and I still don't know how to solve it. If anyone in the group can help, I appreciate it.
The problem is this: I have 40 areas of study. I would like the simulation to take place with one study area and then take the other study area and run the simulation and so on. I got this to happen. However, study areas must be named in sequence to work. For example: L1_MAP.asc ; L2_MAP.asc ; L3_MAP.asc and so on. However, what happened is that I don't actually have this linear sequence of .asc files. What I have is L2_MAP.asc then L18_MAP.asc and so on. To work I would have to rename all files. asc which are multiple blocks of files containing 40 .asc files. However, I THINK there must be a way for NetLogo to read in the files in the folder and run the simulation for each file without being sequential. Is this really possible?  Or do I really have to rename all the .asc files by hand?

Thanks in advance

The part of code

extensions [ gis ]
globals [ Landcover NumLandscapeFiles output? num-LandscapeFiles  ]
patches-own [ habitatcover ]

to setup  
  set NumLandscapeFiles 1
  let num NumLandscapeFiles  
  set Landcover gis:load-dataset ( word "./L" num "_MAP.asc" )  
  gis:set-world-envelope gis:envelope-of Landcover
  gis:apply-raster Landcover habitatcover  
  color-map
end


to color-map ;; color the landcover
  ask patches [
    if habitatcover = 1 [ set pcolor orange ]
    if habitatcover = 2 [ set pcolor orange ]    
  ]
end


to go
  ; ....  
  let n count turtles
  if n = 0 and output? = true [ output ]  
  if NumLandscapeFiles = num-LandscapeFiles + 1 [ stop ] ;; num-LandscapeFile is a input (interface) that the number is 40 areas  
end

to output
;...
end

Charles Staelin

unread,
Sep 29, 2021, 2:36:08 PM9/29/21
to netlogo-users
Rafael,

I'm not sure how you've set up your files, but the Pathdir extension does have a command to get a list of the files in a directory, which you could then iterate through.  Would that help?

Charles

Steve Railsback

unread,
Sep 29, 2021, 3:25:06 PM9/29/21
to netlogo-users
Or: Put all your file names in a text file, one file name per line.

Read those names into a list (actually a list of lists; read the csv extension doc.):
 
  let file-names csv:from-file "my-file-of-file-names.csv"

Then iterate through that list:

  foreach file-names [ next-file-name-list ->
   setup
   go
  ]

And in setup, use this:
set Landcover gis:load-dataset (first next-file-name-list)

Wade Schuette

unread,
Sep 29, 2021, 4:27:56 PM9/29/21
to netlogo-users
Hm, didn't know about Pathdir extension.
Here's a solution by using the py (Python) extension.   You point to the directory where the asc files are located, and then select just that file type ( modify the code from "txt"), and it walks through them. Seems to work for me.

extensions [py]
 to setup
  clear-all

  user-message "Locate your asc file directory"
  set-current-directory user-directory
;; Assumes the user will choose a directory
 
py:setup py:python

show py:runresult "1 + 1"
;; expect 2
 py:run "print('hi')"
;; expect hi
;; https://stackabuse.com/python-list-files-in-a-directory/

py:run  "import os"

  (py:run
     "for root, dirs, files in os.walk('.'):"
     "   x = files "
    )

  let z ( py:runresult "x" )
  let filecount length(z)
  print (word "Found " filecount " files in that directory: \n" z "\n\n")

 
let i -1;
repeat filecount [
set i (i + 1 )
 
        ;; decide whether file has the right suffix
        let fname item i z
        let fnamelength length(fname)
        let suffix substring fname  (fnamelength - 3)  (fnamelength)
        print (word "item "    i   " has suffix "   suffix)
   
    ifelse ( suffix = "txt" ) [process fname ]  ;; or "asc",whatever
       [ print (word ".......skipping over:" fname "\n\n" ) ]  ;; skip not asc files
  ]
  print "All done"
  reset-ticks
end

to process [onefile]
  print (word "......reading " onefile )
  file-open onefile
  print (word "First line: " file-read-line "\n\n")
  file-close
end


--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/CAHs8kB-1aJnw-geGCaJSi_qhEPi%3DDx3k4GvYKiEeiNmD9tpNQw%40mail.gmail.com.

Rafaela Lorena

unread,
Sep 29, 2021, 7:37:41 PM9/29/21
to Wade Schuette, netlogo-users
Thanks so much Charles Staelin, Steve Railsback and Wade Schuette.

Your suggestions helped a lot :)

Cheers



Reply all
Reply to author
Forward
0 new messages