Project

General

Profile

Converting .csv to .nc

Added by Yousef Albuhaisi almost 4 years ago

Hi,

I have data in csv format and it is in (lat lon data) shape. What I want to do is converting this file to NC format. can cdo do this?

Regards.

Yousef.


Replies (5)

RE: Converting .csv to .nc - Added by Karin Meier-Fleischer almost 4 years ago

Hi Yousef,

you can read the CSV data with a ksh script which generates the gridfile and correct time axis.

Here is an example how the script could look like:

#!/bin/ksh

input="input.csv" 
varname="var" 

time="1950-01-01,00:00:00,3hour"        #-- init time axis

lat=$(tail +2 $input | cut -d " " -f 1)     #-- latitude value
lon=$(tail +2 $input | cut -d " " -f 2)     #-- longitude value

numlines=$(cat input.csv | wc -l)
nlines=$(($numlines-1))

#-- generate the grid description file for $input
cat << EOF > gridfile.txt
gridtype  = unstructured
gridsize  = ${nlines}
xname     = lon
xlongname = longitude
xunits    = degrees_east
yname     = lat
ylongname = latitude
yunits    = degrees_north
xsize     = ${nlines}
ysize     = ${nlines}
xvals     = ${lon}
yvals     = ${lat}
EOF

tail +2 $input | cut -d " " -f 3 > var.txt

cdo -r -f nc -settaxis,$time -setname,${varname} -input,gridfile.txt netCDF.nc < var.txt

-Karin

RE: Converting .csv to .nc - Added by Guilherme Martins almost 4 years ago

Dear Karin,

Very cool your example.

I tried to run the script with some modifications, but I'm getting an error. Could you help?

The script and the input file are attached.

RE: Converting .csv to .nc - Added by Guilherme Martins almost 4 years ago

Sorry,

The message shown is:

cdo(2) input (Abort):

RE: Converting .csv to .nc - Added by Karin Meier-Fleischer almost 4 years ago

Hi Guilherme,

you changed the column selecting part from cut to awk, so you have to change the lines

numlines=$(cat input.csv | wc -l)
nlines=$(($numlines-1))

to just

nlines=$(cat input.csv | wc -l)

-Karin

RE: Converting .csv to .nc - Added by Guilherme Martins almost 4 years ago

Thanks Karin,

The file was successfully generated

Guilherme.

    (1-5/5)