|
| 1 | +#========================================= |
| 2 | +# |
| 3 | +# File Name : magMWdust.R |
| 4 | +# Created By : awright |
| 5 | +# Creation Date : 03-06-2026 |
| 6 | +# Last Modified : Wed Jun 3 13:40:13 2026 |
| 7 | +# |
| 8 | +#========================================= |
| 9 | + |
| 10 | +utils::globalVariables("SFD_dust") |
| 11 | + |
| 12 | +# Define a plotting helper that overlays Milky Way dust on a projected sky plot. |
| 13 | +magMWdust = function(dust.data = NULL, dlon = NULL, dlat = NULL, type = "p", pch = 16, pt.cex = 0.5, opacity.range = c(0, 0.5), whiteblack.percentile = c(0.5, 0.95), stretch = "lin", min.opacity.plot = 0.01, show.status = TRUE, ...) { |
| 14 | + |
| 15 | + # Restrict the drawing style to points or polygons. |
| 16 | + if (!type %in% c("pl", "p")) stop("magMWdust function expects type of 'p' (for points) or 'pl' (for polygons) only") |
| 17 | + # If none provided, read the dust map data, which is a dlon=dlat=1 sampling |
| 18 | + if (is.null(dust.data)) { |
| 19 | + # Define the dlon and dlat values |
| 20 | + dlon = dlat = 1 |
| 21 | + # Lazy load the SFD_dust data |
| 22 | + dust_all = SFD_dust |
| 23 | + } else { |
| 24 | + if (!is.data.frame(dust.data)) { |
| 25 | + stop("dust.data is not a data frame; load an example with data(SFD_dust)") |
| 26 | + } |
| 27 | + if (!all(c("ebv","ra","dec")%in%colnames(dust.data))) { |
| 28 | + stop("dust.data is missing required components; load an example with data(SFD_dust)") |
| 29 | + } |
| 30 | + dust_all=dust.data |
| 31 | + if (type=='pl') { |
| 32 | + if (is.null(dlon)) stop("dlon must be provided when providing input dust.data and using type == 'pl'") |
| 33 | + if (is.null(dlat)) stop("dlat must be provided when providing input dust.data and using type == 'pl'") |
| 34 | + if (!is.numeric(dlon)) stop("dlon must be numeric") |
| 35 | + if (!is.numeric(dlat)) stop("dlat must be numeric") |
| 36 | + } |
| 37 | + } |
| 38 | + # Map dust values onto an opacity scale for plotting. |
| 39 | + dust_all$map = magicaxis::magmap(dust_all$ebv, range = opacity.range, hicut = whiteblack.percentile[2], locut = whiteblack.percentile[1], stretch = stretch)$map |
| 40 | + # Drop grid cells that would be too faint to plot usefully. |
| 41 | + dust = dust_all[which(dust_all$map > min.opacity.plot), ] |
| 42 | + # Stop on empty result |
| 43 | + if (nrow(dust)==0) stop("threshold for min.opacity.plot causes no data to be plotted. Reduce value to produce a result") |
| 44 | + |
| 45 | + # Draw filled polygons when polygon mode has been requested. |
| 46 | + if (type == "pl") { |
| 47 | + # Open a progress bar for the per-cell polygon loop. |
| 48 | + if (interactive() & isTRUE(show.status)) { |
| 49 | + pb = txtProgressBar(style = 3, min = 1, max = nrow(dust)) |
| 50 | + } |
| 51 | + # Iterate over each retained sky cell. |
| 52 | + for (i in 1:nrow(dust)) { |
| 53 | + # Project and draw the four corners of the current sky cell. |
| 54 | + magicaxis::magproj(c(dust$ra[i] - dlon/2, dust$ra[i] - dlon/2, dust$ra[i] + dlon/2, dust$ra[i] + dlon/2), |
| 55 | + c(dust$dec[i] - dlat/2, dust$dec[i] + dlat/2, dust$dec[i] + dlat/2, dust$dec[i] - dlat/2), |
| 56 | + type = type, add = TRUE, col = hsv(v = 0, alpha = dust$map[i]), border=NA, ...) |
| 57 | + # Advance the progress bar after drawing the current polygon. |
| 58 | + if (interactive() & isTRUE(show.status)) { |
| 59 | + setTxtProgressBar(pb, i) |
| 60 | + } |
| 61 | + } |
| 62 | + # Close the progress bar when the polygon layer is complete. |
| 63 | + if (interactive() & isTRUE(show.status)) { |
| 64 | + close(pb) |
| 65 | + } |
| 66 | + } else { |
| 67 | + # Draw the retained dust grid cells as projected points. |
| 68 | + magicaxis::magproj(dust$ra, dust$dec, type = "p", add = TRUE, pch = pch, cex = pt.cex, col = hsv(v = 0, alpha = dust$map), ...) |
| 69 | + } |
| 70 | + # Return invisibly because this function is used for its plotting side effects. |
| 71 | + return(invisible(NULL)) |
| 72 | +} |
0 commit comments