calc_resid
Called by: main each iteration and after convergence.
Includes: iscloc.h
External: max_depth set in read_ttime
Input arguments:
Pointer to solution structure.
Array of phase structures.
A string, mode, set to "all" if require residuals for all phases, "use" if only want residuals that will be used in solution.
Calls:
function read_ttime
functions add_to_error and handle_error from utils
Structure members updated: resid in phase structure.
Return: 0/1 for success/failure.
First this function checks the depth of the current solution against max_depth, a value passed as an external variable by function read_ttime for the earth model in use. If the source depth is greater than the maximum depth allowed for by the model then no residuals can be calculated and the function returns early.
Next it loops over all associated phases and calculates residuals for those applicable. Each iteration this function is called with mode="use" and only calculates residuals for those phases with weight_factor > 0 , i.e. those that will be used in the solution. After convergence this function is called one last time with mode="all" and calculates residuals with respect to the final solution for all recognised phases. For each applicable phase the observed travel time is calculated and then the model travel time and derivatives are added to the phase structure by calling function read_ttime. The residual is calculated by subtracting the model time from the observed time and is also added to the structure.
Note: The ultimate calculation of residuals with respect to the final solution can be confusing. It is possible for the solution to alter sufficiently in the final iteration that it is impossible to calculate residuals for phases that were used in that final iteration. For example, if the depth of the solution changed form above the Conrad discontinuity to below it then Pg and Sg phases that contributed to the hypocentre can not have residuals with respect to it. In such cases a warning is written to logfile , weight_factor for the phase is set to 0 and phase to "". The effect is that ndef will be less than it should be and the phases concerned will not be included in the calculation of sdobs or mindist .