I have a text-file which looks like outlined below. It contains a header of 6 lines and then only numeric values (integers). I would like to bin these integers to a range lets assume values are in range from 0 ... 50 I would like to bin them to 5 groups i.e. bin to 0...10...20...30...40...50
Is there some elegant way to bin them?
ncols 4
nrows 3
xllcorner 0
yllcorner 0
cellsize 1
nodata_value -999
1 2 3 4
4 21 3 3
3 2 31 1
Currently I use an iterator for the lines of the file and tokenize the values within a line using a scanner - this seems rather clumsy. Looking forward for some elegant ideas.
Expected output would be
ncols 4
nrows 3
xllcorner 0
yllcorner 0
cellsize 1
nodata_value -999
1 1 1 1
1 2 1 1
1 1 3 1
object Test {
def main(args:Array[String]):Unit={
val source = scala.io.Source.fromFile("/ssd2/test.txt")
val lines = source.getLines().toList
val (header, payload) = (lines.take(6), lines.drop(6))
def classify(i:Int):Int = (i / 10) + 1
val binned = payload.map{line =>
line.split(" ").map(n => classify(n.toInt)).mkString(" ")
}
val result = header ++ binned
result.foreach(println(_))
}
}
Although the output is
ncols 4
nrows 3
xllcorner 0
yllcorner 0
cellsize 1
nodata_value -999
1 1 1 1
1 3 1 1
1 1 4 1
I think you have a small bug in your expected output sample.