Here, for example in the United States, there is a grid temperature field data (Usgrid.data) for the United States region, which needs to calculate the average temperature for each state. Of course, there is a shape file containing the administrative regions of each state (the relevant documents can be downloaded in this post: HTTP://BBS.06CLIMATE.COM/FORUM.P ... d=11070&fromuid=106). First, the lattice data array is read, and then the states.shp file is read to generate the US layer, traversing all the elements of the US layer (shape, each entity is a state), and for each state, using the state's elements to maksout the grid point data, and then averaging, maximum, and minimum values, and print out (of course, you can also output to a file, specifically on the web to find Python output text file example). The subsequent drawing statements are just to see how the data is distributed.
Script Program:
#ADD a surfer grid dataf = Addfile_surfer ('D:/temp/ascii/usgrid.dat') Tdata= f['var'][:,:]#Read US shape fileUS = Shaperead ('d:/temp/map/states.shp')#Average temporature for each statei =0 forRpolyinchus.getshapes (): Name= Us.getcellvalue ('State_name', i) Mdata=tdata.maskout (rpoly) Tave=mdata.ave () tmin=mdata.min () Tmax=Mdata.max ()PrintName +', Ave:%.2f, Min:%.2f, Max:%.2f'%(Tave, tmin, Tmax) I+ = 1#Plotaxesm () World= Shaperead ('d:/temp/map/country1.shp') geoshow (World) geoshow (US, Edgecolor=[0,0,255]) layer= Contourfm (tdata,20) title ('temporature Distribution Map') Colorbar (layer)
New products from the Meteoinfo family: Calculating the mean of different regions