#
source:
qesdi/geoplot/trunk/lib/geoplot/grid_builder_base.py
@
6104

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/geoplot/trunk/lib/geoplot/grid_builder_base.py@6104
Revision 6104, 14.1 KB checked in by pnorton, 11 years ago (diff) |
---|

Line | |
---|---|

1 | """ |

2 | grid_builder_base.py |

3 | ==================== |

4 | |

5 | Holds the GridBuilderBase class. A GridBuilder is an object that knows how to |

6 | extract a lat-lon Grid object from a given cdmsVariable. This is an abstract |

7 | baseclass and can't be used directly. |

8 | |

9 | """ |

10 | |

11 | #python modules |

12 | import logging |

13 | import time |

14 | |

15 | #third party modules |

16 | |

17 | #internal modules |

18 | from geoplot.grid import Grid |

19 | |

20 | import numpy as N |

21 | import numpy.ma as MA |

22 | |

23 | #set the log |

24 | log = logging.getLogger(__name__) |

25 | |

26 | class GridBuilderBase(object): |

27 | """ |

28 | Impliments the common GridBuilder functionality. |

29 | |

30 | This is an abstract class. Any methods that iherit from this class need to |

31 | impliment the _resizeVar, _buildGridBounds, _buildGridMidpoints and the |

32 | _buildGridValues methods. |

33 | """ |

34 | |

35 | def __init__(self, cdmsVar): |

36 | """ |

37 | constructs the grid builder object |

38 | |

39 | @param cdmsVar: the cdms variable that conains the grid data. |

40 | @type cdmsVar:a cdms.variable object |

41 | """ |

42 | |

43 | self.cdmsVar = cdmsVar |

44 | self._checkVariable() |

45 | |

46 | # for axis in self.cdmsVar.getAxisList(): |

47 | # _logAxis(axis) |

48 | |

49 | def buildGrid(self, xLimits=None, yLimits=None): |

50 | """ |

51 | builds a grid object from the data in the cdmsVar ascociated with the |

52 | grid builder object. |

53 | |

54 | If xLimits or yLimits values are given then the resulting grid will |

55 | contain the portion of the data in the cdms variable that falls within |

56 | the limits given. |

57 | |

58 | @keyword xLimits: (optional) longitude limits of the resulting grid |

59 | @type xLimits: a tuple of (MinLongitude, MaxLongitude) |

60 | @keyword yLimits: (optional) latitude limits of the resulting grid |

61 | @type yLimits: a tuple of (MinLatitude, MaxLatitude) |

62 | @return: a grid built using the cdms variable data |

63 | @rtype: geoplot.Grid |

64 | """ |

65 | |

66 | # log.debug("building grid with cdms variable id = %s" % (self.cdmsVar.id,)) |

67 | # log.debug("self.cdmsVar.getAxisIds() = %s" % (self.cdmsVar.getAxisIds(),)) |

68 | # log.debug("self.cdmsVar.shape = %s" % (self.cdmsVar.shape,)) |

69 | |

70 | |

71 | # xmid, ymid = self._buildGridMidpoints(self.cdmsVar) |

72 | |

73 | # log.debug("y midpoints min =[" + str(ymid.min()) + \ |

74 | # "] max =[" + str(ymid.max()) + "]") |

75 | # log.debug("x midpoints min =[" + str(xmid.min()) + \ |

76 | # "] max =[" + str(xmid.max()) + "]") |

77 | |

78 | varMin, varMax = self._getVarMinAndMax() |

79 | |

80 | infValueFound, varMax = self._checkVarMaxForInf(varMax) |

81 | |

82 | st = time.time() |

83 | reducedVar = self._getResizedVar(xLimits, yLimits) |

84 | log.debug("Reduced variable in %ss" % (time.time() - st,)) |

85 | |

86 | st = time.time() |

87 | (gridBoundsX, gridBoundsY) = self._buildGridBounds(reducedVar) |

88 | log.debug("built bounds in %ss" % (time.time() - st,)) |

89 | |

90 | st = time.time() |

91 | (gridMidpointX, gridMidpointY) = self._buildGridMidpoints(reducedVar) |

92 | log.debug("built midpoints in %ss" % (time.time() - st,)) |

93 | |

94 | st = time.time() |

95 | gridValues = self._buildGridValues(reducedVar) |

96 | log.debug("built values in %ss" % (time.time() - st,)) |

97 | |

98 | if infValueFound: |

99 | # if there was an inf value found in the full variable |

100 | # there may be one in the reduced one, so mask any found |

101 | gridValues = self._maskInfValsInVar(gridValues) |

102 | |

103 | # log.debug("After resize:") |

104 | # log.debug("y midpoints min =[" + str(gridMidpointY.min()) + \ |

105 | # "] max =[" + str(gridMidpointY.max()) + "]") |

106 | # log.debug("x midpoints min =[" + str(gridMidpointX.min()) + \ |

107 | # "] max =[" + str(gridMidpointX.max()) + "]") |

108 | # |

109 | # log.debug("Diff: y [" + str(gridMidpointY.min() - ymid.min()) + \ |

110 | # "][" + str(gridMidpointY.max() - ymid.max()) ) |

111 | # log.debug("Diff: x [" + str(gridMidpointX.min() - xmid.min()) + \ |

112 | # "][" + str(gridMidpointX.max() - xmid.max()) +"]") |

113 | # |

114 | # log.debug("self.cdmsVar.shape = %s" % (self.cdmsVar.shape,)) |

115 | # log.debug("gridValues.shape = %s" % (gridValues.shape,)) |

116 | # log.debug("gridBoundsX.shape = %s" % (gridBoundsX.shape,)) |

117 | |

118 | return Grid(gridBoundsX, gridBoundsY, gridMidpointX, gridMidpointY, gridValues, |

119 | varMax, varMin) |

120 | |

121 | def _checkVarMaxForInf(self, varMax): |

122 | """ |

123 | Checks if varMax is an infinite value, if it is will compute the |

124 | non-infitite maximum. |

125 | """ |

126 | |

127 | infValueFound = False |

128 | |

129 | if not hasattr(varMax, 'mask') and varMax == N.inf: |

130 | infValueFound = True |

131 | |

132 | # need to make sure we get the max from the non-reduced variable |

133 | varMax = self._maskInfValsInVar(self.cdmsVar).max() |

134 | |

135 | return infValueFound, varMax |

136 | |

137 | def _getResizedVar(self, xLimits, yLimits): |

138 | """ |

139 | Replaces any None's in the limits with the max/min midpoint values then |

140 | returns the reduced variable from self.resizeVar(). |

141 | |

142 | If both of these limits are None then the original self.cdmsVar will be |

143 | returned. If any part of these limits is None then it will be replaced |

144 | with the maximum or minimum midpoint value from the self.cdms variable. |

145 | |

146 | After filling any Nones in the limits this method calls the |

147 | self._resizeVar method to do the resizing. |

148 | |

149 | @param xLimits: (optional) longitude limits of the resulting grid |

150 | @type xLimits: a tuple of (MinLongitude, MaxLongitude) |

151 | @param yLimits: (optional) latitude limits of the resulting grid |

152 | @type yLimits: a tuple of (MinLatitude, MaxLatitude) |

153 | @return: A variable containing the subset of data |

154 | @rtype: cdms.variable |

155 | """ |

156 | |

157 | if xLimits == None: xLimits = (None, None) |

158 | if yLimits == None: yLimits = (None, None) |

159 | |

160 | if xLimits == (None, None) and yLimits == (None, None): |

161 | reducedVar = self.cdmsVar |

162 | else: |

163 | if None in xLimits or None in yLimits: |

164 | xLimits, yLimits = self._replaceNoneInLimitsWithMaxMin(xLimits, yLimits) |

165 | |

166 | # log.debug("limits:" + str(xLimits) + ", " + str(yLimits)) |

167 | reducedVar = self._resizeVar(xLimits, yLimits) |

168 | |

169 | return reducedVar |

170 | |

171 | def _resizeVar(self, xLimits, yLimits): |

172 | """ |

173 | Returns a cdms variable that contains the subset of self.cdmsVar that is between |

174 | the limits. |

175 | |

176 | @param xLimits: (optional) longitude limits of the resulting grid |

177 | @type xLimits: a tuple of (MinLongitude, MaxLongitude) |

178 | @param yLimits: (optional) latitude limits of the resulting grid |

179 | @type yLimits: a tuple of (MinLatitude, MaxLatitude) |

180 | @return: A variable containing the subset of data |

181 | @rtype: cdms.variable |

182 | """ |

183 | raise NotImplementedError |

184 | |

185 | def _buildGridBounds(self, cdmsVar): |

186 | """ |

187 | Builds two 2d Numpy array of the x and y positions of the grid |

188 | boundaries. |

189 | |

190 | Returns one array for the x bounds and one for the y bound. These |

191 | arrays contain the lower boundary for a given grid box. |

192 | |

193 | e.g. xBounds[x,y] will give the lower x boundary for the gridbox x,y. |

194 | |

195 | @param cdmsVar: the variable to extract the boundry data from |

196 | @type cdmsVar: cdms.variable |

197 | @return: the position data as (xPositions, yPositions) |

198 | @rtype: 2 Numpy Arrays |

199 | |

200 | """ |

201 | raise NotImplementedError |

202 | |

203 | def _buildGridMidpoints(self, cdmsVar): |

204 | """ |

205 | Builds two 2d Numpy arrays for the x and y midpoints of a given grid |

206 | box, this position corresponds to the location of the grid box value. |

207 | |

208 | Returns one array for the midpoint x position and another for the y |

209 | postion. |

210 | |

211 | e.g. xMidpoints[x,y] will give the midpoint of the gridbox x,y and the |

212 | position of the ascociated measurment (value[x,y]). |

213 | |

214 | @param cdmsVar: the variable to extract the midpoint data from |

215 | @type cdmsVar: cdms.variable |

216 | @return: the midpoint data as (xMidpoints, yMidpoints) |

217 | @rtype: 2 Numpy Arrays |

218 | """ |

219 | raise NotImplementedError |

220 | |

221 | @staticmethod |

222 | def _getBoundsFromAxis(axis): |

223 | """ |

224 | returns the bounding grid for the values in an axis, These bounds will |

225 | be retrieved form the cdms variable if present or generated. |

226 | |

227 | The bounds array is |

228 | |

229 | @param axis: the axis to get bounds for |

230 | @type axis: cdms.axis.Axis |

231 | @return: the axis bounds |

232 | @rtype: numpy.aarray |

233 | """ |

234 | if axis.getBounds() == None: |

235 | axisBounds = GridBuilderBase._createBoundsFormList(axis.getValue()) |

236 | else: |

237 | axisBounds = GridBuilderBase._mergeBounds(axis.getBounds()) |

238 | |

239 | return axisBounds |

240 | |

241 | @staticmethod |

242 | def _createBoundsFormList(values): |

243 | """ |

244 | Creates a list of boundries from a given list of vlaues. |

245 | |

246 | These bounding values are created by halfing the distance between the first |

247 | two items in the list and then adding this value to every other item in the list. |

248 | The first bounding value is the first item in the list minus this shift. |

249 | |

250 | @params values: a list of values to create bounds from |

251 | @type values: a list of int or float |

252 | """ |

253 | bounds = [] |

254 | shift = (values[1] - values[0])/2 |

255 | bounds.append(values[0] - shift) |

256 | for item in values: |

257 | bounds.append(item + shift) |

258 | |

259 | return N.array(bounds) |

260 | |

261 | @staticmethod |

262 | def _mergeBounds(bounds): |

263 | """ |

264 | Folds a bounds array of shape (x, 2) into a 1D array of shape (x + 1,). |

265 | |

266 | We assume that grid boxes are contiguous. I.e. the |

267 | right-hand edge of grid box (x, y) is the same as the left-hand |

268 | edge of grid box (x + 1, y) and similarly in y. |

269 | |

270 | @param lonBounds: The longitude bounds array |

271 | @param latBounds: the latitude bounds array |

272 | """ |

273 | |

274 | # Get grid dimensions |

275 | n = bounds.shape[0] |

276 | |

277 | # Take the lower bounds as the mesh point |

278 | # except for the last index where the upper bounds is taken |

279 | merged = N.resize(bounds[:, 0], (n + 1,)) |

280 | merged[-1] = bounds[-1, 1] |

281 | |

282 | return merged |

283 | |

284 | |

285 | |

286 | |

287 | @staticmethod |

288 | def _fillMissingLimitsFromArray(limits, array): |

289 | |

290 | newLimits = [limits[0], limits[1]] |

291 | |

292 | if newLimits[0] == None: |

293 | newLimits[0] = array.min() |

294 | |

295 | if newLimits[1] == None: |

296 | newLimits[1] = array.max() |

297 | |

298 | return tuple(newLimits) |

299 | |

300 | def _replaceNoneInLimitsWithMaxMin(self, xLimits, yLimits): |

301 | |

302 | (newXLimits, newYLimits) = (xLimits, yLimits) |

303 | xBounds, yBounds = self._buildGridMidpoints(self.cdmsVar) |

304 | #log.debug("bounds" + str(yBounds)) |

305 | |

306 | if None in xLimits: |

307 | axis = self.cdmsVar.getAxisList()[1] |

308 | newXLimits = GridBuilderBase._fillMissingLimitsFromArray(xLimits, xBounds) |

309 | |

310 | if None in yLimits: |

311 | axis = self.cdmsVar.getAxisList()[0] |

312 | newYLimits = GridBuilderBase._fillMissingLimitsFromArray(yLimits, yBounds) |

313 | |

314 | return (newXLimits, newYLimits) |

315 | |

316 | def _checkVariable(self): |

317 | """ |

318 | checks the cdms variable to make sure that it is suitable for plotting |

319 | """ |

320 | |

321 | self._checkVariableAxis() |

322 | |

323 | def _checkVariableAxis(self): |

324 | """ |

325 | Checks the axis on a given variable, if axes that arn't expected are |

326 | found then a warning is written to the log. |

327 | """ |

328 | |

329 | if len(self.cdmsVar.getAxisList()) > 2: |

330 | log.warning('cdms variable contains ' \ |

331 | +str(len(self.cdmsVar.getAxisList())) + ' axes.') |

332 | |

333 | if self.cdmsVar.getTime() != None: |

334 | log.warning('cdms variable contains a time axes.') |

335 | #firstTime = self.cdmsVar.getTime().getValue()[0] |

336 | #self.cdmsVar = self.cdmsVar(time = firstTime, squeeze = 1) |

337 | #log.warning('using first time variable of ' + str(firstTime)) |

338 | |

339 | if self.cdmsVar.getLevel() != None: |

340 | log.warning('cdms variable contains a level axes.') |

341 | #firstLevel = self.cdmsVar.getLevel().getValue()[0] |

342 | #self.cdmsVar = self.cdmsVar(level = float(firstLevel), squeeze = 1) |

343 | #log.warning('using first level variable of ' + str(firstLevel)) |

344 | |

345 | def _buildGridValues(self,cdmsVar): |

346 | """ |

347 | Builds a numpy array of values for each of the grid boxes. |

348 | |

349 | e.g. values[y,x] is the value for grid box y, x. |

350 | """ |

351 | data = cdmsVar.getValue() |

352 | missing = cdmsVar.getMissing() |

353 | |

354 | # this data is extracted such that if the axis are in order x/y |

355 | # data[xIndex, yIndex] = value |

356 | # but if they are in y/x it is: |

357 | # data[yIndex, xIndex] = value |

358 | # this function needs to return the value in terms of data[y,x] for the |

359 | # imshow call to work. |

360 | |

361 | if not self.__class__._areAxisInOrderYX(cdmsVar): |

362 | log.warning("axis are in order x,y. Swapping them.") |

363 | data = data.swapaxes(0,1) |

364 | |

365 | if missing == None: |

366 | missing = 1e20 |

367 | |

368 | return MA.masked_values(data, missing) |

369 | |

370 | def _getVarMinAndMax(self): |

371 | st = time.time() |

372 | varMax = self.cdmsVar.max() |

373 | varMin = self.cdmsVar.min() |

374 | |

375 | log.debug("got min (%s) and max (%s) in %ss" % (varMin, varMax, time.time() - st,)) |

376 | return varMin, varMax |

377 | |

378 | def _maskInfValsInVar(self, var): |

379 | |

380 | #inds = N.argwhere(var == numpy.inf) |

381 | |

382 | return N.ma.masked_equal(var, N.inf) |

383 | |

384 | def _logAxis(axis): |

385 | """ |

386 | A function that writes the details of a particular axis to log.debug. |

387 | """ |

388 | |

389 | if axis.isLongitude() : |

390 | msg = 'Longitude Axis' |

391 | elif axis.isLatitude(): |

392 | msg = 'Latitude Axis' |

393 | elif axis.isLevel(): |

394 | msg = 'Level Axis' |

395 | elif axis.isTime(): |

396 | msg = 'Time Axis' |

397 | else: |

398 | msg = 'Unknown axis' |

399 | |

400 | log.debug(msg + ' ' + str(axis.id) + '(' + str(len(axis.getValue())) +') :' +\ |

401 | str(axis.getValue()[0]) + ' - ' + str(axis.getValue()[-1])) |

**Note:**See TracBrowser for help on using the repository browser.