Re: merge subset from different netcdf files in the same netcdf file?

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Thu Mar 07 2013 - 08:41:20 MST


If the time arrays on both files have the same "units" attribute, then the "easy" way to do this is:


load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

;---Open the two files
f1 = addfile("","r")
f2 = addfile("","r")

;---Read the data
x1 = f1->x
x2 = f2->x
xdims = dimsizes(x1) ; 1460 x 360 x 720

;---Get the dimension sizes
ntim = xdims(0)
nlat = xdims(1)
nlat = xdims(2)

;---Create new array so we can fill it in
xnew = new((/2,nlat,nlon/),typeof(x1)) ; 2 x 360 x 720

;---Fill in new array
xnew(0,:,:) = x1(ntim-1,:,:) ; Last time step
xnew(1,:,:) = x2(0,:,:) ; First time step

copy_VarAtts(x1,xnew) ; Copy the attributes

;---Add more attributes to xnew here if desiredů
xnew@att1 = ...att1_value...
xnew@att2 = ...att2_value...

;---Create new time coordinate array
newtime = new(2,typeof(x1&time))
newtime(0) = x1&time(ntim-1)
newtime(1) = x2&time(0)
copy_VarAtts(x1&time,newtime) ; Copy over x1's "time" attributes

xnew!0 = "time"
xnew&time = newtime ; Assign the new time coordinate array

;---Copy other two coordinate arrays
copy_VarCoords_n(x1,xnew,(/1,2/)) ; Copy the lat, lon coordinate arrays, if any

;---Write to new NetCDF file
outfile = ""
system("rm -f " + outfile)
fout = addfile(outfile,"c")
fout->x = xnew ;

If the "time" arrays don't have the same units, then you will need to use "cd_convert" to convert one of them to the same units as the other:

;---Create new time coordinate array
x2time_to_x1 = cd_convert(x2&time,x1&time@units) ; Convert x2's time units to x1's time units

newtime = new(2,typeof(x1&time))
newtime(0) = x1&time(ntim-1)
newtime(1) = x2time_to_x1(0)
copy_VarAtts(x1&time,newtime) ; Copy over "time" attributes


On Mar 5, 2013, at 11:12 AM, Montane Caminal, Francesc wrote:

> Hi,
> I have 2 netcdf files (with the same variable of interest for different years) with the same dimensions (time, latitude, longitude) (1460, 360, 720) for both files.
> I know how to extract a subset (data for 1 time or some different times) of a particular netcdf file to create another netcdf. However, in this case I want to combine the last value of the variable in one year with the first value of the variable the following year in the same netcdf.
> Is it possible to merge these two different times from different netcdfs in the same netcdf file with dimensions (time, latitude, longitude) (2, 360, 720)?
> In case it is possible to do it, what is the best way to proceed?
> Thanks,
> Francesc
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:

ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
Received on Thu Mar 7 08:41:30 2013

This archive was generated by hypermail 2.1.8 : Thu Mar 07 2013 - 08:55:58 MST