首页 > 解决方案 > 多个 netcdf 文件函数定义特定的 lat lon

问题描述

按照建议,我关注了这个R-bloggers 网站并编辑了代码以开发多个 .nc 文件工作流程......

编辑后的代码运行,具有目录、路径和可计算的 dim、var 的属性,所有似乎都被正确定义,因为没有进一步的错误阻止绿色 rstudio 块。

操作系统挂起(x86_64-pc-linux-gnu,3.6.3),htop显示 4 个 CPU 中的 1 个 CPU 在 100% 时最大化,RAM(内存)@ 6.55 的 6.75 Gb 可用。

虽然我记得在时间段内将 .nc 文件下载子设置到一个相对较小的全球区域(纬度:-10、-30 S 和经度:110、120 E),但print(nc)定义了全球覆盖的文件数据(经度大小:6000 , valid_min: -180, valid_max: 360; lat Size:4500, valid_min: -90, valid_max: 90)

如何在以下循环(底部)中定义一个特定的全局域,由 lat 和 lon 限制,可以最小程度地更改以更改感兴趣的域的大小和位置?

我想在for循环中的某个地方用nc_lat& nc_lon..

仅包含 .nc 文件的库和目录

library(ncdf4) 
library(tidyverse)


flist <- list.files(path = "/home/rdfleay/Documents/Study/WAdata/AODN/AODN_WA_CoralBay/files", pattern = "^.*\\.(nc|NC|Nc|Nc)$")

例如 .nc “20210504152000-ABOM-L3S_GHRSST-SSTskin-AVHRR_D-1d_night.nc”

process_nc <- function(files){
    # iterate through the nc
    for (i in 1:length(files)){
        # open a conneciton to the ith nc file
        nc_tmp <- nc_open(paste0("/home/rdfleay/Documents/Study/WAdata/AODN/AODN_WA_CoralBay/files/", files[i])) # nc_tmp = 'access all the .nc files'
        # store values from variables and attributes
        nc_sst <- ncvar_get(nc_tmp, attributes(nc_tmp$var)$names[1]) # var[1] = sea_surface_temp
        nc_lat <- ncvar_get(nc_tmp, attributes(nc_tmp$dim)$names[2]) # dim[2] = lat
        nc_lon <- ncvar_get(nc_tmp, attributes(nc_tmp$dim)$names[1]) # dim[1] = lon
        nc_atts <- ncatt_get(nc_tmp, 0)
        nc_start_date <- as.Date(nc_atts$time_coverage_start, format = "%Y%m%d", tz = "UTC")
        # both start/stop var's from nc_atts formatted identical
        # nc_atts$time_coverage_end
          #  [1] "20210505T000631Z"
        # nc_atts$stop_time
          #  [1] "20210505T000631Z"
        
        # close the connection since were finished
        nc_close(nc_tmp)
        # set the dimension names and values of your matrix to the appropriate latitude and longitude values
        dimnames(nc_sst) <- list(lon=nc_lon, lat=nc_lat)

        # I'm choosing to store all the data in long format.
        # depending on your workflow you can make different choices here...
        # Your variable may get unmanageably large here
        # if you have high spatial and temporal resolution nc data.
        tmp_sst_df <- reshape2::melt(nc_sst, value.name = "sst")
        tmp_sst_df$date_start <- nc_start_date

        # set the name of my new variable and bind the new data to it
        if (exists("sst_data_monthly")){
            sst_data_monthly <- bind_rows(sst_data_monthly, tmp_sst_df)
        }else{
            sst_data_monthly <- tmp_sst_df
        }
        # tidy up, not sure if necesarry really, but neater
        rm(nc_sst, nc_lat, nc_lon, nc_tmp, nc_atts, nc_start_date, tmp_sst_df)
    }

    return(sst_data_monthly)
}


data <- process_nc(flist)

标签: rfunctionncdf4

解决方案


推荐阅读