calc_misfit.c 8.42 KB
Newer Older
Tilman Steinweg's avatar
Tilman Steinweg committed
1
/*-----------------------------------------------------------------------------------------
2
 * Copyright (C) 2016  For the list of authors, see file AUTHORS.
Tilman Steinweg's avatar
Tilman Steinweg committed
3
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
4
 * This file is part of IFOS.
5
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
6
 * IFOS is free software: you can redistribute it and/or modify
Tilman Steinweg's avatar
Tilman Steinweg committed
7 8
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
9
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
10
 * IFOS is distributed in the hope that it will be useful,
Tilman Steinweg's avatar
Tilman Steinweg committed
11 12 13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14
 *
Tilman Steinweg's avatar
Tilman Steinweg committed
15
 * You should have received a copy of the GNU General Public License
Florian Wittkamp's avatar
Florian Wittkamp committed
16
 * along with IFOS. See file COPYING and/or <http://www.gnu.org/licenses/gpl-2.0.html>.
17
 -----------------------------------------------------------------------------------------*/
Tilman Steinweg's avatar
Tilman Steinweg committed
18 19

/*------------------------------------------------------------------------
20
 *   Calculate Misfit
Tilman Steinweg's avatar
Tilman Steinweg committed
21 22 23
 *  ----------------------------------------------------------------------*/
#include "fd.h"

24
double calc_misfit(float **sectiondata, float **section, int ntr, int ns, int LNORM, float L2, int itest, int sws, int swstestshot,int ntr_glob, int **recpos_loc, int nsrc_glob, int ishot, int iter, float ** srcpos, int ** recpos){
25
    
26 27 28
    /* declaration of variables */
    extern float DT;
    extern int MYID, USE_WORKFLOW, WORKFLOW_STAGE;
29
    extern int TRKILL, NORMALIZE, F_LOW_PASS, TIMEWIN;
30 31 32 33 34
    extern char TRKILL_FILE[STRING_SIZE];
    extern int VELOCITY;
    
    int i,j;
    float l2;
35 36
    int h;
    float abs_section = 0.0, abs_sectiondata = 0.0;
37 38 39 40 41 42 43 44 45 46 47 48 49
    float intseis_s, intseis_sd;
    float *picked_times=NULL;
    float **intseis_section=NULL, **intseis_sectiondata=NULL;
    float **intseis_sectiondata_envelope=NULL, **intseis_section_envelope=NULL;
    if(LNORM==8){
        intseis_section_envelope = matrix(1,ntr,1,ns);
        intseis_sectiondata_envelope = matrix(1,ntr,1,ns);
    }
    intseis_section = matrix(1,ntr,1,ns);  /* declaration of variables for integration */
    intseis_sectiondata = matrix(1,ntr,1,ns);
    if(TIMEWIN) picked_times = vector(1,ntr); /* declaration of variables for TIMEWIN */
    
    /* TRACE KILLING */
50
    int ** kill_tmp = NULL, *kill_vector = NULL;	/* declaration of variables for trace killing */
51 52 53 54 55 56 57 58 59 60 61 62
    char trace_kill_file[STRING_SIZE];
    FILE *ftracekill;
    extern int TRKILL_OFFSET;
    extern float TRKILL_OFFSET_LOWER;
    extern float TRKILL_OFFSET_UPPER;
    /*-----------------*/
    /* Start Tracekill */
    /*-----------------*/
    if(TRKILL){
        kill_tmp = imatrix(1,ntr_glob,1,nsrc_glob);
        kill_vector = ivector(1,ntr);
        
63 64 65 66 67 68 69 70 71 72 73 74
        /*------------------*/
        /* clear kill table */
        /*------------------*/
        for(i=1;i<=nsrc_glob;i++){
            for (j=1; j<=ntr_glob; j++) {
                kill_tmp[j][i]=0;
            }
        }
        
        /* Use ONLY offset based TraceKill */
        if(TRKILL_OFFSET==1) {
            /* Generate TraceKill file on the fly based on the offset from the source */
75 76
            create_trkill_table(kill_tmp,ntr_glob,recpos,nsrc_glob,srcpos,ishot,TRKILL_OFFSET_LOWER,TRKILL_OFFSET_UPPER);
        } else {
77
            
78 79 80 81 82 83
            /* READ TraceKill file from disk */
            
            if(USE_WORKFLOW){
                sprintf(trace_kill_file,"%s_%i.dat",TRKILL_FILE,WORKFLOW_STAGE);
                ftracekill=fopen(trace_kill_file,"r");
                if (ftracekill==NULL){
84
                    /* If Workflow TraceKill file not found use File without workflow extensions */
85 86 87
                    sprintf(trace_kill_file,"%s.dat",TRKILL_FILE);
                    ftracekill=fopen(trace_kill_file,"r");
                    if (ftracekill==NULL){
88
                        declare_error(" Trace kill file could not be opened!");
89 90 91 92 93 94
                    }
                }
            }else{
                sprintf(trace_kill_file,"%s.dat",TRKILL_FILE);
                ftracekill=fopen(trace_kill_file,"r");
                if (ftracekill==NULL){
95
                    declare_error(" Trace kill file could not be opened!");
96 97 98 99 100 101
                }
            }
            
            for(i=1;i<=ntr_glob;i++){
                for(j=1;j<=nsrc_glob;j++){
                    fscanf(ftracekill,"%d",&kill_tmp[i][j]);
102 103 104
                    if(feof(ftracekill)){
                        declare_error(" Error while reading TraceKill file. Check dimensions!");
                    }
105
                }
106
            }
107 108 109
            
            fclose(ftracekill);
            
110 111 112 113
            /* Use Tracekill FILE and add the offset based TraceKill */
            if(TRKILL_OFFSET==2) {
                create_trkill_table(kill_tmp,ntr_glob,recpos,nsrc_glob,srcpos,-100,TRKILL_OFFSET_LOWER,TRKILL_OFFSET_UPPER);
            }
114
        }
115 116 117 118 119 120
        
        /* Generate local kill vector */
        h=1;
        for(i=1;i<=ntr;i++){
            kill_vector[h] = kill_tmp[recpos_loc[3][i]][ishot];
            h++;
121 122
        }
    }
123 124 125
    /*---------------*/
    /* End Tracekill */
    /*---------------*/
126
    
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    /* Integration of measured and synthetic data  */
    for(i=1;i<=ntr;i++){
        
        intseis_s=0;
        intseis_sd=0;
        if (VELOCITY==0){	/* only integtration if displacement seismograms are inverted */
            for(j=1;j<=ns;j++){
                intseis_s += section[i][j];
                intseis_section[i][j]=intseis_s*DT;
                intseis_sd += sectiondata[i][j];
                intseis_sectiondata[i][j]=intseis_sd*DT;
            }
        }
        else{
            for(j=1;j<=ns;j++){
                intseis_section[i][j]=section[i][j];
                intseis_sectiondata[i][j]=sectiondata[i][j];
            }
        }
        
    }
Tilman Steinweg's avatar
Tilman Steinweg committed
148
    
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    /* TIME WINDOWING */
    if(TIMEWIN==1){
        time_window(intseis_section, iter, ntr_glob,recpos_loc, ntr, ns, ishot);
        time_window(intseis_sectiondata, iter, ntr_glob,recpos_loc, ntr, ns, ishot);
    }
    
    /* NORMALIZE TRACES */
    if(NORMALIZE==1){
        normalize_data(intseis_section,ntr,ns);
        normalize_data(intseis_sectiondata,ntr,ns);
    }
    
    /* calculate envelope for LNORM==8 */
    if (LNORM==8){
        calc_envelope(intseis_sectiondata,intseis_sectiondata_envelope,ns,ntr);
        calc_envelope(intseis_section,intseis_section_envelope,ns,ntr);
    }
    /* end of calculate envelope for LNORM==8 */
    
    /* calculate kind of "energy" */
    
    for(i=1;i<=ntr;i++){
        
        if((TRKILL==1)&&(kill_vector[i]==1))
            continue;
        
        if ((LNORM==5) || (LNORM==7)){
            abs_sectiondata=0.0;
            abs_section=0.0;
            
            for(j=1;j<=ns;j++){
                abs_sectiondata+=intseis_sectiondata[i][j]*intseis_sectiondata[i][j];
                abs_section+=intseis_section[i][j]*intseis_section[i][j];
            }
183 184 185 186
            if (abs_sectiondata==0) abs_sectiondata=1;
	    else abs_sectiondata=sqrt(abs_sectiondata);
	    if (abs_section==0) abs_section==1;
            else abs_section=sqrt(abs_section);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        }
        
        /* calculate L2 residuals */
        
        for(j=1;j<=ns;j++){
            
            if(LNORM==2){
                L2 += (intseis_section[i][j]-intseis_sectiondata[i][j]) * (intseis_section[i][j]-intseis_sectiondata[i][j]);}
            
            if((LNORM==5) || (LNORM==7)){
                L2 -= (intseis_sectiondata[i][j]*intseis_section[i][j]) / (abs_sectiondata*abs_section);}
            
            if(LNORM==8){
                L2 += (intseis_section_envelope[i][j]-intseis_sectiondata_envelope[i][j]) * (intseis_section_envelope[i][j]-intseis_sectiondata_envelope[i][j]);}
            
        }
    }
    
205 206 207 208 209 210 211 212
    for(i=1;i<=ntr;i++){
        for(j=1;j<=ns;j++){
            sectiondata[i][j]=intseis_sectiondata[i][j];
            section[i][j]=intseis_section[i][j];
        }
    }
    
    
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    l2=L2;
    
    /*  printf("\n MYID = %i   IN CALC_MISFIT: L2 = %10.12f \n",MYID,l2); */
    
    /* free memory for integrated seismograms */
    free_matrix(intseis_section,1,ntr,1,ns);
    free_matrix(intseis_sectiondata,1,ntr,1,ns);
    
    /* free memory for time windowing and trace killing */
    if(TIMEWIN==1) free_vector(picked_times,1,ntr);
    if(TRKILL==1){
        free_imatrix(kill_tmp,1,ntr_glob,1,nsrc_glob);
        free_ivector(kill_vector,1,ntr);}
    if(LNORM==8){
        free_matrix(intseis_sectiondata_envelope,1,ntr,1,ns);
        free_matrix(intseis_section_envelope,1,ntr,1,ns);
    }
    return l2;
Tilman Steinweg's avatar
Tilman Steinweg committed
231
} /* end of function */