将 HadUK-Grid (NetCDF) 文件读入 R 并转换为数据帧

问题描述 投票:0回答:1

数据读取自:https://data.ceda.ac.uk/badc/ukmo-hadobs/data/insitu/MOHC/HadOBS/HadUK-Grid/v1.1.0.0/region/tasmin/day/v20220310

读取每日最低温度数据

nc <- nc_open("xxxxx")     
print(nc)

*Time variable info printed....*     
    time  Size:22281`    
    axis: T`    
    bounds: time_bounds
    units: hours since 1800-01-01 00:00:00  
    standard_name:time    
    calendar: gregorian 

转换变量

region <- ncvar_get(nc, "geo_region")    
temp <- ncvar_get(nc, "tasmin")    
time <- nc.get.time.series(f = nc,    
                correct.for.gregorian.julian = TRUE,    
                time.dim.name = "time") 

问题

使该文件进入可用的每日区域温度 DF 的最佳方法是什么?我不确定我是否已将时间变量转换为 2021 年 11 月结束,但应该是 12 月。

r netcdf ncdf4
1个回答
0
投票

我下载了该文件,得到了 22,646 个观察值的时间维度。

使用

CFtime
,您可以轻松处理气候观测文件中的时间维度,如下所示:

library(CFtime)

nc <- nc_open("./tasmin_hadukgrid_uk_region_day_19600101-20211231.nc")

# Use CFtime to read in the time dimension from the file.
cf <- CFtime(nc$dim$time$units, nc$dim$time$calendar, nc$dim$time$vals)

# Get the dates as a character vector
dates <- CFtimestamp(cf)

# The "geo_region" variable has the names of the administrative units
region <- ncvar_get(nc, "geo_region")

# Read the data, transpose to get regions in columns and dates in rows
tasmin <- t(ncvar_get(nc, "tasmin"))
nc_close(nc)

# Set the dimnames on the array
dimnames(tasmin) <- list(dates, region)

# Convert the array to a data.frame
tasmin <- as.data.frame(tasmin)
© www.soinside.com 2019 - 2024. All rights reserved.