Commit 600da64765f9e92539e13eb06c39e5199cbb7c5b
1 parent
252ad2dc
Changed spatial composite model write method to use temp copies of parameters.
Showing
2 changed files
with
29 additions
and
48 deletions
src/model/GModelSpatialComposite.cpp
| ... | ... | @@ -387,18 +387,16 @@ void GModelSpatialComposite::write(GXmlElement& xml) const |
| 387 | 387 | "Spatial model is not of type \""+type()+"\"."); |
| 388 | 388 | } |
| 389 | 389 | |
| 390 | - // Verify that XML element is composed from spatial models | |
| 391 | - if (xml.elements("spatialModel") != xml.elements()) { | |
| 392 | - throw GException::model_invalid_spatial(G_WRITE, type(), | |
| 393 | - "Composite spatial model must be composed solely from spatial models."); | |
| 394 | - } | |
| 395 | - | |
| 396 | 390 | // Write all model components |
| 397 | - for (unsigned int idx_comp = 0; idx_comp < m_components.size(); idx_comp++) { | |
| 391 | + for (unsigned int idx_comp = 0; idx_comp < m_components.size(); ++idx_comp) { | |
| 398 | 392 | GModelSpatial *component = m_components[idx_comp]; |
| 399 | 393 | |
| 394 | + // Check for NULL | |
| 395 | + if (!component) | |
| 396 | + continue; | |
| 397 | + | |
| 400 | 398 | // Find xml element with matching name |
| 401 | - GXmlElement *matching_model = 0; | |
| 399 | + GXmlElement *matching_model = NULL; | |
| 402 | 400 | for (unsigned int idx_elt = 0; idx_elt < xml.elements("spatialModel"); idx_elt++) { |
| 403 | 401 | if (xml.element("spatialModel", idx_elt)->attribute("name") == |
| 404 | 402 | m_component_names[idx_comp]) { |
| ... | ... | @@ -407,65 +405,48 @@ void GModelSpatialComposite::write(GXmlElement& xml) const |
| 407 | 405 | } |
| 408 | 406 | } // endfor: loop over all xml elements |
| 409 | 407 | |
| 408 | + // Create temporary copy of the spatial model | |
| 409 | + GModelSpatial* spat = component->clone(); | |
| 410 | + | |
| 410 | 411 | // Strip prefix from parameter names |
| 411 | 412 | |
| 412 | 413 | // Loop over all parameters of component |
| 413 | - for (unsigned int idx_par = 0; idx_par < component->size(); idx_par++) { | |
| 414 | + for (unsigned int idx_par = 0; idx_par < spat->size(); ++idx_par) { | |
| 414 | 415 | |
| 415 | 416 | // Get parameter |
| 416 | - GModelPar& par = (*component)[idx_par]; | |
| 417 | + GModelPar& par = (*spat)[idx_par]; | |
| 418 | + | |
| 419 | + // Check if name contains colon | |
| 420 | + if (gammalib::contains(par.name(), ":")) { | |
| 417 | 421 | |
| 418 | - // Get name components | |
| 419 | - std::vector<std::string> name_components = gammalib::split(par.name(), ":"); | |
| 422 | + // Get name components | |
| 423 | + std::vector<std::string> name_components = gammalib::split(par.name(), ":"); | |
| 420 | 424 | |
| 421 | - // Remove first component | |
| 422 | - name_components.erase(name_components.begin()); | |
| 423 | 425 | |
| 424 | - // Join new name | |
| 425 | - std::string new_name = name_components[0]; | |
| 426 | - for (unsigned int i = 1; i < name_components.size(); i++) { | |
| 427 | - new_name.append(":"); | |
| 428 | - new_name.append(name_components[i]); | |
| 426 | + // Join new name | |
| 427 | + std::string new_name = name_components[1]; | |
| 428 | + for (unsigned int i = 2; i < name_components.size(); i++) { | |
| 429 | + new_name.append(":"); | |
| 430 | + new_name.append(name_components[i]); | |
| 431 | + } | |
| 432 | + | |
| 433 | + // Set new name | |
| 434 | + par.name(new_name); | |
| 429 | 435 | } |
| 430 | - | |
| 431 | - // Set new name | |
| 432 | - par.name(new_name); | |
| 433 | 436 | |
| 434 | 437 | } // endfor looped over all parameters |
| 435 | 438 | |
| 436 | 439 | // Write component |
| 437 | 440 | if (matching_model) { |
| 438 | 441 | // Use existing xml element |
| 439 | - component->write(*matching_model); | |
| 442 | + spat->write(*matching_model); | |
| 440 | 443 | } else { |
| 441 | 444 | // Create new xml element |
| 442 | 445 | GXmlElement new_element("spatialModel"); |
| 443 | - component->write(new_element); | |
| 446 | + spat->write(new_element); | |
| 444 | 447 | xml.append(new_element); |
| 445 | 448 | } |
| 446 | 449 | |
| 447 | - // Re-add prefix to parameter names | |
| 448 | - | |
| 449 | - std::string prefix = m_component_names[idx_comp]; | |
| 450 | - | |
| 451 | - // Loop over all parameters of component | |
| 452 | - for (unsigned int idx_par = 0; idx_par < component->size(); idx_par++) { | |
| 453 | - | |
| 454 | - // Get parameter | |
| 455 | - GModelPar& par = (*component)[idx_par]; | |
| 456 | - | |
| 457 | - // Prepend prefix | |
| 458 | - std::string new_name = prefix; | |
| 459 | - new_name.append(":"); | |
| 460 | - new_name.append(par.name()); | |
| 461 | - | |
| 462 | - // Set new name | |
| 463 | - par.name(new_name); | |
| 464 | - | |
| 465 | - } // endfor looped over all parameters | |
| 466 | - | |
| 467 | - | |
| 468 | - | |
| 469 | 450 | } // endfor: loop over all components |
| 470 | 451 | |
| 471 | 452 | // TODO: Remove unused xml elements | ... | ... |
test/test_GModel.cpp
| ... | ... | @@ -1412,7 +1412,7 @@ void TestGModel::test_spatial_composite(void) |
| 1412 | 1412 | { |
| 1413 | 1413 | // Test void constructor |
| 1414 | 1414 | GModelSpatialComposite model1; |
| 1415 | - test_value(model1.type(), "SpatialComposite", "Check void model type"); | |
| 1415 | + test_value(model1.type(), "Composite", "Check void model type"); | |
| 1416 | 1416 | |
| 1417 | 1417 | GSkyDir dir = GSkyDir(); |
| 1418 | 1418 | dir.radec_deg(83.6331, 22.01); |
| ... | ... | @@ -1429,7 +1429,7 @@ void TestGModel::test_spatial_composite(void) |
| 1429 | 1429 | |
| 1430 | 1430 | GModelSpatialComposite model2(*element); |
| 1431 | 1431 | test_value(model2.size(), 5); |
| 1432 | - test_value(model2.type(), "SpatialComposite", "Check model type"); | |
| 1432 | + test_value(model2.type(), "Composite", "Check model type"); | |
| 1433 | 1433 | |
| 1434 | 1434 | // Test access of individual parameters |
| 1435 | 1435 | test_value(model2["2:RA"].value(), 83.6331); | ... | ... |