how to make a loop for reading several nc files as a raster and then write them as envi?

StackOverflow https://stackoverflow.com/questions/13725007

Pergunta

This code given below (developed by stackoverflow users) will read a variable from a netcdf file and write it as an envi file.
This code is working fine but I have several netcdf files like this one and I want to make a loop to do this for all files in the directory.
The files are stored in a directory whose path is

d <-  flip(t(d), direction = "x") 
rf <- writeRaster(d, filename="last.envi", overwrite=TRUE)

I tried this:

for(i in 1:length(a)){
    d <- raster(a[i], varname = "soil_moisture_c")
    rf <- writeRaster(d[[i]], filename="%d.envi", overwrite=TRUE)
    }

but got this error

Error in function (classes, fdef, mtable) : 
unable to find an inherited method for function ‘writeRaster’ for signature ‘"numeric", "character"’
Foi útil?

Solução

2 bugs : change d[[i]] by d and use a new output file for each input.

 fileName <- strsplit(a[i],split='\\.')[[1]][1]
 outputFile <- paste(fileName,'_amenlast','.envi',sep='')
 rf <- writeRaster(d, filename=outputFile, overwrite=TRUE)

PS : I keep overwrite=TRUE , that means if you launch your loop next time it erase previous generated file.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top