Last active
December 14, 2015 18:00
-
-
Save DomBennett/161a294d61bc1744275e to your computer and use it in GitHub Desktop.
Splitting Lat and Lon observations into grid cells
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# R script for splitting grid space | |
# Variables: | |
# obs_lat = vector of observed lats | |
# obs_lon = vector of observed lons | |
# lats = vector of lists for the max and min values of each grid in lat-space | |
# lons = vector of lists for the max and min values of each grid in lon-space | |
# gird = matrix of indexes pointing to the lists in =lats and lons | |
# res = vector of indexes referring to indexes in grid | |
# Steps: | |
# 1. Create lists of the min and max values for each lon and lat (calls them lats and lons) | |
# 2. construct a grid using the expand.grid function that will find all possible combinations | |
# for the lats and lons | |
# 3. Loop through each element in the grid and determine which observations are in which grid square | |
# 4. Produce a vector of each grid element index for each observation [RESULT] | |
# FUNCTIONS | |
genLonsandLats <- function(max_lat, min_lat, max_lon, min_lon, gsize=0.5) { | |
# create list of list of list of grid cells | |
# LATS! | |
lats_values <- seq(from=min_lat, to=max_lat, by=gsize) | |
lats_list <- list() | |
for(i in 2:length(lats_values)) { | |
lats_list[[i-1]] <- list(min=lats_values[i-1], max=lats_values[i]) | |
} | |
# LONS! | |
lons_values <- seq(from=min_lon, to=max_lon, by=gsize) | |
lons_list <- list() | |
for(i in 2:length(lons_values)) { | |
lons_list[[i-1]] <- list(min=lons_values[i-1], max=lons_values[i]) | |
} | |
latslons <- list("lats"=lats_list, | |
"lons"=lons_list) | |
return(latslons) | |
} | |
# INPUT | |
# This is fake data for testing | |
obs_lons <- runif(min=0, max=1, 100) | |
obs_lats <- runif(min=0, max=1, 100) | |
# TODO Read in real data, make sure the two vectors are "paired", same length and identities | |
# CREATE GRID-SPACE | |
latslons <- genLonsandLats() | |
lats <- latslons[["lats"]] # unpack | |
lons <- latslons[["lons"]] | |
grid <- expand.grid(1:length(lats),1:length(lons)) | |
colnames(grid) <- c("lat", "lon") | |
# DESIGNATE | |
res <- rep(NA, length(obs_lons)) | |
for (i in 1:nrow(grid)) { | |
lon <- lons[[grid[i, "lon"]]] | |
lat <- lats[[grid[i, "lat"]]] | |
# cat("Corrds lon:\n") | |
# cat("... min:", coords_lon$min, "\n") | |
# cat("... max:", coords_lon$max, "\n") | |
# cat("Corrds lat:\n") | |
# cat("... min:", coords_lat$min, "\n") | |
# cat("... max:", coords_lat$max, "\n") | |
res_bool <- obs_lons >= lon$min & obs_lons < lon$max & | |
obs_lats >= lat$min & obs_lats < lat$max | |
res_is <- which(res_bool) | |
res[res_is] <- i | |
} | |
# OUTPUT | |
# TODO | |
cat("Script complete!!! Have a nice day :-)\n") | |
# DEFUNCT | |
# constructObs <- function(lat, lat) { | |
# # combine two separate vectors into a single list of $lon and $lat | |
# obs <- list() | |
# for(i in 1:length(lon)) { | |
# obs[[i]] <- list(lon=lon[i], lat=lat[i]) | |
# } | |
# obs | |
# } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
species <- c ("Sp1", "Sp1","Sp1","Sp2","Sp1","Sp2","Sp3","Sp1","Sp1","Sp1") | |
obs_vals <- runif(min=0, max=1, n=length(species)) | |
table(species) | |
tapply(obs_vals, species, mean, na.rm=TRUE) # remember to use rm.na if there are any missing values | |
tapply(obs_vals, species, sd, na.rm=TRUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment