helpers
Roughness helper functions
as_cart3D(vecs)
Return list of vecs as shape (N, M, 3) if they are not already.
Source code in roughness/helpers.py
615 616 617 618 619 620 |
|
build_jupyter_notebooks(nbpath=cfg.EXAMPLES_DIR)
Build Jupyter notebooks using Jupytext.
Source code in roughness/helpers.py
363 364 365 366 367 368 369 370 371 372 |
|
cart2pol(x, y)
Convert ordered coordinate pairs from Cartesian (X,Y) to polar (r,theta).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x,y |
array
|
Cartesian coordinates |
required |
Returns:
Name | Type | Description |
---|---|---|
r |
array
|
Distance from origin. |
theta |
array
|
Polar angle, [radians]. |
Source code in roughness/helpers.py
688 689 690 691 692 693 694 695 696 697 698 699 700 701 |
|
cart2sph(x, y, z)
Convert from cartesian (x, y, z) to spherical (theta, phi, r).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x, |
y, z (array
|
Cartesian coordinates. |
required |
Returns:
Name | Type | Description |
---|---|---|
theta |
array
|
Polar angle [rad]. |
phi |
array
|
Azimuthal angle [rad]. |
radius |
array
|
Radius. |
Source code in roughness/helpers.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 |
|
change_working_directory(path)
Change working directory and revert to previous on exit.
Source code in roughness/helpers.py
319 320 321 322 323 324 325 326 327 |
|
check_data_updated()
Check if data version is up to date.
Source code in roughness/helpers.py
292 293 294 295 296 297 298 299 |
|
compile_lineofsight(los_f90=cfg.FLOS_F90, verbose=False)
Compile lineofsight module with numpy f2py. Error if no compiler.
Source code in roughness/helpers.py
331 332 333 334 335 336 337 338 339 340 341 |
|
element_cross(A, B)
Return element-wise cross product of two 3D arrays in cartesian coords.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A, |
B (array
|
3D arrays of vectors in cartesian coords. |
required |
Returns:
Name | Type | Description |
---|---|---|
out |
array
|
3D array of vector cross products in cartesian coords. |
Source code in roughness/helpers.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
|
element_dot(A, B)
Return element-wise dot product of two 3D arrays in Cartesian coords.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A, |
B (array
|
3D arrays of vectors in cartesian coords. |
required |
Returns:
Name | Type | Description |
---|---|---|
out |
array
|
2D array of vector dot products. |
Source code in roughness/helpers.py
641 642 643 644 645 646 647 648 649 650 651 652 |
|
element_norm(A)
Return input array of vectors normalized to length 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A |
array
|
3D array of vectors in cartesian coords. |
required |
Returns:
Name | Type | Description |
---|---|---|
out |
array
|
3D array of normalized vectors. |
Source code in roughness/helpers.py
655 656 657 658 659 660 661 662 663 664 665 666 667 |
|
element_triple_cross(A, B, C)
Return element-wise triple cross product of three 3D arr in Cartesian
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A, |
B, C (array
|
3D arrays of vectors in cartesian coords. |
required |
Returns:
Name | Type | Description |
---|---|---|
out |
array
|
3D array of triple cross products. |
Source code in roughness/helpers.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
|
facet_grids(los_table, units='degrees')
Return 2D grids of surface facet slope and azimuth angles of los_table.
Assumes los_table axes are (az, theta) with ranges: az: [0, 360] degrees theta: [0, 90] degrees
Parameters:
Name | Type | Description | Default |
---|---|---|---|
los_table |
arr
|
Line of sight table (dims: az, theta) |
required |
units |
str
|
Return grids in specified units ('degrees' or 'radians') |
'degrees'
|
Return
thetas, azs (tuple of 2D array): Coord grids of facet slope and az
Source code in roughness/helpers.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
fname_with_demsize(filename, demsize)
Return filename with demsize appended to the end.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fname |
str or Path
|
Filename to append to. |
required |
demsize |
int
|
Length of dem in pixels. |
required |
Returns:
Name | Type | Description |
---|---|---|
fname_with_demsize |
str
|
New filename with new demsize appended. |
Source code in roughness/helpers.py
256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
get_angle_between(vec1, vec2, safe_arccos=False)
Return angle between cartesian vec1 and vec2 using dot product.
If vec1 or vec2 are NxMx3, compute element-wise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vec1, |
vec2 (array
|
Cartesian vectors. |
required |
safe_arccos |
bool
|
Clip dot product to [-1, 1] before arccos. |
False
|
Source code in roughness/helpers.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
|
get_angle_between_xr(vec1, vec2, safe_arccos=False)
Return angle between cartesian DataArrays using dot product.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vec1, |
vec2 (DataArray
|
Cartesian vectors. |
required |
safe_arccos |
bool
|
Clip dot product to [-1, 1] before arccos. |
False
|
Source code in roughness/helpers.py
451 452 453 454 455 456 457 458 459 460 461 462 463 |
|
get_azim(ground_sc_ground, ground_sun_ground)
Return the azimuth arccos(dot product of the spacecraft and sun vectors)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_sc_ground |
array
|
Ground to spacecraft vector. |
required |
ground_sun_ground |
array
|
Ground to sun vector. |
required |
Source code in roughness/helpers.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
|
get_data_version(data_version_file=cfg.FDATA_VERSION)
Get data version in data/data_version.txt.
Source code in roughness/helpers.py
302 303 304 305 306 307 308 309 310 |
|
get_facet_bins(naz=36, ntheta=45)
Return az, theta bin arrays of los_table.
Assumes los_table axes are (az, theta) with ranges: az: [0, 360] degrees theta: [0, 90] degrees
Parameters:
Name | Type | Description | Default |
---|---|---|---|
los_table |
array
|
Line of sight table (dims: az, theta) |
required |
units |
str
|
Return grids in specified units ('degrees' or 'radians') |
required |
Return
thetas, azs (tuple of 2D array): Bin edges of facet slope and az
Source code in roughness/helpers.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
get_ieg(ground_zen, ground_az, sun_zen, sun_az, sc_zen, sc_az)
Return local solar incidence, spacecraft emergence and phase angles (i, e, g) and azimuth given input viewing geometry.
Input zeniths and azimuths in radians.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_zen |
float
|
Ground zenith angle [rad]. |
required |
ground_az |
float
|
Ground azimuth angle [rad]. |
required |
sun_zen |
float
|
Sun zenith angle [rad]. |
required |
sun_az |
float
|
Sun azimuth angle [rad]. |
required |
sc_zen |
float
|
Spacecraft zenith angle [rad]. |
required |
sc_az |
float
|
Spacecraft azimuth angle [rad]. |
required |
Returns:
Type | Description |
---|---|
i, e, g, az) (tuple of float
|
Geometry relative to ground. |
Source code in roughness/helpers.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
get_ieg_xr(ground_zen, ground_az, sun_zen, sun_az, sc_zen, sc_az)
Return local solar incidence, spacecraft emergence and phase angles (i, e, g) and azimuth given input viewing geometry as xarray.DataArray.
Input zeniths and azimuths in radians.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_zen |
DataArray
|
Ground zenith angle [rad]. |
required |
ground_az |
DataArray
|
Ground azimuth angle [rad]. |
required |
sun_zen |
DataArray
|
Sun zenith angle [rad]. |
required |
sun_az |
DataArray
|
Sun azimuth angle [rad]. |
required |
sc_zen |
DataArray
|
Spacecraft zenith angle [rad]. |
required |
sc_az |
DataArray
|
Spacecraft azimuth angle [rad]. |
required |
Returns:
Type | Description |
---|---|
i, e, g, az) (tuple of DataArray
|
Geometry relative to ground. |
Source code in roughness/helpers.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
|
get_inc_az_from_subspacecraft(sslon, sslat, lon, lat)
Return inc and az angle at (lat, lon) given subspacecraft point (sslat, sslon). Return azimuth in degrees Clockwise from North (0 - 360).
From calculator at https://the-moon.us/wiki/Sun_Angle
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sslon |
float
|
Subsolar longitude [deg] |
required |
sslat |
float
|
Subsolar latitude [deg] |
required |
lon |
float
|
Longitude of point of interest [deg] |
required |
lat |
float
|
Latitude of point of interest [deg] |
required |
Returns:
Type | Description |
---|---|
inc, az (float): Solar incidence and azimuth angles [deg] |
Source code in roughness/helpers.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
|
get_local_az(ground, sun, sc)
Return azimuth angle of the spacecraft with respect to the sun and local slope. Assumes inputs are same size and shape and are in 3D Cartesian coords (ixjxk).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground |
array
|
Ground vector. |
required |
sun |
array
|
Sun vector. |
required |
sc |
array
|
Spacecraft vector. |
required |
Source code in roughness/helpers.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
get_local_az_xr(ground, sun, sc)
Return azimuth angle of the spacecraft with respect to the sun and local slope. Assumes inputs are same size and shape and are in 3D Cartesian coords (ixjxk).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground |
DataArray
|
Ground vector. |
required |
sun |
DataArray
|
Sun vector. |
required |
sc |
DataArray
|
Spacecraft vector. |
required |
Source code in roughness/helpers.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
|
get_lookup_bins(nrms=10, ninc=10, naz=36, ntheta=45)
Return coordinate arrays corresponding to number of elements in each axis.
Return lookup axes in the following order with ranges
rms: [0, 50] degrees inc: [0, 90] degrees az: [0, 360] degrees theta: [0, 90] degrees
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nrms |
int
|
Number of RMS slopes in [0, 50] degrees. |
10
|
ninc |
int
|
Number of incidence angles in [0, 90] degrees. |
10
|
naz |
int
|
Number of facet azimuth bins in [0, 360] degrees. |
36
|
ntheta |
int
|
Number of facet slope bins in [0, 90] degrees. |
45
|
Return
lookup_coords (tuple of array): Coordinate bins (rms, cinc, az, theta)
Source code in roughness/helpers.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
get_lookup_coords(nrms=10, ninc=10, naz=36, ntheta=45)
Return lookup table coordinate arrays
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nrms |
int
|
Number of RMS slopes in [0, 50] degrees. |
10
|
ninc |
int
|
Number of incidence angles in [0, 90] degrees. |
10
|
naz |
int
|
Number of facet azimuth bins in [0, 360] degrees. |
36
|
ntheta |
int
|
Number of facet slope bins in [0, 90] degrees. |
45
|
Returns:
Name | Type | Description |
---|---|---|
lookup_coords |
list of array
|
Coord arrays (rms, cinc, az, theta) |
Source code in roughness/helpers.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
import_lineofsight(fortran_dir=cfg.FORTRAN_DIR)
Import lineofsight module. Compile first if not found.
Source code in roughness/helpers.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
inc_rel_ground_xr(vec, ground)
Return vec relative to ground vector.
Source code in roughness/helpers.py
521 522 523 524 525 526 |
|
inc_to_tloc(inc, az, lat)
Convert solar incidence and az to decimal local time (in 6-18h).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inc |
float
|
Solar incidence in degrees [0, 90) |
required |
az |
float
|
Solar azimuth in degrees [0, 360) |
required |
lat |
float
|
Latitude in degrees [-90, 90) |
required |
Source code in roughness/helpers.py
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
|
lookup2xarray(lookups, rms_coords=None, inc_coords=None, nodata=-999)
Convert list of default lookups to xarray.DataSet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lookups |
array
|
Lookup tables (e.g. from make_los_table). |
required |
rms_coords |
array
|
RMS slope coordinates (default: None). |
None
|
inc_coords |
array
|
Incidence angle coordinates (default: None). |
None
|
nodata |
float
|
Value to replace NaNs with (default: -999). |
-999
|
Returns:
Type | Description |
---|---|
xarray.DataArray: xarray.DataArray with labeled dims and coords. |
Source code in roughness/helpers.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
np2xr(arr, dims=None, coords=None, name=None, cnames=None, cunits=None)
Convert numpy array to xarray.DataArray.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
array
|
Numpy array to convert to xarray.DataArray. |
required |
dims |
list of str
|
Dimension names (default: cfg.LUT_DIMS). |
None
|
coords |
list of arr
|
Coordinate arrays of each dim. |
None
|
name |
str
|
Name of xarray.DataArray (default: None) |
None
|
cnames |
list of str
|
Coordinate names of each param. |
None
|
cunits |
list of str
|
Coordinate units of each param (default: deg). |
None
|
Returns:
Type | Description |
---|---|
xarray.DataArray |
Source code in roughness/helpers.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
pol2cart(rho, phi)
Convert ordered coordinate pairs from polar (r,theta) to Cartesian (X,Y).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rho |
array
|
Distance from origin. |
required |
phi |
array
|
Polar angle, [radians]. |
required |
Returns:
Type | Description |
---|---|
x,y (array): Cartesian coordinates |
Source code in roughness/helpers.py
704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
|
rm_regex(dirpath, regex)
Remove all files matching regex in dirpath.
Source code in roughness/helpers.py
249 250 251 252 253 |
|
set_data_version(data_version_file=cfg.FDATA_VERSION)
Set data version in data/data_version.txt.
Source code in roughness/helpers.py
313 314 315 316 |
|
spec2xr(arr, wls, units='W/m²/sr/μm', wl_units='μm')
Return spectral numpy array as xarray.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr |
array
|
Spectral array. |
required |
wls |
array
|
Wavelength array. |
required |
units |
str
|
Units of spectral array (default: W/m²/sr/μm). |
'W/m²/sr/μm'
|
wl_units |
str
|
Units of wavelength array (default: μm). |
'μm'
|
Returns:
Type | Description |
---|---|
xarray.DataArray |
Source code in roughness/helpers.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
sph2cart(theta, phi, radius=1)
Convert from spherical (theta, phi, r) to cartesian (x, y, z) coordinates.
Theta and phi must be specified in radians and returned units correspond to units of r, default is unitless (r=1 is unit vector). Returns vectors along z-axis (e.g., if theta and phi are int/float return 1x1x3 vector; if theta and phi are MxN arrays return 3D array of vectors MxNx3).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
theta |
num or array
|
Polar angle [rad]. |
required |
phi |
num or array
|
Azimuthal angle [rad]. |
required |
radius |
num or array
|
Radius (default=1). |
1
|
Returns:
Type | Description |
---|---|
array
|
Cartesian vector(s), same shape as theta and phi. |
Source code in roughness/helpers.py
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 |
|
sph2cart_xr(theta, phi, radius=1)
Convert spherical to cartesian coordinates using xarray.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
theta |
DataArray
|
Polar angle [rad]. |
required |
phi |
DataArray
|
Azimuthal angle [rad]. |
required |
radius |
num or array
|
Radius (default=1). |
1
|
Returns:
Type | Description |
---|---|
DataArray
|
Cartesian vector(s), same shape as theta and phi. |
Source code in roughness/helpers.py
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 |
|
tloc_to_inc(tloc, lat=0, az=False)
Return the solar incidence angle given the local time in hrs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
az |
bool) Return azimuth angle (default
|
False) |
False
|
Returns:
Type | Description |
---|---|
inc (float) Solar incidence angle [deg] |
|
az (float) Solar azimuth angle (if az=True) [deg] |
Source code in roughness/helpers.py
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
|
triple_cross_xr(a, b, c)
Return cross product of a, b, c in 3D Cartesian coordinates.
Source code in roughness/helpers.py
529 530 531 |
|
versions_match(version_a, version_b, precision=2)
Check if semantic versions match to precision (default 2).
Examples
versions_match('1.0.0', '1.2.3', precision=1) True versions_match('1.2.0', '1.2.3', precision=2) True versions_match('1.2.3', '1.2.3', precision=3) True
Source code in roughness/helpers.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
wl2xr(arr, units='μm')
Return wavelength numpy array as xarray.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr |
array
|
Wavelength array. |
required |
units |
str
|
Units of wavelengths (default: μm). |
'μm'
|
Returns:
Type | Description |
---|---|
xarray.DataArray |
Source code in roughness/helpers.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
wn2xr(arr, units='cm^-1')
Return wavenumber numpy array as xarray.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr |
array
|
Wavenumber array. |
required |
units |
str
|
Units of wavenumbers (default: cm^-1). |
'cm^-1'
|
Returns:
Type | Description |
---|---|
xarray.DataArray |
Source code in roughness/helpers.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
xarr2geotiff(xarr, savefile, crs=cfg.MOON2000_ESRI)
Write xarray image to geotiff with rasterio.
xarr can must have dims (wavelength, lat, lon) or (lat, lon).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xarr |
DataArray
|
Image to save. |
required |
savefile |
str
|
Filename to save to. |
required |
crs |
str
|
Coordinate reference system (default: MOON2000_ESRI). |
MOON2000_ESRI
|
Source code in roughness/helpers.py
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 |
|
xy2lonlat_coords(x, y, extent)
Convert x,y coordinates to lat,lon coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x, |
y (array
|
x,y coordinates. |
required |
extent |
tuple
|
Lon/lat extent (lonmin, lonmax, latmin, latmax). |
required |
Returns:
Type | Description |
---|---|
lon, lat (array): Lon/lat coordinates. |
Source code in roughness/helpers.py
790 791 792 793 794 795 796 797 798 799 800 801 802 803 |
|