Import netCDF file to Pandas dataframe

后端 未结 3 1259
梦如初夏
梦如初夏 2020-12-14 02:16

Merry Christmas! I am still very new to Python and Pandas, so any help is appreciated. I am trying to read in a netCDF file, which I can do and then import that into a Pand

3条回答
  •  庸人自扰
    2020-12-14 03:00

    If your NetCDF file (or OPeNDAP dataset) follows CF Metadata conventions you can take advantage of them by using the NetCDF4-Python package, which makes accessing them in Pandas really easy. (I'm using the Enthought Python Distribution which includes both Pandas and NetCDF4-Python).

    In the example below, the NetCDF file is being served via OPeNDAP, and the NetCDF4-Python library lets you open and work with a remote OPeNDAP dataset just as if it was a local NetCDF file, which is pretty slick. If you want to see the attributes of the NetCDF4 file, point your browser at this link http://geoport-dev.whoi.edu/thredds/dodsC/HUDSON_SVALLEY/5951adc-a1h.nc.html

    You should be able to run this without changes:

    from matplotlib import pyplot as plt
    import pandas as pd
    import netCDF4
    
    url='http://geoport-dev.whoi.edu/thredds/dodsC/HUDSON_SVALLEY/5951adc-a1h.nc'
    vname = 'Tx_1211'
    station = 0
    
    nc = netCDF4.Dataset(url)
    h = nc.variables[vname]
    times = nc.variables['time']
    jd = netCDF4.num2date(times[:],times.units)
    hs = pd.Series(h[:,station],index=jd)
    
    fig = plt.figure(figsize=(12,4))
    ax = fig.add_subplot(111)
    hs.plot(ax=ax,title='%s at %s' % (h.long_name,nc.id))
    ax.set_ylabel(h.units)
    

    The result may be seen here in the Ipython Notebook: http://nbviewer.ipython.org/4615153/

提交回复
热议问题