jeffreyevans / spatialEco

R package for spatial analysis and modelling of ecological systems
GNU General Public License v3.0
108 stars 29 forks source link

spatialEco time-series data #5

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hi Jeffrey,

Currently, I am working on time-series data. I have created raster stack with LST as dependent variable (each band corresponds to acquisition date) and raster stack with 2 explanatory variables (NDVI and DEM saved as raster stack in list) for every date as well. Now, I am trying to use loop to downscale my time-series images.

However, after iteration of 75 layers (I have 225 dates) I get the error: ''Error in seq_len(p) : argument must be coercible to non-negative integer''. The problem is strange because when I try to downscale the image (as 76th layer) separately I don't have problem with downscaling.

I would be grateful for help.

Best, Paulina

jeffreyevans commented 6 years ago

Without the code that you are using for the analysis, I can only speculate on the issue. Honestly, it is looking like an issue in how you are setting up you iterator. The clue is in the “seq_len(p) “ error. This is not a function call within the model so, it is occurring outside of the function, leaving the for statment.

Best, Jeff

Jeffrey S. Evans, Ph.D., | Senior Landscape Ecologist

The Nature Conservancy | Global Lands Science Team

Visiting Professor | University of Wyoming | Zoology & Physiology

Laramie, WY | jeffrey_evans@tnc.orgmailto:jeffrey_evans@tnc.org | (970) 672-6766<tel:(970)%20672-6766>

From: aniluap5 [mailto:notifications@github.com] Sent: Wednesday, April 18, 2018 8:38 AM To: jeffreyevans/spatialEco spatialEco@noreply.github.com Cc: Subscribed subscribed@noreply.github.com Subject: [jeffreyevans/spatialEco] spatialEco time-series data (#5)

Hi Jeffrey,

Currently, I am working on time-series data. I have created raster stack with LST as dependent variable (each band corresponds to acquisition date) and raster stack with 2 explanatory variables (NDVI and DEM saved as raster stack in list) for every date as well. Now, I am trying to use loop to downscale my time-series images.

However, after iteration of 75 layers (I have 225 dates) I get the error: ''Error in seq_len(p) : argument must be coercible to non-negative integer''. The problem is strange because when I try to downscale the image (as 76th layer) separately I don't have problem with downscaling.

I would be grateful for help.

Best, Paulina

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/jeffreyevans/spatialEco/issues/5, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJaj_wFffO9n9XEshytYhFzcfAS1KkWWks5tp0_agaJpZM4TaMQy.

ghost commented 6 years ago

As an attachment I am sending my script. In every approach I am using loop to get raster stacks in list. Thank you very much for help.

Best, Paulina SpatialEco.zip

jeffreyevans commented 6 years ago

Wow, that is some code. This is off topic but, I will say out the gate, there is no expected relationship between NDVI and land surface temperature. I have never seen a hypothesized, or quantified, relationship between temperature and photosynthetically active radiation (PAR), as measured by NDVI. You add elevation into the mix and you really confound thing. In effect, low elevation grasslands could exhibit the same NDVI as high elevation meadows or forest thus, creating a hyperbolic relationship with temperature. There are only a few biophysical settings that I can think of that this relationship would hold but, the statistical significance would be nonexistent and the relationship would be highly localized and non-extrapolatable.

At the initiation of your code, It would be considerably more efficient to store a vector of dates in an R object and load it rather than the gymnastics you are doing with an external file. I often store my data as separate rasters, with the date in the naming convention. Then I can create a date vector on-the-fly by manipulating the name string. This also has the advantage of reading the stack in a date ordered structure. You can also use the data index to reorder the stack. Since you have a date vector, you do not need to rename the rasters in the stack, just operate on the date vector and then use the resulting index on the raster stack using a double bracket.

###################################

Create formatted data class object from raster names (f represents example raster names)

( f=c("NDVI_20140110.tif","NDVI_20140120.tif","NDVI_20140131.tif","NDVI_20140210.tif") ) ( ndvidates <- unlist(lapply(strsplit(f, "[|.+]+"), function(x) x[2])) ) ( ndvi_dates <- as.Date(paste(substr(ndvi_dates, 1,4), substr(ndvi_dates, 5,6), substr(ndvi_dates, 7,8),sep="-"), "%Y-%m-%d") ) ###################################

I can see a few issues with your for loop. First, never use existing R functions or internals as variables. In this case “t” is an R internal for transposing data. Using this as a variable indexing data could produce some quite unexpected behavior. Also, you do not have to create a new object, representing the index of an existing object. You can pass the object index directly to a function. The big unknown, without hands on your data, is what is going on with the DEM. Is it a single large raster covering several scenes, is it different extents representing multiple scenes, is it just a replicated raster? If it is the same raster replicated 225 times so that you can having a preexisting stack with ndvi, there is absolutely no reason to do this. I would imagine that the reason your for loop is failing is due to a mismatch in object dimensions. If the downscaled rasters in the for loop represent the same extent through time, there is really no reason to create a list object, just make a copy of the stack and pipe in the results using a double bracket or using raster::addLayer. Since you are doing this in a for loop, leaving scatter = FALSE will speed things up. You can call stack on the fly so, you do not need to create the “Terrastack_NDVI_DEM_250m” object, just call the respective rasters. I imagine that this is what your for loop should look like.

###################################

Check to see if data dimensions match

if(length(Terra_LST_final_resampled) != length(Terra_NDVI_names)) stop("The data dimensions do not match")

lst.downscaled_timeseries <- stack(dem250) for(i in 1:nlayers(Terra_LST_final_resampled)){ if( nlayers(Terra_LST_final_resampled) != lst.downscaled_timeseries <- addLayer(lst.downscaled_timeseries, raster.downscale( stack(dem250, Terra_NDVI_names[[i]]) , Terra_LST_final_resampled[[i]])) } lst.downscaled_timeseries <- [[-1]] # remove first layer used to initiate stack ###################################

With a little bit of attention you could notably streamline this code. As it is, there are numerous unnecessary operations and objects being created. It is very difficult to track exactly what is going on here and, as written, this analysis would not be very repeatable. This is about all I can tell you without seeing how your data is stored on disk and what the structure of your analysis is.

Best, Jeff

Jeffrey S. Evans, Ph.D., | Senior Landscape Ecologist

The Nature Conservancy | Global Lands Science Team

Visiting Professor | University of Wyoming | Zoology & Physiology

Laramie, WY | jeffrey_evans@tnc.orgmailto:jeffrey_evans@tnc.org | (970) 672-6766<tel:(970)%20672-6766>

From: aniluap5 [mailto:notifications@github.com] Sent: Wednesday, April 18, 2018 9:57 AM To: jeffreyevans/spatialEco spatialEco@noreply.github.com Cc: Jeffrey Evans jeffrey_evans@TNC.ORG; Comment comment@noreply.github.com Subject: Re: [jeffreyevans/spatialEco] spatialEco time-series data (#5)

As an attachment I am sending my script. In every approach I am using loop to get raster stacks in list. Thank you very much for help.

Best, Paulina SpatialEco.ziphttps://github.com/jeffreyevans/spatialEco/files/1924797/SpatialEco.zip

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/jeffreyevans/spatialEco/issues/5#issuecomment-382437542, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJaj_6HNwmm1WpcKZz2PM2cfmfjLrp8nks5tp2I5gaJpZM4TaMQy.