我想使用R中的循环提取多个netcdf文件。

huangapple go评论65阅读模式
英文:

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 &lt;- &quot;1981_01_20_25.nc&quot;
nc_ds &lt;- nc_open(nc_fname)
dim_lon &lt;- ncvar_get(nc_ds, &quot;longitude&quot;)
dim_lat &lt;- ncvar_get(nc_ds, &quot;latitude&quot;)
dim_time &lt;- ncvar_get(nc_ds, &quot;time&quot;)
t_units &lt;- ncatt_get(nc_ds, &quot;time&quot;, &quot;units&quot;)
t_ustr &lt;- strsplit(t_units$value, &quot; &quot;)
t_dstr &lt;- strsplit(unlist(t_ustr)\[3\], &quot;-&quot;)
date &lt;- ymd(t_dstr) + dhours(dim_time)
date
coords &lt;- as.matrix(expand.grid(dim_lon, dim_lat, date))
var1 &lt;- ncvar_get(nc_ds, &quot;mn2t&quot;, collapse_degen=FALSE)
var2 &lt;- ncvar_get(nc_ds, &quot;mx2t&quot;, collapse_degen=FALSE)
var3 &lt;- ncvar_get(nc_ds, &quot;t2m&quot;, collapse_degen=FALSE)
var4 &lt;- ncvar_get(nc_ds, &quot;slt&quot;, collapse_degen=FALSE)
var5 &lt;- ncvar_get(nc_ds, &quot;swvl1&quot;, collapse_degen=FALSE)
var6 &lt;- ncvar_get(nc_ds, &quot;u10&quot;, collapse_degen=FALSE)
var7 &lt;- ncvar_get(nc_ds, &quot;e&quot;, collapse_degen=FALSE)
var8 &lt;- ncvar_get(nc_ds, &quot;ro&quot;, collapse_degen=FALSE)
var9 &lt;- ncvar_get(nc_ds, &quot;tp&quot;, collapse_degen=FALSE)
var10 &lt;- ncvar_get(nc_ds, &quot;lai_hv&quot;, collapse_degen=FALSE)
var11 &lt;- ncvar_get(nc_ds, &quot;lai_lv&quot;, collapse_degen=FALSE)
nc_df &lt;- data.frame(cbind(coords, var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11))
names(nc_df) &lt;- c(&quot;lon&quot;, &quot;lat&quot;, &quot;time&quot;, &quot;Mintemp&quot;, &quot;Maxtemp&quot;,&quot;Temp&quot;,&quot;SoilType&quot;,&quot;SoilVol_1&quot;,
                    &quot;Wind&quot;,&quot;Evap&quot;,&quot;Runoff&quot;,&quot;Prec&quot;,&quot;LAI_H&quot;,&quot;LAI_L&quot;)
csv_fname &lt;- &quot;netcdf_filename.csv&quot;
write.csv(nc_df, &quot;1981_01_20_25.csv&quot;)

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(&quot;YOUR/WORKING/DIRECTORY&quot;)
FilesList&lt;-dir(pattern=&quot;\\.nc$&quot;)
for(i in 1:length(FilesList)){
nc_fname &lt;- FilesList[i]
nc_ds &lt;- nc_open(nc_fname)
dim_lon &lt;- ncvar_get(nc_ds, &quot;longitude&quot;)
dim_lat &lt;- ncvar_get(nc_ds, &quot;latitude&quot;)
dim_time &lt;- ncvar_get(nc_ds, &quot;time&quot;)
t_units &lt;- ncatt_get(nc_ds, &quot;time&quot;, &quot;units&quot;)
t_ustr &lt;- strsplit(t_units$value, &quot; &quot;)
t_dstr &lt;- strsplit(unlist(t_ustr)\[3\], &quot;-&quot;)
date &lt;- ymd(t_dstr) + dhours(dim_time)
date
coords &lt;- as.matrix(expand.grid(dim_lon, dim_lat, date))
var1 &lt;- ncvar_get(nc_ds, &quot;mn2t&quot;, collapse_degen=FALSE)
var2 &lt;- ncvar_get(nc_ds, &quot;mx2t&quot;, collapse_degen=FALSE)
var3 &lt;- ncvar_get(nc_ds, &quot;t2m&quot;, collapse_degen=FALSE)
var4 &lt;- ncvar_get(nc_ds, &quot;slt&quot;, collapse_degen=FALSE)
var5 &lt;- ncvar_get(nc_ds, &quot;swvl1&quot;, collapse_degen=FALSE)
var6 &lt;- ncvar_get(nc_ds, &quot;u10&quot;, collapse_degen=FALSE)
var7 &lt;- ncvar_get(nc_ds, &quot;e&quot;, collapse_degen=FALSE)
var8 &lt;- ncvar_get(nc_ds, &quot;ro&quot;, collapse_degen=FALSE)
var9 &lt;- ncvar_get(nc_ds, &quot;tp&quot;, collapse_degen=FALSE)
var10 &lt;- ncvar_get(nc_ds, &quot;lai_hv&quot;, collapse_degen=FALSE)
var11 &lt;- ncvar_get(nc_ds, &quot;lai_lv&quot;, collapse_degen=FALSE)
nc_df &lt;- data.frame(cbind(coords, var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11))
names(nc_df) &lt;- c(&quot;lon&quot;, &quot;lat&quot;, &quot;time&quot;, &quot;Mintemp&quot;, &quot;Maxtemp&quot;,&quot;Temp&quot;,&quot;SoilType&quot;,&quot;SoilVol_1&quot;,
&quot;Wind&quot;,&quot;Evap&quot;,&quot;Runoff&quot;,&quot;Prec&quot;,&quot;LAI_H&quot;,&quot;LAI_L&quot;)
csv_fname &lt;- &quot;netcdf_filename.csv&quot;  ## I am not sure you need this line, it doesn&#39;t get called again
write.csv(nc_df, paste0(gsub(&quot;\\.nc$&quot;,&quot;&quot;,FilesList[i]),&quot;.csv&quot;))
}

huangapple
  • 本文由 发表于 2023年2月16日 10:29:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定