爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 6281|回复: 2

[经验总结] xgrads库在ctl转nc中存在的不足及修改

[复制链接]

新浪微博达人勋

发表于 2022-1-23 22:19:38 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
xgrads库在ctl转nc中存在的不足及修改
1、问题描述
ctl文件
dset C:\Users\马冠龙\Desktop\python\EOF\sstpx.grd
title Coads SSTA
undef -999.0
xdef 18 linear 120 10
ydef 12 linear -27.5 5
zdef 1 linear 1000 1
tdef 516 linear 1jan1948 1month
vars 1
S    0 99  Coads SST anomaly interperated using
endvarsfrom xgrads import CtlDescriptor, open_CtlDataset
​
#ctl数据转nc
ds = open_CtlDataset(r'SSTPX.ctl',encoding='utf-8')
ctl = CtlDescriptor(file=r'SSTPX.ctl',encoding='utf-8')
ds.attrs['pdef' ] = 'None'
ds.to_netcdf('E:SSTPX.nc')
报错
ValueError: invalid literal for int() with base 10: '1mon'
2、解决方案
出现这种情况是因为xgrads库中没有考虑到 month year minute等情况,所以我们在字典中加入,并做简单修改
原始库代码见下图
修改代码为:
def GrADS_increment_to_timedelta64(incre):
    """
    Convert GrADS time increment string to numpy.timedelta64
   
    Parameters
    ----------
    incre : str
        Grads time increment in str format e.g., 1dy.
   
    Returns
    ----------
        re : timedelta64
    """
    amount = re.sub("\D", "", incre)
    unit = re.sub(amount, "", incre)
​
    unitDict = {
        'se': 's',
        'mn': 'm',
        'hr': 'h',
        'dy': 'D',
        'mo': 'M',
        'yr': 'Y',
        'second':'s',
        'minute':'m',
        'hour':'h',
        'day':'d',
        'month':'M',
        'year':'Y'}
​
    return timedelta64(int(amount), unitDict[unit])


image-20220123220519115.png
image-20220123220016561.png
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2022-1-24 22:21:59 | 显示全部楼层
用cdo一行代码解决
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2022-1-25 19:44:27 来自手机 | 显示全部楼层

确实,cdo超级好用
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表