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’
出现这种情况是因为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])



