<div dir="ltr"><div class="gmail_default" style="color:#000000"><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jun 5, 2013 at 9:07 PM, Sudheer Joseph <span dir="ltr"><<a href="mailto:sjo.india@gmail.com" target="_blank">sjo.india@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear Members,<br>
                  Is there a way to get the time:origin attribute from a netcdf file as string using the Python netcdf?<br></blockquote><div><br></div><div style="color:rgb(0,0,0)" class="gmail_default">Attributes of the NetCDF file and attributes of each of the variables can be accessed via the dot-operator, as per standard Python.</div>
<div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">For instance, suppose that your NetCDF file has a Conventions attribute, you can access it via:</div><div style="color:rgb(0,0,0)" class="gmail_default">
<br></div><div style="color:rgb(0,0,0)" class="gmail_default">ncfile.Conventions</div><div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">Suppose that your variable, time, has an attribute "origin", you can get it via:</div>
<div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">ncfile.variables['time'].origin</div><div style="color:rgb(0,0,0)" class="gmail_default"><br></div>
<div style="color:rgb(0,0,0)" class="gmail_default">Of course there's the question of what NetCDF bindings you're going to use.  The options that I'm familiar with are the ScientificPython's NetCDFFile class (Scientific.IO.NetCDF.NetCDFFile), pynetcdf (which is just the ScientificPython's class in a standalone format), and the netCDF4 package.  Each option has a similar API with attributes accessed the same way.</div>
<div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">An example with netCDF4 (which is newer, has NetCDF 4 capabilities, and appears to be more supported):</div>
<div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">from netCDF4 import Dataset</div><div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">
ncfile = Dataset('<a href="http://my_netcdf_file.nc">my_netcdf_file.nc</a>', 'r')</div><div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">origin = ncfile.variables['time'].origin</div>
<div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">etc. etc.</div><div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">
The variables and dimensions of a NetCDF file are stored in dictionaries, and the data from variables are accessible via slicing:</div><div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">
time_data = ncfile.variables['time'][:]</div><div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">The slice returns a numpy ndarray.</div><div style="color:rgb(0,0,0)" class="gmail_default">
<br></div><div style="color:rgb(0,0,0)" class="gmail_default">HTH,</div><div style="color:rgb(0,0,0)" class="gmail_default">Jason</div></div></div></div>