【问题标题】:How to stop xarray from automatically changing time attributes when writing out a netcdf file?写出netcdf文件时如何阻止xarray自动更改时间属性?
【发布时间】:2019-02-08 01:17:59
【问题描述】:

我注意到,在将多个年度 NetCDF 文件连接到一个文件或将时间序列文件拆分为年度组时,xarray 的 .to_netcdf() 会自动更新时间单位。我的意思的例子

# time attribute of the file
ncdump -h file_1970_2017.nc
>>double time(time) ;
    time:_FillValue = NaN ;
    time:units = "Hours since 1900-01-01T00:00:00+00:00" ;
    time:calendar = "proleptic_gregorian" ;

#  after splitting the files into yearly files using group-by method the time attribute is automatically modified

# example
ncdump -h file_splitted_2005.nc
>>double time(time) ;
    time:_FillValue = NaN ;
    time:units = "Hours since 2005-01-01T00:00:00+00:00" ;
    time:calendar = "proleptic_gregorian" ;

反之亦然,即当我将单个年度文件连接到一个公共文件时,也会遇到同样的问题。有什么方法可以强制它不改变时间属性吗?从documentation 看来,“编码”参数可能会有所帮助,但我不知道如何?

【问题讨论】:

    标签: python-3.x netcdf python-xarray


    【解决方案1】:

    想通了。使用编码参数作为嵌套字典可以实现这一点

    # when writing out the dataset ds encoding can be used as
    ds.to_netcdf('file_splitted_2005.nc', encoding={'my_variable':{'_FillValue': -999.0},'time':{'units': "seconds since 1900-01-01 00:00:00"}})
    

    如果我理解正确,在写出数据时,如果我们的时间数组是日期时间对象,xarray 将根据我们指定的单位属性自动重新计算时间值。在这种情况下,它在后台使用智能日期时间功能。这意味着我也可以指定

    'time':{'units': "seconds since 2000-01-01 00:00:00"}
    

    它会自动重新计算它存储在时间数组中的值,让我们的生活更轻松。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 2022-06-28
      相关资源
      最近更新 更多