Esempio n. 1
0
def pdfSetup(lonlat, pdfoption='kde', delta=None, nMesh=50):
    nSamples, dims = np.shape(lonlat)
    if dims != 2:
        print 'Error in lonlat, dimensions are not correct'
        print 'Check inputs to pdfSetup in casualtyEstimate.py'
        exit()
    '''
    #checks if KDE is necessary...no rotation. Debris always lands in a given location
    lonCheck = lonlat[:,0]
    latCheck = lonlat[:,1]
    lonSTD = np.std(lonCheck)
    latSTD = np.std(latCheck)
    '''

    xyz, x, y, z = getxyz(lonlat)
    xmean = np.mean(x)
    ymean = np.mean(y)
    zmean = np.mean(z)
    mag = (xmean**2 + ymean**2 + zmean**2)
    lonlat0 = getlonlat(xmean / mag, ymean / mag, zmean / mag)
    lon0 = lonlat0[0, 0]
    lat0 = lonlat0[0, 1]
    beta = 0  # initial guess
    beta = optimizeHeadingAngle(lonlat, nSamples, lon0, lat0, beta)

    R1 = Rotz(lon0 * np.pi / 180.)  # generating rotation matrices
    R2 = Roty(-lat0 * np.pi / 180.)
    R3 = Rotx(beta * np.pi / 180.)
    transformDetails = [R1, R2, R3, lon0, lat0, beta]

    Uint = np.dot(R2, R1)
    U = np.dot(R3, Uint)
    lonlatPframe = originalFrame2Pframe(lonlat, U)

    if pdfoption.lower() == 'kde' or pdfoption.lower() == 'kernel':
        xMeshP, yMeshP, xlen, ylen, areaInt = statsPython.areaOfInterestKDE(
            lonlatPframe, nMesh=nMesh)
    #xMeshP,yMeshP,xlen,ylen,areaInt = statsPython.areaOfInterestKDE(lonlatPframe,delta) # A frame could also be used
    elif pdfoption == 'normal' or pdfoption == 'gaussian' or pdfoption == 'gauss':
        #mean,covar = meancov.meancovmatrix(lonlatPframe,nSamples)
        #xMeshP,yMeshP,xlen,ylen = statsPython.areaOfInterest(mean,covar,delta,nsigma)
        xMeshP, yMeshP, xlen, ylen, areaInt = statsPython.areaOfInterestKDE(
            lonlatPframe, nMesh=50)  # A frame could also be used
    lonlatPframeMesh = np.zeros((np.size(xMeshP), 2))
    lonlatPframeMesh[:, 0] = np.reshape(xMeshP, np.size(xMeshP))
    lonlatPframeMesh[:, 1] = np.reshape(yMeshP, np.size(yMeshP))

    lonlatOrFrameMesh = Pframe2originalFrame(lonlatPframeMesh, U)
    lonOrMesh = np.reshape(lonlatOrFrameMesh[:, 0], np.shape(xMeshP))
    latOrMesh = np.reshape(lonlatOrFrameMesh[:, 1], np.shape(xMeshP))

    PframeVals = [xMeshP, yMeshP, lonlatPframe]
    OrFrameVals = [lonOrMesh, latOrMesh]
    rotVals = [U, transformDetails, areaInt]

    return (PframeVals, OrFrameVals, rotVals)
Esempio n. 2
0
def pdfSetup(lonlat,pdfoption='kde',delta=None,nMesh=50):
    nSamples,dims = np.shape(lonlat)
    if dims!=2:
        print 'Error in lonlat, dimensions are not correct'
        print 'Check inputs to pdfSetup in casualtyEstimate.py'
        exit()
    '''
    #checks if KDE is necessary...no rotation. Debris always lands in a given location
    lonCheck = lonlat[:,0]
    latCheck = lonlat[:,1]
    lonSTD = np.std(lonCheck)
    latSTD = np.std(latCheck)
    '''
    
    xyz,x,y,z = getxyz(lonlat)
    xmean = np.mean(x)
    ymean = np.mean(y)
    zmean = np.mean(z)
    mag = (xmean**2+ymean**2+zmean**2)
    lonlat0 = getlonlat(xmean/mag,ymean/mag,zmean/mag)
    lon0 = lonlat0[0,0]
    lat0 = lonlat0[0,1]
    beta = 0 # initial guess
    beta = optimizeHeadingAngle(lonlat,nSamples,lon0,lat0,beta)

    R1 = Rotz(lon0*np.pi/180.) # generating rotation matrices
    R2 = Roty(-lat0*np.pi/180.)
    R3 = Rotx(beta*np.pi/180.)
    transformDetails = [R1,R2,R3,lon0,lat0,beta]
    
    Uint = np.dot(R2,R1)
    U  = np.dot(R3,Uint)
    lonlatPframe = originalFrame2Pframe(lonlat,U)
    
    
    if pdfoption.lower()=='kde' or pdfoption.lower()=='kernel':
        xMeshP,yMeshP,xlen,ylen,areaInt= statsPython.areaOfInterestKDE(lonlatPframe,nMesh = nMesh)
    #xMeshP,yMeshP,xlen,ylen,areaInt = statsPython.areaOfInterestKDE(lonlatPframe,delta) # A frame could also be used
    elif pdfoption=='normal' or pdfoption=='gaussian' or pdfoption=='gauss':
        #mean,covar = meancov.meancovmatrix(lonlatPframe,nSamples)
        #xMeshP,yMeshP,xlen,ylen = statsPython.areaOfInterest(mean,covar,delta,nsigma)
        xMeshP,yMeshP,xlen,ylen,areaInt= statsPython.areaOfInterestKDE(lonlatPframe,nMesh = 50) # A frame could also be used 
    lonlatPframeMesh = np.zeros((np.size(xMeshP),2))
    lonlatPframeMesh[:,0] = np.reshape(xMeshP,np.size(xMeshP))
    lonlatPframeMesh[:,1] = np.reshape(yMeshP,np.size(yMeshP))
    
    
    lonlatOrFrameMesh = Pframe2originalFrame(lonlatPframeMesh,U)
    lonOrMesh = np.reshape(lonlatOrFrameMesh[:,0],np.shape(xMeshP))
    latOrMesh = np.reshape(lonlatOrFrameMesh[:,1],np.shape(xMeshP))
    
    PframeVals = [xMeshP,yMeshP,lonlatPframe]
    OrFrameVals = [lonOrMesh,latOrMesh]
    rotVals = [U,transformDetails,areaInt]

    return (PframeVals,OrFrameVals,rotVals)
Esempio n. 3
0
def calculateKLMain(refLonLat, lonlatList, delta, nsigma, pdfoption):

    # NEED RECTANGULAR MESH IN ORIGINAL FRAME

    xMesh, yMesh, xlen, ylen = statsPython.areaOfInterestKDE(refLonLat,
                                                             delta)  #

    A = delta**2

    lonMesh, latMesh, ZZpdfNMain = calculatePDF(refLonLat, xMesh, yMesh,
                                                pdfoption)
    nlist = []
    KLlist = []
    ZZpdfNMain = ZZpdfNMain * A
    ZZpdfNMain[ZZpdfNMain == 0] = 10.**-50.
    KLsurf = []
    #print 'part1 done'
    for index in range(len(lonlatList)):
        lonlat = lonlatList[index]
        nSamples, colvals = np.shape(lonlat)
        lonMesh, latMesh, ZZpdfC = calculatePDF(lonlat, xMesh, yMesh,
                                                pdfoption)
        ZZpdfCarea = A * ZZpdfC
        #KL dist
        ZZpdfCarea[ZZpdfCarea == 0] = 10.**-50.
        Log1 = np.log(ZZpdfNMain) - np.log(ZZpdfCarea)
        intMult = ZZpdfNMain * Log1
        KLDist = np.sum(intMult)
        KLlist.append(KLDist)
        nlist.append(nSamples)
        KLsurf.append([lonMesh, latMesh, ZZpdfC])
    return (nlist, KLlist, KLsurf)
Esempio n. 4
0
def calculateKLMain(refLonLat,lonlatList,delta,nsigma,pdfoption):
    
    # NEED RECTANGULAR MESH IN ORIGINAL FRAME
    
    xMesh,yMesh,xlen,ylen= statsPython.areaOfInterestKDE(refLonLat,delta) # 

    A = delta**2

    lonMesh,latMesh,ZZpdfNMain = calculatePDF(refLonLat,xMesh,yMesh,pdfoption)
    nlist = []
    KLlist = []
    ZZpdfNMain = ZZpdfNMain*A
    ZZpdfNMain[ZZpdfNMain==0] = 10.**-50.
    KLsurf = []
    #print 'part1 done'
    for index in range(len(lonlatList)):
        lonlat = lonlatList[index]
        nSamples,colvals = np.shape(lonlat)
        lonMesh,latMesh,ZZpdfC = calculatePDF(lonlat,xMesh,yMesh,pdfoption)
        ZZpdfCarea = A*ZZpdfC
        #KL dist
        ZZpdfCarea[ZZpdfCarea==0] = 10.**-50.
        Log1 = np.log(ZZpdfNMain) - np.log(ZZpdfCarea)
        intMult = ZZpdfNMain*Log1
        KLDist = np.sum(intMult)
        KLlist.append(KLDist)
        nlist.append(nSamples)
        KLsurf.append([lonMesh,latMesh,ZZpdfC])
    return (nlist,KLlist,KLsurf)
Esempio n. 5
0
def pdfSetup(lonlat=None,nSamples=None,delta=None,nsigma=None,pdfoption=None):
         

    # lon0, lat0 : main vehicle lat and lon used as initial conditions for debris propagation
    # beta : main vehicle heading angle (counterclockwise starting East) [deg]
    #import time

    xyz,x,y,z = getxyz(lonlat)
    xmean = np.mean(x)
    ymean = np.mean(y)
    zmean = np.mean(z)
    mag = (xmean**2+ymean**2+zmean**2)
    lonlat0 = getlonlat(xmean/mag,ymean/mag,zmean/mag)
    lon0 = lonlat0[0,0]
    lat0 = lonlat0[0,1]
    beta = 0 # initial guess
    beta = optimizeHeadingAngle(lonlat,nSamples,lon0,lat0,beta)

    R1 = Rotz(lon0*np.pi/180.) # generating rotation matrices
    R2 = Roty(-lat0*np.pi/180.)
    R3 = Rotx(beta*np.pi/180.)
    transformDetails = [R1,R2,R3,lon0,lat0,beta]

    Uint = np.dot(R2,R1)
    U  = np.dot(R3,Uint)
    lonlatPframe = originalFrame2Pframe(lonlat,U)

    # error check for this case not yet implemented

    #mean,covar = meancov.meancovmatrix(lonlatPframe,nSamples)
    #D,Ulocal = np.linalg.eig(covar)

    #lonlatAframe = np.dot(lonlatPframe,Ulocal) 

    #    ZZ = meancov.normalbivariate(mean,covar,XX,YY,xlen,ylen) #fortran version
    if pdfoption.lower()=='kde' or pdfoption.lower()=='kernel':
        xMeshP,yMeshP,xlen,ylen,areaInt= statsPython.areaOfInterestKDE(lonlatPframe,nMesh = 50) # A frame could also be used
        #xMeshP,yMeshP,xlen,ylen,areaInt = statsPython.areaOfInterestKDE(lonlatPframe,delta) # A frame could also be used

    elif pdfoption=='normal' or pdfoption=='gaussian' or pdfoption=='gauss':
        mean,covar = meancov.meancovmatrix(lonlatPframe,nSamples)
        xMeshP,yMeshP,xlen,ylen = statsPython.areaOfInterest(mean,covar,delta,nsigma)
            
            
            
    lonlatPframeMesh = np.zeros((np.size(xMeshP),2))
    lonlatPframeMesh[:,0] = np.reshape(xMeshP,np.size(xMeshP))
    lonlatPframeMesh[:,1] = np.reshape(yMeshP,np.size(yMeshP))

            
    lonlatOrFrameMesh = Pframe2originalFrame(lonlatPframeMesh,U)
    lonOrMesh = np.reshape(lonlatOrFrameMesh[:,0],np.shape(xMeshP))
    latOrMesh = np.reshape(lonlatOrFrameMesh[:,1],np.shape(xMeshP))


    return (lonlatPframe,lonOrMesh,latOrMesh,U,xMeshP,yMeshP,transformDetails,areaInt)
Esempio n. 6
0
def getPDFWeighted(lonlat, delta=None, nMesh=50, weightArray=1.0):
    #this routine returns a weighted KDE formulation...useful for Ec calculation
    #each sample get weighted by its corresponded weightArray
    nSamples, dims = np.shape(lonlat)
    if dims != 2:
        print 'Error in lonlat, dimensions are not correct'
        print 'Check inputs to getPDF in casualtyEstimate.py'
        exit()

    xyz, x, y, z = getxyz(lonlat)
    xmean = np.mean(x)
    ymean = np.mean(y)
    zmean = np.mean(z)
    mag = (xmean**2 + ymean**2 + zmean**2)
    lonlat0 = getlonlat(xmean / mag, ymean / mag, zmean / mag)
    lon0 = lonlat0[0, 0]
    lat0 = lonlat0[0, 1]
    beta = 0  # initial guess
    beta = optimizeHeadingAngle(lonlat, nSamples, lon0, lat0, beta)

    R1 = Rotz(lon0 * np.pi / 180.)  # generating rotation matrices
    R2 = Roty(-lat0 * np.pi / 180.)
    R3 = Rotx(beta * np.pi / 180.)
    transformDetails = [R1, R2, R3, lon0, lat0, beta]

    Uint = np.dot(R2, R1)
    U = np.dot(R3, Uint)
    lonlatPframe = originalFrame2Pframe(lonlat, U)

    xMeshP, yMeshP, xlen, ylen, areaInt = statsPython.areaOfInterestKDE(
        lonlatPframe, delta=delta, nMesh=nMesh)

    lonlatPframeMesh = np.zeros((np.size(xMeshP), 2))
    lonlatPframeMesh[:, 0] = np.reshape(xMeshP, np.size(xMeshP))
    lonlatPframeMesh[:, 1] = np.reshape(yMeshP, np.size(yMeshP))
    row, col = np.shape(xMeshP)

    ZZpdfPframe = SK.serialWeightKDE(int(nSamples), lonlatPframe, row, col,
                                     xMeshP, yMeshP, weightArray)

    lonlatOrFrameMesh = Pframe2originalFrame(lonlatPframeMesh, U)
    lonOrMesh = np.reshape(lonlatOrFrameMesh[:, 0], [ylen, xlen])
    latOrMesh = np.reshape(lonlatOrFrameMesh[:, 1], [ylen, xlen])
    OrFrameVals = [lonOrMesh, latOrMesh]
    PframeVals = [xMeshP, yMeshP, ZZpdfPframe]
    return (PframeVals, OrFrameVals, transformDetails, areaInt)
Esempio n. 7
0
def getPDFWeighted(lonlat,delta=None,nMesh=50,weightArray=1.0):
    #this routine returns a weighted KDE formulation...useful for Ec calculation
    #each sample get weighted by its corresponded weightArray
    nSamples,dims = np.shape(lonlat)
    if dims!=2:
        print 'Error in lonlat, dimensions are not correct'
        print 'Check inputs to getPDF in casualtyEstimate.py'
        exit()
    
    xyz,x,y,z = getxyz(lonlat)
    xmean = np.mean(x)
    ymean = np.mean(y)
    zmean = np.mean(z)
    mag = (xmean**2+ymean**2+zmean**2)
    lonlat0 = getlonlat(xmean/mag,ymean/mag,zmean/mag)
    lon0 = lonlat0[0,0]
    lat0 = lonlat0[0,1]
    beta = 0 # initial guess
    beta = optimizeHeadingAngle(lonlat,nSamples,lon0,lat0,beta)
    
    R1 = Rotz(lon0*np.pi/180.) # generating rotation matrices
    R2 = Roty(-lat0*np.pi/180.)
    R3 = Rotx(beta*np.pi/180.)
    transformDetails = [R1,R2,R3,lon0,lat0,beta]
    
    Uint = np.dot(R2,R1)
    U  = np.dot(R3,Uint)
    lonlatPframe = originalFrame2Pframe(lonlat,U)
 
    xMeshP,yMeshP,xlen,ylen,areaInt = statsPython.areaOfInterestKDE(lonlatPframe,delta=delta,nMesh=nMesh)     
    
    lonlatPframeMesh = np.zeros((np.size(xMeshP),2))
    lonlatPframeMesh[:,0] = np.reshape(xMeshP,np.size(xMeshP))
    lonlatPframeMesh[:,1] = np.reshape(yMeshP,np.size(yMeshP))
    row,col = np.shape(xMeshP)
    
    ZZpdfPframe = SK.serialWeightKDE(int(nSamples),lonlatPframe,row,col,xMeshP,yMeshP,weightArray)

    lonlatOrFrameMesh = Pframe2originalFrame(lonlatPframeMesh,U)
    lonOrMesh = np.reshape(lonlatOrFrameMesh[:,0],[ylen,xlen])
    latOrMesh = np.reshape(lonlatOrFrameMesh[:,1],[ylen,xlen])
    OrFrameVals = [lonOrMesh,latOrMesh]
    PframeVals = [xMeshP,yMeshP,ZZpdfPframe]
    return (PframeVals,OrFrameVals,transformDetails,areaInt)
Esempio n. 8
0
def getPDF(lonlat,nSamples,delta,nsigma,pdfoption):
    # lon0, lat0 : main vehicle lat and lon used as initial conditions for debris propagation
    # beta : main vehicle heading angle (counterclockwise starting East) [deg]
    
    
    xyz,x,y,z = getxyz(lonlat)
    xmean = np.mean(x)
    ymean = np.mean(y)
    zmean = np.mean(z)
    mag = (xmean**2+ymean**2+zmean**2)**.5
    lonlat0 = getlonlat(xmean/mag,ymean/mag,zmean/mag)
    lon0 = lonlat0[0,0]
    lat0 = lonlat0[0,1]
    beta = 0 # initial guess
    beta = optimizeHeadingAngle(lonlat,nSamples,lon0,lat0,beta)

    R1 = Rotz(lon0*np.pi/180.) # generating rotation matrices
    R2 = Roty(-lat0*np.pi/180.)
    R3 = Rotx(beta*np.pi/180.)
    transformDetails = [R1,R2,R3,lon0,lat0,beta]
    
    Uint = np.dot(R2,R1)
    U  = np.dot(R3,Uint)
    lonlatPframe = originalFrame2Pframe(lonlat,U)
    # error check for this case not yet implemented
    
    #mean,covar = meancov.meancovmatrix(lonlatPframe,nSamples)
    #D,Ulocal = np.linalg.eig(covar)

    #lonlatAframe = np.dot(lonlatPframe,Ulocal) 
    
    
    
    #    ZZ = meancov.normalbivariate(mean,covar,XX,YY,xlen,ylen) #fortran version
    if pdfoption.lower()=='kde' or pdfoption.lower()=='kernel':
        xMeshP,yMeshP,xlen,ylen,areaInt = statsPython.areaOfInterestKDE(lonlatPframe,delta) # A frame could also be used
    elif pdfoption=='normal' or pdfoption=='gaussian' or pdfoption=='gauss':
        mean,covar = meancov.meancovmatrix(lonlatPframe,nSamples)
        xMeshP,yMeshP,xlen,ylen = statsPython.areaOfInterest(mean,covar,delta,nsigma)
   
        
    lonlatPframeMesh = np.zeros((np.size(xMeshP),2))
    lonlatPframeMesh[:,0] = np.reshape(xMeshP,np.size(xMeshP))
    lonlatPframeMesh[:,1] = np.reshape(yMeshP,np.size(yMeshP))
    lonlatOrFrameMesh = Pframe2originalFrame(lonlatPframeMesh,U)
    lonMesh,latMesh = getLonLatMesh(lonlatOrFrameMesh[:,0],lonlatOrFrameMesh[:,1],delta)

    lonVec = np.reshape(lonMesh,np.size(lonMesh))
    latVec = np.reshape(latMesh,np.size(latMesh))
    lonlatVec = np.zeros((len(lonVec),2))
    lonlatVec[:,0] = lonVec
    lonlatVec[:,1] = latVec
    lonlatVecP = originalFrame2Pframe(lonlatVec,U)
    ylen,xlen = np.shape(lonMesh)
    lonMeshP = np.reshape(lonlatVecP[:,0],[ylen,xlen])
    latMeshP = np.reshape(lonlatVecP[:,1],[ylen,xlen])


    pqrVec ,pVec,qVec,rVec= getxyz(lonlatVecP) # mesh locations in P frame

          
    if pdfoption.lower()=='kde' or pdfoption.lower()=='kernel':
        #ZZpdf = meancov.kde(lonlatPframe,lonMeshP,latMeshP,[nSamples, xlen,ylen])
        print 'Starting quad KDE'
        ZZpdf = SK.serialK(nSamples,lonlatPframe,ylen,xlen,lonMeshP,latMeshP,.1,1)
        print 'Done with quad KDE'
    elif pdfoption=='normal' or pdfoption=='gaussian' or pdfoption=='gauss':
        ZZpdf = meancov.normalbivariate(mean,covar,lonMeshP,latMeshP,xlen,ylen) #fortran version

 
    pdfN = transformPDF(U,pqrVec,np.pi/180*lonlatVec[:,0],np.pi/180*lonlatVec[:,1],np.reshape(ZZpdf,xlen*ylen))
    ZZpdfN = np.reshape(pdfN,[ylen,xlen])
    #plt.figure()
    #plt.contourf(lonMesh,latMesh,np.reshape(pdfN,[ylen,xlen]))
    #plt.show()
    return (lonMesh,latMesh,ZZpdfN,lonMeshP,latMeshP,xlen,ylen,transformDetails,areaInt) #
Esempio n. 9
0
def getPDFWeighted(lonlat,nSamples,delta,nsigma,AcasArray):
    # lon0, lat0 : main vehicle lat and lon used as initial conditions for debris propagation
    # beta : main vehicle heading angle (counterclockwise starting East) [deg]
    #import time
    
    xyz,x,y,z = getxyz(lonlat)
    xmean = np.mean(x)
    ymean = np.mean(y)
    zmean = np.mean(z)
    mag = (xmean**2+ymean**2+zmean**2)
    lonlat0 = getlonlat(xmean/mag,ymean/mag,zmean/mag)
    lon0 = lonlat0[0,0]
    lat0 = lonlat0[0,1]
    beta = 0 # initial guess
    beta = optimizeHeadingAngle(lonlat,nSamples,lon0,lat0,beta)
    
    R1 = Rotz(lon0*np.pi/180.) # generating rotation matrices
    R2 = Roty(-lat0*np.pi/180.)
    R3 = Rotx(beta*np.pi/180.)
    transformDetails = [R1,R2,R3,lon0,lat0,beta]
    
    Uint = np.dot(R2,R1)
    U  = np.dot(R3,Uint)
    lonlatPframe = originalFrame2Pframe(lonlat,U)
    
    # error check for this case not yet implemented
    
    #mean,covar = meancov.meancovmatrix(lonlatPframe,nSamples)
    #D,Ulocal = np.linalg.eig(covar)
    
    #lonlatAframe = np.dot(lonlatPframe,Ulocal) 
    
    
    #    ZZ = meancov.normalbivariate(mean,covar,XX,YY,xlen,ylen) #fortran version
    xMeshP,yMeshP,xlen,ylen= statsPython.areaOfInterestKDE(lonlatPframe,delta) # A frame could also be used

    
    lonlatPframeMesh = np.zeros((np.size(xMeshP),2))
    lonlatPframeMesh[:,0] = np.reshape(xMeshP,np.size(xMeshP))
    lonlatPframeMesh[:,1] = np.reshape(yMeshP,np.size(yMeshP))
    row,col = np.shape(xMeshP)
    #print xMeshP
       
    ZZpdfPframe = SK.serialWeightKDE(int(nSamples),lonlatPframe,row,col,xMeshP,yMeshP,AcasArray)

    
    
    lonlatOrFrameMesh = Pframe2originalFrame(lonlatPframeMesh,U)
    lonOrMesh = np.reshape(lonlatOrFrameMesh[:,0],[ylen,xlen])
    latOrMesh = np.reshape(lonlatOrFrameMesh[:,1],[ylen,xlen])
    
    #ZZpdfPframe0 = np.reshape(ZZpdfPframe0,[ylen,xlen])
    '''
        print 'maxABS',np.max(np.abs(ZZpdfPframe-ZZpdfPframe0))
        plt.figure()
        plt.contourf(xMeshP,yMeshP,ZZpdfPframe)
        plt.figure()
        plt.contourf(xMeshP,yMeshP,ZZpdfPframe0)
        plt.figure()
        plt.contourf(xMeshP,yMeshP,ZZpdfPframe0-ZZpdfPframe)
        
        
        plt.figure()
        plt.plot(lonlatPframe[:,0],lonlatPframe[:,1],'x')
        plt.figure()
        plt.contourf(lonOrMesh,latOrMesh,ZZpdfPframe)
        plt.plot(lonlat[:,0],lonlat[:,1],'x')
        plt.show()
        '''
    return (xMeshP,yMeshP,ZZpdfPframe,lonOrMesh,latOrMesh,xlen,ylen,transformDetails) #
Esempio n. 10
0
def getPDF(lonlat,pdfoption='kde',delta=None,nMesh=50):
    #lonlat is a nx2 matrix with lon lat locations
    # nsigma is only used for getting PDF area when using normal distribution
    # pdf option 'kde' or 'normal' dist 
    # delta is spacing used for mesh in PDF
    # if delta not specified,then it will use nMesh which is the number of points used in one dim...total mesh will be nMesh x nMesh
    
    #this routine rotates the SPHERICAL planet to get a better 'flat planet' approximation by using local lat and lons and then applies the KDE or normal PDF routines
    
    xyz,x,y,z = getxyz(lonlat)
    xmean = np.mean(x)
    ymean = np.mean(y)
    zmean = np.mean(z)
    mag = (xmean**2+ymean**2+zmean**2)**.5
    lonlat0 = getlonlat(xmean/mag,ymean/mag,zmean/mag)
    lon0 = lonlat0[0,0]
    lat0 = lonlat0[0,1]
    beta = 0 # initial guess
    nSamples,dims = np.shape(lonlat)
    if dims!=2:
        print 'Error in lonlat, dimensions are not correct'
        print 'Check inputs to getPDF in casualtyEstimate.py'
        exit()
    
    beta = optimizeHeadingAngle(lonlat,nSamples,lon0,lat0,beta)
    
    R1 = Rotz(lon0*np.pi/180.) # generating rotation matrices
    R2 = Roty(-lat0*np.pi/180.)
    R3 = Rotx(beta*np.pi/180.)
    transformDetails = [R1,R2,R3,lon0,lat0,beta]
    
    Uint = np.dot(R2,R1)
    U  = np.dot(R3,Uint)
    lonlatPframe = originalFrame2Pframe(lonlat,U)

    if pdfoption.lower()=='kde' or pdfoption.lower()=='kernel':
        xMeshP,yMeshP,xlen,ylen,areaInt = statsPython.areaOfInterestKDE(lonlatPframe,delta=delta,nMesh=nMesh) # A frame could also be used
    elif pdfoption=='normal' or pdfoption=='gaussian' or pdfoption=='gauss':
        #mean,covar = meancov.meancovmatrix(lonlatPframe,nSamples)
        #xMeshP,yMeshP,xlen,ylen = statsPython.areaOfInterest(mean,covar,delta,nsigma)
        xMeshP,yMeshP,xlen,ylen,areaInt = statsPython.areaOfInterestKDE(lonlatPframe,delta=delta,nMesh=nMesh) # A frame could also be used

    
    
    lonlatPframeMesh = np.zeros((np.size(xMeshP),2))
    lonlatPframeMesh[:,0] = np.reshape(xMeshP,np.size(xMeshP))
    lonlatPframeMesh[:,1] = np.reshape(yMeshP,np.size(yMeshP))
    lonlatOrFrameMesh = Pframe2originalFrame(lonlatPframeMesh,U)
    lonMesh,latMesh = getLonLatMesh(lonlatOrFrameMesh[:,0],lonlatOrFrameMesh[:,1],delta)
    
    lonVec = np.reshape(lonMesh,np.size(lonMesh))
    latVec = np.reshape(latMesh,np.size(latMesh))
    lonlatVec = np.zeros((len(lonVec),2))
    lonlatVec[:,0] = lonVec
    lonlatVec[:,1] = latVec
    lonlatVecP = originalFrame2Pframe(lonlatVec,U)
    ylen,xlen = np.shape(lonMesh)
    lonMeshP = np.reshape(lonlatVecP[:,0],[ylen,xlen])
    latMeshP = np.reshape(lonlatVecP[:,1],[ylen,xlen])
    
    
    pqrVec ,pVec,qVec,rVec= getxyz(lonlatVecP) # mesh locations in P frame
    
    
    if pdfoption.lower()=='kde' or pdfoption.lower()=='kernel':
        #ZZpdf = meancov.kde(lonlatPframe,lonMeshP,latMeshP,[nSamples, xlen,ylen])
        print 'Starting quad KDE'
        ZZpdf = SK.serialK(nSamples,lonlatPframe,ylen,xlen,lonMeshP,latMeshP,.1,1)
        print 'Done with quad KDE'
    elif pdfoption=='normal' or pdfoption=='gaussian' or pdfoption=='gauss':
        ZZpdf = meancov.normalbivariate(mean,covar,lonMeshP,latMeshP,xlen,ylen) #fortran version
    
    
    pdfN = transformPDF(U,pqrVec,np.pi/180*lonlatVec[:,0],np.pi/180*lonlatVec[:,1],np.reshape(ZZpdf,xlen*ylen))
    ZZpdfN = np.reshape(pdfN,[ylen,xlen])

    PframeVals = [lonPMesh,latPMesh,ZZpdf]
    OrFrameVals = [lonMesh,latMesh,ZZpdfN]
    return (PframeVals,OrFrameVals,transformDetails,areaInt)
Esempio n. 11
0
def getPDF(lonlat, pdfoption='kde', delta=None, nMesh=50):
    #lonlat is a nx2 matrix with lon lat locations
    # nsigma is only used for getting PDF area when using normal distribution
    # pdf option 'kde' or 'normal' dist
    # delta is spacing used for mesh in PDF
    # if delta not specified,then it will use nMesh which is the number of points used in one dim...total mesh will be nMesh x nMesh

    #this routine rotates the SPHERICAL planet to get a better 'flat planet' approximation by using local lat and lons and then applies the KDE or normal PDF routines

    xyz, x, y, z = getxyz(lonlat)
    xmean = np.mean(x)
    ymean = np.mean(y)
    zmean = np.mean(z)
    mag = (xmean**2 + ymean**2 + zmean**2)**.5
    lonlat0 = getlonlat(xmean / mag, ymean / mag, zmean / mag)
    lon0 = lonlat0[0, 0]
    lat0 = lonlat0[0, 1]
    beta = 0  # initial guess
    nSamples, dims = np.shape(lonlat)
    if dims != 2:
        print 'Error in lonlat, dimensions are not correct'
        print 'Check inputs to getPDF in casualtyEstimate.py'
        exit()

    beta = optimizeHeadingAngle(lonlat, nSamples, lon0, lat0, beta)

    R1 = Rotz(lon0 * np.pi / 180.)  # generating rotation matrices
    R2 = Roty(-lat0 * np.pi / 180.)
    R3 = Rotx(beta * np.pi / 180.)
    transformDetails = [R1, R2, R3, lon0, lat0, beta]

    Uint = np.dot(R2, R1)
    U = np.dot(R3, Uint)
    lonlatPframe = originalFrame2Pframe(lonlat, U)

    if pdfoption.lower() == 'kde' or pdfoption.lower() == 'kernel':
        xMeshP, yMeshP, xlen, ylen, areaInt = statsPython.areaOfInterestKDE(
            lonlatPframe, delta=delta,
            nMesh=nMesh)  # A frame could also be used
    elif pdfoption == 'normal' or pdfoption == 'gaussian' or pdfoption == 'gauss':
        #mean,covar = meancov.meancovmatrix(lonlatPframe,nSamples)
        #xMeshP,yMeshP,xlen,ylen = statsPython.areaOfInterest(mean,covar,delta,nsigma)
        xMeshP, yMeshP, xlen, ylen, areaInt = statsPython.areaOfInterestKDE(
            lonlatPframe, delta=delta,
            nMesh=nMesh)  # A frame could also be used

    lonlatPframeMesh = np.zeros((np.size(xMeshP), 2))
    lonlatPframeMesh[:, 0] = np.reshape(xMeshP, np.size(xMeshP))
    lonlatPframeMesh[:, 1] = np.reshape(yMeshP, np.size(yMeshP))
    lonlatOrFrameMesh = Pframe2originalFrame(lonlatPframeMesh, U)
    lonMesh, latMesh = getLonLatMesh(lonlatOrFrameMesh[:, 0],
                                     lonlatOrFrameMesh[:, 1], delta)

    lonVec = np.reshape(lonMesh, np.size(lonMesh))
    latVec = np.reshape(latMesh, np.size(latMesh))
    lonlatVec = np.zeros((len(lonVec), 2))
    lonlatVec[:, 0] = lonVec
    lonlatVec[:, 1] = latVec
    lonlatVecP = originalFrame2Pframe(lonlatVec, U)
    ylen, xlen = np.shape(lonMesh)
    lonMeshP = np.reshape(lonlatVecP[:, 0], [ylen, xlen])
    latMeshP = np.reshape(lonlatVecP[:, 1], [ylen, xlen])

    pqrVec, pVec, qVec, rVec = getxyz(lonlatVecP)  # mesh locations in P frame

    if pdfoption.lower() == 'kde' or pdfoption.lower() == 'kernel':
        #ZZpdf = meancov.kde(lonlatPframe,lonMeshP,latMeshP,[nSamples, xlen,ylen])
        print 'Starting quad KDE'
        ZZpdf = SK.serialK(nSamples, lonlatPframe, ylen, xlen, lonMeshP,
                           latMeshP, .1, 1)
        print 'Done with quad KDE'
    elif pdfoption == 'normal' or pdfoption == 'gaussian' or pdfoption == 'gauss':
        ZZpdf = meancov.normalbivariate(mean, covar, lonMeshP, latMeshP, xlen,
                                        ylen)  #fortran version

    pdfN = transformPDF(U, pqrVec, np.pi / 180 * lonlatVec[:, 0],
                        np.pi / 180 * lonlatVec[:, 1],
                        np.reshape(ZZpdf, xlen * ylen))
    ZZpdfN = np.reshape(pdfN, [ylen, xlen])

    PframeVals = [lonPMesh, latPMesh, ZZpdf]
    OrFrameVals = [lonMesh, latMesh, ZZpdfN]
    return (PframeVals, OrFrameVals, transformDetails, areaInt)