1+ # coding=UTF-8
2+ import sys
3+ import importlib
4+ import os
5+ #import pygrib
6+ import ArrayOper
7+ import QuickPositioning
8+ #import reload
9+ # 具体步骤:
10+ # 1 获取文件的一些信息(传入参数)
11+ # 2 找到对应文件——目前先根据预报要素、时间、时效
12+ # 3 读取文件
13+ # 4 根据经纬度二维数组以及层级获取指定的gribmessage
14+ # 5 根据经纬度找到对应的区域并取出对应的结果一维数组
15+ importlib .reload (sys )
16+
117
218class GribReader :
3- """grib文件读取类
4- """
5- def __init__ (self ):
6- self .filePath = ''
19+ """grib读取类
20+ """
21+ def __init__ (self ,sourcePath ,filePath ,forecastType ,targetDateTime ):
22+ """
23+ 构造函数需传入:
24+ 1 文件路径(不含文件名)
25+ 2 文件路径
26+ 3 类型
27+ 4 预报时间
28+ """
29+ self .sourcePath = sourcePath
30+ self .fileName = fileName
31+ self .forecastType = forecastType
32+ self .targetDateTime = targetDateTime
33+
734
835 def display (self ):
936 print ("" )
1037
1138 def __str__ (self ):
12- return "读取的grib文件所在路径为:%s" % self .filePath
39+ return "读取的grib的文件路径为:%s" % self .filePath
40+
41+ def getLatLonValues (self ,grbs ,latlon_array ,index ):
42+ # 获取纬度二维数组
43+ # 纬度二维数组
44+ # 纬度只需要取出第一列即可
45+ lat_array = [x [0 ] for x in grbs [index ].latlons ()[0 ]]
46+ # 取出经度——取出第一行即可
47+ lon_array = grbs [index ].latlons ()[1 ][0 ].tolis ()
48+ return lat_array ,lon_array
49+
50+ def read (self ,level ):
51+ """
52+ """
53+ # 1 找到对应文件——目前先根据预报要素、时间、时效
54+ # 测试文件名为:HTBCG2016120100-006.grb
55+ forecastType = self .forecastType
56+ targetDateTime = self .targetDateTime
57+ #使用此种方式会出错,注释掉
58+ #targetForecastDate="from "+str(self.targetDateTime)
59+ targetForecastDate = ""
60+ targetForecastDate = "from 2016120100"
61+ agingForecast = "fcst time 6 hrs"
62+ # 注意此处需要手动将6转换为str类型,并向前填充0,保持三位数字
63+ aging = str (6 ).zfill (3 )
64+ grbname_array = [forecastType ,targetDateTime ,'-' ,aging ,'.grb' ]
65+ targetName = '' .join (grbname_array )
66+ targetPath = self .sourcePath
67+ targetFullName = targetPath + targetName
68+ # 判断指定路径及是否存在指定文件
69+ #/usr/testFIles/HTBCG2016120100-006.grb
70+ if not os .path .exists (targetPath ):
71+ print ('路径' + targetName + '不存在' )
72+ return
73+ elif not os .path .exists (targetFullName ):
74+ print ('指定文件' + targetFullName + '不存在' )
75+ return
76+ else :
77+ print ('文件:' + targetName + '存在' )
78+
79+ # 2 读取文件
80+ # 使用Pygrib打开文件
81+ grbs = pygrib .open (targetFullName )
82+ grbs .seek (0 )
83+
84+ # 3 根据经纬度二维数组以及层级获取指定的gribmessage
85+ str_level = 'level ' + level
86+ # 获取满足条件的片区编号
87+ index = getTargetArea (grbs ,str_level ,agingForecast ,targetForecastDate )
88+ # 根据经纬度找到对应的区域并取出对应的结果一维数组
89+ lat_array ,lon_array = getLatLonValues (grbs ,latlon_array ,index )
90+
91+ # 此种方式的思路,现在已经拥有lat和lon这两个数组了
92+ # lat,lon分别代表经度和纬度
93+ # latlon_array是一个经纬度信息的二维数组
94+ # [经度... ]
95+ # [纬度... ]
96+ # 思路:
97+ # 计算lat与lon的间隔,与最大值与最小值
98+ # 根据latlon_array数组中的值计算其在lat与lon数组中的位置,定位后获取该值
99+ values = grbs [index ].values
100+
101+ latlon_array_value = [[90 ,80 ],[0 ,1 ]]
102+ for temp in latlon_array_value :
103+ latlon_array_index .append (QuickPositioning .getPosition (temp [0 ],temp [1 ],lat_array ,lon_array ))
104+
105+
106+ # def readGrib(self,level):
107+ # """
108+ # """
109+ # # 1 找到对应文件——目前先根据预报要素、时间、时效
110+ # # 测试文件名为:HTBCG2016120100-006.grb
111+ # forecastType=self.forecastType
112+ # targetDateTime=self.targetDateTime
113+ # #使用此种方式会出错,注释掉
114+ # #targetForecastDate="from "+str(self.targetDateTime)
115+ # targetForecastDate=""
116+ # targetForecastDate="from 2016120100"
117+ # agingForecast="fcst time 6 hrs"
118+ # # 注意此处需要手动将6转换为str类型,并向前填充0,保持三位数字
119+ # aging=str(6).zfill(3)
120+ # grbname_array=[forecastType,targetDateTime,'-',aging,'.grb']
121+ # targetName=''.join(grbname_array)
122+ # targetPath=self.sourcePath
123+ # targetFullName=targetPath+targetName
124+ # # 判断指定路径及是否存在指定文件
125+ # #/usr/testFIles/HTBCG2016120100-006.grb
126+ # if not os.path.exists(targetPath):
127+ # print('路径'+targetName+'不存在')
128+ # return
129+ # elif not os.path.exists(targetFullName):
130+ # print('指定文件'+targetFullName+'不存在')
131+ # return
132+ # else :
133+ # print('文件:'+targetName+'存在')
134+
135+ # # 2 读取文件
136+ # # 使用Pygrib打开文件
137+ # grbs=pygrib.open(targetFullName)
138+ # grbs.seek(0)
139+
140+ # # 3 根据经纬度二维数组以及层级获取指定的gribmessage
141+ # str_level='level '+level
142+ # # 获取满足条件的片区编号
143+ # index=getTargetArea(grbs,str_level,agingForecast,targetForecastDate)
144+ # # 根据经纬度找到对应的区域并取出对应的结果一维数组
145+ # lat_array,lon_array= getLatLonValues(grbs,latlon_array,index)
146+
147+ # # 此种方式的思路,现在已经拥有lat和lon这两个数组了
148+ # # lat,lon分别代表经度和纬度
149+ # # latlon_array是一个经纬度信息的二维数组
150+ # # [经度... ]
151+ # # [纬度... ]
152+ # # 思路:
153+ # # 计算lat与lon的间隔,与最大值与最小值
154+ # # 根据latlon_array数组中的值计算其在lat与lon数组中的位置,定位后获取该值
155+ # values=grbs[index].values
156+
157+ # latlon_array_value=[[90,80],[0,1]]
158+ # for temp in latlon_array_value:
159+ # latlon_array_index.append(QuickPositioning.getPosition(temp[0],temp[1],lat_array,lon_array))
160+
161+ #根据高度,预报时效以及起始时间获取指定片区的所在位置(数组中的位置)
162+ def getTargetArea (grbs ,level ,aging ,fromDate ):
163+ index = 0
164+ isExist = False
165+ index_final = - 1
166+ #遍历grbs
167+ for grb in grbs :
168+ area_name = str (grb )
169+ print (area_name )
170+ index += 1
171+ # 当当前片区的名字满足条件时
172+ # fromDate:'from 201612010000'
173+ if level in area_name and aging in area_name and fromDate in area_name :
174+ isExist = True
175+ index_final = index
176+ break
177+ return index_final
0 commit comments