I am trying to take one variable from a netCDF file and print it. here is my code
import netCDF4
import netCDF4_utils
from netCDF4 import Dataset
from numpy.random import uniform
import csv
B = []
rootgrp = Dataset('test.cdf', 'r', format = 'NETCDF4')
f = open('testoutput.csv','wb')
b = (rootgrp.variables['grid_optical_depth'][:])
for x in b:
B.append(x)
f.write(str(B))
rootgrp.close()
f.close()
when I run this I get a very large set of data that seems to be repeating, but I don't see how my for loop is doing that, shouldn't it only run through the data set once?
also could anyone speak to why the data prints out in sets of four per line? If I run print rootgrp.variables['grid_optical_depth'] I get
<type 'netCDF4.Variable'>
float32 grid_optical_depth(grid_time, range)
long_name: Grid_Aerosol_Optical_Depth_Profile
units: (n/a)
temporal_average: 20.0
unlimited dimensions:
current shape = (1440, 399)
so does that mean that two of the numbers correspond to the grid_time and rage value? I don't think this is the case because all the numbers are much smaller then 1 (on the order of 10^-3 and -4).
Any help is appreciated
b = (rootgrp.variables['grid_optical_depth'][:,:])from [stackoverflow.com/questions/16641437/… got me the same answer I was getting before(rootgrp.variables['grid_optical_depth'][:])directly to a file