calc_energy.c 6.21 KB
Newer Older
tilman.metz's avatar
tilman.metz committed
1
/*-----------------------------------------------------------------------------------------
2
 * Copyright (C) 2016  For the list of authors, see file AUTHORS.
tilman.metz's avatar
tilman.metz 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.metz's avatar
tilman.metz 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.metz's avatar
tilman.metz 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.metz's avatar
tilman.metz 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.metz's avatar
tilman.metz committed
18 19

/*------------------------------------------------------------------------
20
 *   Calculate energy of measured data
tilman.metz's avatar
tilman.metz committed
21 22 23
 *  ----------------------------------------------------------------------*/
#include "fd.h"

24 25 26 27 28
double calc_energy(float **sectiondata, int ntr, int ns, float energy, int ntr_glob, int **recpos_loc, int nsrc_glob, int ishot, int iter, float ** srcpos, int ** recpos){
    
    /* 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
    extern char TRKILL_FILE[STRING_SIZE];
    extern int VELOCITY;
    int i, j;
33 34
    float energy_dummy;
    int h;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    float intseis_sd;
    float **intseis_sectiondata=NULL;
    float *picked_times=NULL;
    
    intseis_sectiondata = matrix(1,ntr,1,ns); /* declaration of variables for integration */
    if(TIMEWIN) picked_times = vector(1,ntr); /* declaration of variables for TIMEWIN */
    
    /*extern int MYID;*/
    
    /*printf("MYID: %d; ns=%d\n",MYID,ns);
     printf("MYID: %d; ntr=%d\n",MYID,ntr);*/
    
    /* TRACE KILLING */
    int ** kill_tmp, *kill_vector;	/* declaration of variables for trace killing */
    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);
        
62 63 64 65 66 67 68 69 70 71 72 73
        /*------------------*/
        /* 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 */
74 75 76 77 78 79 80 81 82
            create_trkill_table(kill_tmp,ntr_glob,recpos,nsrc_glob,srcpos,ishot,TRKILL_OFFSET_LOWER,TRKILL_OFFSET_UPPER);
        } else {
            
            /* 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){
83
                    /* If Workflow TraceKill file not found use File without workflow extensions */
84 85 86
                    sprintf(trace_kill_file,"%s.dat",TRKILL_FILE);
                    ftracekill=fopen(trace_kill_file,"r");
                    if (ftracekill==NULL){
87
                        declare_error(" Trace kill file could not be opened!");
88 89 90 91 92 93
                    }
                }
            }else{
                sprintf(trace_kill_file,"%s.dat",TRKILL_FILE);
                ftracekill=fopen(trace_kill_file,"r");
                if (ftracekill==NULL){
94
                    declare_error(" Trace kill file could not be opened!");
95 96 97 98 99 100
                }
            }
            
            for(i=1;i<=ntr_glob;i++){
                for(j=1;j<=nsrc_glob;j++){
                    fscanf(ftracekill,"%d",&kill_tmp[i][j]);
101 102 103
                    if(feof(ftracekill)){
                        declare_error(" Error while reading TraceKill file. Check dimensions!");
                    }
104 105 106 107 108
                }
            }
            
            fclose(ftracekill);
            
109 110 111 112
            /* Use Tracekill FILE and add the offset based TraceKill */
            if(TRKILL_OFFSET==2) {
                create_trkill_table(kill_tmp,ntr_glob,recpos,nsrc_glob,srcpos,ishot,TRKILL_OFFSET_LOWER,TRKILL_OFFSET_UPPER);
            }
113
        }
114 115 116 117 118 119
        
        /* Generate local kill vector */
        h=1;
        for(i=1;i<=ntr;i++){
            kill_vector[h] = kill_tmp[recpos_loc[3][i]][ishot];
            h++;
120 121
        }
    }
122 123 124
    /*---------------*/
    /* End Tracekill */
    /*---------------*/
125
    
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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
    /* Integration of measured data  */
    for(i=1;i<=ntr;i++){
        
        intseis_sd=0;
        
        if (VELOCITY==0){	/* only integtration if displacement seismograms are inverted */
            for(j=1;j<=ns;j++){
                intseis_sd += sectiondata[i][j];
                intseis_sectiondata[i][j]=intseis_sd*DT;
            }
        }
        else{
            for(j=1;j<=ns;j++){
                intseis_sectiondata[i][j]=sectiondata[i][j];
            }
        }
    }
    
    /* TIME WINDOWING */
    if(TIMEWIN==1) time_window(intseis_sectiondata, iter, ntr_glob,recpos_loc, ntr, ns, ishot);
    
    /* NORMALIZE TRACES */
    if(NORMALIZE==1) normalize_data(intseis_sectiondata,ntr,ns);
    
    
    /* calculate energy of measured data */
    for(i=1;i<=ntr;i++){
        if((TRKILL==1)&&(kill_vector[i]==1))
            continue;
        
        for(j=1;j<=ns;j++){
            energy += intseis_sectiondata[i][j]*intseis_sectiondata[i][j];
        }
    }
    
    energy_dummy=energy;
    
    /* printf("\n MYID = %i   IN CALC_ENERGY: energy = %10.12f \n",MYID,energy); */
    
    /* free memory for integrated seismogram */
    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);}
    
    return energy_dummy;
tilman.metz's avatar
tilman.metz committed
175
} /* end of function */