0

So for the first time I am working with a NetCDF format and I need to use the ncdump command. I just downloaded Anaconda on my new laptop and used conda install netcdf4 to work with the NetCDF format. Why can't I use ncdump? Do I also need to install it or am I doing something else wrong? What I'm doing is:

import ncdump from ncdump 

Which gives the error:

No module named ncdump

Bart
  • 9,825
  • 5
  • 47
  • 73
Ralph
  • 11
  • 1
  • 1
  • 1
    `ncdump` is a command-line tools from NetCDF, you can't use it like that in Python, and to use it on the command line you have to install NetCDF (not the Python NetCDF4 package), for example with your normal package manager. What kind of OS are you using (Windows, Linux, ..)? – Bart May 02 '18 at 04:34
  • https://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-install.html – Bart May 02 '18 at 04:35
  • Thank you for your answer! This helped me further https://stackoverflow.com/questions/1449494/how-do-i-install-python-packages-on-windows – Ralph May 02 '18 at 07:10

1 Answers1

1

A friend helped me with this problem.... Copy the following code to a file named ncdump.py and put the file in the dir you run your python script.

from netCDF4 import Dataset
import glob

def ncdump(nc_fid, verb=True):
    '''
    ncdump outputs dimensions, variables and their attribute information.
    The information is similar to that of NCAR's ncdump utility.
    ncdump requires a valid instance of Dataset.

    Parameters
    ----------
    nc_fid : netCDF4.Dataset
        A netCDF4 dateset object
    verb : Boolean
        whether or not nc_attrs, nc_dims, and nc_vars are printed

    Returns
    -------
    nc_attrs : list
        A Python list of the NetCDF file global attributes
    nc_dims : list
        A Python list of the NetCDF file dimensions
    nc_vars : list
        A Python list of the NetCDF file variables
    '''
    def print_ncattr(key):
        """
        Prints the NetCDF file attributes for a given key

        Parameters
        ----------
        key : unicode
            a valid netCDF4.Dataset.variables key
        """
        try:
            print("\t\ttype:", repr(nc_fid.variables[key].dtype))
            for ncattr in nc_fid.variables[key].ncattrs():
                print('\t\t%s:' % ncattr,\
                      repr(nc_fid.variables[key].getncattr(ncattr)))
        except KeyError:
            print("\t\tWARNING: %s does not contain variable attributes" % key)

    # NetCDF global attributes
    nc_attrs = nc_fid.ncattrs()
    if verb:
        print ("NetCDF Global Attributes:")
        for nc_attr in nc_attrs:
            print('\t%s:' % nc_attr, repr(nc_fid.getncattr(nc_attr)))
    nc_dims = [dim for dim in nc_fid.dimensions]  # list of nc dimensions
    # Dimension shape information.
    if verb:
        print("NetCDF dimension information:")
        for dim in nc_dims:
            print("\tName:", dim)
            print("\t\tsize:", len(nc_fid.dimensions[dim]))
            print_ncattr(dim)
    # Variable information.
    nc_vars = [var for var in nc_fid.variables]  # list of nc variables
    if verb:
        print("NetCDF variable information:")
        for var in nc_vars:
            if var not in nc_dims:
                print('\tName:', var)
                print("\t\tdimensions:", nc_fid.variables[var].dimensions)
                print("\t\tsize:", nc_fid.variables[var].size)
                print_ncattr(var)
    return nc_attrs, nc_dims, nc_vars

import sys
import os
import fileinput


ncfiles = list(glob.glob('W:/kimberley/Vectors' + '/*.nc'))
for ifile in range(len(ncfiles)):
            sfile = Dataset(ncfiles[ifile],mode='r', format='NETCDF4')
            base, extension = os.path.splitext(ncfiles[ifile])
            output =  base + '_readme.txt'
            f = open(output, 'w')
            sys.stdout = f
            ncdump(sfile)
            f.close()
orig_stdout = sys.stdout

Now in your code you can write: import ncdump from ncdump

It will work!

thaliaxen
  • 11
  • 1