python气象信息系统工程 第六章

第六章

6.1 文本文件

6.1.2 CSV文件

1
2
3
4
5
6
# 读取csv,同时将时间字符串解析为时间戳类型
import pandas as pd
dat = pd.read_csv('/home/mw/input/pythonbook7300/sta.csv', parse_dates=['time'])
print(dat.dtypes)
print('--------')
print(dat)
time       datetime64[ns]
station             int64
t                 float64
tmax              float64
tmin              float64
dtype: object
--------
         time  station     t  tmax  tmin
0  2020-03-01    58238   8.7  12.5   1.3
1  2020-03-01    58235   7.5  11.3   0.8
2  2020-03-01    58237   8.5  11.8   1.2
3  2020-03-02    58238   9.5  13.3   4.4
4  2020-03-02    58235   8.3  12.8   4.0
5  2020-03-02    58237   8.8  12.9   4.2
6  2020-03-03    58238  13.1  14.5  10.1
7  2020-03-03    58235  12.4  13.7   9.6
8  2020-03-03    58237  13.0  14.1  10.0
9  2020-03-04    58238  10.3  13.8   5.2
10 2020-03-04    58235   9.8  12.9   4.5
11 2020-03-04    58237  10.1  13.1   4.8
1
2
3
4
5
6
7
8
9
# 将站号指定为字符串类型
import numpy as np
import pandas as pd
dat = pd.read_csv('/home/mw/input/pythonbook7300/sta.csv',
parse_dates=['time'],
dtype={'station': np.unicode_})
print(dat.dtypes)
print('--------')
print(dat)
time       datetime64[ns]
station            object
t                 float64
tmax              float64
tmin              float64
dtype: object
--------
         time station     t  tmax  tmin
0  2020-03-01   58238   8.7  12.5   1.3
1  2020-03-01   58235   7.5  11.3   0.8
2  2020-03-01   58237   8.5  11.8   1.2
3  2020-03-02   58238   9.5  13.3   4.4
4  2020-03-02   58235   8.3  12.8   4.0
5  2020-03-02   58237   8.8  12.9   4.2
6  2020-03-03   58238  13.1  14.5  10.1
7  2020-03-03   58235  12.4  13.7   9.6
8  2020-03-03   58237  13.0  14.1  10.0
9  2020-03-04   58238  10.3  13.8   5.2
10 2020-03-04   58235   9.8  12.9   4.5
11 2020-03-04   58237  10.1  13.1   4.8

6.1.3 空格(制表符)作为分隔符的文件

1
2
3
4
5
6
7
8
9
10
# 读取空格为分隔符的文件
import numpy as np
import pandas as pd
dat = pd.read_csv('/home/mw/input/pythonbook7300/sta.txt',
sep='\s', #如果是空格分隔符,则此处参数值为1s
parse_dates=['time'],
dtype={'station': np.unicode_})
print(dat.dtypes)
print('--------')
print(dat)
time       datetime64[ns]
station            object
t                 float64
tmax              float64
tmin              float64
dtype: object
--------
                 time station     t  tmax  tmin
2020-03-01 2023-03-02   58238   8.7  12.5   1.3
2020-03-01 2023-03-02   58235   7.5  11.3   0.8
2020-03-01 2023-03-02   58237   8.5  11.8   1.2
2020-03-02 2023-03-02   58238   9.5  13.3   4.4
2020-03-02 2023-03-02   58235   8.3  12.8   4.0
2020-03-02 2023-03-02   58237   8.8  12.9   4.2
2020-03-03 2023-03-02   58238  13.1  14.5  10.1
2020-03-03 2023-03-02   58235  12.4  13.7   9.6
2020-03-03 2023-03-02   58237  13.0  14.1  10.0
2020-03-04 2023-03-02   58238  10.3  13.8   5.2
2020-03-04 2023-03-02   58235   9.8  12.9   4.5
2020-03-04 2023-03-02   58237  10.1  13.1   4.8


/opt/conda/lib/python3.7/site-packages/pandas/util/_decorators.py:311: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
  return func(*args, **kwargs)
1
2
3
4
5
6
7
8
9
10
11
12
# 读取栅格型文本数据
# 行列与经纬栅格相关
import numpy as np
import xarray as xr
array_raw = np.loadtxt('/home/mw/input/pythonbook7300/grid_1.txt', dtype=np.float64)
print(array_raw) # 这一行用于演示读取结果,并非必要输入, 数据量过大时可能会导致卡顿
lon = np.linspace(120, 122.5, 5)
lat = np.linspace(10, 13, 4)
print(lon) # 此行仅用于演示
print(lat) # 此行仅用于演示
t_grid = xr.DataArray(array_raw, coords=[lat, lon], dims=["lat", "lon"])
print(t_grid)
Warning: ecCodes 2.21.0 or higher is recommended. You are running version 2.14.1
[[10.12 11.15 11.3  10.25  9.97]
 [10.23 11.54 11.12 10.01  9.85]
 [10.07 11.37 10.98 10.42  9.58]
 [10.33 12.01 10.38 10.36 10.12]]
[120.    120.625 121.25  121.875 122.5  ]
[10. 11. 12. 13.]
<xarray.DataArray (lat: 4, lon: 5)>
array([[10.12, 11.15, 11.3 , 10.25,  9.97],
       [10.23, 11.54, 11.12, 10.01,  9.85],
       [10.07, 11.37, 10.98, 10.42,  9.58],
       [10.33, 12.01, 10.38, 10.36, 10.12]])
Coordinates:
  * lat      (lat) float64 10.0 11.0 12.0 13.0
  * lon      (lon) float64 120.0 120.6 121.2 121.9 122.5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 读取栅格型文本数据
# 行列与经纬栅格无关
import numpy as np
import xarray as xr
array_raw = np.loadtxt('/home/mw/input/pythonbook7300/grid_2.txt', dtype=np.float64)
print(array_raw) # 这一行用于演示结果,并非必要输入,数据量过大时可能会导致卡顿
array_raw = array_raw.reshape((4, 5))
print(array_raw) # 这一行用于演示结果,并非必要输入,数据量过大时可能会导致卡顿
lon = np.linspace(120, 122.5, 5)
lat = np.linspace(10, 13, 4)
print(lon) # 此行仅用于演示
print(lat) # 此行仅用于演示
t_grid = xr.DataArray(array_raw, coords=[lat, lon], dims=["lat", "lon"])
print(t_grid)
[[10.12 11.15 11.3  10.25  9.97 10.23 11.54 11.12 10.01  9.85]
 [10.07 11.37 10.98 10.42  9.58 10.33 12.01 10.38 10.36 10.12]]
[[10.12 11.15 11.3  10.25  9.97]
 [10.23 11.54 11.12 10.01  9.85]
 [10.07 11.37 10.98 10.42  9.58]
 [10.33 12.01 10.38 10.36 10.12]]
[120.    120.625 121.25  121.875 122.5  ]
[10. 11. 12. 13.]
<xarray.DataArray (lat: 4, lon: 5)>
array([[10.12, 11.15, 11.3 , 10.25,  9.97],
       [10.23, 11.54, 11.12, 10.01,  9.85],
       [10.07, 11.37, 10.98, 10.42,  9.58],
       [10.33, 12.01, 10.38, 10.36, 10.12]])
Coordinates:
  * lat      (lat) float64 10.0 11.0 12.0 13.0
  * lon      (lon) float64 120.0 120.6 121.2 121.9 122.5

6.2 Excel文件

1
2
3
4
5
import pandas as pd
dat = pd.read_excel('/home/mw/input/pythonbook7300/read.xlsx', sheet_name='Sheet1')
print(dat)
print('-----')
print(dat.dtypes)
          时间  最高温度  降水
0 2020-01-01  10.3   0
1 2020-01-02  13.3   0
2 2020-01-03  12.1   0
3 2020-01-04   9.9   0
-----
时间      datetime64[ns]
最高温度           float64
降水               int64
dtype: object

6.3 NetCDF文件

1
2
3
4
5
6
7
# 读取NetCDF文件
import xarray as xr
# 将文件路径改为自己的
dataset = xr.open_dataset('/home/mw/input/pythonbook9857/1980060106.nc')
print(dataset)
t = dataset['t']
print(t)
<xarray.Dataset>
Dimensions:    (longitude: 721, latitude: 361, level: 13, time: 1)
Coordinates:
  * longitude  (longitude) float32 0.0 0.25 0.5 0.75 ... 179.2 179.5 179.8 180.0
  * latitude   (latitude) float32 90.0 89.75 89.5 89.25 ... 0.75 0.5 0.25 0.0
  * level      (level) int32 1000 975 925 850 700 600 ... 300 250 200 150 100
  * time       (time) datetime64[ns] 1980-06-01T06:00:00
Data variables:
    pv         (time, level, latitude, longitude) float32 ...
    z          (time, level, latitude, longitude) float32 ...
    t          (time, level, latitude, longitude) float32 ...
    q          (time, level, latitude, longitude) float32 ...
    w          (time, level, latitude, longitude) float32 ...
    vo         (time, level, latitude, longitude) float32 ...
    d          (time, level, latitude, longitude) float32 ...
    u          (time, level, latitude, longitude) float32 ...
    v          (time, level, latitude, longitude) float32 ...
    r          (time, level, latitude, longitude) float32 ...
    clwc       (time, level, latitude, longitude) float32 ...
Attributes:
    Conventions:  CF-1.6
    history:      2017-03-27 13:09:45 GMT by grib_to_netcdf-2.1.0: grib_to_ne...
<xarray.DataArray 't' (time: 1, level: 13, latitude: 361, longitude: 721)>
[3383653 values with dtype=float32]
Coordinates:
  * longitude  (longitude) float32 0.0 0.25 0.5 0.75 ... 179.2 179.5 179.8 180.0
  * latitude   (latitude) float32 90.0 89.75 89.5 89.25 ... 0.75 0.5 0.25 0.0
  * level      (level) int32 1000 975 925 850 700 600 ... 300 250 200 150 100
  * time       (time) datetime64[ns] 1980-06-01T06:00:00
Attributes:
    units:          K
    long_name:      Temperature
    standard_name:  air_temperature

6.4 GRIB文件

6.4.1 使用PyNIO

1
2
3
4
5
6
# 使用PyNIO读取GRIB文件
import xarray as xr
dataset = xr.open_dataset('/home/mw/input/pythonbook9857/fnl_201906011800.grib2', engine='pynio') # 将文件路径改为自己的
print(dataset)
data = dataset['TMP_P0_L1_GLL0']
print(data)
<xarray.Dataset>
Dimensions:              (lat_0: 181, lon_0: 360, lv_ISBL0: 31, lv_AMSL1: 3, lv_HTGL2: 3, lv_PVL3: 2, lv_HTGL4: 2, lv_SIGL5: 4, lv_ISBL6: 21, lv_HTGL7: 3, lv_ISBL8: 26, lv_SPDL9: 2, lv_ISBL10: 17, lv_DBLL11: 4)
Coordinates:
  * lv_ISBL10            (lv_ISBL10) float32 100.0 200.0 300.0 ... 3.5e+04 4e+04
  * lv_ISBL8             (lv_ISBL8) float32 1e+03 2e+03 3e+03 ... 9.75e+04 1e+05
  * lv_HTGL7             (lv_HTGL7) float32 10.0 80.0 100.0
  * lv_ISBL6             (lv_ISBL6) float32 1e+04 1.5e+04 ... 9.75e+04 1e+05
  * lv_HTGL4             (lv_HTGL4) float32 2.0 80.0
  * lv_PVL3              (lv_PVL3) float32 -2e-06 2e-06
  * lv_HTGL2             (lv_HTGL2) float32 2.0 80.0 100.0
  * lv_AMSL1             (lv_AMSL1) float32 1.829e+03 2.743e+03 3.658e+03
  * lv_ISBL0             (lv_ISBL0) float32 100.0 200.0 300.0 ... 9.75e+04 1e+05
  * lat_0                (lat_0) float32 90.0 89.0 88.0 ... -88.0 -89.0 -90.0
  * lon_0                (lon_0) float32 0.0 1.0 2.0 3.0 ... 357.0 358.0 359.0
Dimensions without coordinates: lv_SIGL5, lv_SPDL9, lv_DBLL11
Data variables: (12/98)
    TMP_P0_L1_GLL0       (lat_0, lon_0) float32 ...
    TMP_P0_L6_GLL0       (lat_0, lon_0) float32 ...
    TMP_P0_L7_GLL0       (lat_0, lon_0) float32 ...
    TMP_P0_L100_GLL0     (lv_ISBL0, lat_0, lon_0) float32 ...
    TMP_P0_L102_GLL0     (lv_AMSL1, lat_0, lon_0) float32 ...
    TMP_P0_L103_GLL0     (lv_HTGL2, lat_0, lon_0) float32 ...
    ...                   ...
    lv_DBLL11_l1         (lv_DBLL11) float32 ...
    lv_DBLL11_l0         (lv_DBLL11) float32 ...
    lv_SPDL9_l1          (lv_SPDL9) float32 ...
    lv_SPDL9_l0          (lv_SPDL9) float32 ...
    lv_SIGL5_l1          (lv_SIGL5) float32 ...
    lv_SIGL5_l0          (lv_SIGL5) float32 ...
<xarray.DataArray 'TMP_P0_L1_GLL0' (lat_0: 181, lon_0: 360)>
array([[273.23535, 273.23535, 273.23535, ..., 273.23535, 273.23535, 273.23535],
       [273.13535, 273.13535, 273.13535, ..., 273.13535, 273.13535, 273.13535],
       [273.13535, 273.13535, 273.13535, ..., 273.03537, 273.03537, 273.13535],
       ...,
       [219.43535, 219.43535, 219.53535, ..., 219.83536, 219.73535, 219.53535],
       [218.63536, 218.63536, 218.63536, ..., 218.63536, 218.63536, 218.73535],
       [219.93535, 219.93535, 219.93535, ..., 219.93535, 219.93535, 219.93535]],
      dtype=float32)
Coordinates:
  * lat_0    (lat_0) float32 90.0 89.0 88.0 87.0 ... -87.0 -88.0 -89.0 -90.0
  * lon_0    (lon_0) float32 0.0 1.0 2.0 3.0 4.0 ... 356.0 357.0 358.0 359.0
Attributes:
    center:                                         US National Weather Servi...
    production_status:                              Operational products
    long_name:                                      Temperature
    units:                                          K
    grid_type:                                      Latitude/longitude
    parameter_discipline_and_category:              Meteorological products, ...
    parameter_template_discipline_category_number:  [0 0 0 0]
    level_type:                                     Ground or water surface
    level:                                          [0.]
    forecast_time:                                  [0]
    forecast_time_units:                            hours
    initial_time:                                   06/01/2019 (18:00)

6.4.2 使用cfgrib

1
2
3
4
5
6
7
8
9
10
11
12
13
# 6.4.2  使用cfgrib读取GRIB文件
import xarray as xr
# 将文件路径改为自己的
dataset = xr.open_dataset('/home/mw/input/pythonbook9857/fnl_201906011800.grib2',
engine='cfgrib',
backend_kwargs={
'filter_by_keys': {
'typeOfLevel': 'isobaricInhPa'
}
}
)
print(dataset)
print(dataset['u'])
skipping variable: paramId==260131 shortName='o3mr'
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components
    dict_merge(variables, coord_vars)
  File "/opt/conda/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge
    "key=%r value=%r new_value=%r" % (key, master[key], value)
cfgrib.dataset.DatasetBuildError: key present and new value is different: key='isobaricInhPa' value=Variable(dimensions=('isobaricInhPa',), data=array([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.,    7.,
          5.,    3.,    2.,    1.])) new_value=Variable(dimensions=('isobaricInhPa',), data=array([400., 350., 300., 250., 200., 150., 100.,  70.,  50.,  30.,  20.,
        10.,   7.,   5.,   3.,   2.,   1.]))
skipping variable: paramId==3041 shortName='absv'
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components
    dict_merge(variables, coord_vars)
  File "/opt/conda/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge
    "key=%r value=%r new_value=%r" % (key, master[key], value)
cfgrib.dataset.DatasetBuildError: key present and new value is different: key='isobaricInhPa' value=Variable(dimensions=('isobaricInhPa',), data=array([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.,    7.,
          5.,    3.,    2.,    1.])) new_value=Variable(dimensions=('isobaricInhPa',), data=array([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.]))
skipping variable: paramId==135 shortName='w'
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components
    dict_merge(variables, coord_vars)
  File "/opt/conda/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge
    "key=%r value=%r new_value=%r" % (key, master[key], value)
cfgrib.dataset.DatasetBuildError: key present and new value is different: key='isobaricInhPa' value=Variable(dimensions=('isobaricInhPa',), data=array([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.,    7.,
          5.,    3.,    2.,    1.])) new_value=Variable(dimensions=('isobaricInhPa',), data=array([1000.,  975.,  950.,  925.,  900.,  850.,  800.,  750.,  700.,
        650.,  600.,  550.,  500.,  450.,  400.,  350.,  300.,  250.,
        200.,  150.,  100.]))
skipping variable: paramId==260018 shortName='clwmr'
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components
    dict_merge(variables, coord_vars)
  File "/opt/conda/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge
    "key=%r value=%r new_value=%r" % (key, master[key], value)
cfgrib.dataset.DatasetBuildError: key present and new value is different: key='isobaricInhPa' value=Variable(dimensions=('isobaricInhPa',), data=array([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.,    7.,
          5.,    3.,    2.,    1.])) new_value=Variable(dimensions=('isobaricInhPa',), data=array([1000.,  975.,  950.,  925.,  900.,  850.,  800.,  750.,  700.,
        650.,  600.,  550.,  500.,  450.,  400.,  350.,  300.,  250.,
        200.,  150.,  100.]))
skipping variable: paramId==260080 shortName='5wavh'
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components
    dict_merge(variables, coord_vars)
  File "/opt/conda/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge
    "key=%r value=%r new_value=%r" % (key, master[key], value)
cfgrib.dataset.DatasetBuildError: key present and new value is different: key='isobaricInhPa' value=Variable(dimensions=('isobaricInhPa',), data=array([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.,    7.,
          5.,    3.,    2.,    1.])) new_value=Variable(dimensions=(), data=500.0)


<xarray.Dataset>
Dimensions:        (isobaricInhPa: 31, latitude: 181, longitude: 360)
Coordinates:
    time           datetime64[ns] ...
    step           timedelta64[ns] ...
  * isobaricInhPa  (isobaricInhPa) float64 1e+03 975.0 950.0 ... 3.0 2.0 1.0
  * latitude       (latitude) float64 90.0 89.0 88.0 87.0 ... -88.0 -89.0 -90.0
  * longitude      (longitude) float64 0.0 1.0 2.0 3.0 ... 357.0 358.0 359.0
    valid_time     datetime64[ns] ...
Data variables:
    gh             (isobaricInhPa, latitude, longitude) float32 ...
    t              (isobaricInhPa, latitude, longitude) float32 ...
    r              (isobaricInhPa, latitude, longitude) float32 ...
    u              (isobaricInhPa, latitude, longitude) float32 ...
    v              (isobaricInhPa, latitude, longitude) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP 
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP 
    history:                 2023-03-02T09:18 GRIB to CDM+CF via cfgrib-0.9.9...
<xarray.DataArray 'u' (isobaricInhPa: 31, latitude: 181, longitude: 360)>
[2019960 values with dtype=float32]
Coordinates:
    time           datetime64[ns] ...
    step           timedelta64[ns] ...
  * isobaricInhPa  (isobaricInhPa) float64 1e+03 975.0 950.0 ... 3.0 2.0 1.0
  * latitude       (latitude) float64 90.0 89.0 88.0 87.0 ... -88.0 -89.0 -90.0
  * longitude      (longitude) float64 0.0 1.0 2.0 3.0 ... 357.0 358.0 359.0
    valid_time     datetime64[ns] ...
Attributes: (12/29)
    GRIB_paramId:                             131
    GRIB_dataType:                            fc
    GRIB_numberOfPoints:                      65160
    GRIB_typeOfLevel:                         isobaricInhPa
    GRIB_stepUnits:                           1
    GRIB_stepType:                            instant
    ...                                       ...
    GRIB_name:                                U component of wind
    GRIB_shortName:                           u
    GRIB_units:                               m s**-1
    long_name:                                U component of wind
    units:                                    m s**-1
    standard_name:                            eastward_wind

6.5 GrADS二进制文件

6.5.1 站点数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 定义如下读取函数
import numpy as np
import pandas as pd
def grads_sta(path_file, n_sta, n_time):
dt = np.dtype([('sta', 'S8'), ('lat', 'f4'), ('lon', 'f4'),
('time', 'f4'), ('nlev', 'i4'), ('nflag', 'i4'),
('var', 'f4')])
df_group = []
with open(path_file, 'rb') as fid:
for t in range(n_time):
data_raw = np.frombuffer(fid.read(n_sta * 32), dtype=dt)
fid.read(28)
df = pd.DataFrame(data_raw)
try:
df['sta'] = [x.decode() for x in df['sta']]
except:
pass
df_group.append(df)
return df_group
# 读取示例文件,可参考.ctl描述文件
dat = grads_sta('/home/mw/input/pythonbook9857/r160.grd', 160, 1) # 将文件路径改为自己的
print(dat)
print(type(dat))
[                sta        lat         lon  time  nlev  nflag       var
0    b'\x01       '  51.720001  126.650002   0.0     1      1  0.098331
1    b'\x02       '  48.770000  121.919998   0.0     1      1 -0.167619
2    b'\x03       '  49.220001  119.750000   0.0     1      1 -0.064597
3    b'\x04       '  50.450001  121.699997   0.0     1      1 -0.046360
4    b'\x05       '  49.169998  125.230003   0.0     1      1  0.017577
..              ...        ...         ...   ...   ...    ...       ...
155  b'\x9c       '  47.730000   88.080002   0.0     1      1 -0.036620
156  b'\x9d       '  46.730000   83.000000   0.0     1      1  0.071207
157  b'\x9e       '  44.430000   84.660004   0.0     1      1 -0.205051
158  b'\x9f       '  43.950001   81.330002   0.0     1      1  0.170275
159  b'\xa0       '  43.779999   87.620003   0.0     1      1 -0.103539

[160 rows x 7 columns]]
<class 'list'>

6.5.2 栅格数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 定义如下读取函数
import numpy as np
import xarray as xr
import pandas as pd
def grads_grid(path_file, nx, ny, nt,
lon_0, lon_step, lat_0, lat_step,
t_0, t_freq, var_list, z_list):
data_raw = np.fromfile(path_file, dtype=np.float32)
n_var = len(var_list)
nz = len(z_list)
data_raw = data_raw.reshape((nt, n_var, nz, ny, nx))
time_list = pd.date_range(t_0, periods=nt, freq=t_freq)
lon = np.arange(lon_0, lon_0 + lon_step * nx, lon_step)
lat = np.arange(lat_0, lat_0 + lat_step * ny, lat_step)
data = xr.DataArray(data_raw,
coords=[time_list, var_list, z_list, lat, lon],
dims=['time', 'var', 'level', 'lat', 'lon']
).to_dataset('var')
return data
# 读取示例文件,可参考.ctl描述文件
dataset = grads_grid('/home/mw/input/pythonbook9857/hgt500.grd',
nx=144, ny=73, nt=732,
lon_0=0, lon_step=2.5, lat_0=-90, lat_step=2.5,
t_0='1948-01-01', t_freq='1M', var_list=['hgt'],
z_list=[500])
print(dataset)
<xarray.Dataset>
Dimensions:  (time: 732, level: 1, lat: 73, lon: 144)
Coordinates:
  * time     (time) datetime64[ns] 1948-01-31 1948-02-29 ... 2008-12-31
  * level    (level) int64 500
  * lat      (lat) float64 -90.0 -87.5 -85.0 -82.5 -80.0 ... 82.5 85.0 87.5 90.0
  * lon      (lon) float64 0.0 2.5 5.0 7.5 10.0 ... 350.0 352.5 355.0 357.5
Data variables:
    hgt      (time, level, lat, lon) float32 5.189e+03 5.189e+03 ... 5.075e+03

6.6 WRF-ARW输出文件

1
2
3
4
5
6
7
8
9
10
11
12
13
# 读取WRF-ARW输出文件
import xarray as xr
import wrf
from netCDF4 import Dataset
# 将文件路径改为自己的
ncfile = Dataset('/home/mw/input/pythonbook9857/wrfout_d01_2017-08-10_000000.nc')
p = wrf.getvar(ncfile, 'pressure', timeidx=wrf.ALL_TIMES) # 对于getvar()函数,可以指定timeidx参数的值为整数以选择提取时次,例如timeidx=0
rh = wrf.getvar(ncfile, 'rh', timeidx=wrf.ALL_TIMES) # timeidx=wrf.ALL_TIMES表明提取全部时次
print(rh)
# 将数据插值到等压面
rh_500 = wrf.interplevel(rh, p, 500) # interplevel()函数支持同时插值到多个等压面
rh_850_to_500 = wrf.interplevel(rh, p, [850, 700, 500])
print(rh_850_to_500)
<xarray.DataArray 'rh' (Time: 5, bottom_top: 29, south_north: 49, west_east: 79)>
array([[[[87.765434 , 85.98064  , 84.91124  , ..., 81.3592   ,
          79.81483  , 81.41704  ],
         [87.995636 , 86.22767  , 85.25411  , ..., 83.49108  ,
          81.253586 , 81.55239  ],
         [88.70185  , 87.21322  , 86.30212  , ..., 84.36489  ,
          83.808624 , 83.382866 ],
         ...,
         [43.890274 , 45.305763 , 46.74152  , ..., 79.794525 ,
          79.27631  , 78.8375   ],
         [39.819405 , 41.26361  , 42.63604  , ..., 79.73092  ,
          79.23843  , 78.76903  ],
         [37.56467  , 38.025745 , 39.262104 , ..., 79.73279  ,
          79.23924  , 78.71969  ]],

        [[89.051445 , 86.849625 , 85.17071  , ..., 79.81156  ,
          78.14068  , 79.16232  ],
         [89.22471  , 87.07232  , 85.47679  , ..., 81.908    ,
          80.53091  , 80.28641  ],
         [89.795204 , 87.968445 , 86.57346  , ..., 83.796936 ,
          82.867424 , 82.598114 ],
...
         [ 4.049299 ,  4.0986323,  4.12636  , ...,  6.4341807,
           6.5087857,  6.608485 ],
         [ 3.9992058,  4.052754 ,  4.0801926, ...,  6.392681 ,
           6.4727077,  6.563373 ],
         [ 3.947622 ,  3.9738657,  4.002276 , ...,  6.363432 ,
           6.4367847,  6.513951 ]],

        [[ 4.21838  ,  4.2060227,  4.201893 , ...,  5.0465074,
           5.0882134,  5.14203  ],
         [ 4.2712355,  4.2646527,  4.2615657, ...,  5.06661  ,
           5.1020627,  5.126723 ],
         [ 4.3191833,  4.324505 ,  4.3139987, ...,  5.089982 ,
           5.105507 ,  5.109809 ],
         ...,
         [ 3.166993 ,  3.1939094,  3.2173321, ...,  4.870332 ,
           4.891847 ,  4.9187264],
         [ 3.1322336,  3.1641045,  3.1914012, ...,  4.8407125,
           4.8697286,  4.8977094],
         [ 3.100257 ,  3.123251 ,  3.1545112, ...,  4.8102155,
           4.8391423,  4.8720045]]]], dtype=float32)
Coordinates:
    XLONG    (south_north, west_east) float32 113.2 113.3 113.4 ... 121.3 121.4
    XLAT     (south_north, west_east) float32 29.68 29.68 29.68 ... 33.99 33.99
    XTIME    (Time) float32 0.0 180.0 360.0 540.0 720.0
  * Time     (Time) datetime64[ns] 2017-08-10 ... 2017-08-10T12:00:00
Dimensions without coordinates: bottom_top, south_north, west_east
Attributes:
    FieldType:    104
    MemoryOrder:  XYZ
    description:  relative humidity
    units:        %
    stagger:      
    coordinates:  XLONG XLAT XTIME
    projection:   LambertConformal(stand_lon=117.19999694824219, moad_cen_lat...
<xarray.DataArray 'rh_interp' (Time: 5, level: 3, south_north: 49, west_east: 79)>
array([[[[ 49.581455 ,  49.90141  ,  49.964336 , ...,  93.02744  ,
           92.50517  ,  91.436905 ],
         [ 49.854908 ,  50.216183 ,  50.33729  , ...,  94.29466  ,
           93.958336 ,  92.881165 ],
         [ 50.29943  ,  50.652588 ,  50.81059  , ...,  94.8869   ,
           94.64059  ,  94.331085 ],
         ...,
         [ 43.85136  ,  44.185024 ,  44.534695 , ...,  65.72999  ,
           66.07933  ,  66.988106 ],
         [ 43.501328 ,  43.93895  ,  44.335472 , ...,  66.38105  ,
           66.68885  ,  67.59361  ],
         [ 43.071743 ,  43.59577  ,  44.11645  , ...,  67.04757  ,
           67.32303  ,  68.26489  ]],

        [[ 59.510532 ,  58.794968 ,  58.20066  , ...,  88.38223  ,
           89.8404   ,  90.4845   ],
         [ 60.2473   ,  59.721775 ,  59.27535  , ...,  88.13034  ,
           89.4728   ,  90.00002  ],
         [ 60.890114 ,  60.67282  ,  60.48124  , ...,  87.15515  ,
           88.844986 ,  89.848656 ],
...
         [ 32.703304 ,  32.478714 ,  32.44395  , ...,  50.050068 ,
           50.41752  ,  50.632175 ],
         [ 33.32784  ,  33.011208 ,  32.852413 , ...,  48.91975  ,
           49.255787 ,  49.405983 ],
         [ 34.13029  ,  33.958523 ,  33.643333 , ...,  47.629578 ,
           47.947124 ,  48.399437 ]],

        [[ 24.025608 ,  23.000294 ,  21.980774 , ...,  16.391787 ,
           17.90786  ,  19.738607 ],
         [ 20.825268 ,  20.007761 ,  19.12427  , ...,  12.906076 ,
           14.448949 ,  16.162752 ],
         [ 18.05197  ,  17.35045  ,  16.605772 , ...,  12.18835  ,
           12.27763  ,  14.094156 ],
         ...,
         [  4.564085 ,   4.439832 ,   4.360229 , ...,  18.472599 ,
           19.609535 ,  19.641474 ],
         [  4.6399007,   4.5138626,   4.4195275, ...,  18.654158 ,
           19.900602 ,  20.3621   ],
         [  4.7261467,   4.597047 ,   4.4673424, ...,  18.454733 ,
           19.761063 ,  20.774572 ]]]], dtype=float32)
Coordinates:
    XLONG    (south_north, west_east) float32 113.2 113.3 113.4 ... 121.3 121.4
    XLAT     (south_north, west_east) float32 29.68 29.68 29.68 ... 33.99 33.99
    XTIME    (Time) float32 0.0 180.0 360.0 540.0 720.0
  * Time     (Time) datetime64[ns] 2017-08-10 ... 2017-08-10T12:00:00
  * level    (level) int64 850 700 500
Dimensions without coordinates: south_north, west_east
Attributes:
    FieldType:      104
    units:          %
    stagger:        
    coordinates:    XLONG XLAT XTIME
    projection:     LambertConformal(stand_lon=117.19999694824219, moad_cen_l...
    missing_value:  9.969209968386869e+36
    _FillValue:     9.969209968386869e+36
    vert_units:     hPa

6.7 雷达基数据文件

1
2
3
4
5
6
7
8
import cinrad
f = cinrad.io.CinradReader('/home/mw/input/pythonbook9857/RADA_CHN_DOR_L2_O-Z9551-SA-CAP-20200708032300.bin.bz2')
print(f.available_product(0))
# 读取反射率
tilt_number = 0
data_radius = 230
r = f.get_data(tilt_number, data_radius, 'REF')
print(r)
['REF', 'VEL', 'SW', 'azimuth', 'RF']
<xarray.Dataset>
Dimensions:    (azimuth: 366, distance: 230)
Coordinates:
  * azimuth    (azimuth) float64 1.38 1.397 1.415 1.432 ... 1.349 1.366 1.383
  * distance   (distance) float64 1.0 2.0 3.0 4.0 ... 227.0 228.0 229.0 230.0
Data variables:
    REF        (azimuth, distance) float64 nan -8.5 2.0 2.0 ... nan nan nan nan
    longitude  (azimuth, distance) float64 117.3 117.3 117.3 ... 119.7 119.7
    latitude   (azimuth, distance) float64 31.87 31.87 31.87 ... 32.25 32.25
    height     (azimuth, distance) float64 0.1764 0.1873 0.1983 ... 5.709 5.747
Attributes:
    elevation:        0.615234375
    range:            230
    scan_time:        2020-07-08 03:23:11.811000
    site_code:        Z9551
    site_name:        合肥
    site_longitude:   117.25777777777778
    site_latitude:    31.866944444444446
    tangential_reso:  1.0
    nyquist_vel:      8.52
    task:             VCP21

6.8 CIMISS的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import cimiss
import numpy as np
# host 不带http前缀,通常为zero-ice服务的纯IP地址
client = cimiss.Query(user_id='myuserid', password='mypasswd', host='myhost') # 请修改为你的账号、密码、IP地址
resp_array_2d = client.array_2d(
interface_id="getSurfEleByTime",
params={
"dataCode": "SURF_CHN_MUL_HOR",
"elements": "Station_ID_C,PRE_1h,PRS",
"times": "20181224000000",
"orderby": "Station_ID_C:ASC",
"limitCnt": "3",
},
dtypes={'PRE_1h': np.float, 'PRS': np.float}
)
print(resp_array_2d)