cdo-climate

How to add time dimension and merge TRMM daily data using cdo or any other software?


I have downloaded the TRMM daily data which doesn't contain time dimension and I tried cdo mergetime *nc4 out.nc4 but the file size is too small if I compare to original files as combined and there is no time dimension to it and also in outfile global attributes it is showing all inputfiles name. I have 5 years data which is around 1825 files.

So, anyone can help me with this?

Thanks, Utkarsh


Solution

  • It might be late but I was struggling with this issue for a long time, so:

    1. I have downloaded TRMM_3B42_Daily data. Each NetCDF file contains one-day data without a time attribute or axis. Therefore Climate Data Operators (CDO) will not work.
    2. This code in R should read each file and obtain timely information from the file name. and then write and new NetCDF file with the time attribute.
    #1.LIBRARIES=================================================================
    library(ncdf4)
    library(ncdf4.helpers)
    library(stringr)
    library(stringi)
    
    # Set directory where TRMM data are.
    setwd()
    
    #list all .nc4 files
    nc.files<- list.files(pattern = ".nc4$")
    
    # open first file to obtain lon and lat information 
    ncin<- nc_open(nc.files[1])
    
    lon<- ncvar_get(nc = ncin,"lon")
    
    lat<- ncvar_get(nc = ncin,"lat")
    
    Time<-vector(length = length(nc.files))
    
    #it better to close NetCDF files in R especially when writing them.
    
    nc_close(ncin)
    
    for ( i in 1:length(nc.files)){
    
     ncin<-nc_open(nc.files[i])
         
     med<-ncvar_get(nc = ncin,"precipitation")
        
     raw_time<-unlist(str_split(nc.files[i], "[.]"))[2]
        
     Time[i] <- as.Date(strptime(raw_time, "%Y%m%d"))
        
      nc_close(ncin)
    
     #=======================================================================
     # Writing NetCDF file of the  
        
     dimLON  <- ncdim_def('lon', "degree",
                                                 longname='longitude',
                                                 vals=lon)
        
     dimLAT  <- ncdim_def('lat', "degree",
                                                 longname='latitude',
                                                 vals=lat)
        
     dimTime <- ncdim_def('time', "days since 1970-01-01 00:00:0.0",
                                                 longname='time', 
                                                 calendar="standard",
                                                 vals=Time[i],unlim=TRUE)
     # define variables
     fillvalue <- 1e32
        
     dlname <- "Precipitation"
        
     var_def <- ncvar_def(name = "precip",
                                                 units = "mm day -1",
                                                 list(dimLAT,dimLON,dimTime),
                                                 fillvalue,dlname,prec="double")
        
        
     #netcdf file name 
     ncname<-paste0("precip_TRMM_",raw_time,"_time_.nc")
    
    
     #netCDF file location 
        
     # where to write the netcdf file
     ncpath <- ""
        
     ncfname <- paste0(ncpath, ncname)
        
     #create netCDF file and put arrays
    
     ncoutput<-nc_create(ncfname,list(var_def),force_v4=TRUE,verbose =F) 
    
     #put variable 
        
     ncvar_put(ncoutput,var_def,med)
        
     #put additional attributes into dimension and data variables
    
     ncatt_put(ncoutput,"lon","axis","X") 
    
     ncatt_put(ncoutput,"lat","axis","Y")
    
     ncatt_put(ncoutput,"time","axis","T")
        
    
     nc_close(ncoutput)
        
     # I Use R CMD BATCH and this just for control
     print(c(i,raw_time))
        
    
     rm(var_def,ncoutput,med)
    
        
    }
    
    1. After this Climate Data Operators (CDO) can be implemented and shall work :) (e.g., to merge all the file in a folder: cdo mergetime *.nc outfile.nc