mortie.orders
Querying, changing and validating a packed word's HEALPix order, plus the
resolution ladder those orders sit on. Split out of mortie.tools by domain
(issue #159) so the Python surface mirrors the Rust tree (morton.rs); the
dense batch kernel behind generate_morton_children's array form lives in
mortie.batch as a private function (issues #170, #187).
The names stay flat on the package (mortie.orders_of, mortie.clip2order).
HEALPix order query, change and validation over packed morton words.
Everything here reads, rewrites or validates a word's order: :func:orders_of
and :func:orders_of_uniq decode it per element, :func:infer_order_from_morton
collapses a uniform array to one, :func:validate_morton and :func:is_point
check the word itself, :func:clip2order coarsens and
:func:generate_morton_children refines, and :func:order2res /
:func:res2display give the resolution ladder those orders sit on. The bulk
refiner behind :func:generate_morton_children's array form lives in
:mod:mortie.batch as the private kernel :func:~mortie.batch._children_of
(issues #170, #187).
Split out of mortie.tools (issue #159) so the Python surface mirrors the
Rust tree's own decomposition -- this module is the Python side of
mortie-core/src/morton.rs (re-exported as mortie_rustie::morton). The
names stay flat on the package (mortie.orders_of, mortie.clip2order):
this module is where they live, not how they are spelled.
infer_order_from_morton(morton)
Infer the single HEALPix order of packed morton word(s).
Decodes through the packed-u64 kernel (issue #48): the order is carried in
the word's suffix, not in any decimal-digit count. The return is one
scalar order, so array input must be uniform-order; mixed-order input
raises, naming the distinct orders (issue #116 — previously the first
element's order was returned silently). For per-element orders of a mixed
array use :func:orders_of.
Batch vectorized: array in, one order out — a reduction, not elementwise, so any input shape is accepted (issue #219). Empty is the one input with no answer to give, and it is refused by name rather than through an index error (see Raises).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morton
|
int or array - like
|
Packed morton word(s), all at one order. Must be non-empty. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The HEALPix order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the words are at mixed orders, naming the distinct orders. Or if
|
Source code in mortie/orders.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
orders_of(morton)
Per-element HEALPix order of packed morton words.
Vectorized numpy decode of the 6-bit suffix (bits 5-0) per the spec page's
suffix table (docs/specification.md §1):
- suffix
0..=27— variable-length area element; the order is the suffix value (0= base-cell-only). - suffix
28..=47— order-28/29 area cells in parent-first preordersuffix = 28 + t28*5 + (t29 present ? t29 + 1 : 0): eacht28owns a 5-block (the order-28 parent, then its four order-29 children), so(suffix - 28) % 5 == 0is order 28 and everything else is order 29. - suffix
48..=63— order-29 point (max-encoded, no area claim — spec §4); points are order 29 by definition.
Pure bit arithmetic — words are not validated (the empty sentinel 0
decodes as order 0; use :func:validate_morton to reject malformed
words). This is the per-element, mixed-order-native counterpart of
:func:infer_order_from_morton.
Batch vectorized: array in, array out, elementwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morton
|
int or array - like
|
Packed morton word(s) ( |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in mortie/orders.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
orders_of_uniq(uniq)
Per-element HEALPix order decoded from UNIQ cell numbers.
UNIQ is self-describing: uniq = 4 * 4**order + nested with
0 <= nested < 12 * 4**order, so order-k values occupy exactly
[4**(k+1), 4**(k+2)) and consecutive orders tile that line without
gaps. The order is therefore a pure function of the value — no
caller-supplied order is needed and mixed-resolution input decodes element
by element (issue #136).
Implemented as an exact integer bucket search rather than the
log2(uniq / 4) // 2 form this module used previously: the float64
round-trip is not exact above ~2**53, so e.g. 4**30 - 1 (the last
order-28 value) rounds up to 4**30 and mis-decodes as order 29.
The UNIQ counterpart of :func:orders_of, and mirrors its contract:
per-element, mixed-order-native, uint8 out, scalar in -> length-1
ndarray. One deliberate difference: :func:orders_of is pure bit
arithmetic and never validates, because every 6-bit morton suffix decodes
to some order. UNIQ has no such total decode -- a value outside
[4, 4**31) names no cell at any order -- so this raises instead of
inventing an answer.
Batch vectorized: array in, array out, elementwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uniq
|
int or array - like
|
UNIQ encoded cell number(s). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in mortie/orders.py
146 147 148 149 150 151 152 153 154 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
is_point(morton)
Per-element point-kind predicate for packed morton words.
Kind is carried by the encoding itself (spec §4): suffix 0..=47
decodes as an area word, suffix 48..=63 as an order-29 point
(a location with no area claim — docs/specification.md §1 suffix
table). Pure bit arithmetic; words are not validated (see
:func:validate_morton).
Batch vectorized: array in, array out, elementwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morton
|
int or array - like
|
Packed morton word(s) ( |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in mortie/orders.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
validate_morton(morton, order=None)
Validate that packed morton word(s) are well-formed.
The kernel decode rejects the empty sentinel (0) and any word with an
invalid base-cell prefix; this also checks the decoded order matches
order when one is supplied.
Batch vectorized (issue #187): array in, one verdict out — a
reduction, since the answer is "every word is valid", and any offender
raises. Any input shape is accepted; an offender in an N-D array is
named by its flat C-order index (issue #219). The order check
covers every element: it used to compare order against the
first word alone, so a mixed-order array passed validation on the
strength of its first element while the rest went unchecked (the decode
itself has always run per element).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morton
|
int or array - like
|
Packed morton word(s) to validate. |
required |
order
|
int
|
Expected HEALPix order -- one order, checked against every element.
If None, no order check is made. Per-element expectations are not a
thing here: a non-scalar |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if every word is a valid morton word. Empty in, True out,
for any |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
Notes
An empty input returns True whatever order says, including an
order no word could have: both checks quantify over the words, and there
are none. That is the numpy reduction reading (np.all([]) is
True) and it is what keeps the batch form usable on a legal empty
column -- a refusal there would make an empty partition an error rather
than a no-op. Before issue #187 this raised IndexError from indexing
depths[0], which was the accident of a first-element check rather
than a verdict.
Source code in mortie/orders.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
clip2order(clip_order, midx)
Coarsen packed morton words to a lower resolution.
Degrades each packed word to clip_order by coarsening it through the
kernel (the inverse of refining): the base cell and the first clip_order
tuples are kept, finer detail is dropped, and the suffix is rewritten. Words
already at or below clip_order are returned unchanged.
The print_factor flag was removed for the 1.x freeze (issue #68). It
returned 18 - clip_order, a level count anchored to the retired
decimal encoding's order-18 ceiling, so it went negative for the
order-19..29 words this package now encodes. There is no replacement: the
levels a word actually drops is order - clip_order for its own decoded
order, which :func:orders_of gives directly.
Batch vectorized: array in, array out, elementwise (one shared
clip_order). N-D input keeps its shape (issue #219); it used to be
silently flattened to 1-D.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clip_order
|
int
|
HEALPix order to degrade to. |
required |
midx
|
array-like of int
|
Packed morton words (see :func: |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Coarsened packed words, one per input word, in the input's shape (scalar in -> length-1). |
Source code in mortie/orders.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | |
generate_morton_children(parent_morton, target_order, *, max_cells=None)
Generate all child morton indices at a target order.
Batch vectorized (issue #187), with numpy semantics: a scalar parent
gives the 1-D (4**d,) block of its children, and an array of parents
gives the dense (n, 4**d) matrix — one row per parent, in input order.
Every parent in an array must sit at one shared order, which is what makes
the result dense rather than ragged. The array form is new behaviour,
not a re-spelling: an array used to be coerced through np.uint64 and
describe only its first element whenever target_order was finer than
the parents' order (at equal order it happened to return the parents
themselves), so a caller who was passing one gets a corrected answer of a
different shape. Only 1-D is accepted — a higher-rank array raises rather
than flattening, since a flattened result could not be indexed back.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent_morton
|
int or array_like
|
Parent packed morton word, or a 1-D array of them (all at one order). |
required |
target_order
|
int
|
Target order for children (must be >= parent order). |
required |
max_cells
|
int or None
|
Budget on the total children produced, refused pre-emptively in both
forms ( |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
children |
ndarray
|
Array of child packed morton words at target_order.
If target_order equals parent_order, returns array with parent_morton.
For array input, the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
See Also
mortie.batch._children_of : the dense batch kernel the array form delegates to.
Notes
Children are generated in HEALPix NESTED space — descending level_diff
orders multiplies the cell count by 4**level_diff — then packed back to
morton words via the kernel. If already at target_order, returns the parent
itself.
Source code in mortie/orders.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | |
order2res(order)
Approximate cell scale (km) at a HEALPix tessellation order.
The exact RMS cell spacing on the mean-radius HEALPix sphere: every
order-k cell has identical area 4*pi*R**2 / (12 * 4**order) (HEALPix is
equal-area), and the cell scale is the square root of that area. Derived
from :data:EARTH_RADIUS_KM so code and the spec page (§3) share one Earth
model (issue #119).
order may be a scalar (returns a float) or an array of orders such
as :func:orders_of yields (returns an ndarray).
Batch vectorized: array in, array out, elementwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
order
|
int or array - like
|
HEALPix tessellation order(s). |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Approximate cell scale in kilometres (scalar in -> |
See Also
res2display : the same ladder as display-ready records, order by order.
Source code in mortie/orders.py
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 79 80 81 82 | |
res2display(max_order=MAX_ORDER)
Resolution ladder for tessellation orders 0 through max_order.
Returns one record per order rather than printing (issue #68): each
resolution is expressed in the largest sensible unit -- km at coarse
orders, m once it drops below 1 km, cm once it drops below 1 m --
rounded to three decimals within that bracket, so fine orders read
naturally (order 12 -> 1.592 km, order 13 -> 795.852 m) rather
than as tiny km fractions.
Not batch vectorized: one ladder per call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_order
|
int
|
Highest order to include, inclusive. Must lie in |
MAX_ORDER
|
Returns:
| Type | Description |
|---|---|
list of ResolutionLevel
|
One named tuple |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
See Also
order2res : the raw kilometres for a single order.
Examples:
>>> from mortie import res2display
>>> levels = res2display(max_order=3)
>>> levels[0].order, levels[0].unit
(0, 'km')
>>> for lvl in res2display(max_order=2):
... print(f"{lvl.value} {lvl.unit} at tessellation order {lvl.order}")
...
Source code in mortie/orders.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |