栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

xgrads库在ctl转nc中存在的不足及修改

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

xgrads库在ctl转nc中存在的不足及修改

xgrads库在ctl转nc中存在的不足及修改 1、问题描述

ctl文件

dset C:Users马冠龙DesktoppythonEOFsstpx.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
endvars
from 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')

报错
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])
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/725368.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号