0

I have a text file containing latitude and temperature values for points around the globe. I would like to take the average of all the temperature points between a specified latitude interval (i.e. every degree, from the South to the North Pole). This is the code I have so far:

data_in = genfromtxt('temperatures.txt', usecols = (0,1)) 
lat = data_in[:,0]
temp = data_in[:,1]

in_1N = np.where((lat>=0) & (lat<=1)) # outputs an array of indexes for all latitudes between 0°-1° North
temp_1N = temp[in_1N] # outputs an array of temperature values between 0°-1° North
avg_1N = np.nanmean(temp_1N) # works out the average of temperatures between 0°-1° North

plt.scatter(1, avg_1N) # plots the average temperature against the latitude interval
plt.show()

How could I improve this code, so it can be implemented 180 times to cover the Earth between -90°S and 90°N? Thanks

1
  • Thanks Unutbu. How would this work if I have a lot of NaN values in the temperature column? I get a blank graph with my data set. Commented Aug 26, 2014 at 21:59

1 Answer 1

1

You could use np.histogram to put the latitudes into bins. Usually, np.histogram would merely count the number of latitudes in each bin. But if you weight the latitudes by the associated temp value, then instead of a count you get the sum of the temps. If you divide the sum of temps by the bin count, you get the average temp in each bin:

import numpy as np
import matplotlib.pyplot as plt

# N = 100
# lat = np.linspace(-90, 90, N)
# temp = 50*(1-np.cos(np.linspace(0, 2*np.pi, N)))
# temp[::5] = np.nan
# np.savetxt(filename, np.column_stack([lat, temp]))

lat, temp = np.genfromtxt('temperatures.txt', usecols = (0,1), unpack=True) 
valid = np.isfinite(temp)
lat = lat[valid]
temp = temp[valid]

grid = np.linspace(-90, 90, 40)

count, bin_edges = np.histogram(lat, bins=grid)
temp_sum, bin_edges = np.histogram(lat, bins=grid, weights=temp)
temp_avg = temp_sum / count

plt.plot(bin_edges[1:], temp_avg, 'o')
plt.show()

enter image description here


Note that if you have scipy installed, then you could replace the two calls to np.histogram:

count, bin_edges = np.histogram(lat, bins=grid)
temp_sum, bin_edges = np.histogram(lat, bins=grid, weights=temp)

with one call to stats.binned_statistic:

import scipy.stats as stats
temp_avg, bin_edges, binnumber = stats.binned_statistic(
    x=lat, values=temp, statistic='mean', bins=grid)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.