-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get_Vegetation.r
57 lines (49 loc) · 1.57 KB
/
Get_Vegetation.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
rm(list=ls())
library(MASS)
library(ncdf4)
library(fields)
library(maps)
library(hydroGOF)
#library(plotrix)
library(rworldmap)
library(RColorBrewer)
library(humidity)
esmap.dir = "/Volumes/LabShare/SMAP/Gridded_ESMAP/"
veg.dir = "/Volumes/LabShare/SMAP/ESMAP_for_Ronnie/"
#Open file to get dimensions
tmp.file = paste(veg.dir,"AVHRR_8km_LANDCOVER_1981_1994.GLOBAL.nc", sep='')
p.nc = nc_open(tmp.file)
veg.lon = ncvar_get(p.nc,'lon')
veg.lat = ncvar_get(p.nc,'lat')
veg.data = ncvar_get(p.nc,'Band1')
nc_close(p.nc)
veg.lon = veg.lon + 360
smap.lat = list.files(esmap.dir)
smap.lat = as.numeric(smap.lat)
#remove NAs: (other files in directory that are not coordinates)
idx<-which(is.na(smap.lat))
if (length(idx)>0) {
smap.lat<-smap.lat[-idx]
}
nlat = length(smap.lat)
for(i in 1:nlat){
smap.lon = list.files(paste(esmap.dir,smap.lat[i],"/",sep=''))
lat.loc = which.min(abs(veg.lat - smap.lat[i]))
nlon = length(smap.lon)
smap.lon = as.numeric(smap.lon)
for(j in 1:nlon){
lon.loc = which.min(abs(veg.lon - smap.lon[j]))
tmp.veg = veg.data[lon.loc,lat.loc]
if(tmp.veg == 0 | tmp.veg == 13){
tmp.data = veg.data[,lat.loc]
tmp.data[which(tmp.data == 0)] = NA
tmp.data[which(tmp.data == 13)] = NA
lon.loc = which(is.na(tmp.data) == FALSE)[which.min(abs(lon.loc - which(is.na(tmp.data) == FALSE)))]
tmp.veg = veg.data[lon.loc,lat.loc]
}
new.file = paste(esmap.dir,smap.lat[i],"/",smap.lon[j],"/Vegetation_raw",sep='')
write.table(tmp.veg,new.file,sep="\t", col.names = F, row.names = F)
to.screen = c(smap.lat[i], smap.lon[j], tmp.veg)
print(to.screen)
}
}