Project

General

Profile

Need help converting ascii to netcdf files

Added by Mengli Wei over 8 years ago

Hello to all,

I'm trying to build a hydrologic model, for which I downloaded the daily evapotranspiration data from DWD, all files in ascii format, in the form of a matrix of 654x866 pixels, 1 km cell size. I attached the description file and an ascii file.

I would very much appreciate it if anyone could tell me how to convert this ascii file into a netcdf file. I tried many times using the input command, but the problem is that I don't know how to set up a valid grid or a grid description for my purpose.
At first I used the command "cdo -f nc input,r654x866 out.nc in.asc", however, it turned out to be totoally wrong. I lost all the data after using sellonlatbox command.
Then I generated a grid file, which is also attached (mygrid), hoping the xvals and yvals could appear automatically. Proved me wrong again. I got the nc file with all the data but without lat lon. Neither setgrid nor setgridtype could help in this situation.
I also tried using another grid description, which I got with the griddes command applied to a precipitation file of the entire Germany, with both xsize and ysize = 1200. It didn't work because of "too few input elements".
Now I am stuck... Any suggestions would be so much appreciated!

Many thanks in advance and best wishes,
Mengli


Replies (4)

RE: Need help converting ascii to netcdf files - Added by Ralf Mueller over 8 years ago

Examples for griddes files can be created with the griddes operators:

ram@luthien:~ cdo griddes -remapnn,r180x80 -topo                                     [11:33:03|15-09-10]
cdo griddes: Started child process "remapnn,r180x80 -topo (pipe1.1)".
cdo(2) remapnn: Started child process "topo (pipe2.1)".
cdo(3) topo: Set default filetype to GRIB
#
# gridID 1
#
gridtype  = lonlat
gridsize  = 14400
xname     = lon
xlongname = longitude
xunits    = degrees_east
yname     = lat
ylongname = latitude
yunits    = degrees_north
xsize     = 180
ysize     = 80
xfirst    = 0
xinc      = 2
yfirst    = -88.875
yinc      = 2.25

For a curviliear grid, this looks different, see attachement:curvi-griddes.txt
All locations have to be given in lon lat, not in meters. If you do not want to regrid, compute area weights or anything that involves coordinated, they might be set some unrealistic values. But pay attention on what operators you're using.

If that does not work, I recomment to write the netcdf manually with e.g. python-netcdf4 bindings: https://netcdf4-python.googlecode.com/svn/trunk/docs/netCDF4-module.html or http://www-pord.ucsd.edu/~cjiang/python.html

RE: Need help converting ascii to netcdf files - Added by Mengli Wei over 8 years ago

Hello Ralf,

thank you for your quick answer!

I did the conversion again with following command:
cdo -f nc input,grid 19910101.nc < grids_germany_daily_evapo_p_19910101.asc
It turned out that the data were kept, somehow the nc file generated 322 timesteps, which is weird, because the original data is daily.

Could you please have a look at attached files to see what I did wrong? Many many thanks for your help!

Best wishes,
Mengli

RE: Need help converting ascii to netcdf files - Added by Ralf Mueller over 8 years ago

you text input describes

  1. a single timestep of a 654x866 gridpoints grid or
  2. 866 timesteps on a 654 points grid or
  3. 654 timesteps on a 866 points grid or ....

your grid desciption defines a 42x42 points grid

IMO this does not fit together.

I did some tests and wrote in little python script, which reads in your text file

You can just call

python txt2nc.py

I changed the header information in the txt input, but not much

hth
ralf

fromTxt.nc (2.17 MB) fromTxt.nc output of python script
in.txt (3.24 MB) in.txt little reformated text input
txt2nc.py (640 Bytes) txt2nc.py python script to create the nc file

RE: Need help converting ascii to netcdf files - Added by Mengli Wei over 8 years ago

Hi Ralf,

thank you, your script helped me a great deal! It's very kind of you :)

Best wishes,
Mengli

    (1-4/4)