Thanks for your comments. I decided to give python a go (newbie), took a while
to get setup I'm now closer to a solution.
I've tried ncdump and nc2text I need the data formatted to the CSV in a
specific format (input to a program).
My main question is after I have written the coordinates how to find the
correct data from sfc (variable 'MIN_SFC') for the lat/long and date?
I've written the header to the file, looking into parsing and saving the dates
instead of date/time.
Latitude Longitude Date1 Date2 Date3 etc.
I wrote the first set of coordinates Lat/long to cells A2, B2 of the CSV, when
I tried to create a function to loop through I'm getting a ValueError: I/O
operation on closed file. The file close for the CSV is at the end of the
script.
Thanks!
[Code]
from netCDF4 import Dataset;
from netCDF4 import num2date;
filename = "C:/filename.nc"nc = Dataset(filename, 'r', Format='NETCDF4')
print nc.variables
print 'Variable List'
for i in nc.variables:
print [i, nc.variables[i].units, nc.variables[i].shape]
# get coordinates variables
lats = nc.variables['latitude'][:]
lons = nc.variables['longitude'][:]
try:
assert(nc.variables['latitude'].units == 'degrees_north')
except:
raise AttributeError('latitude units attribute not what was expected')
try:
assert(nc.variables['longitude'].units == 'degrees_east')
except:
raise AttributeError('longitude units attribute not what was expected')
sfc= nc.variables['Min_SFC'][:]
times = nc.variables['time'][:]
# convert time
print "Converting Dates"
units = nc.variables['time'].units
dates = num2date (times[:], units=units, calendar='365_day')
#print [dates.strftime('%Y%m%d%H') for date in dates]
header = ['Latitude', 'Longitude']
# append dates to header stringfor d in dates:
print d
header.append(d)
# write to file
import csv
with open('Output.csv', 'wb') as csvFile:
outputwriter = csv.writer(csvFile, delimiter=',')
outputwriter.writerow(header)
outputwriter.writerow(content)
def WriteContent(outputwriter, content):
outputwriter.writerow(content) return
for l in lons:
content =[lats[l], lons[l]]
WriteContent(outputwriter, content)
# close the output file
csvFile.close()
# close netcdf
nc.close()
[/Code]
From: chris.barker@xxxxxxxx
Date: Tue, 3 Feb 2015 08:34:20 -0800
Subject: Re: [netcdfgroup] Read NETCDF using Java library
To: jules0080@xxxxxxxxxxx
CC: netcdfgroup@xxxxxxxxxxxxxxxx
On Mon, Feb 2, 2015 at 9:03 PM, Jules A <jules0080@xxxxxxxxxxx> wrote:
I'm having troubles accessing data from a NetCDF file.
My aim is to access the data and write to a CSV file in the following format:
Latitude Longitude Date1 Date2 Date 3
<-- MIN_SFC values -->
are you committed to using Java? This would be pretty easy with Python and the
NetcCDF4 python package (http://unidata.github.io/netcdf4-python/) (or IRIS)
Though this should be pretty straightforward with Java, too.
I have been able to iterate through the MIN_SFC Variable, I was going to save
to a dictionary or hash table then realised I don't know which date it belongs
to or Lat/long and vise versa.
Dimensions:
time = 7 // the date field how to convert to a date?
latitude = 292
longitude =341
that means there is dimension for time with 7 items -- so there are 7 time
steps in the file.
There should be a variable called something like "time", with units like:
"hours since 2015-02-02T01:43:19Z"
you will want to convert those units to a regular time stamp -- the python
package has a utility to do this -- probably netcdfJava does too.
Variables:
float MIN_SFC (time=7, latitude = 292, longitude = 341)
you'll be helped by the "ncdump" utility -- it will dump a netcdf file in a
plain text form, making it esy to see what's in there. I suggest:
ncdump -h the_file_name.nc
that will dump only the heady, which is probably what you want.
HTH,
-Chris
I've looked through the online resources, please post a comment or link. I
found the ReadNetCDF example useful however it was for version 1.0 and had
quite a few deprecated methods.
Thanks!
<code>
List<Variable> variableList = ncfile.getVariables();
Array data;
for(int j = 0; j < variableList.size(); j++){
Variable r = variableList.get(j);
System.out.println(r.getFullName());
System.out.println(r.getDatasetLocation());
System.out.println(r.getNameAndDimensions());
System.out.println(r.getDataType());
System.out.println(r.getElementSize());
System.out.println("-------------")
data = r.read(); int[] shape = data.getShape();
Index index = data.getIndex();
for (int u=0; u<shape[0]; u++) {
for (int x=0; x<shape[1]; x++) {
double dval = data.getDouble(index.set(u,x));
}}}
</code>
_______________________________________________
netcdfgroup mailing list
netcdfgroup@xxxxxxxxxxxxxxxx
For list information or to unsubscribe, visit:
http://www.unidata.ucar.edu/mailing_lists/
--
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