|
4 | 4 |
|
5 | 5 | # Will Holmgren, University of Arizona, 2014-2016. |
6 | 6 |
|
| 7 | +import os |
7 | 8 | import datetime |
8 | 9 | import warnings |
9 | 10 |
|
10 | 11 | import pandas as pd |
11 | 12 | import pytz |
| 13 | +import h5py |
12 | 14 |
|
13 | 15 | from pvlib import solarposition, clearsky, atmosphere, irradiance |
| 16 | +from pvlib.tools import _degrees_to_index |
14 | 17 |
|
15 | 18 | class Location: |
16 | 19 | """ |
@@ -356,3 +359,88 @@ def get_sun_rise_set_transit(self, times, method='pyephem', **kwargs): |
356 | 359 | 'one of pyephem, spa, geometric' |
357 | 360 | .format(method)) |
358 | 361 | return result |
| 362 | + |
| 363 | + |
| 364 | +def lookup_altitude(latitude, longitude): |
| 365 | + """ |
| 366 | + Look up location altitude from low-resolution altitude map |
| 367 | + supplied with pvlib. The data for this map comes from multiple open data |
| 368 | + sources with varying resolutions aggregated by Mapzen. |
| 369 | +
|
| 370 | + More details can be found here |
| 371 | + https://github.com/tilezen/joerd/blob/master/docs/data-sources.md |
| 372 | +
|
| 373 | + Altitudes from this map are a coarse approximation and can have |
| 374 | + significant errors (100+ meters) introduced by downsampling and |
| 375 | + source data resolution. |
| 376 | +
|
| 377 | + Parameters |
| 378 | + ---------- |
| 379 | + latitude : float. |
| 380 | + Positive is north of the equator. |
| 381 | + Use decimal degrees notation. |
| 382 | +
|
| 383 | + longitude : float. |
| 384 | + Positive is east of the prime meridian. |
| 385 | + Use decimal degrees notation. |
| 386 | +
|
| 387 | + Returns |
| 388 | + ------- |
| 389 | + altitude : float |
| 390 | + The altitude of the location in meters. |
| 391 | +
|
| 392 | + Notes |
| 393 | + ----------- |
| 394 | + Attributions: |
| 395 | +
|
| 396 | + * ArcticDEM terrain data DEM(s) were created from DigitalGlobe, Inc., |
| 397 | + imagery and funded under National Science Foundation awards 1043681, |
| 398 | + 1559691, and 1542736; |
| 399 | + * Australia terrain data © Commonwealth of Australia |
| 400 | + (Geoscience Australia) 2017; |
| 401 | + * Austria terrain data © offene Daten Österreichs - Digitales |
| 402 | + Geländemodell (DGM) Österreich; |
| 403 | + * Canada terrain data contains information licensed under the Open |
| 404 | + Government Licence - Canada; |
| 405 | + * Europe terrain data produced using Copernicus data and information |
| 406 | + funded by the European Union - EU-DEM layers; |
| 407 | + * Global ETOPO1 terrain data U.S. National Oceanic and Atmospheric |
| 408 | + Administration |
| 409 | + * Mexico terrain data source: INEGI, Continental relief, 2016; |
| 410 | + * New Zealand terrain data Copyright 2011 Crown copyright (c) Land |
| 411 | + Information New Zealand and the New Zealand Government |
| 412 | + (All rights reserved); |
| 413 | + * Norway terrain data © Kartverket; |
| 414 | + * United Kingdom terrain data © Environment Agency copyright and/or |
| 415 | + database right 2015. All rights reserved; |
| 416 | + * United States 3DEP (formerly NED) and global GMTED2010 and SRTM |
| 417 | + terrain data courtesy of the U.S. Geological Survey. |
| 418 | +
|
| 419 | + References |
| 420 | + ---------- |
| 421 | + .. [1] `Mapzen, Linux foundation project for open data maps |
| 422 | + <https://www.mapzen.com/>`_ |
| 423 | + .. [2] `Joerd, tool for downloading and processing DEMs, Used by Mapzen |
| 424 | + <https://github.com/tilezen/joerd/>`_ |
| 425 | + .. [3] `AWS, Open Data Registry Terrain Tiles |
| 426 | + <https://registry.opendata.aws/terrain-tiles/>`_ |
| 427 | +
|
| 428 | + """ |
| 429 | + |
| 430 | + pvlib_path = os.path.dirname(os.path.abspath(__file__)) |
| 431 | + filepath = os.path.join(pvlib_path, 'data', 'Altitude.h5') |
| 432 | + |
| 433 | + latitude_index = _degrees_to_index(latitude, coordinate='latitude') |
| 434 | + longitude_index = _degrees_to_index(longitude, coordinate='longitude') |
| 435 | + |
| 436 | + with h5py.File(filepath, 'r') as alt_h5_file: |
| 437 | + alt = alt_h5_file['Altitude'][latitude_index, longitude_index] |
| 438 | + |
| 439 | + # 255 is a special value that means nodata. Fallback to 0 if nodata. |
| 440 | + if alt == 255: |
| 441 | + return 0 |
| 442 | + # Altitude is encoded in 28 meter steps from -450 meters to 6561 meters |
| 443 | + # There are 0-254 possible altitudes, with 255 reserved for nodata. |
| 444 | + alt *= 28 |
| 445 | + alt -= 450 |
| 446 | + return float(alt) |
0 commit comments