I'm working on crop-climate modelling using Robert Hijmans' package 'dismo' and more specifically the function 'ecocrop'. Ecocrop calls a series of 'hidden' functions including, .doEcocrop (ecocrop and .doEcocrop pasted below), which calculate the final output of ecocrop.
.doEcocrop <‐ function(crop, tmin, tavg, prec, rainfed) {
if (rainfed)
{
nasum <‐ sum(is.na(c(tmin, tavg, prec)))
} else {
nasum <‐ sum(is.na(c(tmin, tavg)))
}
if (nasum > 0) { return( new('ECOCROP')) }
duration <‐ round((crop@GMIN + crop@GMAX) / 60)
tmp <‐ c(crop@TMIN, crop@TOPMN, crop@TOPMX, crop@TMAX)
temp <‐ .getY(tmp, tavg)
ktmp <‐ c(crop@KTMP, crop@KTMP, Inf, Inf)
tmin <‐ .getY(ktmp, tmin‐5)
if (rainfed) {
pre <‐ c(crop@RMIN, crop@ROPMN, crop@ROPMX, crop@RMAX)
shftprec <‐ c(prec[12], prec[‐12])
cumprec <‐ movingFun(prec, n=duration+1, fun=sum, type='from', circular=TRUE) + shftprec
prec <‐ .getY(pre, cumprec)
allv <‐ cbind(temp, tmin, prec)
} else {
allv <‐ cbind(temp, tmin)
}
minv <‐ apply(allv, 1, min)
obj <‐ new('ECOCROP')
obj@crop <‐ crop
obj@suitability <‐ movingFun(minv, n=duration, fun=min, type='from', circular=TRUE)
obj@maxsuit <‐ max(obj@suitability)
if (obj@maxsuit > 0) {
obj@maxper <‐ which(obj@suitability==max(obj@suitability))
} else {
obj@maxper <‐ 0
}
return(obj)
}
ecocrop <‐ function(crop, tmin, tavg, prec, rainfed=TRUE, ...) {
if (class(crop) == 'character') {
crop <‐ getCrop(crop)
}
if (missing(prec) & rainfed) {
stop('prec missing while rainfed=TRUE' )
}
if (inherits(tmin, 'Raster')) {
if (nlayers(tmin) != 12) {
stop()
}
.ecoSpat(crop, tmin, tavg, prec, rainfed)
} else {
.doEcocrop(crop=crop, tmin=tmin, tavg=tavg, prec=prec, rainfed=rainfed, ...)
}
}
I was just wondering whether there was a simple method for extracting for example 'allv' and 'minv' from the function, or whether I'd need to redefine them as objects and re-write the function? I've tried this repeatedly, defining both allv and minv as objects, but come up against an error, which I think I've traced back to issues of the environment and/or namespace.
I'm certain there must be a simpler way of doing this, if anyone has any ideas, or Robert if you're out there and could shine a light on which way to go I'd very much appreciate it.
Thanks very much and apologies if I've not not been sufficiently specific.