-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_tc.R~
76 lines (62 loc) · 2.5 KB
/
extract_tc.R~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# This script extracts delta tc metrics of points shp from the raster to the csv
rm(list=ls())
# load raster packages
library(rgdal)
library(raster)
# function to abbrevaite paste
"%+%" <- function(x,y) paste(x,y,sep="")
#tile_name = "Bh12v10"
args <- commandArgs(trailingOnly=TRUE)
tile_name <- args[1]
out_dir = "/projectnb/landsat/users/shijuan/above/training_data/"%+%tile_name%+%"/"
tc_dir = "/projectnb/landsat/projects/ABOVE/CCDC/"%+%tile_name%+%"/out_tc_4type/"
shp_loc = "/projectnb/landsat/users/shijuan/above/training_data/"%+%tile_name%+%"/"%+%tile_name%+%"_pts.shp" # can also be a directory
shp_name = tile_name%+%"_pts" # no .shp
#out_dir = "/projectnb/landsat/users/shijuan/above/bh09v15/rand_forest_v3/"
#tc_dir = "/projectnb/landsat/projects/ABOVE/CCDC/Bh09v15/out_tc_pre/"
#shp_loc = "/projectnb/landsat/users/shijuan/above/bh09v15/rand_forest_v3/training_sample.shp" # can also be a directory
#shp_name = "training_sample"
#tile_name = 'Bh09v15'
# read the shapefile
pts_df = readOGR(shp_loc, shp_name)
#print(pts_df)
# set an array to save the values
npix = length(pts_df[1])
print(npix)
n_mets = 6 # delta b,g,w pre-d,g,w, and agent
out_tab = array(NA,dim=c(npix,n_mets*29+1))
col_names = array(NA, dim=c(n_mets*29+1))
out_tab[,c(1:1)] = c(pts_df[['shp_id']]) # if polygon, cannot do it this way
for(year in 1986:2013){
tc_year = tc_dir%+%tile_name%+%"_dTC_NN_"%+%toString(year)%+%".tif"
print(tc_year)
n = year - 1985
# extract delta brightness
col_names[6*n-4] = "db_"%+%toString(year)
db_ras = raster(tc_year,band=1)
out_tab[,c(6*n-4)] <- extract(db_ras,pts_df)
# extract delta greenness
col_names[6*n-3] = "dg_"%+%toString(year)
dg_ras = raster(tc_year,band=2)
out_tab[,c(6*n-3)] <- extract(dg_ras,pts_df)
# extract delta wetness
col_names[6*n-2] = "dw_"%+%toString(year)
dw_ras = raster(tc_year,band=3)
out_tab[,c(6*n-2)] <- extract(dw_ras,pts_df)
# extract pre- brightness
col_names[6*n-1] = "pb_"%+%toString(year)
db_ras = raster(tc_year,band=4)
out_tab[,c(6*n-1)] <- extract(db_ras,pts_df)
# extract pre- greenness
col_names[6*n] = "pg_"%+%toString(year)
dg_ras = raster(tc_year,band=5)
out_tab[,c(6*n)] <- extract(dg_ras,pts_df)
# extract pre wetness
col_names[6*n+1] = "pw_"%+%toString(year)
dw_ras = raster(tc_year,band=6)
out_tab[,c(6*n+1)] <- extract(dw_ras,pts_df)
}
col_names[1] = "shp_id"
colnames(out_tab) = col_names
out_file = out_dir%+%tile_name%+%"_tc.csv"
write.table(out_tab, file=out_file,sep=",",col.names=T,row.names=F, quote=F)