Hi Ping,
You might want to look into storing your data in a database, and then accessing the db from R. That might speed things up.
Good luck,
John
--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maxent+un...@googlegroups.com.
To post to this group, send email to max...@googlegroups.com.
Visit this group at http://groups.google.com/group/maxent.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maxent+un...@googlegroups.com.
To post to this group, send email to max...@googlegroups.com.
Visit this group at http://groups.google.com/group/maxent.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to a topic in the Google Groups "Maxent" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/maxent/045l4NYb4JI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to maxent+un...@googlegroups.com.
Hi Jenna,
Did you manage to get R to write out the .grd files ok? Note that Maxent should also work fine with bil grids.
- John
--
--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maxent+un...@googlegroups.com.
To post to this group, send email to max...@googlegroups.com.
Visit this group at http://groups.google.com/group/maxent.
For more options, visit https://groups.google.com/d/optout.
Thank you so much for you help. I will give it a try and hope it works.
Sam I am not sure what you are suggesting. Yes i think I might get confused if they are named the same...
I know this might be a stupid question but wats the best code to start off by loading your bioclim variables into R? And then that will follow by clipping them to same resolution and extent.
To get the extent values for Madagascar. Would you use Google earth?
You help is truly appreciated.
Kind regards
Ryan
You received this message because you are subscribed to a topic in the Google Groups "Maxent" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/maxent/045l4NYb4JI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to maxent+un...@googlegroups.com.
That's perfect. Thanks so much. I will give it a try and i will let you know if it works or if I have any problems.
Ryan
I find the easiest way to import all your raster data is to pass a vector of file names to stack(). Assuming you want to read in all of the .grd files from within a particular directory (for example, c:/mydir), you can then use list.files to create that vector:
s <- stack(list.files('c:/mydir', patt='\\.grd$', full.names=TRUE))
If there are grd files of interest in subdirectories within c:/mydir, just add recursive=TRUE to the list.files call.
This requires that all grids are the same extent and resolution.
You can then clip the whole stack with crop.
> Bio1<-raster(("E:/MSc 2014/R/ccsmbio1.bil"), pattern="grd", full.names=T) Error in .local(.Object, ...) : `E:\MSc 2014\R\ccsmbio1.bil' not recognised as a supported file format. Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file.
I get the same message doing the stack code provided.
s <- stack(list.files('E:/MSc 2014/R/', patt='\\.grd$', full.names=TRUE))
Can anyone help me please.
Kind regards
Ryan
s <- stack(list.files('E:/MSc 2014/R/', patt='\\.bil$', full.names=TRUE))
Cheers,
John
> s <- stack(list.files('E:/MSc 2014/R/', patt='\\.bil$', full.names=TRUE))
Error in .local(.Object, ...) : `E:\MSc 2014\R\ccsmbio1.bil' not recognised as a supported file format. Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file.
I don't have a clue what it could be.
Ryan
Last glacial maximum (LGM; ~21,000 years BP)
source: Paleoclimate Modelling Intercomparison Project Phase II (PMIP2) use conditions: please register with the PMIP2
project if you want to use these data; or want access to the original
variables, we only provided data derived from the PMIP2 database.
2.5 arc-minutes
CCSM: bio
MIROC: bio
Yes, as long as filenames are prefixed by "bio" and the variable number. If that's not the case you might need to change the pattern in this line:
vars <- sort(unique(as.numeric(gsub('^bio([0-9]+)_.*', '\\1', files))))
--
Hi Ryan,
It's best to start a new thread for new questions.
Assuming you want area in units of "cells", you can do something like:
library(raster)
r <- raster('c:/path/to/prediction.asc')
sum(values(r > thr), na.rm=T)
Where thr is your threshold value. The inequality returns a logical grid (coerced to numeric with false as 0 and true as 1). The values function returns the vector values in the raster object's cells, and sum adds these ones and zeros to return the number of cells that exceed the threshold. Use >= if you prefer.
Correct the path to your prediction grid first.
Bear in mind, though, that if your prediction grids are not in an equal area projection, then cell areas will vary across the landscape (particularly problematic if your study area spans a large latitudinal range).
I'm new to R and new to ENM. I'm trying to use some code that was previously posted to do the same thing (I think) as the original thread starter. I was hoping someone could explain where I am going wrong so that I can learn my way through this.
Essentially, I have Bioclim layers for all 19 variables and two different tiles (tiles #22 & #23). I need to stack and merge all 19 variables and the two tiles and crop it all to my study region. I'm using the code that is pasted below, which is the same code that was posted by John Baumgartner on 4/12/12 in this thread. This code gives me an error (pasted below) when I run lines 12-17 starting with grids. I got this to run successfully when I did only two .bil files (bio1_22 & bio1_23) and even produced plots with no problem. When I tried to run it with bio1_22 through bio9_22 just to make sure it worked before running all 38 .bil files, it gives me the error. I tried to play around with L8 starting with vars where it says bio([0-9] to adjust the numbers but that didn't seem to fix the problem. Does anyone have any insight on what is going wrong here? Please let me know if you need any other info, and apologies if the answer is painfully obvious (it's not to me!).
Thank you!
"Error in as.data.frame.default(x) :
cannot coerce class "structure("RasterLayer", package = "raster")" to a data.frame"
CODE (originally from John Baumgartner on 4/12/12):
# install.packages('rgdal') # install rgdal if not already installed
library(raster)
# set the working directory to the path that contains your .bil files
setwd('C:/Users/zwc/Documents/Rdir')
# create a list of .bil files that exist in the wd
files <- list.files(pattern='\\.bil$')
# vars is a vector of bioclim variable numbers
vars <- sort(unique(as.numeric(gsub('^bio([0-9]+)_.*', '\\1', files))))
# for each of vars, create raster object for each tile and merge
# (this is a bit messy, but all I could think of for now...)
# grids will be a list of rasters, each of which is the merged tiles for a BC var.
grids <- sapply(vars, function(x) {
patt <- paste('bio', x, '_', sep='')
tiles <- files[grep(patt, files)]
merged <- eval(parse(text=paste('merge(', toString(paste('raster(', tiles, ')',
sep='"')), ')', sep='')))
})
# give the list elements names
names(grids) <- paste('bio', vars, sep='')
# combine all list elements into a stack
s <- stack(grids)
# quick plot to make sure nothing went drastically wrong
plot(s)
# crop to your study extent
s.crop <- crop(s, WashingtonBoundary) #This last line will obviously change for my boundaries, but I haven't made it that far yet!
Just to update... I did not find a fix to the error I was getting, but a friend sent me some code that I modified to get out what I needed. This is just basic code and someone could probably easily add a loop in here so that you won't have to manually do all these. This code at least allowed me to just change the input and output filenames, and create the 19 merged and cropped raster layers that I needed for the 19 bioclim variables. This also outputs them in .asc format so they are ready for use in Maxent.
library(raster)
library(rgdal)
setwd("C:/Users/zwc/Documents/Rdir2")
#r22 is a given bioclim raster for tile 22
r22<-stack(list.files(pattern="*_22.bil", full.names=TRUE))
#r23 is a given bioclim raster for tile 23
r23<-stack(list.files(pattern="*_23.bil", full.names=TRUE))
#merge the two rasters
r2223<-merge(r22,r23)
bbox <- as(extent(-102.5,-85,13,28), 'SpatialPolygons')
#specify lat lon to crop the data, xmin xmax ymin ymax
z <- crop(r2223,bbox)
plot(z) #check to make sure the rasters are merged and cropped to the correct area
z2<-writeRaster(z,filename="bio1.asc",format="ascii",overwrite=TRUE) #change name for each bioclim variable
lapply(vars, function(x) {
patt <- paste('bio', x, '_', sep='')
files[grep(patt, files)]
})
Maybe there's something odd about one of the variables - perhaps one of them only has a single tile?
Glad you have a workaround, anyway.
Cheers,
John
Visit this group at https://groups.google.com/group/maxent.