Skip to content

Commit 7e5cd91

Browse files
committed
fixed bugs in template processing
1 parent 82802f2 commit 7e5cd91

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[![Publish to PyPI](https://github.com/miniufo/xgrads/actions/workflows/PyPI-publish.yml/badge.svg)](https://github.com/miniufo/xgrads/actions/workflows/PyPI-publish.yml)
88
[![pytest](https://github.com/miniufo/xgrads/actions/workflows/tests.yml/badge.svg)](https://github.com/miniufo/xgrads/actions/workflows/tests.yml)
99
[![Build Status](https://app.travis-ci.com/miniufo/xgrads.svg?branch=master)](https://app.travis-ci.com/miniufo/xgrads)
10+
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/f86676a904ef4ebfa4c2f9cda46264f5)](https://app.codacy.com/gh/miniufo/xgrads/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
1011

1112
![3D plot](https://raw.githubusercontent.com/miniufo/xgrads/master/pics/3D.png)
1213

@@ -26,6 +27,11 @@ This python package [`xgrads`](https://github.com/miniufo/xgrads) is designed fo
2627
pip install xgrads
2728
```
2829

30+
**Install via conda**
31+
```
32+
conda install -c conda-forge xgrads
33+
```
34+
2935
**Install from github**
3036
```
3137
git clone https://github.com/miniufo/xgrads.git

tests/test_openDataset.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""
88
import numpy as np
99
import xarray as xr
10-
import sys
1110
from xgrads import open_CtlDataset, open_mfdataset
1211

1312

xgrads/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
from .utils import interp_to_latlon, get_coordinates_from_PDEF, \
55
get_data_projection, oacressman
66

7-
__version__ = "0.2.6"
7+
__version__ = "0.2.7"

xgrads/core.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
from numpy import datetime64, timedelta64
1212

1313

14+
template_patterns = [
15+
r'%x1', r'%x3', r'%y2', r'%y4', r'%m1', r'%m2',
16+
r'%mc', r'%d1', r'%d2', r'%h1', r'%h2', r'%h3',
17+
r'%n2', r'%f2', r'%f3', r'%fn2', r'%fhn', r'%fdhn',
18+
r'%j3', r'%t1', r'%t2', r'%t3', r'%t4', r'%t5',
19+
r'%t6', r'%tm1', r'%tm2', r'%tm3', r'%tm4', r'%tm5', r'%tm6'
20+
]
21+
1422
"""
1523
Core classes are defined below
1624
"""
@@ -243,26 +251,20 @@ def _processDSets(self, dpath_str):
243251
if strPos == -1:
244252
raise Exception('template is used in ctl but no % in dset')
245253

246-
endPos = len(dpath_str) - dpath_str[::-1].find('.') - 1
247-
# endPos = len(dpath_str) if endPos == -1 else endPos
248-
249-
template = dpath_str[strPos:endPos]
254+
matches = find_patterns(dpath_str)
255+
fmtO = ''.join(matches)
250256

251-
tokens = [self._get_template_format('%'+token)
252-
for token in filter(lambda x: x != '', template.split('%'))]
253-
# tokens = []
254-
# for token in filter(lambda x: x != '', template.split('%')):
255-
# tokens.append(self._get_template_format('%' + token))
256-
fmt = ''.join(tokens)
257+
tokens = [self._get_template_format(token) for token in matches]
258+
fmtN = ''.join(tokens)
257259

258260
fileList = []
259261

260262
times = self.tdef.samples
261263
base = self._get_field(times[0])
262264
for l in range(len(times)):
263-
part = times[l].item().strftime(fmt)
265+
part = times[l].astype('datetime64[s]').item().strftime(fmtN)
264266

265-
fname = dpath_str[:strPos] + part + dpath_str[endPos:]
267+
fname = dpath_str.replace(fmtO, part)
266268
fname = self._replace_forecast_template(fname, l, base)
267269

268270
# remove duplicated file
@@ -622,16 +624,10 @@ def _get_template_format(self, part):
622624
str
623625
The format in python datetime
624626
"""
625-
template_cases = ['%x1', '%x3', '%y2', '%y4', '%m1', '%m2',
626-
'%mc', '%d1', '%d2', '%h1', '%h2', '%h3',
627-
'%n2', '%f2', '%f3', '%fn2', '%fhn', '%fdhn',
628-
'%j3', '%t1', '%t2', '%t3', '%t4', '%t5',
629-
'%t6', '%tm1', '%tm2', '%tm3', '%tm4', '%tm5', '%tm6']
630-
631-
for c in template_cases:
627+
for c in template_patterns:
632628
if c in part:
633629
length = len(c)
634-
fmt = part[:length] # format in template_cases
630+
fmt = part[:length] # format in template_patterns
635631
rem = part[length:] # remaining str in part
636632
break
637633
else:
@@ -779,7 +775,7 @@ def _times_to_array(self, strTime, incre, tnum):
779775
y, m = y+int((m+l-1)/12), int((m+l-1)%12)+1
780776
lst.append(start.replace(year=y, month=m))
781777

782-
return np.asarray(lst, dtype='datetime64[s]')
778+
return np.asarray(lst, dtype='datetime64[s]').astype('datetime64[ns]')
783779

784780
elif 'yr' in incre:
785781
start = GrADStime_to_datetime(strTime)
@@ -789,7 +785,7 @@ def _times_to_array(self, strTime, incre, tnum):
789785
y = start.year + l
790786
lst.append(start.replace(year=y))
791787

792-
return np.asarray(lst, dtype='datetime64[s]')
788+
return np.asarray(lst, dtype='datetime64[s]').astype('datetime64[ns]')
793789

794790
else:
795791
start = GrADStime_to_datetime64(strTime)
@@ -804,11 +800,11 @@ def _times_to_array(self, strTime, incre, tnum):
804800
lst.append(start)
805801
start += intv
806802

807-
return np.asarray(lst)
803+
return np.asarray(lst).astype('datetime64[ns]')
808804

809805
else:
810806

811-
return np.arange(start, start + intv * tnum, intv)
807+
return np.arange(start, start + intv * tnum, intv).astype('datetime64[ns]')
812808

813809
def __repr__(self):
814810
"""Print this class as a string"""
@@ -1146,5 +1142,20 @@ def GrADS_increment_to_timedelta64(incre):
11461142
return timedelta64(int(amount), unitDict[unit])
11471143

11481144

1149-
1145+
def find_patterns(template_path):
1146+
"""Find template patterns in a given path
1147+
1148+
Parameters
1149+
----------
1150+
template_path: str
1151+
A path containing template strings.
1152+
1153+
Returns
1154+
-------
1155+
re: list
1156+
matched strings
1157+
"""
1158+
pattern = re.compile('|'.join(template_patterns))
1159+
1160+
return pattern.findall(template_path)
11501161

0 commit comments

Comments
 (0)