miniufo / xgrads

Parse and read ctl and associated binary file commonly used by GrADS into xarray
https://xgrads.readthedocs.io/
MIT License
71 stars 27 forks source link

Avoid forgetting to name and causing errors #40

Closed jesieleo closed 2 years ago

jesieleo commented 2 years ago

When reading such a ctl, it will report an error because we forgot to name the variable ValueError: not enough values to unpack (expected 4, got 3)

dset D:\pycharm_data\12all.dat
title uv,rh
undef -9.99e33
xdef 241 linear 0 0.25
ydef 241 linear 0 0.25
zdef 34 levels 1000 975 950 925 900 850 800 750 700 650 600 550 500 450 400 350 300 250 200 150 100 70 50 30 20 10 9 8 7 6 5 4 3 2
tdef 1 linear 06Z14OCT2010 360mn
vars 8
u 21 99 
v 21 99 
hgt 21 99 
vor 21 99 
div 21 99 
w 21 99 
q 21 99 
tem 21 99 
endvars

Change it to this and it will work normally

dset D:\pycharm_data\12all.dat
title uv,rh
undef -9.99e33
xdef 241 linear 0 0.25
ydef 241 linear 0 0.25
zdef 34 levels 1000 975 950 925 900 850 800 750 700 650 600 550 500 450 400 350 300 250 200 150 100 70 50 30 20 10 9 8 7 6 5 4 3 2
tdef 1 linear 06Z14OCT2010 360mn
vars 8
u 21 99 u
v 21 99 v
hgt 21 99 hgt 
vor 21 99 vor
div 21 99 div
w 21 99 w
q 21 99 q
tem 21 99 term 
endvars

This problem occurs in

File "D:\Anaconda3\lib\site-packages\xgrads\core.py", line 880, in init self.name, self.zcount, self.storage, self.comment = \

change it like this can solve the problem

if len(CtlVar.__reBlank.split(oneLineStr.strip(), maxsplit=3))== 3:
            self.name, self.zcount, self.storage = \
                CtlVar.__reBlank.split(oneLineStr.strip(), maxsplit=3)
            self.comment = self.name
else:
            self.name, self.zcount, self.storage, self.comment = \
                CtlVar.__reBlank.split(oneLineStr.strip(), maxsplit=3)
miniufo commented 2 years ago

Hi, thanks for your interests in xgrads and raising this.

I have a rule that xgrads should follow GrADS. For your example, I tried with OpenGrADS and it reports an error of the ctl file: image

This means that the vars records are not complete (variable comments are missing). So xgrads raises a similar exception, which is not a surprise. After adding the comments, as you did, things get fixed.

jesieleo commented 2 years ago

Thank you for your reply. Now I fully understand. Your library has helped me solve a big problem. Thank you very much!

jesieleo commented 2 years ago

The problem may be caused by a version inconsistency between v2.0.2.oga.2 & v2.1.a2.oga.1 ? image

miniufo commented 2 years ago

So the new version is more tolerant. OK, I think your solution is OK. Could you please send a PR and then becomes one of the contributor?

jesieleo commented 2 years ago

Thank you for your quick feedback. As I am a novice Githuber, I have spent some time learning PR. Now I have submitted it, please check if it is correct.

Also, I found that the./xgrads/core.py which I downloaded from pypi differs from those on github, is it because you updated pypi in June 2022 and the core.py file the following August?

miniufo commented 2 years ago

Yes, the pypi version is not the latest version. I'll check your PR.

jesieleo commented 2 years ago

Thank you again for your attention and warm response, and thank you for creating this project and helping me solve a pressing problem.