暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Python气象数据处理与绘图:基于Python的Grads文件解析

气海无涯 2021-08-25
1810

1、导入模块

1from xgrads import CtlDescriptor
2from xgrads import open_CtlDataset
3import xarray as xr

2、解析.ctl文件

1ctl = CtlDescriptor(file='../input/ctls6788/ctls/ctls/test1.ctl')
2# print all the info in ctl file
3print(ctl)

输出:
 1   dsetPath: ctls/19855pt.grb
2   descPath: ctls/test1.ctl
3   indxPath: ctls/19855pt.grb.idx
4   stnmPath: 
5      title: 19855pt.grb
6      undef: 9.999e+20
7       zrev: False
8       yrev: True
9      dtype: grib 255
10   template: False
11  periodicX: True
12 cal365Days: False
13 sequential: False
14  byteOrder: little
15       xdef: [231.  231.5 232.  232.5 233.  233.5 234.  234.5 235.  235.5 236.  236.5
16 237.  237.5 238.  238.5 239.  239.5 240.  240.5 241.  241.5 242.  242.5
17 243.  243.5 244.  244.5 245.  245.5 246.  246.5 247.  247.5 248.  248.5
18 249.  249.5 250.  250.5 251.  251.5 252.  252.5 253.  253.5 254.  254.5
19 255.  255.5 256.  256.5 257.  257.5 258.  258.5 259.  259.5 260.  260.5
20 261.  261.5 262.  262.5 263.  263.5 264.  264.5 265.  265.5 266.  266.5
21 267.  267.5 268.  268.5 269.  269.5 270.  270.5 271.  271.5 272.  272.5
22 273.  273.5 274.  274.5 275.  275.5 276.  276.5 277.  277.5 278.  278.5
23 279.  279.5 280.  280.5 281.  281.5 282.  282.5 283.  283.5 284.  284.5
24 285.  285.5 286.  286.5 287.  287.5 288.  288.5 289.  289.5 290.  290.5
25 291.  291.5 292.  292.5 293.  293.5 294.  294.5 295.  295.5 296.  296.5
26 297.  297.5 298.  298.5 299.  299.5 300.  300.5 301.  301.5 302.  302.5
27 303.  303.5 304.  304.5 305.  305.5 306.  306.5 307.  307.5 308.  308.5
28 309.  309.5 310.  310.5 311.  311.5 312.  312.5 313.  313.5 314.  314.5
29 315.  315.5 316.  316.5 317.  317.5 318.  318.5 319.  319.5 320.  320.5
30 321.  321.5 322.  322.5 323.  323.5 324.  324.5 325.  325.5 326.  326.5
31 327.  327.5 328.  328.5 329.  329.5 330.  330.5 331.  331.5 332.  332.5
32 333.  333.5 334.  334.5 335.  335.5 336.  336.5 337.  337.5 338.  338.5
33 339.  339.5 340.  340.5 341.  341.5 342.  342.5 343.  343.5 344.  344.5
34 345.  345.5 346.  346.5 347.  347.5 348.  348.5 349.  349.5 350.  350.5
35 351.  351.5 352.  352.5 353.  353.5 354.  354.5 355.  355.5 356.  356.5
36 357. ]
37       ydef: [63.  62.5 62.  61.5 61.  60.5 60.  59.5 59.  58.5 58.  57.5 57.  56.5
38 56.  55.5 55.  54.5 54.  53.5 53.  52.5 52.  51.5 51.  50.5 50.  49.5
39 49.  48.5 48.  47.5 47.  46.5 46.  45.5 45.  44.5 44.  43.5 43.  42.5
40 42.  41.5 41.  40.5 40.  39.5 39.  38.5 38.  37.5 37.  36.5 36.  35.5
41 35.  34.5 34.  33.5 33.  32.5 32.  31.5 31.  30.5 30.  29.5 29.  28.5
42 28.  27.5 27.  26.5 26.  25.5 25.  24.5 24.  23.5 23.  22.5 22.  21.5
43 21.  20.5 20.  19.5 19.  18.5 18.  17.5 17.  16.5 16.  15.5 15.  14.5
44 14.  13.5 13.  12.5 12.  11.5 11.  10.5 10.   9.5  9.   8.5  8.   7.5
45  7.   6.5  6.   5.5  5.   4.5  4.   3.5  3.   2.5  2.   1.5  1.   0.5
46  0.  -0.5 -1.  -1.5 -2.  -2.5 -3.  -3.5 -4.  -4.5 -5.  -5.5 -6.  -6.5
47 -7.  -7.5 -8.  -8.5 -9. ]
48       zdef: [265. 275. 285. 300. 315. 330. 350. 370. 395. 430.]
49       tdef: ['1985-08-28T00:00:00' '1985-08-28T06:00:00' '1985-08-28T12:00:00'
50 '1985-08-28T18:00:00' '1985-08-29T00:00:00' '1985-08-29T06:00:00'
51 '1985-08-29T12:00:00' '1985-08-29T18:00:00' '1985-08-30T00:00:00'
52 '1985-08-30T06:00:00' '1985-08-30T12:00:00' '1985-08-30T18:00:00'
53 '1985-08-31T00:00:00' '1985-08-31T06:00:00' '1985-08-31T12:00:00'
54 '1985-08-31T18:00:00' '1985-09-01T00:00:00' '1985-09-01T06:00:00'
55 '1985-09-01T12:00:00' '1985-09-01T18:00:00' '1985-09-02T00:00:00'
56 '1985-09-02T06:00:00' '1985-09-02T12:00:00' '1985-09-02T18:00:00'
57 '1985-09-03T00:00:00' '1985-09-03T06:00:00' '1985-09-03T12:00:00'
58 '1985-09-03T18:00:00' '1985-09-04T00:00:00' '1985-09-04T06:00:00'
59 '1985-09-04T12:00:00' '1985-09-04T18:00:00']
60       pdef: 
61       vdef: [CtlVar: Dtht     in shape (t=32, z=10, y=145, x=253)
62 CtlVar: MONTtht  in shape (t=32, z=10, y=145, x=253)
63 CtlVar: PREStht  in shape (t=32, z=10, y=145, x=253)
64 CtlVar: Qtht     in shape (t=32, z=10, y=145, x=253)
65 CtlVar: Utht     in shape (t=32, z=10, y=145, x=253)
66 CtlVar: Vtht     in shape (t=32, z=10, y=145, x=253)
67 CtlVar: VOtht    in shape (t=32, z=10, y=145, x=253)]

3、转为xarray.Dataset格式

1dset = open_CtlDataset('../input/ctls6788/ctls/ctls/test8.ctl')
2# print all the info in ctl file
3print(dset)

输出:

 1<xarray.Dataset>
2Dimensions:  (lat: 25, lon: 53time4)
3Coordinates:
4  * time     (time) datetime64[ns] 2013-01-01 ... 2013-01-01T18:00:00
5  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
6  * lon      (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
7Data variables:
8    air      (time, lat, lon) >f4 dask.array<chunksize=(12553), meta=np.ndarray>
9Attributes:
10    title:    
11    undef:    -9.99e+33

4、转为nc格式

1open_CtlDataset('../input/ctls6788/ctls/ctls/test8.ctl').to_netcdf('../input/ctls6788/ctls/ctls/output.nc')

5、读取nc格式

1ds = xr.open_dataset('../input/ctls6788/ctls/ctls/output.nc')
2ds

输出:

6、绘图

1ds.air[0].plot()





有问题可以到QQ群里进行讨论,我们在那边等大家。

QQ群号:854684131

文章转载自气海无涯,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论