简体   繁体   English

使用 R 中的特定条件聚合 nc 文件

[英]Aggregate nc file using specific condition in R

I need your help again.我再次需要你的帮助。 I have.nc file, metadata: File minty.nc (NC_FORMAT_64BIT):我有 .nc 文件,元数据:文件 minty.nc (NC_FORMAT_64BIT):

 1 variables (excluding dimension variables):
    short mn2t[longitude,latitude,time]   
        scale_factor: 0.000940940342005054
        add_offset: 259.294916797895
        _FillValue: -32767
        missing_value: -32767
        units: K
        long_name: Minimum temperature at 2 metres since previous post-processing

 3 dimensions:
    longitude  Size:57
        units: degrees_east
        long_name: longitude
    latitude  Size:49
        units: degrees_north
        long_name: latitude
    time  Size:90240
        units: hours since 1900-01-01 00:00:00.0
        long_name: time
        calendar: gregorian

2 global attributes:
    Conventions: CF-1.6
    

I have a code, which works well with smaller.nc files:我有一个代码,它适用于 smaller.nc 文件:

 library(raster)
 library(rgdal)
 library(ggplot2)
 nc_data = nc_open('file.nc')
 lon = ncvar_get(nc_data, "longitude")
 lat = ncvar_get(nc_data, "latitude", verbose = F)
 t = ncvar_get(nc_data, "time")
 head(lon)
 head(t)
 head(lat)
 mint.array = ncvar_get(nc_data, "mn2t")
 dim(mint.array)
 fillvalue = ncatt_get(nc_data, "mn2t", "_FillValue")
 fillvalue
 mint.array[mint.array == fillvalue$value] <- NA
 r_brick <- brick(mint.array, xmn=min(lat), xmx=max(lat), ymn=min(lon), ymx=max(lon), crs=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0"))
 r_brick = flip(t(r_brick), direction = 'y')

Because of the large file size, I got an error: "cannot allocate vector of size 1.4 Mb" I also used gc() to clear unused memory. It didn't help.由于文件太大,我得到一个错误:“无法分配大小为 1.4 Mb 的向量”我还使用 gc() 清除未使用的 memory。它没有帮助。 I do not need all the data in my file.nc.我不需要 file.nc 中的所有数据。 In this case, I need somehow to aggregate this.在这种情况下,我需要以某种方式汇总它。 For my further calculations, I need only daily minima.对于我的进一步计算,我只需要每日最小值。 In this case, for df I used: df(ff) <- aggregate(df, list(rep(1:(nrow(df)%(%n+1), each=24, len=nrow(df))), min)在这种情况下,对于我使用的 df: df(ff) <- aggregate(df, list(rep(1:(nrow(df)%(%n+1), each=24, len=nrow(df))), min)

Unfortunately, I find it difficult to adapt this code for.nc file.不幸的是,我发现很难将此代码改编为 .nc 文件。 Maybe someone could help me.也许有人可以帮助我。 Thank you in advance!先感谢您!

To avoid memory problems, you can do this instead:为避免 memory 问题,您可以改为这样做:

library(raster)
r_brick <- brick('file.nc', "mn2t")

It also prevents mistakes.它还可以防止错误。 For example, in your code, this is wrong in two ways:例如,在您的代码中,这在两个方面是错误的:

xmn=min(lat), xmx=max(lat), ymn=min(lon), ymx=max(lon)

because x should be lon and y should be lat and because the ncdf coordinates refer to the centers of the cells, whereas xmn , xmx , ymn , and ymx refer to the borders.因为x应该是lon并且y应该是lat并且因为 ncdf 坐标指的是单元格的中心,而xmnxmxymnymx指的是边界。

You can also use the modern equivalent你也可以使用现代的等价物

library(terra)
r <- rast('file.nc')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM