[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[python #MBM-874792]: NetCDF viewer recommendation



Greetings!

That math can definitely be tricky, which is why cftime is so handy:

    import cftime
    dt = cftime.num2date(738004.5, 'days since 1-1-1 00:00:0.0')
    print(dt)

gives "2021-08-01 12:00:00". `num2date` also supports being given an entire 
array of times that you get from the file, and you can avoid hard-coding the 
units with:

    import cftime
    from netCDF4 import Dataset

    nc = 
Dataset('http://nomads.ncep.noaa.gov:80/dods/gfs_0p25/gfs20210802/gfs_0p25_00z')
    time_var = nc.variables['time']
    time_vals = cftime.num2date(time_var[:], time_var.units)

Hope this helps!

Ryan

> One other note on this subject netcdf time.
> 
> I guess I got to learn to use cftime to convert my netcdf to units that make 
> sense. Looking at the GRADS server I finally understand what the unites mean
> and also what my numbers I see are for.
> 
> My start time unit is 738004.5, which was my start unit for Aug 1, 2021.
> 
> I did the math below. I got close enough, so I'm missing a leap day her or 
> there, but getting close.
> 
> Don't need to read below, but do you have an example of using cftime to 
> convert number of days since the beginning of time
> to our datetime object?
> 


Ticket Details
===================
Ticket ID: MBM-874792
Department: Support Python
Priority: Low
Status: Closed
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.