从NetCDF文件中提取特定变量值的数据吗?

时间:2020-08-24 22:32:18

标签: python r netcdf netcdf4

我有一个NetCDF文件,它表示粒子模拟中的轨迹,我想从每个轨迹的终点捕获变量。该文件包含两个维度,particleID和时间,但是每个粒子的轨迹在不同的时间开始和结束,因此我不能在特定的时间提取。相反,我有一个状态变量,在不活动时返回NA,在活动时返回0,并且在轨迹完成的单个时间步中返回非零整数。如何为每个粒子的最后时间步骤提取所需的变量(经度,纬度等)?

此刻,我正在R中使用netcdf4软件包,是否在考虑类似finallongitude <- ncvar_get(nc, "longitude" where "status" != 0 and != NA)的问题?但最终,我将全部移入python。

谢谢!

编辑:样本数据集的dput。抱歉,我第一次使用dput,不确定发布此内容的最佳格式是什么?

structure(list("Desktop/ncsmall.nc", 0L, id = 589824L, error = 0L, 
    nvars = 3, filename = "Desktop/ncsmall.nc", writable = TRUE, 
    ndims = 2, dim = list(time = structure(list(name = "time", 
        units = "seconds", vals = 1:25, len = 25L, unlim = FALSE, 
        id = 0, dimvarid = structure(list(id = 0, group_index = 1, 
            group_id = 589824, list_index = 1, isdimvar = TRUE), class = "ncid4")), class = "ncdim4"), 
        particleID = structure(list(name = "particleID", units = "ID#", 
            vals = 1:17, len = 17L, unlim = FALSE, id = 1, dimvarid = structure(list(
                id = 1, group_index = 1, group_id = 589824, list_index = 2, 
                isdimvar = TRUE), class = "ncid4")), class = "ncdim4")), 
    var = list(longitude = structure(list(name = "longitude", 
        units = "decimal degrees", longname = "longitude", shuffle = FALSE, 
        compression = NA, chunksizes = NA, make_missing_value = FALSE, 
        id = structure(list(id = 2L, group_index = 1, group_id = 589824L, 
            list_index = 1, isdimvar = FALSE), class = "ncid4"), 
        prec = "float", dim = list(structure(list(name = "time", 
            longname = "time", units = "seconds", vals = 1:25, 
            len = 25L, id = -1, dimvarid = structure(list(id = -1, 
                group_index = -1, group_id = -1, list_index = -1, 
                isdimvar = TRUE), class = "ncid4"), unlim = FALSE, 
            create_dimvar = TRUE), class = "ncdim4"), structure(list(
            name = "particleID", longname = "particleID", units = "ID#", 
            vals = 1:17, len = 17L, id = -1, dimvarid = structure(list(
                id = -1, group_index = -1, group_id = -1, list_index = -1, 
                isdimvar = TRUE), class = "ncid4"), unlim = FALSE, 
            create_dimvar = TRUE), class = "ncdim4")), ndims = 2L, 
        varsize = c(25L, 17L), unlim = FALSE, hasAddOffset = FALSE, 
        hasScaleFact = FALSE), class = "ncvar4"), latitude = structure(list(
        name = "latitude", units = "decimal degrees", longname = "latitude", 
        shuffle = FALSE, compression = NA, chunksizes = NA, make_missing_value = FALSE, 
        id = structure(list(id = 3L, group_index = 1, group_id = 589824L, 
            list_index = 2, isdimvar = FALSE), class = "ncid4"), 
        prec = "float", dim = list(structure(list(name = "time", 
            longname = "time", units = "seconds", vals = 1:25, 
            len = 25L, id = -1, dimvarid = structure(list(id = -1, 
                group_index = -1, group_id = -1, list_index = -1, 
                isdimvar = TRUE), class = "ncid4"), unlim = FALSE, 
            create_dimvar = TRUE), class = "ncdim4"), structure(list(
            name = "particleID", longname = "particleID", units = "ID#", 
            vals = 1:17, len = 17L, id = -1, dimvarid = structure(list(
                id = -1, group_index = -1, group_id = -1, list_index = -1, 
                isdimvar = TRUE), class = "ncid4"), unlim = FALSE, 
            create_dimvar = TRUE), class = "ncdim4")), ndims = 2L, 
        varsize = c(25L, 17L), unlim = FALSE, hasAddOffset = FALSE, 
        hasScaleFact = FALSE), class = "ncvar4"), status = structure(list(
        name = "status", units = "status", longname = "status", 
        shuffle = FALSE, compression = NA, chunksizes = NA, make_missing_value = FALSE, 
        id = structure(list(id = 4L, group_index = 1, group_id = 589824L, 
            list_index = 3, isdimvar = FALSE), class = "ncid4"), 
        prec = "float", dim = list(structure(list(name = "time", 
            longname = "time", units = "seconds", vals = 1:25, 
            len = 25L, id = -1, dimvarid = structure(list(id = -1, 
                group_index = -1, group_id = -1, list_index = -1, 
                isdimvar = TRUE), class = "ncid4"), unlim = FALSE, 
            create_dimvar = TRUE), class = "ncdim4"), structure(list(
            name = "particleID", longname = "particleID", units = "ID#", 
            vals = 1:17, len = 17L, id = -1, dimvarid = structure(list(
                id = -1, group_index = -1, group_id = -1, list_index = -1, 
                isdimvar = TRUE), class = "ncid4"), unlim = FALSE, 
            create_dimvar = TRUE), class = "ncdim4")), ndims = 2L, 
        varsize = c(25L, 17L), unlim = FALSE, hasAddOffset = FALSE, 
        hasScaleFact = FALSE), class = "ncvar4")), safemode = FALSE, 
    group = list(list(name = "", fqgn = "", fqpn = "", var = list(
        structure(list(name = "longitude", units = "decimal degrees", 
            longname = "longitude", shuffle = FALSE, compression = NA, 
            chunksizes = NA, make_missing_value = FALSE, id = structure(list(
                id = -1, group_index = -1, group_id = -1, list_index = -1, 
                isdimvar = FALSE), class = "ncid4"), prec = "float", 
            dim = list(structure(list(name = "time", longname = "time", 
                units = "seconds", vals = 1:25, len = 25L, id = -1, 
                dimvarid = structure(list(id = -1, group_index = -1, 
                  group_id = -1, list_index = -1, isdimvar = TRUE), class = "ncid4"), 
                unlim = FALSE, create_dimvar = TRUE), class = "ncdim4"), 
                structure(list(name = "particleID", longname = "particleID", 
                  units = "ID#", vals = 1:17, len = 17L, id = -1, 
                  dimvarid = structure(list(id = -1, group_index = -1, 
                    group_id = -1, list_index = -1, isdimvar = TRUE), class = "ncid4"), 
                  unlim = FALSE, create_dimvar = TRUE), class = "ncdim4")), 
            ndims = 2L, varsize = c(25L, 17L), unlim = FALSE), class = "ncvar4"), 
        structure(list(name = "latitude", units = "decimal degrees", 
            longname = "latitude", shuffle = FALSE, compression = NA, 
            chunksizes = NA, make_missing_value = FALSE, id = structure(list(
                id = -1, group_index = -1, group_id = -1, list_index = -1, 
                isdimvar = FALSE), class = "ncid4"), prec = "float", 
            dim = list(structure(list(name = "time", longname = "time", 
                units = "seconds", vals = 1:25, len = 25L, id = -1, 
                dimvarid = structure(list(id = -1, group_index = -1, 
                  group_id = -1, list_index = -1, isdimvar = TRUE), class = "ncid4"), 
                unlim = FALSE, create_dimvar = TRUE), class = "ncdim4"), 
                structure(list(name = "particleID", longname = "particleID", 
                  units = "ID#", vals = 1:17, len = 17L, id = -1, 
                  dimvarid = structure(list(id = -1, group_index = -1, 
                    group_id = -1, list_index = -1, isdimvar = TRUE), class = "ncid4"), 
                  unlim = FALSE, create_dimvar = TRUE), class = "ncdim4")), 
            ndims = 2L, varsize = c(25L, 17L), unlim = FALSE), class = "ncvar4"), 
        structure(list(name = "status", units = "status", longname = "status", 
            shuffle = FALSE, compression = NA, chunksizes = NA, 
            make_missing_value = FALSE, id = structure(list(id = -1, 
                group_index = -1, group_id = -1, list_index = -1, 
                isdimvar = FALSE), class = "ncid4"), prec = "float", 
            dim = list(structure(list(name = "time", longname = "time", 
                units = "seconds", vals = 1:25, len = 25L, id = -1, 
                dimvarid = structure(list(id = -1, group_index = -1, 
                  group_id = -1, list_index = -1, isdimvar = TRUE), class = "ncid4"), 
                unlim = FALSE, create_dimvar = TRUE), class = "ncdim4"), 
                structure(list(name = "particleID", longname = "particleID", 
                  units = "ID#", vals = 1:17, len = 17L, id = -1, 
                  dimvarid = structure(list(id = -1, group_index = -1, 
                    group_id = -1, list_index = -1, isdimvar = TRUE), class = "ncid4"), 
                  unlim = FALSE, create_dimvar = TRUE), class = "ncdim4")), 
            ndims = 2L, varsize = c(25L, 17L), unlim = FALSE), class = "ncvar4")), 
        dim = list(structure(list(name = "time", longname = "time", 
            units = "seconds", vals = 1:25, len = 25L, id = -1, 
            dimvarid = structure(list(id = -1, group_index = -1, 
                group_id = -1, list_index = -1, isdimvar = TRUE), class = "ncid4"), 
            unlim = FALSE, create_dimvar = TRUE), class = "ncdim4"), 
            structure(list(name = "particleID", longname = "particleID", 
                units = "ID#", vals = 1:17, len = 17L, id = -1, 
                dimvarid = structure(list(id = -1, group_index = -1, 
                  group_id = -1, list_index = -1, isdimvar = TRUE), class = "ncid4"), 
                unlim = FALSE, create_dimvar = TRUE), class = "ncdim4")), 
        level = 1, id = 589824L)), ngroups = 1L, fqgn2Rindex = list(
        `/` = 1), format = "NC_FORMAT_CLASSIC"), class = "ncdf4")

0 个答案:

没有答案