3

So, I'm using a function from this website to (try) to make stick plots of some netCDF4 data. There is an excerpt of my code below. I got my data from here.

The stick_plot(time,u,v) function is EXACTLY as it appears in the website I linked which is why I did not show a copy of that function below.

When I run my code I get the following error. Any idea on how to get around this?

AttributeError: 'numpy.float64' object has no attribute 'toordinal'

The description of time from the netCDF4 file:

<type 'netCDF4._netCDF4.Variable'>
float64 time(time)
    long_name: time
    standard_name: time
    units: days since 1900-01-01 00:00:00Z
    axis: T
    ancillary_variables: time_quality_flag
    data_min: 2447443.375
    data_max: 2448005.16667
unlimited dimensions: 
current shape = (13484,)
filling off

Here is an excerpt of my code:

imports:

import matplotlib.pyplot as plot
import numpy as np
from netCDF4 import Dataset
import os
from matplotlib.dates import date2num
from datetime import datetime

trying to generate the plots:

path = '/Users/Kyle/Documents/Summer_Research/east_coast_currents/'
currents = [x for x in os.listdir('%s' %(path)) if '.DS' not in x]

for datum in currents:
    working_data = Dataset('%s' %(path+datum), 'r', format = 'NETCDF4')
    u = working_data.variables['u'][:][:100]
    v = working_data.variables['v'][:][:100]
    time = working_data.variables['time'][:][:100]
    q = stick_plot(time,u,v)
    ref = 1
    qk = plot.quiverkey(q, 0.1, 0.85, ref,
                      "%s N m$^{-2}$" % ref,
                      labelpos='N', coordinates='axes')

    _ = plot.xticks(rotation=70)    
user3666197
  • 1
  • 6
  • 50
  • 92
K. Shores
  • 875
  • 1
  • 18
  • 46
  • 2
    It sounds like you're calling `date2num` on a float. `date2num` expects `datetime` objects. However, you're in luck. Your time field in your NetCDF file appears to already be in exactly the format that `date2num` would be converting it do. Replace `date2num(time)` with just `time` in your `stick_plot` function and you should be good to go. – Joe Kington Sep 25 '15 at 19:09
  • Oh, thanks! You are correct! @JoeKington – K. Shores Sep 25 '15 at 19:48
  • Now, how do I mark this question as answered? – K. Shores Sep 25 '15 at 19:49
  • If @JoeKington wants to add an answer, you can accept it and upvote it to show that it's solved. If not, you could self-answer and accept it (attributing the solution to Joe's comment :) ). I think that's worth doing, as this information might be useful to others in the future. – J Richard Snape Sep 25 '15 at 23:23
  • Yes, datetime are not the same - further details in >>> http://stackoverflow.com/a/32728754/3666197 – user3666197 Sep 26 '15 at 00:05

1 Answers1

2

Joe Kington answered my question. The netCDF4 file read the times in as a datetime object. All I had to do was replace date2num(time) with time which fixed everything.

K. Shores
  • 875
  • 1
  • 18
  • 46