简体   繁体   English

使用 NetCDF python 编写时间日期系列

[英]Write time date series using in NetCDF python

I have created a time series using pandas like this:我使用 pandas 创建了一个时间序列,如下所示:

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.其中 yy 是文件名中的年份(例如:2001),mm 是月份(例如:01)。 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'> <class 'pandas.core.indexes.datetimes.DatetimeIndex'>

How I could write this time series in a netCDF files我如何在 netCDF 文件中编写这个时间序列

Thanks in advance提前致谢

I made a script like this, where I use netCDF4 library to save the netCDf file:我做了一个这样的脚本,我使用 netCDF4 库来保存 netCDf 文件:

#!/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:来自控制台( ncdump -h $fileout )的 output 是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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