Skip to content

Commit 039055e

Browse files
committed
added ability to process new collection 1 sr imagery
1 parent 868a4f3 commit 039055e

2 files changed

Lines changed: 138 additions & 57 deletions

File tree

R/oliunpackr.r

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,61 @@ oliunpackr = function(file, proj="default", overwrite=F){
2121
#set new directories
2222
randomstring = paste(sample(c(0:9, letters, LETTERS), 6, replace=TRUE),collapse="")
2323
tempdir = file.path(dirname(file),randomstring) #temp
24-
year = substr(basename(file),10,13)
2524

26-
pieces = unlist(strsplit(dirname(file), "/")) #break up the directory and unlist so the pieces can be called by index
27-
len = length(pieces)-1 #get the ending index for "scene"
28-
newpieces = paste(pieces[1:len], collapse = "/") #subset the directory pieces so the last piece is the scene
29-
outdir = file.path(newpieces, "images", year)
30-
dir.create(tempdir, recursive=T, showWarnings=F)
31-
dir.create(outdir, recursive=T, showWarnings=F)
25+
targzbname = basename(file)
26+
27+
if(nchar(targzbname) == 46){
28+
year = substr(targzbname,11,14)
29+
30+
pieces = unlist(strsplit(dirname(file), "/")) #break up the directory and unlist so the pieces can be called by index
31+
len = length(pieces)-1 #get the ending index for "scene"
32+
newpieces = paste(pieces[1:len], collapse = "/") #subset the directory pieces so the last piece is the scene
33+
outdir = file.path(newpieces, "images", year)
34+
dir.create(tempdir, recursive=T, showWarnings=F)
35+
dir.create(outdir, recursive=T, showWarnings=F)
36+
37+
#decompress the image and get/set files names
38+
untar(file, exdir=tempdir, tar="internal") #decompress the file
39+
files = list.files(tempdir, full.names=T)
40+
bands = sort(grep("sr_band", files, value=T))
41+
42+
# create the mask
43+
pixelqafile = grep("pixel_qa.tif", files, value=T)
44+
pixelqar = getValues(raster(pixelqafile))
45+
mask = as.numeric(pixelqar == 322 | pixelqar == 386 | pixelqar == 324 | pixelqar == 388 | pixelqar == 836 | pixelqar == 900)
46+
pixelqar = 0 #clear memory
47+
48+
# make the basename for final output files
49+
mtlfile = grep("MTL.txt", files, value=T)
50+
tbl = unlist(read.delim(mtlfile, header=F, skipNul=T))
51+
string = as.character(grep("LANDSAT_SCENE_ID = ", tbl, value=T))
52+
pieces = unlist(strsplit(string, " "))
53+
sceneid = pieces[length(pieces)]
54+
outbase = substr(sceneid,1,16)
55+
56+
} else{
57+
outbase = substr(targzbname,1,16)
58+
year = substr(targzbname,10,13)
59+
60+
pieces = unlist(strsplit(dirname(file), "/")) #break up the directory and unlist so the pieces can be called by index
61+
len = length(pieces)-1 #get the ending index for "scene"
62+
newpieces = paste(pieces[1:len], collapse = "/") #subset the directory pieces so the last piece is the scene
63+
outdir = file.path(newpieces, "images", year)
64+
dir.create(tempdir, recursive=T, showWarnings=F)
65+
dir.create(outdir, recursive=T, showWarnings=F)
66+
67+
# decompress the image and get/set files names
68+
untar(file, exdir=tempdir, tar="internal") #decompress the file
69+
files = list.files(tempdir, full.names=T)
70+
bands = sort(grep("band", files, value=T))
71+
72+
# create the mask
73+
fmask = grep("cfmask.tif", files, value=T) # <= 1 okay background 255
74+
mask = as.matrix(raster(fmask))
75+
mask = mask <= 1
76+
mask = mask*1 #convert from logial to numeric
77+
}
3278

33-
#decompress the image and get/set files names
34-
untar(file, exdir=tempdir, tar="internal") #decompress the file
35-
files = list.files(tempdir, full.names=T)
36-
bands = grep("band", files, value=T)
37-
fmask = grep("cfmask.tif", files, value=T) # <= 1 okay background 255
38-
outbase = substr(basename(file),1,16)
3979
tempstack = file.path(tempdir,paste(outbase,"_tempstack.tif",sep=""))
4080
tempvrt = sub("tempstack.tif", "tempstack.vrt", tempstack)
4181
tempmask = sub("tempstack", "tempmask", tempstack)
@@ -52,14 +92,10 @@ oliunpackr = function(file, proj="default", overwrite=F){
5292
gdalbuildvrt(gdalfile=bands, output.vrt = tempvrt, separate=T) #, tr=c(reso,reso)
5393
gdal_translate(src_dataset=tempvrt, dst_dataset=tempstack, of = "GTiff", co="INTERLEAVE=BAND")
5494

55-
#create the mask
56-
mask = as.matrix(raster(fmask))
57-
mask = mask <= 1
58-
mask = mask*1 #convert from logial to numeric
5995
mask = setValues(ref,mask)
6096
mask = as(mask, "SpatialGridDataFrame") #convert the raster to SGHF so it can be written using GDAL (faster than writing it with the raster package)
6197
writeGDAL(mask, tempmask, drivername = "GTiff", type = "Byte", mvFlag = 255)
62-
98+
mask = 0 # clear memory
6399

64100
#reproject the image #need to add in writing proj file for default
65101
if(proj == "default"){proj = origproj}

R/tmunpackr.r

Lines changed: 84 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,85 @@ tmunpackr = function(file, proj="default", overwrite=F){
1919
#set new directories
2020
randomstring = paste(sample(c(0:9, letters, LETTERS), 6, replace=TRUE),collapse="")
2121
tempdir = file.path(dirname(file),randomstring) #temp
22-
year = substr(basename(file),10,13)
23-
24-
pieces = unlist(strsplit(dirname(file), "/")) #break up the directory and unlist so the pieces can be called by index
25-
len = length(pieces)-1 #get the ending index for "scene"
26-
newpieces = paste(pieces[1:len], collapse = "/") #subset the directory pieces so the last piece is the scene
27-
outdir = file.path(newpieces, "images", year)
28-
dir.create(tempdir, recursive=T, showWarnings=F)
29-
dir.create(outdir, recursive=T, showWarnings=F)
30-
31-
#decompress the image and get/set files names
32-
untar(file, exdir=tempdir, tar="internal") #decompress the file
33-
files = list.files(tempdir, full.names=T)
34-
bands = grep("band", files, value=T)
35-
shadow = grep("cloud_shadow_qa.tif", files, value=T) #0 okay, 255 bad
36-
cloud = grep("sr_cloud_qa.tif", files, value=T) #0 okay, 255 bad
37-
snow = grep("sr_snow_qa.tif", files, value=T) #0 okay, 255 bad
38-
fmask = grep("cfmask.tif", files, value=T) # <= 1 okay background 255
39-
outbase = substr(basename(file),1,16)
22+
23+
targzbname = basename(file)
24+
25+
if(nchar(targzbname) == 46){
26+
year = substr(targzbname,11,14)
27+
28+
pieces = unlist(strsplit(dirname(file), "/")) #break up the directory and unlist so the pieces can be called by index
29+
len = length(pieces)-1 #get the ending index for "scene"
30+
newpieces = paste(pieces[1:len], collapse = "/") #subset the directory pieces so the last piece is the scene
31+
outdir = file.path(newpieces, "images", year)
32+
dir.create(tempdir, recursive=T, showWarnings=F)
33+
dir.create(outdir, recursive=T, showWarnings=F)
34+
35+
#decompress the image and get/set files names
36+
untar(file, exdir=tempdir, tar="internal") #decompress the file
37+
files = list.files(tempdir, full.names=T)
38+
bands = sort(grep("sr_band", files, value=T))
39+
40+
pixelqafile = grep("pixel_qa.tif", files, value=T)
41+
pixelqar = getValues(raster(pixelqafile))
42+
pixelqar = pixelqar == 66 | pixelqar == 130 | pixelqar == 68 | pixelqar == 132
43+
44+
srcloudqafile = grep("sr_cloud_qa.tif", files, value=T)
45+
srcloudqar = getValues(raster(srcloudqafile))
46+
srcloudqar = srcloudqar == 0 | srcloudqar == 1 | srcloudqar == 32
47+
48+
mask = pixelqar*srcloudqar
49+
pixelqar = srcloudqar = 0 #clear memory
50+
51+
# make the basename for final output files
52+
mtlfile = grep("MTL.txt", files, value=T)
53+
tbl = unlist(read.delim(mtlfile, header=F, skipNul=T))
54+
string = as.character(grep("LANDSAT_SCENE_ID = ", tbl, value=T))
55+
pieces = unlist(strsplit(string, " "))
56+
sceneid = pieces[length(pieces)]
57+
outbase = substr(sceneid,1,16)
58+
59+
} else{
60+
outbase = substr(targzbname,1,16)
61+
year = substr(targzbname,10,13)
62+
63+
pieces = unlist(strsplit(dirname(file), "/")) #break up the directory and unlist so the pieces can be called by index
64+
len = length(pieces)-1 #get the ending index for "scene"
65+
newpieces = paste(pieces[1:len], collapse = "/") #subset the directory pieces so the last piece is the scene
66+
outdir = file.path(newpieces, "images", year)
67+
dir.create(tempdir, recursive=T, showWarnings=F)
68+
dir.create(outdir, recursive=T, showWarnings=F)
69+
70+
#decompress the image and get/set files names
71+
untar(file, exdir=tempdir, tar="internal") #decompress the file
72+
files = list.files(tempdir, full.names=T)
73+
bands = sort(grep("band", files, value=T))
74+
shadow = grep("cloud_shadow_qa.tif", files, value=T) #0 okay, 255 bad
75+
cloud = grep("sr_cloud_qa.tif", files, value=T) #0 okay, 255 bad
76+
snow = grep("sr_snow_qa.tif", files, value=T) #0 okay, 255 bad
77+
fmask = grep("cfmask.tif", files, value=T) # <= 1 okay background 255
78+
79+
#make a composite cloudmask
80+
s = as.matrix(raster(shadow))
81+
c = as.matrix(raster(cloud))
82+
sn = as.matrix(raster(snow))
83+
f = as.matrix(raster(fmask))
84+
85+
check = s[1,1] # if is.na(check) == T new else old
86+
if(is.na(check) == T){s = is.na(s)} else {s = !is.na(s)}
87+
check = c[1,1] # if is.na(check) == T new else old
88+
if(is.na(check) == T){c = is.na(c)} else {c = !is.na(c)}
89+
check = sn[1,1] # if is.na(check) == T new else old
90+
if(is.na(check) == T){sn = is.na(sn)} else {sn = !is.na(sn)}
91+
f = f <= 1
92+
mask = s*c*f*sn
93+
s=c=sn=f=0
94+
}
95+
96+
#mask = setValues(ref,mask)
97+
#plot(mask)
98+
#writeRaster(mask, "D:/work/proj/llr_dev/collection1/tm/wrs2/032033/test.tif")
99+
100+
# create outfile paths
40101
tempstack = file.path(tempdir,paste(outbase,"_tempstack.tif",sep=""))
41102
tempvrt = sub("tempstack.tif", "tempstack.vrt", tempstack)
42103
tempmask = sub("tempstack", "tempmask", tempstack)
@@ -48,32 +109,20 @@ tmunpackr = function(file, proj="default", overwrite=F){
48109
tcafile = file.path(outdir,paste(outbase,"_tca.tif", sep=""))
49110
outprojfile = file.path(outdir,paste(outbase,"_proj.txt", sep=""))
50111

51-
ref = raster(bands[1]) #set a reference raster for setting values and getting projection
112+
# set a reference raster for setting values and getting projection
113+
ref = raster(bands[1])
52114
origproj = projection(ref)
53115

54116
#stack the image bands and write out
55117
gdalbuildvrt(gdalfile=bands, output.vrt = tempvrt, separate=T) #, tr=c(reso,reso)
56118
gdal_translate(src_dataset=tempvrt, dst_dataset=tempstack, of = "GTiff", co="INTERLEAVE=BAND")
57119

58-
#make a composite cloudmask
59-
s = as.matrix(raster(shadow))
60-
c = as.matrix(raster(cloud))
61-
sn = as.matrix(raster(snow))
62-
f = as.matrix(raster(fmask))
63-
64-
check = s[1,1] # if is.na(check) == T new else old
65-
if(is.na(check) == T){s = is.na(s)} else {s = !is.na(s)}
66-
check = c[1,1] # if is.na(check) == T new else old
67-
if(is.na(check) == T){c = is.na(c)} else {c = !is.na(c)}
68-
check = sn[1,1] # if is.na(check) == T new else old
69-
if(is.na(check) == T){sn = is.na(sn)} else {sn = !is.na(sn)}
70-
f = f <= 1
71-
mask = s*c*f*sn
120+
# write the mask out
72121
mask = setValues(ref,mask)
73122
mask = as(mask, "SpatialGridDataFrame") #convert the raster to SGHF so it can be written using GDAL (faster than writing it with the raster package)
74123
writeGDAL(mask, tempmask, drivername = "GTiff", type = "Byte", mvFlag = 255, options="INTERLEAVE=BAND")
75-
76-
s=c=sn=f=mask=0 #clear the memory
124+
125+
mask=0 #clear the memory
77126

78127
#reproject the image #need to add in writing proj file for default
79128
if(proj == "default"){proj = origproj}
@@ -89,13 +138,10 @@ tmunpackr = function(file, proj="default", overwrite=F){
89138
r="mode", srcnodata=255, dstnodata=255, multi=T,
90139
tr=c(30,30), co="INTERLEAVE=BAND")
91140

92-
93-
94141
#trim the na rows and cols
95142
trim_na_rowcol(projstack, finalstack, projmask, finalmask)
96143

97144
#tasseled cap
98-
ref = raster(finalstack, 1)
99145
b1 = as.matrix(raster(finalstack, 1))
100146
b2 = as.matrix(raster(finalstack, 2))
101147
b3 = as.matrix(raster(finalstack, 3))
@@ -107,7 +153,6 @@ tmunpackr = function(file, proj="default", overwrite=F){
107153
gcoef = c(-0.1603, -0.2819, -0.4934, 0.7940, -0.0002, -0.1446)
108154
wcoef = c(0.0315, 0.2021, 0.3102, 0.1594,-0.6806, -0.6109)
109155

110-
111156
bright = (b1*bcoef[1])+(b2*bcoef[2])+(b3*bcoef[3])+(b4*bcoef[4])+(b5*bcoef[5])+(b6*bcoef[6])
112157
green = (b1*gcoef[1])+(b2*gcoef[2])+(b3*gcoef[3])+(b4*gcoef[4])+(b5*gcoef[5])+(b6*gcoef[6])
113158
wet = (b1*wcoef[1])+(b2*wcoef[2])+(b3*wcoef[3])+(b4*wcoef[4])+(b5*wcoef[5])+(b6*wcoef[6])

0 commit comments

Comments
 (0)