[R-sig-Geo] insert NODATA_value -999.0 into asc file

Rainer Hurling rhurlin at gwdg.de
Tue Jun 21 08:54:57 CEST 2011


Am 20.06.2011 23:11 (UTC+1) schrieb Johannes Signer:
> Hello,
>
> I would do that on a command line not R:
>
> echo "ncols         601
> nrows         351
> xllcorner     100000.0
> yllcorner     250000.0
> cellsize      1000.0
> 0 0 0 0 0 0
> 0 0 0 0 0 0">  test.asc
>
> head -n 5 test.asc>  tmp_header
> awk 'NR>5{print $0}' test.asc>  tmp_body
> echo NODATA -999.0>>  tmp_header
> cat tmp_header rest.asc>  new.asc
> mv new.asc test.asc
> rm tmp_header tmp body

When using the stream editor 'sed' you would only need a one liner for that:

All you have to do before you can start converting, is creating a text 
file, say 'asc.sed' with only two lines:

--------------------
5a\
nodata_value  -999.0
--------------------

This is your script file for 'sed'. It appends the text (second script 
line) after the fifth line of the original data.

A conversion of a single .asc-file should then work like this:

sed -f asc.sed file0001_old.asc > file0001_new.asc

Please be aware that you have to choose a new file name, because sed 
reads in the old file and writes to the new file at the same time 
(potential data loss!).

> You can easily wrap a loop arround it to apply it to a  few thousand rasters.

Yes, and in this loop you could handle your file naming scheme. Both awk 
and sed are little tools on Unix alike systems and they are very fast.

Just as a second way to solve this issue,
Rainer Hurling


> Of course you could do it also in R with readLines().
>
> Hope this helps
> Johannes
>
> On Mon, Jun 20, 2011 at 4:51 PM, Alexander Arpaci
> <alexander.arpaci at boku.ac.at>  wrote:
>> Dear List,
>>
>> I have a large number (several thousand) of asci files which were
>> delivered to me without a NODATA_value  -999.0 line in the header
>> I can´t use them with the raster package like that so far.  I got an
>> error message about the missing NODATAVAL when I try to read it into a
>> raster object.
>> Does somebody have a suggestion how to insert that line into my existing
>> asci files?
>> I can´t really figure out how do insert that row in line number 5
>> without replacing the old line 5...especially with a format like that
>> ncols         601
>> nrows         351
>> xllcorner     100000.0
>> yllcorner     250000.0
>> cellsize      1000.0
>> NODATA_value  -999.0
>>
>> while the other rows have 601 entries
>> every help apreciated
>> alexander
>>
>> MSc. Alexander Arpaci
>> Assistant researcher
>>
>> Department of Forest - and Soil Sciences
>> Institute of Silviculture
>> Tel. +43-1-47654-4081
>> e-mail: alexander.arpaci at boku.ac.at
>> www.wabo.boku.ac.at



More information about the R-sig-Geo mailing list