I can’t understand why the zoom
loop gets skipped or delayed? Can someone help me understand the difference?
The printed output of the following code is the following. It basically means the next year is iterated before zoom is iterated. Consequently memory fills out and I get an out of memory exception, because records and locations are huge structures.
year = 1986
calculated records
calculated locations
year = 1987
calculated records
calculated locations
year = 1988
def generateFiles(year1:Int,year2:Int): Unit = {
// To write an image into a PNG file, use the output method.
// For instance: “myImage.output(new java.io.File("target/some-image.png"))”.
import Extraction._
for {year <- (year1 to year2)
_1 = println(s"year = $year")
records = locateTemperatures(year, "/stations.csv", "/" + year + ".csv")
_2 = println(s"calculated records")
locations: Iterable[(Location, Double)] = locationYearlyAverageRecords(records)
_3 = println(s"calculated locations")
zoom <- 0 to 2
_4 = println(s"zoom=$zoom")
y <- (0 until 1 << zoom)
_5 = println(s"y=$y")
x <- (0 until 1 << zoom)
_6 = println(s"x=$x")
dirname = s"target/temperatures/$year/$zoom"
fname = s"$dirname/$x-$y.png"
} {
println(s"generating $fname")
val image = tile(locations, palette, zoom, x, y)
val dir = new java.io.File(dirname)
val file = new java.io.File(fname)
dir.mkdirs()
image.output(file)
println(s"generated $fname")
}
}
However, if I break up the for comprehension into several concentric for comprehensions it works and the output is as follows.
year = 1986
calculated records
calculated locations
zoom=0
zoom=1
zoom=2
y=0
x=0
generating target/temperatures/1986/0/0-0.png
generated target/temperatures/1986/0/0-0.png
y=0
y=1
x=0
x=1
generating target/temperatures/1986/1/0-0.png
generated target/temperatures/1986/1/0-0.png
generating target/temperatures/1986/1/1-0.png
The code that works is the following
def generateFiles(year1:Int,year2:Int): Unit = {
// To write an image into a PNG file, use the output method.
// For instance: “myImage.output(new java.io.File("target/some-image.png"))”.
import Extraction._
for {year <- (year1 to year2)} {
val _1 = println(s"year = $year")
val records = locateTemperatures(year, "/stations.csv", "/" + year + ".csv")
for {
_2 <- Some(println(s"calculated records"))
locations: Iterable[(Location, Double)] = locationYearlyAverageRecords(records)
_3 <- Some(println(s"calculated locations"))
}
for {
zoom <- 0 to 2
_4 = println(s"zoom=$zoom")
y <- (0 until 1 << zoom)
_5 = println(s"y=$y")
x <- (0 until 1 << zoom)
_6 = println(s"x=$x")
dirname = s"target/temperatures/$year/$zoom"
fname = s"$dirname/$x-$y.png"
} {
println(s"generating $fname")
val image = tile(locations, palette, zoom, x, y)
val dir = new java.io.File(dirname)
val file = new java.io.File(fname)
dir.mkdirs()
image.output(file)
println(s"generated $fname")
}
}
}