Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading an ASC file into R

Tags:

I'm currently trying to extract information from various "ASC" files into R in order to perform analysis on the data.

The issue is that I am unsure of how exactly to read in the files. I attempted a standard read.table functions, but all the numbers were exactly the same (-9999.00). In order to rule out the possibility of data corruption, I read in another ASC file and got the same results. The only thing I know for certain, is that the file size between them is exactly the same.

Is there anyway that I can read these files in? Any R package I can look at?

I tied this:

    x = read.table("Dropbox/MVZ/aet2009sep.asc")     y = read.table("Dropbox/MVZ/aet2009oct.asc") 

and my outputs were

    > head(x, n =20)          V1        V2     1         ncols    3486.0     2         nrows    4477.0     3     xllcorner -374495.8     4     yllcorner -616153.3     5      cellsize     270.0     6  NODATA_value   -9999.0     7      -9999.00   -9999.0     8      -9999.00   -9999.0     9      -9999.00   -9999.0     10     -9999.00   -9999.0     11     -9999.00   -9999.0     12     -9999.00   -9999.0     13     -9999.00   -9999.0     14     -9999.00   -9999.0     15     -9999.00   -9999.0     16     -9999.00   -9999.0     17     -9999.00   -9999.0     18     -9999.00   -9999.0     19     -9999.00   -9999.0     20     -9999.00   -9999.0      head(y, n =20)          V1        V2     1         ncols    3486.0     2         nrows    4477.0     3     xllcorner -374495.8     4     yllcorner -616153.3     5      cellsize     270.0     6  NODATA_value   -9999.0     7      -9999.00   -9999.0     8      -9999.00   -9999.0     9      -9999.00   -9999.0     10     -9999.00   -9999.0     11     -9999.00   -9999.0     12     -9999.00   -9999.0     13     -9999.00   -9999.0     14     -9999.00   -9999.0     15     -9999.00   -9999.0     16     -9999.00   -9999.0     17     -9999.00   -9999.0     18     -9999.00   -9999.0     19     -9999.00   -9999.0     20     -9999.00   -9999.0 
like image 838
arifyali Avatar asked Nov 24 '13 17:11

arifyali


People also ask

Can R read .ASC files?

ASCII data filesThe main functions used in R to import data from ASCII files are read.

How do I read an ASC file?

Answer. These files can be opened practically by any text editor [such as Notepad or TextPad on Windows; TextEdit on Mac OS] and executed with the proper scripting engine. Launch a . asc file, or any other file on your PC, by double-clicking it.

Is ASC a raster file?

The ASCII Raster File format is a simple format that can be used to transfer raster data between various applications. It is basically a few lines of header data followed by lists of cell values.

What is ASC file format?

asc files is for encryption and securecommunication. In this case, the file will contain text consisting of ASCII characters (American Standard Code for Information Interchange). These . asc files are used to communicate securely via email or other text-based media.


1 Answers

Update: It is possible to read .asc files (aka ESRI ASCII Raster files) with the raster function directly from the 'raster' package. The help says:

If x is a filename, the following additional variables are recognized:

native: logical. Default is FALSE except when package rgdal is missing. If TRUE, reading and writing of ..., and Arc ASCII files is done with native (raster package) drivers, rather than via rgdal....

library(raster) r = raster("C:\\...\\Dropbox/MVZ/aet2009sep.asc") plot(r) 

Edit 2 [obsolete]:

An alternative is the raster() function, having the package rgdal properly installed.

library(rgdal) r = raster("C:\\...\\Dropbox/MVZ/aet2009sep.asc") plot(r) 

Edit 1 [obsolete]:

The package adehabitat is now deprecated. Currently, it provides a warning when loading it:

It is dangerous to use it, as bugs will no longer be corrected. It is now recommended to use the packages adehabitatMA, adehabitatLT, adehabitatHR, and adehabitatHS.
...

Original answer [obsolete]:

Use the import.asc function from R package adehabitat (see page 92):

library(adehabitat) asc = import.asc("C:\\...\\Dropbox/MVZ/aet2009sep.asc")  #plot asc object. library(raster) r = raster(asc) plot(r) 
like image 142
Andre Silva Avatar answered Oct 03 '22 12:10

Andre Silva