英文:
I want to extract multiple netcdf files using loop in R
问题
我想在R中使用循环提取多个NETCDF文件。我已经编写了单个文件的代码,但卡在了如何将循环应用到这段代码上。请帮忙。
nc_fname <- "1981_01_20_25.nc"
nc_ds <- nc_open(nc_fname)
dim_lon <- ncvar_get(nc_ds, "longitude")
dim_lat <- ncvar_get(nc_ds, "latitude")
dim_time <- ncvar_get(nc_ds, "time")
t_units <- ncatt_get(nc_ds, "time", "units")
t_ustr <- strsplit(t_units$value, " ")
t_dstr <- strsplit(unlist(t_ustr)[3], "-")
date <- ymd(t_dstr) + dhours(dim_time)
date
coords <- as.matrix(expand.grid(dim_lon, dim_lat, date))
var1 <- ncvar_get(nc_ds, "mn2t", collapse_degen=FALSE)
var2 <- ncvar_get(nc_ds, "mx2t", collapse_degen=FALSE)
var3 <- ncvar_get(nc_ds, "t2m", collapse_degen=FALSE)
var4 <- ncvar_get(nc_ds, "slt", collapse_degen=FALSE)
var5 <- ncvar_get(nc_ds, "swvl1", collapse_degen=FALSE)
var6 <- ncvar_get(nc_ds, "u10", collapse_degen=FALSE)
var7 <- ncvar_get(nc_ds, "e", collapse_degen=FALSE)
var8 <- ncvar_get(nc_ds, "ro", collapse_degen=FALSE)
var9 <- ncvar_get(nc_ds, "tp", collapse_degen=FALSE)
var10 <- ncvar_get(nc_ds, "lai_hv", collapse_degen=FALSE)
var11 <- ncvar_get(nc_ds, "lai_lv", collapse_degen=FALSE)
nc_df <- data.frame(cbind(coords, var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11))
names(nc_df) <- c("lon", "lat", "time", "Mintemp", "Maxtemp", "Temp", "SoilType", "SoilVol_1",
"Wind", "Evap", "Runoff", "Prec", "LAI_H", "LAI_L")
csv_fname <- "netcdf_filename.csv"
write.csv(nc_df, "1981_01_20_25.csv")
我已经尝试过修改它,但没有成功。
英文:
I want to put extract multiple NETCDF files using loop in R. The code I have made is for single file but I am stuck to apply loop for this code. Need help please.
nc_fname <- "1981_01_20_25.nc"
nc_ds <- nc_open(nc_fname)
dim_lon <- ncvar_get(nc_ds, "longitude")
dim_lat <- ncvar_get(nc_ds, "latitude")
dim_time <- ncvar_get(nc_ds, "time")
t_units <- ncatt_get(nc_ds, "time", "units")
t_ustr <- strsplit(t_units$value, " ")
t_dstr <- strsplit(unlist(t_ustr)\[3\], "-")
date <- ymd(t_dstr) + dhours(dim_time)
date
coords <- as.matrix(expand.grid(dim_lon, dim_lat, date))
var1 <- ncvar_get(nc_ds, "mn2t", collapse_degen=FALSE)
var2 <- ncvar_get(nc_ds, "mx2t", collapse_degen=FALSE)
var3 <- ncvar_get(nc_ds, "t2m", collapse_degen=FALSE)
var4 <- ncvar_get(nc_ds, "slt", collapse_degen=FALSE)
var5 <- ncvar_get(nc_ds, "swvl1", collapse_degen=FALSE)
var6 <- ncvar_get(nc_ds, "u10", collapse_degen=FALSE)
var7 <- ncvar_get(nc_ds, "e", collapse_degen=FALSE)
var8 <- ncvar_get(nc_ds, "ro", collapse_degen=FALSE)
var9 <- ncvar_get(nc_ds, "tp", collapse_degen=FALSE)
var10 <- ncvar_get(nc_ds, "lai_hv", collapse_degen=FALSE)
var11 <- ncvar_get(nc_ds, "lai_lv", collapse_degen=FALSE)
nc_df <- data.frame(cbind(coords, var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11))
names(nc_df) <- c("lon", "lat", "time", "Mintemp", "Maxtemp","Temp","SoilType","SoilVol_1",
"Wind","Evap","Runoff","Prec","LAI_H","LAI_L")
csv_fname <- "netcdf_filename.csv"
write.csv(nc_df, "1981_01_20_25.csv")
I have tried to modified it but its not working.
答案1
得分: 0
以下是翻译好的部分:
如果您可以使用 `dir()` 或 `list.files()` 创建一个包含所有文件名的列表,您可以在第一步和最后一步中像这样索引这些文件:
setwd("YOUR/WORKING/DIRECTORY")
FilesList <- dir(pattern="\\.nc$")
for(i in 1:length(FilesList)){
nc_fname <- FilesList[i]
nc_ds <- nc_open(nc_fname)
dim_lon <- ncvar_get(nc_ds, "longitude")
dim_lat <- ncvar_get(nc_ds, "latitude")
dim_time <- ncvar_get(nc_ds, "time")
t_units <- ncatt_get(nc_ds, "time", "units")
t_ustr <- strsplit(t_units$value, " ")
t_dstr <- strsplit(unlist(t_ustr)[3], "-")
date <- ymd(t_dstr) + dhours(dim_time)
date
coords <- as.matrix(expand.grid(dim_lon, dim_lat, date))
var1 <- ncvar_get(nc_ds, "mn2t", collapse_degen=FALSE)
var2 <- ncvar_get(nc_ds, "mx2t", collapse_degen=FALSE)
var3 <- ncvar_get(nc_ds, "t2m", collapse_degen=FALSE)
var4 <- ncvar_get(nc_ds, "slt", collapse_degen=FALSE)
var5 <- ncvar_get(nc_ds, "swvl1", collapse_degen=FALSE)
var6 <- ncvar_get(nc_ds, "u10", collapse_degen=FALSE)
var7 <- ncvar_get(nc_ds, "e", collapse_degen=FALSE)
var8 <- ncvar_get(nc_ds, "ro", collapse_degen=FALSE)
var9 <- ncvar_get(nc_ds, "tp", collapse_degen=FALSE)
var10 <- ncvar_get(nc_ds, "lai_hv", collapse_degen=FALSE)
var11 <- ncvar_get(nc_ds, "lai_lv", collapse_degen=FALSE)
nc_df <- data.frame(cbind(coords, var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11))
names(nc_df) <- c("lon", "lat", "time", "Mintemp", "Maxtemp","Temp","SoilType","SoilVol_1",
"Wind","Evap","Runoff","Prec","LAI_H","LAI_L")
csv_fname <- "netcdf_filename.csv" ## 我不确定您是否需要这一行,它不会再次被调用
write.csv(nc_df, paste0(gsub("\\.nc$","",FilesList[i]),".csv"))
}
英文:
If you can create a list of all of your file names with dir()
or list.files()
, you can index these files in the first and last steps like this:
setwd("YOUR/WORKING/DIRECTORY")
FilesList<-dir(pattern="\\.nc$")
for(i in 1:length(FilesList)){
nc_fname <- FilesList[i]
nc_ds <- nc_open(nc_fname)
dim_lon <- ncvar_get(nc_ds, "longitude")
dim_lat <- ncvar_get(nc_ds, "latitude")
dim_time <- ncvar_get(nc_ds, "time")
t_units <- ncatt_get(nc_ds, "time", "units")
t_ustr <- strsplit(t_units$value, " ")
t_dstr <- strsplit(unlist(t_ustr)\[3\], "-")
date <- ymd(t_dstr) + dhours(dim_time)
date
coords <- as.matrix(expand.grid(dim_lon, dim_lat, date))
var1 <- ncvar_get(nc_ds, "mn2t", collapse_degen=FALSE)
var2 <- ncvar_get(nc_ds, "mx2t", collapse_degen=FALSE)
var3 <- ncvar_get(nc_ds, "t2m", collapse_degen=FALSE)
var4 <- ncvar_get(nc_ds, "slt", collapse_degen=FALSE)
var5 <- ncvar_get(nc_ds, "swvl1", collapse_degen=FALSE)
var6 <- ncvar_get(nc_ds, "u10", collapse_degen=FALSE)
var7 <- ncvar_get(nc_ds, "e", collapse_degen=FALSE)
var8 <- ncvar_get(nc_ds, "ro", collapse_degen=FALSE)
var9 <- ncvar_get(nc_ds, "tp", collapse_degen=FALSE)
var10 <- ncvar_get(nc_ds, "lai_hv", collapse_degen=FALSE)
var11 <- ncvar_get(nc_ds, "lai_lv", collapse_degen=FALSE)
nc_df <- data.frame(cbind(coords, var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11))
names(nc_df) <- c("lon", "lat", "time", "Mintemp", "Maxtemp","Temp","SoilType","SoilVol_1",
"Wind","Evap","Runoff","Prec","LAI_H","LAI_L")
csv_fname <- "netcdf_filename.csv" ## I am not sure you need this line, it doesn't get called again
write.csv(nc_df, paste0(gsub("\\.nc$","",FilesList[i]),".csv"))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论