简体   繁体   中英

Write time date series using in NetCDF python

I have created a time series using pandas like this:

date_series_min = pd.date_range(yy+'-'+mm, periods=44640, freq='T')

Where yy are years (ex: 2001) and mm are months (ex: 01) from a file name. then I used the method indicated above to create a times series of dates each minute. I see that the variable type for this series is:

<class 'pandas.core.indexes.datetimes.DatetimeIndex'>

How I could write this time series in a netCDF files

Thanks in advance

I made a script like this, where I use netCDF4 library to save the netCDf file:

#!/usr/bin/env ipython
# ---------------------
import pandas as pd
from netCDF4 import Dataset,num2date,date2num
import datetime
# ----------------------
yy = '2001';mm='01';
date_series_min = pd.date_range(yy+'-'+mm, periods=44640, freq='T') # This is DatetimeIndex, perhaps ordinary datetime objects are preferred...
datesout = date_series_min.to_pydatetime() 
# ----------------------
unout = 'days since 2001-01-01 00:00:00'
fileout = 'test.nc'
# ----------------------
with Dataset(fileout,'w','NETCDF3') as ncout:
    ncout.createDimension('time',None);
    timevar = ncout.createVariable('time','float32',('time'));timevar.setncattr('units',unout);
    timevar[:]= date2num(datesout,unout);

The output from console ( ncdump -h $fileout ) is like this:

netcdf test {
 dimensions:    time = UNLIMITED ; // (44640 currently) 
 variables:     
     float time(time) ;         
           time:units = "days since 2001-01-01 00:00:00" ; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM