0

I am using Lat, Lon data and would like to average all the sample_data within a grid cell (say 1km x 1km) uniformly across the whole area, and then plot it similar to this post, but with a basemap, I'm a bit stuck where to start: Heatmap with text in each cell with matplotlib's pyplot

The code below plots values through each time point, and I'd like to average the data within defined grid squares across the whole data area at each time point, and plot the average value onto the basemap with a grid at set time intervals (ie. make a set of images for a timelapse/movie).

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset
import matplotlib.cm as cm

data = Dataset(netcdf_data,'r')

lat = data.variables['lat'][:]
lon = data.variables['lon'][:]
time = data.variables['time'][:]


fig = plt.figure(figsize=(1800,1200))
m = Basemap(projection='ortho',lon_0=5,lat_0=35,resolution='l')
m.drawcoastlines()


for value in range(0,len(sample_data)):
    m.plot(lat[:,value], lon[:,value], alpha=1, latlon=True)

plt.show()
Community
  • 1
  • 1
Abstract Wisdom
  • 11
  • 1
  • 1
  • 5
  • Can you provide more details in the question like the structure of data array and which values are you trying to get average on? – v.coder Sep 07 '16 at 16:41
  • I'd like to average the 'age' of the sample_data, within a defined lat/lon grid, and produce multiple plots of this 'age value' as time progresses. It is a 4D array consisting of Z values as well as time variable - I should have added this in initial question. – Abstract Wisdom Sep 07 '16 at 16:52

2 Answers2

0

Well, you can get the maximum and minimum of latitude and longitude.

And then get the age of sample_data at a particular time t using:

data_within_box = sample_data[minLat:maxLat,minLon:maxLon,:,t]
avg_age = numpy.average(data_within_box) 
v.coder
  • 1,822
  • 2
  • 15
  • 24
0

This is a crude way of doing it:

lat_1 = 58
lat_2 = 60
lon_1 = 2
lon_2 = 5
size = 0 
age2 = 0 

for parts in range(10):
    for length in range(len(lon)):
        if lon[length,parts] < lon_2:
            if lon[length,parts] > lon_1:
                if lat[length,parts] > lat_1:
                    if lat[length,parts] < lat_2:
                        age = (age[length, parts])
                        size = size + 1
                        age2 = age + age2
                        mean = age2/size
Abstract Wisdom
  • 11
  • 1
  • 1
  • 5