00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026
00027
00028
00029
00030 #include <cpl.h>
00031 #include <math.h>
00032 #include <string.h>
00033
00034 #include "muse_rvcorrect.h"
00035
00036 #include "muse_astro.h"
00037
00038
00039
00040
00041
00042
00046
00047
00050
00051 const char *muse_rvcorrect_typestring[] = {
00052 "none",
00053 "bary",
00054 "helio",
00055 "geo",
00056 "unknown"
00057 };
00058
00059
00071
00072 muse_rvcorrect_type
00073 muse_rvcorrect_select_type(const char *aTypeString)
00074 {
00075 cpl_ensure(aTypeString, CPL_ERROR_NULL_INPUT, MUSE_RVCORRECT_UNKNOWN);
00076 muse_rvcorrect_type type = MUSE_RVCORRECT_NONE;
00077 if (!strncmp(aTypeString, muse_rvcorrect_typestring[MUSE_RVCORRECT_BARY],
00078 strlen(muse_rvcorrect_typestring[MUSE_RVCORRECT_BARY]) + 1)) {
00079 type = MUSE_RVCORRECT_BARY;
00080 } else if (!strncmp(aTypeString, muse_rvcorrect_typestring[MUSE_RVCORRECT_HELIO],
00081 strlen(muse_rvcorrect_typestring[MUSE_RVCORRECT_HELIO]) + 1)) {
00082 type = MUSE_RVCORRECT_HELIO;
00083 } else if (!strncmp(aTypeString, muse_rvcorrect_typestring[MUSE_RVCORRECT_GEO],
00084 strlen(muse_rvcorrect_typestring[MUSE_RVCORRECT_GEO]) + 1)) {
00085 type = MUSE_RVCORRECT_GEO;
00086 } else if (strncmp(aTypeString, muse_rvcorrect_typestring[MUSE_RVCORRECT_NONE],
00087 strlen(muse_rvcorrect_typestring[MUSE_RVCORRECT_NONE]) + 1)) {
00088 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT, "Unknown type of "
00089 "radial velocity correction requested:" " \"%s\"",
00090 aTypeString);
00091 type = MUSE_RVCORRECT_UNKNOWN;
00092 }
00093 return type;
00094 }
00095
00096
00118
00119 cpl_error_code
00120 muse_rvcorrect(muse_pixtable *aPixtable, muse_rvcorrect_type aType)
00121 {
00122 cpl_ensure_code(aPixtable && aPixtable->table && aPixtable->header,
00123 CPL_ERROR_NULL_INPUT);
00124 if (aType == MUSE_RVCORRECT_NONE) {
00125 return CPL_ERROR_NONE;
00126 }
00127
00128 if (cpl_propertylist_has(aPixtable->header, MUSE_HDR_PT_RVCORR) &&
00129 fabs(cpl_propertylist_get_double(aPixtable->header, MUSE_HDR_PT_RVCORR)) > 0.) {
00130 cpl_msg_info(__func__, "pixel table already corrected: skipping radial "
00131 "velocity correction");
00132 return CPL_ERROR_NONE;
00133 }
00134
00135 cpl_errorstate state = cpl_errorstate_get();
00136 muse_astro_rvcorr rvcorr = muse_astro_rvcorr_compute(aPixtable->header);
00137 if (!cpl_errorstate_is_equal(state)) {
00138 return cpl_error_set_message(__func__, cpl_error_get_code(), "Computing "
00139 "radial velocity correction failed: %s",
00140 cpl_error_get_message());
00141 }
00142 double rvoffset = 0.;
00143 if (aType == MUSE_RVCORRECT_BARY) {
00144 rvoffset = rvcorr.bary;
00145 } else if (aType == MUSE_RVCORRECT_HELIO) {
00146 rvoffset = rvcorr.helio;
00147 } else if (aType == MUSE_RVCORRECT_GEO) {
00148 rvoffset = rvcorr.geo;
00149 } else {
00150 return cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT, "Unknown "
00151 "type of radial velocity correction, no "
00152 "correction performed!");
00153 }
00154 cpl_msg_info(__func__, "Correcting data for %scentric radial velocity of %.2f"
00155 " km/s", muse_rvcorrect_typestring[aType], rvoffset);
00156
00157
00158 const double cinv = 1000. / CPL_PHYS_C;
00159 float *lbda = cpl_table_get_data_float(aPixtable->table, MUSE_PIXTABLE_LAMBDA);
00160 cpl_size irow, nrow = muse_pixtable_get_nrow(aPixtable);
00161 #pragma omp parallel for default(none) \
00162 shared(lbda, nrow, rvoffset)
00163 for (irow = 0; irow < nrow; irow++) {
00164
00165 lbda[irow] *= sqrt((1. + rvoffset * cinv) / (1. - rvoffset * cinv));
00166 }
00167
00168
00169 cpl_propertylist_update_double(aPixtable->header, MUSE_HDR_PT_RVCORR,
00170 rvoffset);
00171 char *comment = cpl_sprintf(MUSE_HDR_PT_RVCORR_C,
00172 muse_rvcorrect_typestring[aType]);
00173 cpl_propertylist_set_comment(aPixtable->header, MUSE_HDR_PT_RVCORR, comment);
00174 cpl_free(comment);
00175 return CPL_ERROR_NONE;
00176 }
00177