How to write data from netcdf file to an excel or text file?

netcdf, r

Solution

f <- open.ncdf("C:\\BR_Ban.nc")
A <- get.var.ncdf(nc=f,varid="date")
B <- get.var.ncdf(nc=f,varid="Tair")
write.table(t(rbind(A,B)),file="output.txt")

As mentionned in the summary for the netCDF file, `date` has 2 dimensions, dimension 1 (the rows) being `datedim` and dimension 2 (the columns) `time_counter`, while `Tair` has also 2 dimensions with `land` as dimension 1 and `time_counter` as dimension 2. So to output them together you need first to `rbind` them since their shared dimension is the columns (`time_counter`), and then transpose.

Problem

I have a netcdf file that has 6 dimensions: ``` f=open.ncdf("C:\\BR_Ban.nc") ``` I read the varible `date`: ``` A = get.var.ncdf(nc=f,varid="date",verbose=TRUE) ``` Then I read the variable `Tair` as I did for `date`. I wrote the two variables to two text files. May anyone help me to write both of them to one text file or excel? This piece of code will write only one variable: ``` write.table(as.double(A),"C:\\folder\\shwon_Br_Ban_flux net.txt") ``` The problem with this variable `date` is that I got a text file that looks like this: ``` "x" "1" 2004 "2" 1 "3" 0.5 "4" 2004 "5" 1 "6" 1 ``` up to ``` "157675" 2006 "157676" 365 "157677" 23.5 "157678" 2007 "157679" 366 "157680" 0 ``` As you can see, all in one column "year,hour and day". Is there a way to write the time step "half hourly" in one column, the year in another column and the same for the day?

Original source