def iceData(): # 获取ec数据信息(气温、降水、地温、湿度、积雪深度) ectime = ecmwf.ecreptime() fh = [i for i in range(12, 181, 3)] # 20点的预报获取今天8:00的ec预报 *_, dics = Writefile.readxml(glovar.trafficpath, 4) dicslist = dics.split(',') lonlatset, dataset = [], [] for dic in dicslist: newdata = [] lon, lat, data = Datainterface.micapsdata(ectime, dic, fh) lonlatset.append((lon, lat)) for i in range(data.shape[0] - 1): if (np.isnan(data[i]).all() == True) and (i + 1 <= data.shape[0]): data[i] = data[i + 1] / 2 data[i + 1] = data[i + 1] / 2 newdata.append( interp.interpolateGridData(data[i], lat, lon, glovar.lat, glovar.lon)) else: newdata.append( interp.interpolateGridData(data[i], lat, lon, glovar.lat, glovar.lon)) newdata = np.array(newdata) # newdata[newdata<0] = 0 # 保证数据正确性 dataset.append(newdata) # 保存插值后的数据集 return np.array(dataset)
def presnow(): # 得到前一时刻积雪深度 ectime = ecmwf.ecreptime() fh = [0] dic = 'ECMWF_HR/SNOD' lon, lat, data = Datainterface.micapsdata(ectime, dic, fh) return interp.interpolateGridData(data, lat, lon, glovar.lat, glovar.lon)
def mirrorSkint(hours): # 6.14.16测试 # 从cimiss中获取所有站点地温插值,hours控制选取的时间间隔 interfaceId = 'getSurfEleInRegionByTimeRange' elements = "Station_Id_C,Lat,Lon,GST,Year,Mon,Day,Hour" # 设定每天8:00、 20:00运行 lastime = datetime.datetime.now().replace( minute=0, second=0) - datetime.timedelta(hours=8) firstime = lastime - datetime.timedelta(hours=hours) temp = ('[', firstime.strftime('%Y%m%d%H%M%S'), ',', lastime.strftime('%Y%m%d%H%M%S'), ')') Time = ''.join(temp) params = { 'dataCode': "SURF_CHN_MUL_HOR", # 需更改为地面逐小时资料 'elements': elements, 'timeRange': Time, 'adminCodes': "630000" } initData = Datainterface.cimissdata(interfaceId, elements, **params) # 获取出cimiss初始数据 initData.Time = pd.to_datetime(initData.Time) # 需按时间进行划分插值,得到 地温网格数据 timeList = pd.to_datetime(initData.Time.unique()).strftime( '%Y%m%d%H%M%S') # 此处可能存在问题,目的仅按顺序取出来时间 initData.set_index(initData.Time, inplace=True) oneHourgrid = [ np.nan_to_num( interp.interpolateGridData( initData.loc[tm].GST.values.astype('float32'), initData.loc[tm].Lat.values.astype('float32'), initData.loc[tm].Lon.values.astype('float32'), glovar.lat, glovar.lon)) for tm in timeList ] return oneHourgrid # oneHourgrid 格点数据形状应为[12, newlat, newlon]
def main(): ice = Roadic() rep = ecmwf.ecreptime() fh = [i for i in range(12, 181, 3)] region = [float(i) for i in ','.join(Writefile.readxml(glovar.trafficpath, 0)).split(',')] new_lon = np.arange(region[0], region[2], region[-1]) new_lat = np.arange(region[1], region[3], region[-1]) lonlatset, dataset = [], [] # 提取数据及经纬度(双重循环,看能否改进) for dic in ice.dics: lon, lat, data = Datainterface.micapsdata(rep, dic, fh) lonlatset.append((lon, lat)) for i in range(data.shape[0] - 1): if (np.isnan(data[i]).all() == True) and (i + 1 <= data.shape[0]): data[i] = data[i + 1] / 2 data[i+1] = data[i + 1] / 2 interp.interpolateGridData(data,lat,lon,new_lat, new_lon) else: interp.interpolateGridData(data, lat, lon,new_lat, new_lon) dataset.append(data) # 保存插值后的数据集 icgrid = ice.icegrid(dataset, new_lat, new_lon) savepath, indexpath = Writefile.readxml(glovar.trafficpath, 1)[2:] write(savepath, icgrid, 'Roadic', new_lat, new_lon) # 先保存厚度网格数据 iceroad = ice.depth2onezero(icgrid, new_lat, new_lon) ################################################################################ # 获取cimiss数据集,此处仅为读取,实况数据获取及保存由另一程序实现 cmissdata = np.loadtxt('/home/cqkj/QHTraffic/qhroadic/cmsk.csv', delimiter=',') icedays = RoadIceindex(cmissdata, iceroad) roadicing = icedays.iceday() write(indexpath, roadicing, 'RoadicIndex', type=1)
def main(): saltedata = saltedata(path) snowpre = np.random.randint(0, 1, size=(801 * 1381, 1)) snow = SnowDepth() rep = ecmwf.ecreptime() fh = [i for i in range(12, 181, 3)] region = [ float(i) for i in ','.join( Writefile.readxml( r'/home/cqkj/QHTraffic/Product/Traffic/SNOD/config.xml', 0)).split(',') ] new_lon = np.arange(region[0], region[2], region[-1]) new_lat = np.arange(region[1], region[3], region[-1]) lonlatset, dataset = [], [] # 提取数据及经纬度(双重循环,看能否改进) for dic in snow.dics: lon, lat, data = Datainterface.micapsdata(rep, dic, fh) lonlatset.append((lon, lat)) for i in range(data.shape[0] - 1): if (np.isnan(data[i]).all() == True) and (i + 1 <= data.shape[0]): data[i] = data[i + 1] / 2 data[i + 1] = data[i + 1] / 2 interp.interpolateGridData(data, lat, lon, new_lat, new_lon) else: interp.interpolateGridData(data, lat, lon, new_lat, new_lon) dataset.append(data) # 保存插值后的数据集 depthgrid = snow.clcsd(dataset, new_lat, new_lon, saltedata, snowpre) snow.write(depthgrid, new_lat, new_lon) dangerindex = snow.clcindex(depthgrid, new_lat, new_lon) snow.write(dangerindex, type=1)
def rainData(): # 同步降雨智能网格文件并解析 now = datetime.datetime.now() *_, elements, ftp = Writefile.readxml(glovar.trafficpath, 1) #*_, elements, ftp = Writefile.readxml(r'/home/cqkj/LZD/Product/Product/config/Traffic.xml', 5) element = elements.split(',') ftp = ftp.split(',') grib = Datainterface.GribData() remote_url = os.path.join(r'\\SPCC\\BEXN', now.strftime('%Y'), now.strftime('%Y%m%d')) grib.mirror(element[0], remote_url, element[1], ftp, element[2]) rname = sorted(os.listdir(element[1]))[-1] rpath = element[1] + rname dataset, lat, lon, _ = Znwg.arrange((grib.readGrib(rpath))) # result包含data,lat,lon,size return [interp.interpolateGridData(data, lat, lon, glovar.lat, glovar.lon) for data in dataset[:56]]
def mirrorGrib(path): # 6.14.16测试 # 用来同步8-20/20-8的实况格点场数据气温(开式)、降水(累计)、湿度, 20时同步今天8时数据, 8时同步昨日20时数据 grid = Datainterface.GribData() now = datetime.datetime.now() elements, subdirs, localdirs, _, freq, *ftp = Writefile.readxml( path, 0) # freq应为None elements = elements.split(',') subdirs = subdirs.split(',') localdirs = localdirs.split(',') # 为三个文件夹目录 remote_urls = [ os.path.join(subdir, now.strftime('%Y'), now.strftime('%Y%m%d')) for subdir in subdirs ] # 构造三个路径 for localdir, element, remote_url in zip(localdirs, elements, remote_urls): grid.mirror(element, remote_url, localdir, ftp) # 同步至每个文件夹,此处待测试,需保证范围在08-20或次日20-当日08时 # 查看各文件夹里数据信息,此处默认TEM, RAIN, RH 为08-20时的文件名列表 RAINs, RHs, TEMs = [ sorted(os.listdir(localdir)) for localdir in localdirs ] # 零时开始至今 e2tTems = [tem for tem in TEMs if int(tem[-7:-5]) in range(8, 21)] e2tRains = [rain for rain in RAINs if int(rain[-7:-5]) in range(8, 21)] e2tRhs = [rh for rh in RHs if int(rh[-7:-5]) in range(8, 21)] # 认为形状为同一分辨率下的[12, lat * lon] tem = [ Znwg.arrange(grid.readGrib(os.path.join(localdirs[2], TEM))) for TEM in e2tTems ] # temdata 包含四个要素(data, lat, lon, size), 全国范围,需插值到青海 lat, lon = tem[0][1], tem[0][2] temdata = np.array([ np.nan_to_num( interp.interpolateGridData(t[0] - 273.15, lat, lon, glovar.lat, glovar.lon)) for t in tem ]) raindata = np.array([ np.nan_to_num( interp.interpolateGridData( Znwg.arrange(grid.readGrib(os.path.join(localdirs[0], RAIN)))[0], lat, lon, glovar.lat, glovar.lon)) for RAIN in e2tRains ]) rhdata = np.array([ np.nan_to_num( interp.interpolateGridData( Znwg.arrange(grid.readGrib(os.path.join(localdirs[1], RH)))[0], lat, lon, glovar.lat, glovar.lon)) for RH in e2tRhs ]) return temdata, raindata, rhdata
def upSnowele(gbpath, sktpath): # 处理积雪历史要素文件, gbpath为格点要素pkl存放路径, sktpath为cimiss插值pkl路径 grid = Datainterface.GribData() gbNlist, sktList = sorted(os.listdir(gbpath))[-5:], sorted( os.listdir(sktpath))[-5:] #使用正则优化 # 获取每天11时到17时, 23时到05时的数据,考虑取最后 eleData = [ Znwg.arrange(grid.readGrib(os.path.join(gbpath, gblist))) for gblist in gbNlist ] sktData = [ Znwg.arrange(grid.readGrib(os.path.join(gbpath, sktlist))) for sktlist in sktList ] return np.array(eleData), np.array(sktData)
def windData(path): # 获取数据信息 *_, elements, ftp = Writefile.readxml(path, 2) element = elements.split(',') ftp = ftp.split(',') grib = Datainterface.GribData() remote_url = os.path.join(r'\\SPCC\\BEXN', glovar.now.strftime('%Y'), glovar.now.strftime('%Y%m%d')) grib.mirror(element[0], remote_url, element[1], ftp, element[2]) rname = sorted(os.listdir(element[1]))[-1] rpath = element[1] + rname dataset, lat, lon, _ = Znwg.arrange( (grib.readGrib(rpath))) # result包含data,lat,lon,size return [ interp.interpolateGridData(data, lat, lon, glovar.lat, glovar.lon) for data in dataset ] # 返回插值后列表格式数据
def predepth(): # 前一时刻积水深度,此处需在服务器端测试优化 dr = np.zeros(shape=(801, 1381)) # 目前默认前一时刻积水深度为0 now = datetime.datetime.now() znwgtm = Znwg.znwgtime() *_, ftp = Writefile.readxml(glovar.trafficpath, 1) grib = Datainterface.GribData() remote_url = os.path.join(r'\\ANALYSIS\\CMPA', now.strftime('%Y'), now.strftime('%Y%m%d')) localdir = r'/home/cqkj/QHTraffic/Product/Product/mirror/rainlive' grib.mirror('FRT_CHN_0P05_3HOR', remote_url, localdir, ftp) rname = sorted(os.listdir(localdir))[-1] rpath = localdir + rname data, lat, lon, _ = Znwg.arrange((grib.readGrib(rpath))) data = interp.interpolateGridData(data, lat, lon, glovar.lat, glovar.lon) dataset = data[np.newaxis, ] # 符合形状要求 res = FloodModel.cal2(dataset, dr) return res[0]
def mirrorskgrib(path): # 6月15待测试 # 还需写一个同步实况小时所需数据的代码(包括三网格), 滞后15分钟,可使用同一个config文件 grid = Datainterface.GribData() now = datetime.datetime.now() elements, subdirs, localdirs, _, freq, *ftp = Writefile.readxml( path, 0) # freq应为None elements = elements.split(',') subdirs = subdirs.split(',') localdirs = localdirs.split(',') # 为三个文件夹目录 remote_urls = [ os.path.join(subdir, now.strftime('%Y'), now.strftime('%Y%m%d')) for subdir in subdirs ] # 构造三个路径 for localdir, element, remote_url in zip(localdirs, elements, remote_urls): grid.mirror(element, remote_url, localdir, ftp) # 同步至每个文件夹,此处待测试,需保证范围在08-20或次日20-当日08时 # 查看各文件夹里数据信息,此处默认TEM, RAIN, RH 为08-20时的文件名列表 RAIN, RH, TEM = [ sorted(os.listdir(localdir))[-1] for localdir in localdirs ] # 零时开始至今 tem = Znwg.arrange(grid.readGrib(os.path.join(localdirs[2], TEM))) lat, lon = tem[1], tem[2] temdata = np.array( np.nan_to_num( interp.interpolateGridData(tem[0] - 273.15, lat, lon, glovar.lat, glovar.lon))) raindata = np.array( np.nan_to_num( interp.interpolateGridData( Znwg.arrange(grid.readGrib(os.path.join(localdirs[0], RAIN)))[0], lat, lon, glovar.lat, glovar.lon))) rhdata = np.array( np.nan_to_num( interp.interpolateGridData( Znwg.arrange(grid.readGrib(os.path.join(localdirs[1], RH)))[0], lat, lon, glovar.lat, glovar.lon))) Time = datetime.datetime.now().strftime('%Y%m%d%H') savepath = ''.join(r'/home/cqkj/QHTraffic/tmp/ele', Time, r'.pkl') # 存储每个时刻的降水、湿度、温度 with open(savepath, 'wb') as f: # 文件名称用时间区分,精确到小时 pickle.dump([temdata, raindata, rhdata], f) return temdata, raindata, rhdata '''
def Weatherdata(path): # ????????????????????? elements, subdirs, localdir, _, freq, *ftp = Writefile.readxml(path, 1) now = datetime.datetime.now() elements = elements.split(',') subdirs = subdirs.split(',') remote_urls = [ os.path.join(subdir, now.strftime('%Y'), now.strftime('%Y%m%d')) for subdir in subdirs ] # ?????? grib = Datainterface.GribData() ''' [grib.mirror(element, remote_url, localdir, ftp, freq=freq) for element, remote_url in zip(elements[:-1], remote_urls[:-1])] # ???????????????????????????(24003) ''' for element, remote_url in zip(elements[:-1], remote_urls[:-1]): grib.mirror(element, remote_url, localdir, ftp, freq=freq) grib.mirror(elements[-1], remote_urls[-1], localdir, ftp, freq='24024') # ??????????? # ?????????????????????????????????????pattern strings = ','.join(os.listdir(localdir)) patterns = [ r'(\w+.EDA.*?.GRB2)', r'(\w+.ERH.*?.GRB2)', r'(\w+.TMP.*?.GRB2)', r'(\w+.ER24.*?.GRB2)' ] allpath = [ localdir + sorted(Znwg.regex(pattern, strings), key=str.lower)[-1] for pattern in patterns ] # allpath????????????????????? ele14list = slice(1, 74, 8) # ??+2-1??????????????10?????14????????? ####????????wind????u???v?? wind = grib.readGrib(allpath[0])[0] windu_v = np.array([v for _, v in wind.items()]) windu, windv = windu_v[::2][ele14list], windu_v[1::2][ele14list] data = np.array([ Znwg.arrange(grib.readGrib(path))[0][ele14list] for path in allpath[1:-1] ]) # ????????????????? #er, lat, lon, size = Znwg.arrange(grib.readGrib(allpath[-1], nlat=glovar.lat, nlon=glovar.lon)) # ??????????????????????????????????????? er, lat, lon, size = Znwg.arrange( [grib.readGrib(allpath[-1], nlat=glovar.latt, nlon=glovar.lonn)][0]) result = windu, windv, *data, er # ??????????[4,10,181,277]???? return result, lat, lon
def mirrorskskt(newlat, newlon, hours=1): # 6.14.16测试 # 从cimiss中获取所有站点地温插值,hours控制选取的时间间隔 interfaceId = 'getSurfEleInRegionByTimeRange' elements = "Station_Id_C,Lat,Lon,GST,Year,Mon,Day,Hour" # 设定每天8:00、 20:00运行 lastime = datetime.datetime.now().replace( minute=0, second=0) - datetime.timedelta(hours=8) firstime = lastime - datetime.timedelta(hours=hours) temp = ('[', firstime.strftime('%Y%m%d%H%M%S'), ',', lastime.strftime('%Y%m%d%H%M%S'), ']') Time = ''.join(temp) params = { 'dataCode': "SURF_CHN_MUL_HOR", # 需更改为地面逐小时资料 'elements': elements, 'timeRange': Time, 'adminCodes': "630000" } initData = Datainterface.cimissdata(interfaceId, elements, **params) # 获取出cimiss初始数据 initData.Time = pd.to_datetime(initData.Time) # 需按时间进行划分插值,得到 地温网格数据 timeList = pd.to_datetime(initData.Time.unique()).strftime( '%Y%m%d%H%M%S') # 此处可能存在问题,目的仅按顺序取出来时间 initData.set_index(initData.Time, inplace=True) oneHourgrid = [ np.nan_to_num( interp.interpolateGridData( initData.loc[tm].GST.values.astype('float32'), initData.loc[tm].Lat.values.astype('float32'), initData.loc[tm].Lon.values.astype('float32'), glovar.lat, glovar.lon)) for tm in timeList ] Time = datetime.datetime.now().strftime('%Y%m%d%H') savepath = ''.join(r'/home/cqkj/QHTraffic/tmp/skt', Time, r'.pkl') with open(savepath, 'rb') as f: # 文件名用时间区分精确到小时,供updateSnow使用 pickle.dump(oneHourgrid, f) return oneHourgrid
def liverain(path, pklpath): # ???????ZNWG???????????????pickle # ???????????????????????????? elements, _, localdir, historydir, freq, *ftp = Writefile.readxml(path, 1) now = datetime.datetime.now() ytd = now - datetime.timedelta(days=1) dir = r'//ANALYSIS//CMPA//0P05' remote_url = os.path.join(dir, now.strftime('%Y'), now.strftime('%Y%m%d')) grb = Datainterface.GribData() grb.mirror('FAST_CHN_0P05_DAY-PRE', remote_url, localdir, ftp, freq=None) # ?????????????? rainpaths = sorted(os.listdir(localdir))[-1] os.chdir(localdir) rainlive, lat, lon, res = Znwg.arrange( [grb.readGrib(rainpaths, nlat=glovar.latt, nlon=glovar.lonn)][0]) ####?????????????????? with open(pklpath, 'rb') as f: data = pickle.load(f) data.append(rainlive) # ????deque???? with open(pklpath, 'wb') as f: pickle.dump(rainlive, f) return rainlive