Skip to content

Commit

Permalink
Merge pull request #63 from kateharborne/dev-raw_consistency
Browse files Browse the repository at this point in the history
dev-raw_consistency
  • Loading branch information
kateharborne committed Aug 3, 2022
2 parents eee7c31 + a7a9d1e commit 6ab4677
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 35 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SimSpin
Type: Package
Title: SimSpin - A package for the kinematic analysis of galaxy simulations
Version: 2.3.13
Version: 2.3.14
Author: Katherine Harborne
Maintainer: <katherine.harborne@icrar.org>
Description: The purpose of this package is to provide a set of tools to "observe" simulations of galaxies as if you were an observer using an integral field unit (IFU). The galaxy model can be observed at a chosen inclination and an IFU observation can be produced in a selection of different spatial shapes (square, circular, and hexagonal) to account for the variety of CCD arrangements used by current telescopes. This data cube can be used to calculate the value an observer would measure for the spin parameter compared to the value that is measured directly from the simulation.
Expand Down
7 changes: 4 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SimSpin v2.3.13 News
# SimSpin v2.3.14 News

### Author: Kate Harborne

### Last edit: 01/08/22
### Last edit: 03/08/22

Below is a table containing a summary of all changes made to SimSpin, since the date this file was created on 26/08/2021.

Expand All @@ -18,7 +18,8 @@ All changes are noted in the changelog table below.

| Date | Summary of change | Version | Commit |
|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|------------------------------------------|
| 01/08/22 | *Minor bugfix*. Addressing Issue #59 (which was not actually fixed in previous attempt!). Making sure long entries (i.e. wave_seq, vbin_seq or sbin_seq) are never too long to be written to a FITS table. Change to range (i.e. min and max) to remove this as an issue. | 2.3.13 | |
| 03/08/22 | *Minor bugfix*. Addressing issue #62. Ensuring that raw images produced for any stellar mode observations (i.e. `method="spectral"` or `method="velocity"`) are consistent. New images added to spectral mode outputs (age and metallicity summaries) and all updated to mass weighted quantities. | 2.3.14 | |
| 01/08/22 | *Minor bugfix*. Addressing Issue #59 (which was not actually fixed in previous attempt!). Making sure long entries (i.e. wave_seq, vbin_seq or sbin_seq) are never too long to be written to a FITS table. Change to range (i.e. min and max) to remove this as an issue. | 2.3.13 | eee7c31c8686a5056ff3419136cb384d40693e50 |
| 29/07/22 | *Minor bugfix*. Addressing Issue #59. Making sure any NA values within the `observation` list are re-written as "None" in characters before writing to FITS. | 2.3.12 | 91e8e890e3154169b7bb078e80efe979c006667b |
| 28/07/22 | *Minor bugfix*. | 2.3.11 | c7114e4727e7eb2396d9af075c85df17b6c950e2 |
| | (1) Update to `write_simspin_FITS`. The `observation` summary output by `build_datacube` was previously never saved to the FITS file output. However, this makes it difficult to reproduce if the only saved output is a FITS file. With this update, the list `observation` is now written to a table in EXTNUM = 3 for all FITS files produced. | | |
Expand Down
4 changes: 3 additions & 1 deletion R/build_datacube.R
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ build_datacube = function(simspin_file, telescope, observing_strategy,
flux_image = array(data = output[[2]], dim = c(observation$sbin, observation$sbin)),
velocity_image = array(data = output[[3]], dim = c(observation$sbin, observation$sbin)),
dispersion_image = array(data = output[[4]], dim = c(observation$sbin, observation$sbin)),
particle_image = array(data = output[[5]], dim = c(observation$sbin, observation$sbin))
age_image = array(data = output[[5]], dim = c(observation$sbin, observation$sbin)),
metallicity_image = array(data = output[[6]], dim = c(observation$sbin, observation$sbin)),
particle_image = array(data = output[[7]], dim = c(observation$sbin, observation$sbin))
)

output = list("spectral_cube" = cube,
Expand Down
46 changes: 23 additions & 23 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,8 @@ globalVariables(c(".N", ":=", "Age", "Carbon", "CellSize", "Density", "Hydrogen"
vel_los = array(data = 0.0, dim = observation$sbin^2)
dis_los = array(data = 0.0, dim = observation$sbin^2)
lum_map = array(data = 0.0, dim = observation$sbin^2)
age_map = array(data = 0.0, dim = observation$sbin^2)
met_map = array(data = 0.0, dim = observation$sbin^2)
part_map = array(data=0, dim = observation$sbin^2)
filter = stats::approxfun(x = observation$filter$wave, y = abs(observation$filter$response))

Expand Down Expand Up @@ -1149,11 +1151,6 @@ globalVariables(c(".N", ":=", "Age", "Carbon", "CellSize", "Density", "Hydrogen"
luminosity = luminosity + (part_lum*scale_frac)
}

# if (observation$LSF_conv){ # should the spectra be degraded for telescope LSF?
# luminosity = .lsf_convolution(observation=observation, luminosity=luminosity,
# lsf_sigma=observation$lsf_sigma)
# }

if (!is.na(observation$signal_to_noise)){ # should we add noise?
luminosity = .add_noise(luminosity, observation$signal_to_noise)
}
Expand All @@ -1168,10 +1165,12 @@ globalVariables(c(".N", ":=", "Age", "Carbon", "CellSize", "Density", "Hydrogen"
filter = filter)
vel_los[part_in_spaxel$pixel_pos[i]] = .meanwt(galaxy_sample$vy, galaxy_sample$Mass)
dis_los[part_in_spaxel$pixel_pos[i]] = sqrt(.varwt(galaxy_sample$vy, galaxy_sample$Mass))
age_map[part_in_spaxel$pixel_pos[i]] = .meanwt(galaxy_sample$Age, galaxy_sample$Mass)
met_map[part_in_spaxel$pixel_pos[i]] = .meanwt(galaxy_sample$Metallicity, galaxy_sample$Mass)

if (verbose){cat(i, "... ", sep = "")}
}
return(list(spectra, lum_map, vel_los, dis_los, part_map))
return(list(spectra, lum_map, vel_los, dis_los, age_map, met_map, part_map))
}

.spectral_spaxels_mc = function(part_in_spaxel, wavelength, observation, galaxy_data, simspin_data, verbose, cores){
Expand All @@ -1180,14 +1179,16 @@ globalVariables(c(".N", ":=", "Age", "Carbon", "CellSize", "Density", "Hydrogen"
vel_los = array(data = 0.0, dim = observation$sbin^2)
dis_los = array(data = 0.0, dim = observation$sbin^2)
lum_map = array(data = 0.0, dim = observation$sbin^2)
age_map = array(data = 0.0, dim = observation$sbin^2)
met_map = array(data = 0.0, dim = observation$sbin^2)
part_map = array(data=0, dim = observation$sbin^2)
filter = stats::approxfun(x = observation$filter$wave, y = abs(observation$filter$response))

doParallel::registerDoParallel(cores)

i = integer()
output = foreach(i = 1:(dim(part_in_spaxel)[1]), .combine='.comb', .multicombine=TRUE,
.init=list(list(), list(), list(), list(), list())) %dopar% {
.init=list(list(), list(), list(), list(), list(), list(), list())) %dopar% {

num_part = part_in_spaxel$N[i]
part_map = num_part
Expand Down Expand Up @@ -1228,11 +1229,6 @@ globalVariables(c(".N", ":=", "Age", "Carbon", "CellSize", "Density", "Hydrogen"

}

# if (observation$LSF_conv){ # should the spectra be degraded for telescope LSF?
# luminosity = .lsf_convolution(observation=observation, luminosity=luminosity,
# lsf_sigma=observation$lsf_sigma)
# }

if (!is.na(observation$signal_to_noise)){ # should we add noise?
luminosity = .add_noise(luminosity, observation$signal_to_noise)
}
Expand All @@ -1246,8 +1242,10 @@ globalVariables(c(".N", ":=", "Age", "Carbon", "CellSize", "Density", "Hydrogen"
filter = filter)
vel_los = .meanwt(galaxy_sample$vy, galaxy_sample$Mass)
dis_los = sqrt(.varwt(galaxy_sample$vy, galaxy_sample$Mass))
age_map = .meanwt(galaxy_sample$Age, galaxy_sample$Mass)
met_map = .meanwt(galaxy_sample$Metallicity, galaxy_sample$Mass)

result = list(spectra, lum_map, vel_los, dis_los, part_map)
result = list(spectra, lum_map, vel_los, dis_los, age_map, met_map, part_map)
return(result)
closeAllConnections()
}
Expand All @@ -1256,9 +1254,11 @@ globalVariables(c(".N", ":=", "Age", "Carbon", "CellSize", "Density", "Hydrogen"
lum_map[part_in_spaxel$pixel_pos] = matrix(unlist(output[[2]]))
vel_los[part_in_spaxel$pixel_pos] = matrix(unlist(output[[3]]))
dis_los[part_in_spaxel$pixel_pos] = matrix(unlist(output[[4]]))
part_map[part_in_spaxel$pixel_pos] = matrix(unlist(output[[5]]))
age_map[part_in_spaxel$pixel_pos] = matrix(unlist(output[[5]]))
met_map[part_in_spaxel$pixel_pos] = matrix(unlist(output[[6]]))
part_map[part_in_spaxel$pixel_pos] = matrix(unlist(output[[7]]))

return(list(spectra, lum_map, vel_los, dis_los, part_map))
return(list(spectra, lum_map, vel_los, dis_los, age_map, met_map, part_map))
}


Expand Down Expand Up @@ -1328,10 +1328,10 @@ globalVariables(c(".N", ":=", "Age", "Carbon", "CellSize", "Density", "Hydrogen"
# adding the "gaussians" of each particle to the velocity bins
vel_spec[part_in_spaxel$pixel_pos[i],] = .sum_velocities(galaxy_sample = galaxy_sample, observation = observation)
lum_map[part_in_spaxel$pixel_pos[i]] = sum(galaxy_sample$luminosity)
vel_los[part_in_spaxel$pixel_pos[i]] = .meanwt(galaxy_sample$vy, galaxy_sample$luminosity)
dis_los[part_in_spaxel$pixel_pos[i]] = sqrt(.varwt(galaxy_sample$vy, galaxy_sample$luminosity))
age_map[part_in_spaxel$pixel_pos[i]] = .meanwt(galaxy_sample$Age, galaxy_sample$Initial_Mass)
met_map[part_in_spaxel$pixel_pos[i]] = .meanwt(galaxy_sample$Metallicity, galaxy_sample$Initial_Mass)
vel_los[part_in_spaxel$pixel_pos[i]] = .meanwt(galaxy_sample$vy, galaxy_sample$Mass)
dis_los[part_in_spaxel$pixel_pos[i]] = sqrt(.varwt(galaxy_sample$vy, galaxy_sample$Mass))
age_map[part_in_spaxel$pixel_pos[i]] = .meanwt(galaxy_sample$Age, galaxy_sample$Mass)
met_map[part_in_spaxel$pixel_pos[i]] = .meanwt(galaxy_sample$Metallicity, galaxy_sample$Mass)

if (verbose){cat(i, "... ", sep = "")}

Expand Down Expand Up @@ -1410,10 +1410,10 @@ globalVariables(c(".N", ":=", "Age", "Carbon", "CellSize", "Density", "Hydrogen"
# adding the "gaussians" of each particle to the velocity bins
vel_spec = .sum_velocities(galaxy_sample = galaxy_sample, observation = observation)
lum_map = sum(galaxy_sample$luminosity)
vel_los = .meanwt(galaxy_sample$vy, galaxy_sample$luminosity)
dis_los = sqrt(.varwt(galaxy_sample$vy, galaxy_sample$luminosity))
age_map = .meanwt(galaxy_sample$Age, galaxy_sample$Initial_Mass)
met_map = .meanwt(galaxy_sample$Metallicity, galaxy_sample$Initial_Mass)
vel_los = .meanwt(galaxy_sample$vy, galaxy_sample$Mass)
dis_los = sqrt(.varwt(galaxy_sample$vy, galaxy_sample$Mass))
age_map = .meanwt(galaxy_sample$Age, galaxy_sample$Mass)
met_map = .meanwt(galaxy_sample$Metallicity, galaxy_sample$Mass)

result = list(vel_spec, lum_map, vel_los, dis_los, age_map, met_map, part_map)
return(result)
Expand Down
8 changes: 4 additions & 4 deletions R/write_simspin_FITS.R
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ write_simspin_FITS = function(output_file, simspin_datacube, object_name,
"CUNIT2"="Units of coordinate increment and value",
"EXTNAME"="Image extension name")

extnames = c("RAW_FLUX", "RAW_VEL", "RAW_DISP", "NPART")
bunits = c("erg/s/cm**2", "km/s", "km/s", "Particle number")
image_names = c("flux_image", "velocity_image", "dispersion_image", "particle_image")
extnum = c(4,5,6,7)
extnames = c("RAW_FLUX", "RAW_VEL", "RAW_DISP", "RAW_AGE", "RAW_Z", "NPART")
bunits = c("erg/s/cm**2", "km/s", "km/s", "Gyr", "Z_solar", "Particle number")
image_names = c("flux_image", "velocity_image", "dispersion_image", "age_image", "metallicity_image", "particle_image")
extnum = c(4,5,6,7,8,9)
output_image_file_names = paste0(output_dir, "/", output_file_root, "_raw_", image_names, ".FITS")

if (split_save){ # if writing each image to a seperate file
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<p>&nbsp;</p>

v2.3.13 - A package for producing mock observations:
v2.3.14 - A package for producing mock observations:

SimSpin allows you to take a simulation of a galaxy and produce a data cube in the style of an Integral Field Spectroscopy (IFS) instrument. A mock observation is produced using three simple steps:

Expand Down
18 changes: 16 additions & 2 deletions tests/testthat/test_build_datacube.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ temp_loc = tempdir()
built_cube_size = 4
ob_table_loc = 3

spectra_raw_images_size = 4
spectra_raw_images_size = 6
spectra_observed_images_size = NULL
spectra_number_of_hdu = 7
spectra_number_of_hdu = 9
spectral_raw_vel_loc = 5

velocity_raw_images_size = 6
Expand Down Expand Up @@ -559,6 +559,8 @@ test_that("Data cubes can be written to multiple files", {
expect_true(file.exists("GalaxyID_unknown_inc45deg_seeing2fwhm_raw_flux_image.FITS"))
expect_true(file.exists("GalaxyID_unknown_inc45deg_seeing2fwhm_raw_velocity_image.FITS"))
expect_true(file.exists("GalaxyID_unknown_inc45deg_seeing2fwhm_raw_dispersion_image.FITS"))
expect_true(file.exists("GalaxyID_unknown_inc45deg_seeing2fwhm_raw_age_image.FITS"))
expect_true(file.exists("GalaxyID_unknown_inc45deg_seeing2fwhm_raw_metallicity_image.FITS"))
expect_true(file.exists("GalaxyID_unknown_inc45deg_seeing2fwhm_raw_particle_image.FITS"))

spectral_fits = Rfits::Rfits_read("GalaxyID_unknown_inc45deg_seeing2fwhm_spectral_cube.FITS")
Expand Down Expand Up @@ -642,6 +644,8 @@ test_that("Data cubes can be written to multiple files", {
expect_true(file.exists(paste0(temp_loc, "/ss_hdf5_raw_flux_image.FITS")))
expect_true(file.exists(paste0(temp_loc, "/ss_hdf5_raw_velocity_image.FITS")))
expect_true(file.exists(paste0(temp_loc, "/ss_hdf5_raw_dispersion_image.FITS")))
expect_true(file.exists(paste0(temp_loc, "/ss_hdf5_raw_age_image.FITS")))
expect_true(file.exists(paste0(temp_loc, "/ss_hdf5_raw_metallicity_image.FITS")))
expect_true(file.exists(paste0(temp_loc, "/ss_hdf5_raw_particle_image.FITS")))

})
Expand All @@ -651,6 +655,8 @@ unlink(c("GalaxyID_unknown_inc45deg_seeing2fwhm_spectral_cube.FITS",
"GalaxyID_unknown_inc45deg_seeing2fwhm_raw_flux_image.FITS",
"GalaxyID_unknown_inc45deg_seeing2fwhm_raw_velocity_image.FITS",
"GalaxyID_unknown_inc45deg_seeing2fwhm_raw_dispersion_image.FITS",
"GalaxyID_unknown_inc45deg_seeing2fwhm_raw_age_image.FITS",
"GalaxyID_unknown_inc45deg_seeing2fwhm_raw_metallicity_image.FITS",
"GalaxyID_unknown_inc45deg_seeing2fwhm_raw_particle_image.FITS",

paste0(temp_loc, "/ss_gadget_velocity_cube.FITS"),
Expand Down Expand Up @@ -702,6 +708,8 @@ unlink(c("GalaxyID_unknown_inc45deg_seeing2fwhm_spectral_cube.FITS",
paste0(temp_loc, "/ss_hdf5_raw_flux_image.FITS"),
paste0(temp_loc, "/ss_hdf5_raw_velocity_image.FITS"),
paste0(temp_loc, "/ss_hdf5_raw_dispersion_image.FITS"),
paste0(temp_loc, "/ss_hdf5_raw_age_image.FITS"),
paste0(temp_loc, "/ss_hdf5_raw_metallicity_image.FITS"),
paste0(temp_loc, "/ss_hdf5_raw_particle_image.FITS")
))

Expand Down Expand Up @@ -742,6 +750,8 @@ unlink(c(paste0(temp_loc, "/ss_gadget.FITS"),
paste0(temp_loc, "/ss_gadget_raw_flux_image.FITS"),
paste0(temp_loc, "/ss_gadget_raw_velocity_image.FITS"),
paste0(temp_loc, "/ss_gadget_raw_dispersion_image.FITS"),
paste0(temp_loc, "/ss_gadget_raw_age_image.FITS"),
paste0(temp_loc, "/ss_gadget_raw_metallicity_image.FITS"),
paste0(temp_loc, "/ss_gadget_raw_particle_image.FITS"),
paste0(temp_loc, "/ss_gadget_mask.FITS")))

Expand All @@ -757,6 +767,8 @@ test_that("FITS files will be written with automatic names at directory given by
expect_true(file.exists(paste0(temp_loc, "/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_flux_image.FITS")))
expect_true(file.exists(paste0(temp_loc, "/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_velocity_image.FITS")))
expect_true(file.exists(paste0(temp_loc, "/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_dispersion_image.FITS")))
expect_true(file.exists(paste0(temp_loc, "/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_age_image.FITS")))
expect_true(file.exists(paste0(temp_loc, "/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_metallicity_image.FITS")))
expect_true(file.exists(paste0(temp_loc, "/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_particle_image.FITS")))
})

Expand All @@ -765,6 +777,8 @@ unlink(c(paste0(temp_loc,"/GalaxyID_unknown_inc45deg_seeing2fwhm_spectral_cube.F
paste0(temp_loc,"/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_flux_image.FITS"),
paste0(temp_loc,"/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_velocity_image.FITS"),
paste0(temp_loc,"/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_dispersion_image.FITS"),
paste0(temp_loc,"/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_age_image.FITS"),
paste0(temp_loc,"/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_metallicity_image.FITS"),
paste0(temp_loc,"/GalaxyID_unknown_inc45deg_seeing2fwhm_raw_particle_image.FITS")))


Expand Down

0 comments on commit 6ab4677

Please sign in to comment.