average latitudes and longitudes + python

2

Good morning. I have a vector with 17000 latitudes and another with 17000 lengths. Or if the same, in a vector of ordered parts [lat, lon]

The idea would be that all points within an area are unified. That is, suppose we divide the map into cells of 0.25 * 0.25. then, averaging all the points that are in that square.

the lats range from -90 to 90 and longs from 0 to 360.

latmenor = -90.
lonmenor = 0
steplat = 0.25

auxlat = []
auxfinlat = []

print "Lats size!", lats.size

auxlat = []
VectorVectoresLat = []
h = 0

while h != 180:
    if auxlat != []:
        VectorVectoresLat.append(np.average(auxlat))
    auxlat=[]
    for i in range(lats.size):
        if latmenor < lats[i] < (latmenor+steplat):
             auxlat.append(lats[i])
    h=h+steplat
    latmenor = latmenor + steplat

print VectorVectoresLat
print "longitud de aux2", len(VectorVectoresLat)

I could do it with the latitudes, but I can not make it work taking into account the lengths .. any help? Thank you!!

lats = vector with latitudes

lon = vector with lengths.

    
asked by Mr Pepa 16.06.2016 в 19:41
source

2 answers

1

A good thing about python is that it handles tuples quite well. Instead of having two separate lists, with latitudes and longitudes, it is better to have a single list of tuples:

coords = zip(lats, lon)

But first we will do better. The characteristic that identifies each coordinate is the cell to which it belongs. The best way to manipulate cells is with a dictionary:

cells = {(i,j):None for i in range(-90,  90, 0.25)
                    for j in range(  0, 360, 0.25) }

cells will be our final result. The value None will be the value of the cells that do not have a single point.

We should be careful with the edge conditions and start, for example, at the latitude -90+0.25 , but as nothing is specified in the problem we leave it as is.

Calculating the average coordinates of the points in a cell does not make much sense. I suppose that each point has an attribute that is really what we want to average, but let's adjust to the statement:

Suppose we want to average the coordinates. The average of a coordinate list is obtained with the following function average :

def average(coords):
    n = len(coords)
    return (sum(x for (x,_) in coords)/n,
            sum(y for (_,y) in coords)/n)

On the other hand, to know the cell to which a coordinate belongs:

from math import floor

def find_cell(coord):
    return (floor(coord[0]*4)/4, floor(coord[1]*4)/4))

This function can be used as clave to group the points belonging to the same cell with the function itertools.groupby . This function requires that the list be previously sorted.

Combining everything would be:

from itertools import groupby

cells = {(i,j):None for i in range(-90,  90, 0.25)
                    for j in range(  0, 360, 0.25) }

coords = sorted(zip(lats,lon), key=find_cell)

cells.update(
    (k,average(lst)) for (k,lst) in groupby(coords, key=find_cell)
)
    
answered by 18.06.2016 в 02:34
0

First of all, sorry for the delay but I was a little sick. @ChemaCortes, thank you very much for the contribution. I had just read about the tuples and I was trying this way, I also solved the problem and wanted to share with them how I did it. I enclose the code, and any comments you make about it is very welcome! If it can be improved, or if there are ways to optimize it? In fact, I understand that the FOR statement is not the strong one of python: > (and the program is very slow when there is a lot of data. Once again, thanks for the support. Being new in all this, it is good to know that there is a community like this.

    lats = read_var(nc_file, 'lat')
    lon = read_var(nc_file, 'lon')
    print len(lats)
    #lats = lats[0:200] #reduir los vectores para probar funconamiento mas rapido.
    #lon = lon[0:200]

    coordenadas = []
    aux = []

    coordenadas = zip(lats,lon)
    #for i in range(lats.size):
            #aux.append(lats[i])
            #aux.append(lon[i])
            #coordenadas.append(aux)
            #aux = []

    #print coordenadas

    #lats -90 a 90
    #long 0-360

    factor = (int(max(lats)) - int(min(lats))-1) #sumo al max y resto  al min para tomar tmb los datos marginales
    unaLat = int(min(lats)-1)
    todasLat = []
    step = 0.25
    rang = int((1/step)*factor)+1
    for i in range(rang): #multiplicar en funcion de cual es el step
        todasLat.append(unaLat)
        unaLat = unaLat + step
    #print todasLat

    factor =  (int(max(lon)) - int(min(lon)))+1
    unaLon = int(min(lon)-1)
    todasLon = []
    step = 0.25
    #r = 1/step
    rang = int((1/step)*factor)+1
    for i in range(rang): #multiplicar en funcion de cual es el step
        todasLon.append(unaLon)
        unaLon = unaLon + step
    #print todasLon


    aux = []
    paresOrdenados = []

    #paresOrdenados = zip(todasLat,todasLon)
    for i in range(len(todasLon)):
        for j in range(len(todasLat)):
            aux.append(todasLat[j])
            aux.append(todasLon[i])
            paresOrdenados.append(aux)
            aux = []
    #print (paresOrdenados)

    from math import floor

    def find_cell(coord):
        return [floor(coord[0]*2)/2, floor(coord[1]*2)/2]

    vectorVectores = []
    aux = []
    for j in range(len(paresOrdenados)):
        if len(aux) != 0:
            vectorVectores.append(aux)
            aux = []
        for i in range(len(coordenadas)):
            if find_cell(coordenadas[i]) == paresOrdenados[j]:
                aux.append(coordenadas[i])

    auxlat = []
    auxlon = []
    latsProm = []
    lonProm = []
    for j in range(len(vectorVectores)):
        vec = vectorVectores[j]
        for i in range(len(vec)):
            auxlat.append(vec[i][0])
            auxlon.append(vec[i][1])
        latsProm.append(np.average(auxlat))
        lonProm.append(np.average(auxlon))
        auxlat = []
        auxlon = []

    coords = zip(latsProm, lonProm)
    print coords

    print "cantidad de coordenadas originales: ", len(coordenadas)
    print "Cantidad de coordenadas después de promediar: ",         len(vectorVectores)
    
answered by 24.06.2016 в 16:31