Python - Read data from netCDF file with time as seconds since beginning of measurement

2024/10/3 0:24:59

I need to extract values from a netCDf file. I am pretty new to python and even newer this file format. I need to extract time series data at a specific location (lat, lon). I have found that there is a variable (called "base_time") in UNIX time and another variable (called "time") with "seconds since 2013-20-10 00:00:00" (which is the beginning of measurement time in UTC) for now.

When i interrogate the dataset's variables, I get this:

<type 'netCDF4.Variable'>
int32 base_time()units: seconds since 1970-01-01 00:00:00 00:00
unlimited dimensions:
current shape = ()
filling off<type 'netCDF4.Variable'>
float64 time(time)units: seconds since 2013-10-20 00:00:00 00:00interval(sec): 30.0
unlimited dimensions: time
current shape = (2880,)
filling off

When I read the values as arrays, like e.g.

time_var = dataset.variables['time'][:]

I can see that there are 2880 (size is 2880) values in the time variable, but just one (size is 1) in the base_time variable. I think that this answer does exactly what I need, but I have only trouble with the part where I need to convert the time. When I do:

dtime = netCDF4.num2date(time_var[:],time_var.units)

I get the error:

AttributeError: 'numpy.ndarray' object has no attribute 'units'

And I think that I need to convert the time variable (seconds since beginning of measurement) anyway, instead of converting the UNIX time (because there is only one value in the netCDf file?). I tried some variations of the datetime.dateime part but I just don't get it. I only need to convert the "seconds since 2013-10-20 00:00:00" to a readable format to be able to extract und plot the data. Thanks!

Answer

Sorry for being verbose but this is something I've just recently run across and grasped.

In the python NetCDF4 api, which is based on numpy, there is a profound difference between a NetCDF4.Variable and the numpy data array which it contains. Your code:

time_var = dataset.variables['time'][:]

is not a NetCDF4 variable, i.e. not a time_var but just the data values, a numpy ndarray of numbers, the NetCDF variable attributes are lost, in this case units:

units: seconds since 2013-10-20 00:00:00 00:00.

What you want is:

time_var = dataset.variables['time']

Then:

dtime = netCDF4.num2date(time_var[:],time_var.units)

Should work as expected.

https://en.xdnf.cn/q/70792.html

Related Q&A

PyQt Multiline Text Input Box

I am working with PyQt and am attempting to build a multiline text input box for users. However, when I run the code below, I get a box that only allows for a single line of text to be entered. How to …

Calculate the sum of model properties in Django

I have a model Order which has a property that calculates an order_total based on OrderItems linked by foreign key.I would like to calculate the sum of a number of Order instances order_total propertie…

Set Host-header when using Python and urllib2

Im using my own resolver and would like to use urllib2 to just connect to the IP (no resolving in urllib2) and I would like set the HTTP Host-header myself. But urllib2 is just ignoring my Host-header:…

Full-featured date and time library

Im wondering if anyone knows of a good date and time library that has correctly-implemented features like the following:Microsecond resolution Daylight savings Example: it knows that 2:30am did not exi…

Mean of a correlation matrix - pandas data fram

I have a large correlation matrix in a pandas python DataFrame: df (342, 342).How do I take the mean, sd, etc. of all of the numbers in the upper triangle not including the 1s along the diagonal?Thank…

How to set imshow scale

Im fed up with matplotlib in that its so hard to plot images in specified size.Ive two images in 32*32, 20*20 sizes. I just want to plot them in its original size, or in proportion to its original size…

Python distutils gcc path

Im trying to cross-compile the pycrypto package, and Im getting closer and closer however, Ive hit an issue I just cant figure out.I want distutils to use the cross-compile specific gcc- so I set the C…

TypeError: builtin_function_or_method object has no attribute __getitem__

Ive got simple python functions.def readMainTemplate(templateFile):template = open(templateFile, r)data = template.read()index1 = data.index[[] #originally I passed it into data[]index2 = data.index[]]…

Extract currency amount from string in Python

Im making a program that takes currency from a string and converts it in to other currencies. For example, if the string was the car cost me $13,250 I would need to get $ and 13250. I have this regex a…

Error: The elasticsearch backend requires the installation of requests. How do I fix it?

Im having a issue when I ran "python manage.py rebuild_index" in my app supported by haystack and elasticsearch.Python 2.7 Django version 1.6.2 Haystack 2.1.0 Elasticsearch 1.0Please see the …