library(terra) path1 <- dir('G:/ERA5hr/LAI_LV/1/',pattern = '*.tif',full.names = T)#导入地址后批量合并 #path2 <- dir('G:/hourlywindspeed/v/0/',pattern = '*.tif',full.names = T) #nc5 <- stack(paths) for (i in 1:length(path1)){ print(path1[i]) file1_data <- stack(path1[i]) #file2_data <- stack(path2[i]) file_data <- sqrt(1-0.05*file1_data)*sqrt(1+4.5*file1_data) output_path <- paste('G:/ERA5hr/Fy/1/', substr(path1[i],20,33),'.tif',sep='') writeRaster(file_data,output_path, overwrite=TRUE,format = "GTiff") }这个代码原始数据是整数型,怎么将计算结果输出成浮点型
时间: 2024-03-14 08:43:20 浏览: 15
在R中,可以使用`dataType()`函数将栅格数据的数据类型转换为浮点型。在你的代码中,可以在计算完成后使用`dataType()`函数将输出结果的数据类型转换为浮点型,例如:
```
file_data <- sqrt(1-0.05*file1_data)*sqrt(1+4.5*file1_data)
file_data <- dataType(file_data) # 将数据类型转换为浮点型
```
这样,输出结果的数据类型就会变成浮点型。然后,再将其写入到`GTiff`格式的文件中时,它们的数据类型也会被保留为浮点型。
相关问题
请修改以下R语言代码 library(terra) path1 <- dir('G:/ERA5hr/Fw/1/',pattern = '*.tif',full.names = T) path2 <- dir('G:/ERA5hr/Fy/1/',pattern = '*.tif',full.names = T) path3 <- raster("G:/ERA5hr/ut0_225/n1.nc") for (i in 1:length(path1)){ print(path1[i]) fw <- stack(path1[i]) fy <- stack(path2[i]) ut0 <- stack(path3[i]) file_data <- ut0*fy*fw output_path <- paste('G:/ERA5hr/ut_225/1/', substr(path1[i],16,29),'.tif',sep='') writeRaster(file_data,output_path, overwrite=TRUE,format = "GTiff") }
library(terra)
path1 <- list.files('G:/ERA5hr/Fw/1/',pattern = '*.tif',full.names = TRUE)
path2 <- list.files('G:/ERA5hr/Fy/1/',pattern = '*.tif',full.names = TRUE)
path3 <- rast('G:/ERA5hr/ut0_225/n1.nc')
for (i in 1:length(path1)){
print(path1[i])
fw <- rast(path1[i])
fy <- rast(path2[i])
ut0 <- rast(path3, varname = 'ut0')
file_data <- ut0 * fy * fw
output_path <- paste('G:/ERA5hr/ut_225/1/', substr(path1[i],16,29),'.tif',sep='')
writeRaster(file_data,output_path, overwrite=TRUE,format = "GTiff")
}
以下栅格数据计算有错误,请修改library(terra) path1 <- dir('G:/ERA5hr/P/1/', pattern = '*.tif', full.names = TRUE) path2 <- dir('G:/ERA5hr/ZUST/1/', pattern = '*.tif', full.names = TRUE) path3 <- dir('G:/ERA5hr/ut_750/1/', pattern = '*.tif', full.names = TRUE) for (i in 1:length(path1)) { print(path1[i]) # 读取三个.tif文件 p <- stack(path1[i]) zust <- stack(path2[i]) ut <- stack(path3[i]) # 进行计算,使用if语句替代ifelse函数 if (zust > ut) { file_data <- (p/9.8) * zust^3 * (1 - ut/zust) * (1 + (ut/zust)^2) } else { file_data <- 0 } # 输出为新的.tif文件 output_path <- paste('G:/ERA5hr/Q/Q750/1/', substr(path1[i], 15, 28), '.tif', sep = '') writeRaster(file_data, output_path, overwrite = TRUE, format = "GTiff") }
在这段代码中,if语句不能直接用于两个raster对象之间的比较,因此需要使用相关函数来进行计算。以下是修改后的代码:
library(terra)
path1 <- dir('G:/ERA5hr/P/1/', pattern = '*.tif', full.names = TRUE)
path2 <- dir('G:/ERA5hr/ZUST/1/', pattern = '*.tif', full.names = TRUE)
path3 <- dir('G:/ERA5hr/ut_750/1/', pattern = '*.tif', full.names = TRUE)
for (i in 1:length(path1)) {
print(path1[i])
# 读取三个.tif文件
p <- rast(path1[i])
zust <- rast(path2[i])
ut <- rast(path3[i])
# 进行计算,使用ifelse函数替代if语句
file_data <- ifelse(zust > ut,
(p/9.8) * zust^3 * (1 - ut/zust) * (1 + (ut/zust)^2),
0)
# 输出为新的.tif文件
output_path <- paste('G:/ERA5hr/Q/Q750/1/', substr(path1[i], 15, 28), '.tif', sep = '')
writeRaster(file_data, output_path, overwrite = TRUE, format = "GTiff")
}
在修改后的代码中,使用了rast()函数来读取raster对象,使用了ifelse()函数来进行条件判断。