Gopal Krishna
Gopal Krishna

Reputation: 65

Opening an ASCII file using R

I am trying to open an ASCII file in R.This file is exported from a software of thermal camera. It has temperature values of crop. I am using following code:

library(raster)
r = raster("AA092800_1.asc") 
plot(r)

but every time i am getting follwing error "not recognised as a supported file format Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file."

on examining the file, i found that there are commas instead of decimal points, I replaced all the commas with decimals but still the same problem exists.

Please help me out.

The ASCII file is attached herewith.original ascii file

same file comma replaced with decimal point text file. comma replaced with decimal point text file

Upvotes: 1

Views: 6479

Answers (1)

Sathish
Sathish

Reputation: 12703

df1 <- read.table("AA092800_1.asc", skip = 11, header = FALSE, sep = "\t")

head(df1, 2)
#      V1    V2    V3    V4    V5    V6    V7    V8    V9   V10   V11   V12   V13   V14   V15   V16   V17   V18   V19   V20   V21   V22   V23   V24   V25   V26   V27   V28 ...
# 1 36,46 36,33 36,40 36,37 36,10 36,27 36,50 36,49 36,54 36,52 36,51 36,63 36,91 36,97 36,81 36,89 37,12 36,70 37,24 37,29 37,65 37,33 37,13 37,26 37,41 37,14 38,01 37,84 ...
# 2 36,68 36,38 36,39 36,41 36,32 36,42 36,50 36,53 36,39 36,49 36,53 36,54 36,62 36,84 37,06 37,16 37,48 37,60 37,35 37,32 37,39 37,12 37,06 37,33 37,46 37,99 37,87 38,15  ...

This time using dec = "," indicating , as decimal value

df1 <- read.table("AA092800_1.asc", skip = 11, header = FALSE, sep = "\t", dec = ",")

head(df1, 2)
#      V1    V2    V3    V4    V5    V6   V7    V8    V9   V10   V11   V12   V13   V14   V15   V16   V17  V18   V19   V20   V21   V22   V23   V24   V25   V26   V27   V28  ...
# 1 36.46 36.33 36.40 36.37 36.10 36.27 36.5 36.49 36.54 36.52 36.51 36.63 36.91 36.97 36.81 36.89 37.12 36.7 37.24 37.29 37.65 37.33 37.13 37.26 37.41 37.14 38.01 37.84  ...
# 2 36.68 36.38 36.39 36.41 36.32 36.42 36.5 36.53 36.39 36.49 36.53 36.54 36.62 36.84 37.06 37.16 37.48 37.6 37.35 37.32 37.39 37.12 37.06 37.33 37.46 37.99 37.87 38.15  ...

For the other file, find the line number where Data begins and enter that number into skip argument.

Raster data needs data separated by ,

df1 <- read.table("AA092800_1.asc", skip = 11, header = FALSE, sep = "\t")

library('raster')
df2 <- as.raster(as.matrix(df1))

head(df2)
# "36,46" "36,68" "36,82" "36,75" "36,67" "36,94"
class(df2)
# [1] "raster"

Upvotes: 2

Related Questions