简体   繁体   English

在 R 中从两个以最大和最小温度为变量的 netcdf 创建以平均温度为变量的新 netcdf

[英]Create new netcdf with mean temperature as variable from two netcdf with max and min temperature as variable in R

I have two netcdf files with same characteristics (lat, lon, time period), one with max temperature as variable, the other one minimum temperature.我有两个具有相同特征(纬度、经度、时间段)的 netcdf 文件,一个具有最高温度作为变量,另一个具有最低温度。 I would like to create a new netcdf file exactly like the original ones, but instead of max and min temperature, I need the mean temperature as variable.我想创建一个与原始文件完全相同的新 netcdf 文件,但我需要平均温度作为变量,而不是最高和最低温度。 I am quite new with R and manipulating netcdf files, I would highly appreciate any help!我对 R 和操作 netcdf 文件很陌生,我将非常感谢任何帮助! The code and the two files out of which I need to compute the mean are at this link .我需要从中计算平均值的代码和两个文件位于此链接中。 The only code is below this text.唯一的代码在此文本下方。 The code is working well, but the resulting netcdf file is not correct (dimensions etc. are differing).代码运行良好,但生成的 netcdf 文件不正确(尺寸等不同)。

Thanks a lot!非常感谢!

# Compute mean

library(ncdf4)
library(raster)
library(rgdal)
library(lubridate)
library(geosphere)
library(reshape2)
library(ggplot2)

setwd("~/")

      nc_max <- nc_open('Obs_tas_max_1983-2005.nc')
      
      nc_min <- nc_open('Obs_tas_min_1983-2005.nc')
      
        X <- list()
        h <- 1
        lat <- ncvar_get(nc_max, "lat");
        nlat <- nrow(lat)
        lon <- ncvar_get(nc_max, "lon");
        nlon <- nrow(lon)
        # lat <- as.vector(lat);
        # lon <- as.vector(lon)
        t_max <- ncvar_get(nc_max, "tas_max");
        t_min <- ncvar_get(nc_min, "tas_min");
        tas <- (t_max+t_min)/2
        sw <- as.character('tas_max')
        time <- ncvar_get(nc_max, "time");
        ndays <- length(time)
        v <- matrix(NA, length(lat), ndays)
      
        # for (t in 1:ndays) {
        #     x <- ncvar_get(nc_max, sw, start = c(1, 1, t), count = c(nlon, nlat, 1))
        #     x <- as.vector(x)
        #     v[ ,t] <- x
        
        X[[h]] <- (v)
        # h <- h + 1
        # nc_close(nc_max)
        # nc_close(nc_min)
      
      X <- do.call(rbind, X) # Time on the rows, coordinates on the columns
      dt <- seq.Date(as.Date("1983-01-01"), as.Date("2005-12-31"), 'day')
      
      m <- month(dt)
      d <- day(dt)
      idx <- !(m == 2 & d == 29)
      X <- X[idx, ]
      
      dim_idx <- ncdim_def(name='Index',
                           units='m',
                           longname='idx',
                           vals= 1:nrow(X))
      
      dim_time <- ncdim_def('time',
                            units='days from 1983-01-01',
                            longname='time',
                            calendar="standard", vals=1:idx)
      
      varLat <- ncvar_def(name='lat',
                          units='degrees_north',
                          dim=list(dim_idx),
                          missval=NA,
                          longname='latitude',
                          prec='double'
      )
      
      varLon <- ncvar_def(name='lon',
                          units='degrees_east',
                          dim=list(dim_idx),
                          missval=NA,
                          longname='latitude',
                          prec='double'
      )
      
      varX <- ncvar_def(name='tas',
                        units= 'degrees Celsius',
                        dim=list(dim_time, dim_idx),
                        missval=NA,
                        longname='Temperature'
      )
      
      vars <- list(varLat, varLon, varX)
      
      outputfile <- paste('tas', '.nc', sep = '_')
      
      con <- nc_create(outputfile, vars)
      ncvar_put(con, varLat, lat)
      ncvar_put(con, varLon, lon)
      ncvar_put(con, varX, X)
      
      nc_close(con)

I know you are asking for R help, but I think this might be done in a few lines of nco .我知道您正在寻求 R 帮助,但我认为这可以在几行nco中完成。 Something like this:像这样的东西:

# change variable name to be the same in each netcdf file

ncrename -v tas_max,tas Obs_tas_max_1983-2005.nc tmax.nc
ncrename -v tas_min,tas Obs_tas_min_1983-2005.nc tmin.nc

# ensemble average of tas variable across the two files

ncea tmax.nc tmin.nc tas_out.nc

# change the long_name attribute

ncatted -O -a long_name,tas,o,c,Temperature tas_out.nc

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

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