У меня есть данные Landsat за 31 год в 6 файлах NetCDF.
Каждый файл содержит около 4 миллионов строк данных.
Каждый файл имеет пока неизвестное количество временных шагов в каждом
За исключением первого файла, который я использую для написания своего скрипта, который имеет 60 временных шагов и будет использоваться для обработки всех файлов.
Я никогда не использовал R до этого крошечного упражнения.
Моя задача - создать один набор данных только для земли, чтобы статистик мог работать с ним.
Как я могу просмотреть циклы, вычисляя ndwi?
У меня работает этот код:
Но для выполнения 60 временных шагов требуется много операций поиска / замены, копирования / вставки, а затем нужно обработать еще 5 файлов.
###############################################################
# Calculate ndwi for for the each timestep
###############################################################
#convert the timestep_1 for green to numeric value
green_nir_df02$t_1 <- as.numeric(as.character(green_nir_df02$t_1))
head (na.omit(green_nir_df02$t_1, 20))
#convert the timestep_1 for nir to numeric value
green_nir_df02$t_1.1 <- as.numeric(as.character(green_nir_df02$t_1.1))
head (na.omit(green_nir_df02$t_1.1, 20))
# calculate green minus nir for timestep_1
grnMinusNir_t_1 <- green_nir_df02$t_1 - green_nir_df02$t_1.1
head(grnMinusNir_t_1, 20)
# calculate green plus nir for timestep_1
grnPlusNir_t_1 <- green_nir_df02$t_1 + green_nir_df02$t_1.1
head(grnPlusNir_t_1, 20)
# calculate ndwi from greenMinusNir divided by greenPlusNir for timestep_1
ndwi_t_1 <- grnMinusNir_t_1 / grnPlusNir_t_1
head(ndwi_t_1, 20)
# write ndwi to the green_nir_df02 dataframe for timestep_1
green_nir_df02$ndwi_t_1 <- ndwi_t_1
####################################################
#convert the timestep_2 for green to numeric value
green_nir_df02$t_2 <- as.numeric(as.character(green_nir_df02$t_2))
head (na.omit(green_nir_df02$t_2, 20))
#convert the timestep_2 for nir to numeric value
green_nir_df02$t_2.1 <- as.numeric(as.character(green_nir_df02$t_2.1))
head (na.omit(green_nir_df02$t_2.1, 20))
# calculate green minus nir for timestep_2
grnMinusNir_t_2 <- green_nir_df02$t_2 - green_nir_df02$t_2.1
head(grnMinusNir_t_2, 20)
# calculate green plus nir for timestep_2
grnPlusNir_t_2 <- green_nir_df02$t_2 + green_nir_df02$t_2.1
head(grnPlusNir_t_2, 20)
# calculate ndwi from greenMinusNir divided by greenPlusNir for timestep_2
ndwi_t_2 <- grnMinusNir_t_2 / grnPlusNir_t_2
head(ndwi_t_2, 20)
# write ndwi to the green_nir_df02 dataframe for timestep_2
green_nir_df02$ndwi_t_2 <- ndwi_t_2
... и т. Д. До t_60
###############################################################
# END: Calculate ndwi for for the each timestep
###############################################################
и следующий файл начинается в t_61 и так далее
Я попытался использовать этот цикл, который не работает, потому что я не могу указать значение в столбце t_1 в кадре данных, а не значение в массиве имен столбцов.
# initialize the "for" loop to calculate ndwi columns
seq <- 1:nt
i <- 0
green_t <- array(1:nt)
nir_t <- array(1:nt)
ndwi_t <- array(1:nt)
greenMinusNir <- array(1:nt)
greenPlusNir <- array(1:nt)
ndwi <- array(1:nt)
# set the value of the count
# count = nt from previous file #nt = num timesteps#THIS IS THE VALUE TO USE
count <- 0 ################################## EDIT THIS VALUE BEFORE RUNNING
# START: Loop for each timestep
# Step 1: create variable names for green, nir, ndwi
# Step 2: convert t_[i] and t_[i].1 to numeric
# Step 3: calculate green minus nir
# Step 4: calculate green plus nir
# Step 5: calculate ndwi = g-nir/g+nir
# Step 6: write ndwi to the green_nir_df02 dataframe
for(i in seq){
# Step 1: create variable names for green, nir, ndwi
green_t[i] <- paste("green_nir_df02$t_",count+i,sep="")
nir_t[i] <- paste("green_nir_df02$t_",count+i,".1",sep="")
ndwi_t[i] <- paste("green_nir_df02$ndwi_t_",count+i,sep="")
}
# initialize the "for" loop to calculate ndwi columns
seq <- 1:nt
i <- 0
for(i in seq){
# Step 2: convert green (i.e., t_[i]) and nir (i.e., t_[i].1) to numeric
green <- as.numeric(as.character(green_t[i]))
##
#ERROR when i=1 > green <-as.numeric(as.character("green_nir_df02$t_1"))
##
nir_t <- as.numeric(as.character(nir_t))
# Step 3: calculate green minus nir
greenMinusNir[i] <- green_t - nir_t
# Step 4: calculate green plus nir
greenPlusNir[i] <- green_t[i] + nir_t[i]
# Step 5: calculate ndwi = g-nir/g+nir
ndwi[i] <- greenMinusNir[i] / greenPlusNir[i]
# Step 6: write ndwi to the green_nir_df02 dataframe ndwi timestep
ndwi_t[i] <- ndwi[i]
# i <- i+1
}
Пример данных
lon_lat t_1 t_2 t_60 t_1.1 t_2.1 t_60.1 ndwi_t_1 ndwi_t_2
1 -1609787.5_-2275087,5 180 216 247 80 197 192 0,3846154 0,04600484
2 -1609762.5_-2275087,5 102 252 281 80 197 227 0,1208791 0,12249443
3 -1609737.5_-2275087,5 102 216 281 80 156 227 0,1208791 0,16129032
4 -1609712.5_-2275087,5 141 216 281 80 156 227 0,2760181 0,16129032
5 -1609687,5_-2275087,5 180 181 281 80 156 227 0,3846154 0,07418398
6 -1609662.5_-2275087,5 180 216 281 80 197 227 0,3846154 0,04600484