-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get_SMAP_QC.r
81 lines (66 loc) · 1.92 KB
/
Get_SMAP_QC.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
75
76
77
78
79
80
rm(list=ls())
library(MASS)
library(ncdf4)
#library(fields)
#library(maps)
#library(hydroGOF)
#library(plotrix)
#library(rworldmap)
#library(RColorBrewer)
#library(humidity)
smap.dir = "/scratch/summit/roab9675/SMAP/QC/"
smap.datelist = seq.Date(as.Date("2015/04/01"),as.Date("2019/3/31"),"days")
months = as.numeric(format(smap.datelist,'%m'))
years = as.numeric(format(smap.datelist,'%Y'))
days = as.numeric(format(smap.datelist,'%d'))
ndays = length(days)
print(ndays)
tmp.file = paste(smap.dir,"SMAP.2015.04.01.nc",sep='')
p.nc = nc_open(tmp.file)
lon = ncvar_get(p.nc,'lon')
lat = ncvar_get(p.nc,'lat')
nc_close(p.nc)
nlon = length(lon)
nlat = length(lat)
sm.data = array(NA, c(nlon, nlat, ndays))
for(i in 1:ndays){
tmp.file = paste(smap.dir,"SMAP.",years[i],".",sprintf("%02d", months[i]),".",sprintf("%02d", days[i]),".nc",sep='')
#only perform if the tmp.file existis:
if (file.exists(tmp.file)){
p.nc = nc_open(tmp.file)
tmp.data = ncvar_get(p.nc,'SMAP')
nc_close(p.nc)
sm.data[,,i] = tmp.data
print(tmp.file)
}
}
south = 25
north = 50
east = -60
west = -130
#nldas 25 to 53N, -67 to -125W
south.loc = which.min(abs(lat - south))
north.loc = which.min(abs(lat - north))
east.loc = which.min(abs(lon - east))
west.loc = which.min(abs(lon - west))
sm.data = sm.data[west.loc:east.loc,south.loc:north.loc,]
lon = lon[west.loc:east.loc] + 360
lat = lat[south.loc:north.loc]
nlon = length(lon)
nlat = length(lat)
esmap.dir = "/scratch/summit/roab9675/SMAP/Gridded_ESMAP/"
for(i in 1:nlat){
for(j in 1:nlon){
tmp.sm = sm.data[j,i,]
nvalid = length(which(is.na(tmp.sm)==FALSE))
if(nvalid >= 5){
print(lat[i])
print(lon[j])
new.dir = paste(esmap.dir,round(lat[i],5),"/",round(lon[j],5),"/",sep='')
dir.create(new.dir,recursive=TRUE)
new.file = paste(new.dir,"SMAP_raw_QC",sep='')
to.write = cbind(years, months, days, tmp.sm)
write.table(to.write,new.file,sep="\t", col.names = F, row.names = F)
}
}
}