簡體   English   中英

在 R 中從兩個以最大和最小溫度為變量的 netcdf 創建以平均溫度為變量的新 netcdf

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

我有兩個具有相同特征(緯度、經度、時間段)的 netcdf 文件,一個具有最高溫度作為變量,另一個具有最低溫度。 我想創建一個與原始文件完全相同的新 netcdf 文件,但我需要平均溫度作為變量,而不是最高和最低溫度。 我對 R 和操作 netcdf 文件很陌生,我將非常感謝任何幫助! 我需要從中計算平均值的代碼和兩個文件位於此鏈接中。 唯一的代碼在此文本下方。 代碼運行良好,但生成的 netcdf 文件不正確(尺寸等不同)。

非常感謝!

# 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)

我知道您正在尋求 R 幫助,但我認為這可以在幾行nco中完成。 像這樣的東西:

# 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