阅读HadUK-Grid(NetCDF)文件读入R并转换为dataframe

mbzjlibv  于 2023-09-27  发布在  Etcd
关注(0)|答案(1)|浏览(208)

数据读入自: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")

问题

什么是最好的方法,让这个fie到一个可用的DF的每日区域温度?我不确定我是否将时间变量正确转换为2021年11月结束,但应该是12月。

zyfwsgd6

zyfwsgd61#

我下载了这个文件,得到了22,646个观测值的时间维度。
使用CFtime package,您可以轻松处理气候观测文件中的时间维度,如下所示:

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)

相关问题