Using raster to import headerless ASCIIs?
Jonathan,
A little shorter:
library(raster)
s <- read.fwf("soilwhc.dat", widths=rep(5,36))
r <- raster(ncol=720, nrow=360, xmn=0, xmx=360)
values(r) <- as.vector(t(as.matrix(s)))
r <- rotate(flip(r, 'y'))
plot(r)
Robert
On Sat, May 11, 2013 at 10:45 AM, Jonathan Greenberg <jgrn at illinois.edu> wrote:
I was trying to solve this entirely within R -- this was my solution:
# The file is fixed width format, each number has a width of 5,
# with 36 numbers to a line:
soilwhc_dunne_raw_df <- read.fwf("soilwhc.dat",widths=rep(5,36))
soilwhc_dunne_raw_vector <- c(t(data.matrix(soilwhc_dunne_raw_df)))
soilwhc_dunne_raw_matrix <-
matrix(soilwhc_dunne_raw_vector,ncol=360,nrow=720)
# The image comes out flipped along the y axis:
soilwhc_dunne_raw <- flip(raster(t(soilwhc_dunne_raw_matrix)),"y")
extent(soilwhc_dunne_raw) <- extent(0,360,-90,90)
projection(soilwhc_dunne_raw) <- CRS("+proj=longlat +datum=WGS84")
# We'll also rotate it:
soilwhc_dunne_raw_rotate <- rotate(soilwhc_dunne_raw)
I was curious if there was a more elegant solution to this? Either way,
thanks for the response!
--j
On Fri, May 10, 2013 at 3:03 PM, Etienne B. Racine <etiennebr at gmail.com>wrote:
Jonathan, You should set an header in the first file. Dunne_soil.dat is recognized as an Ascii Grid by GDAL because it has an header : ncols 720 nrows 360 xllcorner -180 yllcorner -90 cellsize 0.5 NODATA_value -9999 soilwhc.dat has no header. Also NA is coded with no space (-99.0-99.0). Maybe if you search and replace -99.0 by -9999[space] this would make it more standard. The lines are also rather short (36), but very long (7200). I guess each row is indeed 10 lines. It would share the same header than dunne_soil.dat. So you have some reformatting to get it working. Etienne 2013/5/10 Jonathan Greenberg <jgrn at illinois.edu>
r-sig-geo'ers: I'm trying some random hacks, but was wondering if there is a more efficient way to import the following file: ftp://ftp.daac.ornl.gov/data/global_soil/Dunne/data/soilwhc.dat as a raster(). The file is a fixed width file (width=5) of floating point values, but had the Arc header stripped off. A reference file that DOES work with raster is: ftp://ftp.daac.ornl.gov/data/global_soil/Dunne/data/dunne_soil.dat reference_raster <- raster(dunne_soil.dat) # This has the same number of rows and columns and resolution as # the file above. Thoughts on the most efficient way to convert the first file to a workable raster? --j -- Jonathan A. Greenberg, PhD Assistant Professor Global Environmental Analysis and Remote Sensing (GEARS) Laboratory Department of Geography and Geographic Information Science University of Illinois at Urbana-Champaign 607 South Mathews Avenue, MC 150 Urbana, IL 61801 Phone: 217-300-1924 http://www.geog.illinois.edu/~jgrn/ AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007 [[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
-- Jonathan A. Greenberg, PhD Assistant Professor Global Environmental Analysis and Remote Sensing (GEARS) Laboratory Department of Geography and Geographic Information Science University of Illinois at Urbana-Champaign 607 South Mathews Avenue, MC 150 Urbana, IL 61801 Phone: 217-300-1924 http://www.geog.illinois.edu/~jgrn/ AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007 [[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo