简体   繁体   中英

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. 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. I am quite new with R and manipulating netcdf files, I would highly appreciate any help! 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).

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 . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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