1

I have a NetCDF file with this info:

dimensions:

lon = 238;
lat = 132;
dep = 38;
time = 8;

variables:

float lon(lon=238);
float lat(lat=132);
float dep(dep=38);
double time(time=8);
float eastward_sea_water_velocity(time=8, dep=38, lat=132, lon=238);
float northward_sea_water_velocity(time=8, dep=38, lat=132, lon=238);

I want to read lon, lat, eastward_sea_water_velocity and northward_sea_water_velocity, and write the values to csv file.

So the csv file will be like that:

Lon Lat E-Vel N-Vel

28.4511 41.8866 -3.7 -6.3

Till now, I succeeded only to read the lon and lat, and write them to csv:

x,y = ncfile.variables['lon'], ncfile.variables['lat']
import csv
with open('text2.csv', 'w') as f:
    writer = csv.writer(f, delimiter='\t')
    header = ['Longitude', 'Latitude']
    writer.writerow(header)
    writer.writerows(zip(x,y))

f.close()

When I tried to print the values from 'eastward_sea_water_velocity',with this code:

temp = ncfile.variables['eastward_sea_water_velocity']
print temp

my output was:

<type 'netCDF4.Variable'>
float32 eastward_sea_water_velocity(time, dep, lat, lon)
_FillValue: -999.0
missing_value: -999.0
units: m s-1
long_name: u-component of current
standard_name: eastward_sea_water_velocity
scale_factor: 0.01
add_offset: 0.0
source: MHI NASU Hydrodinamical model version V2
valid_min: -5.0
valid_max: 5.0
unlimited dimensions: 
current shape = (8, 38, 132, 238)

So , how do I read the values from the variable 'eastward_sea_water_velocity' ?

Thank you very much in advance ! All the best !

tuxman
  • 81
  • 1
  • 1
  • 5

3 Answers3

1

Longitude and latitude are 1D variables that represent the horizontal coordinates that represent the x and y axes of your multi-dimensional data. What zip() does is take pairs of items from each list, so even though you got output from that statement, it didn't write all of the possible lon,lat pairs that are in your file. Since eastward_sea_water_velocity is a 4-dimensional, the problem is even greater. For simplicity, we'll assume you want only a 2D slice of this data at a fixed time and depth:

lon = ncfile.variables['lon']
lat = ncfile.variables['lat']
# Grab 2D slice for time index 0 and depth index 0
u = ncfile.variables['eastward_sea_water_velocity'][0, 0]
import csv
import numpy as np
with open('text2.csv', 'w') as f:
    writer = csv.writer(f, delimiter='\t')
    writer.writerow(['Longitude', 'Latitude', 'E-Vel'])
    for inds,val in np.ndenumerate(u):
      writer.writerow([lon[inds[0]], lat[inds[1]], val])
DopplerShift
  • 5,472
  • 1
  • 21
  • 20
  • This is the right way to do it, but there is a small bug in the last line: you want to pass a writerow argument as a list, i.e. `[lon[inds[0]], lat[inds[1]], val]`. – N1B4 Apr 30 '15 at 16:30
  • Fixed, thanks. (That's why I shouldn't write code in a text field.) – DopplerShift Apr 30 '15 at 19:48
0

Use print temp[:] or depending on the shape of the variable print temp[:,:,:,:]

Sadiq Huq
  • 118
  • 1
  • 5
0

When I tried to print with this:

print temp[:,:,:,:]

the output was:

[[[[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
..., 
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]]

But, I am sure that the variable has values, because I open the file with Panoply, and I see the values.

P.S.: Maybe Jules0080 from This post, can help me

Community
  • 1
  • 1
tuxman
  • 81
  • 1
  • 1
  • 5