Re: [netcdfgroup] Need help to convert .csv files to netCDF files

On Tue, Mar 10, 2015 at 5:50 PM, Dumindu Jayasekera <
d.jayasekera@xxxxxxxxxxxxxxxxx> wrote:

> Thanks again Chris.
>
> I think I dont need to core metadata. I was able to produce the .nc file
> using the code below (as you suggested).
>
>
As Rich suggests, you may   be better off using onf the libraries
suggested, Iris, in particular will get all the complex CF metadaat stuff
right for you.

But you're this close, so...


> But, how can I modify to rename the var1 = lat, var2 = lon, var3 =
> precipitation in the code below.?
>

 From the nc file you sent, it looks like you want the precipitiaon to be a
3-d array: (time X ltitude X longitude), so:


# this load the file into a Nx3 array (three columns)
> data = np.loadtxt('TEST_file.csv', delimiter=',')
>

note -- you dont have a 3-d array here, you have a 2-d array, which is
really three vectors -- you will need to re-shuffle these to get the 3-d
array you want, and the time, lat, and long vectors.... but assumign you've
done that:


you'll need three dimensions:
    time = ds.createDimension('time', num_times)
    lat = ds.createDimension('latitude', num_latitude)
    lon = ds.createDimension('longitude', num_longitude)

Then you want your 3-d precip variable -- I"d call it something more
readable than "r", but maybe you need that...

r = ds.createVariable('r', np.float32, ('time', 'latitude', 'longitude'))
var[:] = data[:,:,:]
## adds some attributes
var.units = 'mm'
var.long_name = 'Precipitation'
... and the others that you need.

HTH,

-Chris

-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@xxxxxxxx
  • 2015 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: