def plotSiteIntensityFrequencyDistribution(self,dictSiteInfo,begYear,endYear,figName='SiteIntensityFrequencyDistribution.png'):
     allData = {}  # {'site':{'tyNum':{'lat':21,'lon':120,'grade':2}...}...}
     for iKey in dictSiteInfo.keys():
         # read data
         inputFileName="site_data/"+iKey+"_"+str(begYear)+"-"+str(endYear)+".csv"
         print("reading data from ",inputFileName)
         dataset  = pd.read_csv(inputFileName,header=None,sep=',')
         dataset  = np.array(dataset)
         m ,n     = np.shape(dataset)
         iTyNums  = dataset[:,0]
         iDate    = dataset[:,1]
         iLats    = dataset[:,2]
         iLons    = dataset[:,3]
         iGrades  = dataset[:,5]
         allData[iKey] = {}
         for i in range(m):
             tyNum = iTyNums[i]
             #对于某个台风过程影响某一站点,只从整个台风记录中选取最靠近站点的相应记录
             if tyNum not in allData[iKey].keys():
                 #第一次出现直接写入,防止台风记录只有一条时会遗漏
                 allData[iKey][tyNum] = {'lat':iLats[i],'lon':iLons[i],'grade':iGrades[i],'date':iDate[i]} 
             else:
                 latSite  = dictSiteInfo[iKey]['lat'] 
                 lonSite  = dictSiteInfo[iKey]['lon']
                 latTyPre = allData[iKey][tyNum]['lat']
                 lonTyPre = allData[iKey][tyNum]['lon']
                 latTyNow = iLats[i]
                 lonTyNow = iLons[i]
                 distSiteTyPre = custfunc.SphereDistance(lonSite,latSite,lonTyPre,latTyPre) 
                 distSiteTyNow = custfunc.SphereDistance(lonSite,latSite,lonTyNow,latTyNow) 
                 if distSiteTyNow < distSiteTyPre:
                     allData[iKey].update({tyNum:{'lat':iLats[i],'lon':iLons[i], \
                                             'grade':iGrades[i],'date':iDate[i]}})
     chinesFontSize = 10
     myfont = mpl.font_manager.FontProperties(fname=r'./chinese_font/simhei.ttf',size=chinesFontSize)
     fig = plt.gcf()
     fontsize = 10
     plt.rcParams['savefig.dpi'] = 3000 #像素
     plt.rcParams['figure.dpi'] = 3000 #分辨率
     xticks    = range(1,9)
     xtickName = [u'低于热带低压',u'热带低压',u'热带风暴',u'强热带风暴',\
               u'台风',u'强台风',u'超强台风',u'变性台风']
     countGrade = {} 
     iCountDict = {0:0,1:1,2:2,3:3,4:4,5:5,6:6,9:7}
     iCountArr  = np.zeros(8)
     maker = ['-o','-D','-s','-*','-d','-v','-+','-x','-^']
     mIdx = 0 # makerIndex
     for iKey in allData.keys(): # site is key
         siteData = allData[iKey]
         iCountArr  = np.zeros(8)
         for jKey in siteData.keys(): # tyNum is key
             jGrade = int(siteData[jKey]['grade'])
             index= iCountDict[jGrade]
             iCountArr[index] += 1
         countGrade[iKey] = iCountArr
         iStationInfo = iKey+":"+dictSiteInfo[iKey]['name'] 
         plt.plot(xticks,iCountArr,maker[mIdx],label=iStationInfo,linewidth=1.0,ms=5)     
         mIdx += 1   
     plt.legend(loc='upper right', frameon=False, prop=myfont,fontsize=fontsize) 
     plt.ylabel(u'频数(个)', fontproperties=myfont,fontsize=fontsize)
     plt.xticks(xticks,xtickName,rotation=23,fontproperties=myfont,fontsize=fontsize)
     plt.show()
     fig.savefig(figName)
     plt.close()
     return None
    def plotSiteIntensitySpatialDistribution(self,dictSiteInfo,begYear,endYear,minLat=0,maxLat=70, \
          minLon=90,maxLon=180,figName='SiteIntensitySpatailDistribution.png'):
        allData = {}  # {'site':{'tyNum':{'lat':21,'lon':120,'grade':2}...}...}
        for iKey in dictSiteInfo.keys():
            # read data
            inputFileName="site_data/"+iKey+"_"+str(begYear)+"-"+str(endYear)+".csv"
            print("reading data from ",inputFileName)
            dataset  = pd.read_csv(inputFileName,header=None,sep=',')
            dataset  = np.array(dataset)
            m ,n     = np.shape(dataset)
            iTyNums  = dataset[:,0]
            iDate    = dataset[:,1]
            iLats    = dataset[:,2]
            iLons    = dataset[:,3]
            iGrades  = dataset[:,5]
            allData[iKey] = {}
            for i in range(m):
                tyNum = iTyNums[i]
                #对于某个台风过程影响某一站点,只从整个台风记录中选取最靠近站点的相应记录
                if tyNum not in allData[iKey].keys():
                    #第一次出现直接写入,防止台风记录只有一条时会遗漏
                    allData[iKey][tyNum] = {'lat':iLats[i],'lon':iLons[i],'grade':iGrades[i],'date':iDate[i]} 
                else:
                    latSite  = dictSiteInfo[iKey]['lat'] 
                    lonSite  = dictSiteInfo[iKey]['lon']
                    latTyPre = allData[iKey][tyNum]['lat']
                    lonTyPre = allData[iKey][tyNum]['lon']
                    latTyNow = iLats[i]
                    lonTyNow = iLons[i]
                    distSiteTyPre = custfunc.SphereDistance(lonSite,latSite,lonTyPre,latTyPre) 
                    distSiteTyNow = custfunc.SphereDistance(lonSite,latSite,lonTyNow,latTyNow) 
                    if distSiteTyNow < distSiteTyPre:
                        allData[iKey].update({tyNum:{'lat':iLats[i],'lon':iLons[i],'grade':iGrades[i],'date':iDate[i]}})
        chinesFontSize = 6
        myfont = mpl.font_manager.FontProperties(fname=r'./chinese_font/simhei.ttf',size=chinesFontSize)
        fig = plt.gcf()
        plt.rcParams['savefig.dpi'] = 3000 #像素
        plt.rcParams['figure.dpi'] = 3000 #分辨率
        m = Basemap(llcrnrlon=minLon,llcrnrlat=minLat,urcrnrlon=maxLon,urcrnrlat=maxLat,resolution='h')
        m.drawcoastlines(linewidth=0.3)
        #m.drawcountries(linewidth=0.3),此命令香港会有一个方框
        #m.drawrivers(linewidth=0.3)
        chn_shp = './GADM_Shapefile/gadm36_CHN_1'
        twn_shp = './GADM_Shapefile/gadm36_TWN_1'
        hkg_shp = './GADM_Shapefile/gadm36_HKG_1'
        mac_shp = './GADM_Shapefile/gadm36_MAC_1'
        m.readshapefile(chn_shp,'chn',drawbounds=True)
        m.readshapefile(twn_shp,'twn',drawbounds=True)
        m.readshapefile(hkg_shp,'hkg',drawbounds=True)
        m.readshapefile(mac_shp,'mac',drawbounds=True)

        legendIdx = {0:u'低于热带低压',1:u'热带低压(10.8-17.1m/s)',\
                     2:u'热带风暴(17.2-24.4m/s)',3:u'强热带风暴(24.5-32.6m/s)',\
                     4:u'台风(32.7-41.4m/s)',5:u'强台风(41.5-50.9m/s)',\
                     6:u'超强台风(>51.0m/s)',9:u'变性台风'}
        colorIdx = {0:'grey',1:'brown',2:'blue', \
                    3:'green',4:'red',5:'gold',\
                    6:'magenta',9:'purple'}
        firstIdx = {0:1, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 9:1}
        handlesDict = {} # 收集第一次出现的强度,用于绘制图例
        for iKey in allData.keys():
            iSiteData = allData[iKey]
            for jKey in iSiteData.keys():
                jLat   = iSiteData[jKey]['lat']
                jLon   = iSiteData[jKey]['lon']
                jGrade = iSiteData[jKey]['grade']
                legengColorKey = int(jGrade)
                first = firstIdx[legengColorKey]
                if first != 1 : #强度等级不是第一次出现,绘制
                    plt.scatter(jLon,jLat,s=5, marker="o",\
                      color=colorIdx[legengColorKey])
                else: #强度等级第一次出现,保留以便后面绘制顺便加上图例
                    iHandle = {}
                    iHandle.update({'lon':jLon})
                    iHandle.update({'lat':jLat})
                    iHandle.update({'color':colorIdx[legengColorKey]})
                    iHandle.update({'label':legendIdx[legengColorKey]})
                    handlesDict.update({legengColorKey:iHandle})
                    firstIdx.update({legengColorKey:0})
        #统计每个等级发生的次数
        # 去重:以发生日期和台风编号为唯一索引去重
        deduplicateData = {} # {dateTyNum:{'grade':1,...}
        countGrade = {}  
        for iKey in allData.keys(): # site is key
            siteData = allData[iKey]
            for jKey in siteData.keys(): # tyNum is key
                jDate = siteData[jKey]['date']
                dateTyNum = str(int(jDate))+str(int(jKey))
                if dateTyNum not in deduplicateData:
                    jGrade = int(siteData[jKey]['grade'])
                    deduplicateData[dateTyNum] = {'grade':jGrade}
                    if jGrade in countGrade.keys():
                        numGrades = countGrade[jGrade] + 1
                        countGrade[jGrade] = numGrades
                    else:
                        countGrade[jGrade] = 1
        #print(countGrade)            
        for i in range(10): 
            if i in handlesDict.keys():
                iHandle = handlesDict[i]
                iLon    = iHandle['lon']
                iLat    = iHandle['lat']
                iColor  = iHandle['color']
                iLabel0 = iHandle['label']
                iLabel1 = u' 发生次数:'+str(countGrade[i])
                iLabel  = iLabel0+iLabel1
                plt.scatter(iLon,iLat,color=iColor,\
                    s=5, marker="o",label=iLabel)
        plt.legend(loc='lower right',prop=myfont)
        plt.show()
        fig.savefig(figName)
        plt.close()
        return None
# Read data
inputFileName = siteName + str(begYear) + "-" + str(endYear) + ".csv"
print("reading data from ", inputFileName)
dataset = pd.read_csv(inputFileName, header=None, sep=',')
dataset = np.array(dataset)
m, n = np.shape(dataset)
lats = dataset[:, 2]
lons = dataset[:, 3]
tcNum = dataset[:, 0]

print("calculate translation velocity(VT)")
VT = []
for i in range(0, m - 1):
    if tcNum[i] == tcNum[i + 1]:
        dist = func.SphereDistance(lons[i], lats[i], lons[i + 1],
                                   lats[i + 1])  # unit:KM
        VT_temp = dist / 6.0  # unit: km/h
        if VT_temp >= 2.0 and VT_temp <= 65:
            VT.append(VT_temp)
VT = np.array(VT)
print('Max and Min VT : ', np.max(VT), np.min(VT))

Var = VT
###  Normal Distrbution ##########################################
print("fitting the VT with Normal distribution")
# Fitting Data
loc, scale = st.norm.fit(Var)
Var_NormMu = loc  # mean
Var_NormStd = scale  # standard variation
print('norm Mu and Sigma :', Var_NormMu, Var_NormStd)
    lons = dataset[:, 3]
    Pres = dataset[:, 4]

    tyNum = []
    Date = []
    VT = []
    deltaP = []
    Rmax = []
    Dmin = []
    L_ST = []
    Alpha_ST = []
    Theta = []

    for i in range(0, m - 1):
        ### VT
        dist = func.SphereDistance(lons[i], lats[i], lons[i + 1],
                                   lats[i + 1])  # unit:KM
        VT_temp = dist / 6.0  # unit: km/h
        ### DeltaP
        P0 = 1010.0
        deltaP_temp = P0 - Pres[i]
        ### Rmax samples
        # obtain Rmax from center pressure with empirical formula
        Rmax_temp = 1119 * (1010 - Pres[i])**(-0.805)
        ### Dmin samples
        lon1 = lons[i]
        lat1 = lats[i]
        lon2 = lons[i + 1]
        lat2 = lats[i + 1]
        if lon1 == lon2 and lat1 == lat2:
            #print(i)
            continue
예제 #5
0
                numberTy = numberTy
                yyymmddhh = line[0:10]
                latRec = float(line[13:16]) * 0.1  #unit 1.0 degree
                lonRec = float(line[17:21]) * 0.1
                presRec = line[22:26]
                gradeRec = line[11:12]
                newLine[0] = numberTy
                newLine[1] = yyymmddhh
                newLine[2] = str(latRec)
                newLine[3] = str(lonRec)
                newLine[4] = presRec
                newLine[5] = gradeRec
                #if numberTy == "0000":
                if int(numberTy) == 0:
                    continue  # Skip nameless TC
                distTy2Site = func.SphereDistance(float(lonRec), float(latRec),
                                                  lonSite, latSite)
                if distTy2Site > radiusInflu:
                    # Skip outside the scope of influence radius
                    continue
                else:
                    # print(distTy2Site)
                    pass
                allNewLine.append(newLine)
        fileRead.close
    allNewLineArr = np.array(allNewLine)

    # output
    outFileName = "data_site/" + siteName + "_" + str(begYear) + "-" + str(
        endYear) + ".csv"
    print("output data : ", outFileName)
    np.savetxt(outFileName, allNewLineArr, delimiter=',', fmt='%s')