Code :
/* Specifics factors : for temperature correction S_cal : Scale factor at T_cal T_cal : calibration temperature K_s : factor for temperature correction --> S : Scale factor corrected for temperature effect K_z :factor for temperature correction T: temperature at the measure V_t : measure volatage Theta_corr : measured angle corrected for temperature SV : convertion between bits and voltage ---> S= S_cal.[1+K_s.(T-T_cal)] --> the new scale factor corrected for temperature Theta_corr=S.V_t-K_z.(T-T_cal) --> the new angle corrected for temperature */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> /* ###########################################*/ float S_cal=0.999; float T_cal=25; /*for the numbers 6208 and 6209 */ float K_s=0.05/100; float K_z= 3; /*be careful with this value*/ float SV=0.21; /* 1 bit = 0.210 mV */ float ST=0.1; /*scale factor for temperature*/ float convert (int value, int temp) { float angle; angle = (S_cal*(1+K_s*(SV*temp-T_cal)))*SV*value-K_z*(ST*SV*temp-T_cal); /*return the value in micro radian*/ return angle; } /* ###########################################*/ void lecture (char *file_name,int *count,int *begin) { FILE *fid; char chaine[20]; char id[20]; int debut; if ((fid = fopen(file_name,"r" )) == NULL) { printf("\n Error: Impossible to read the file" ); } fscanf(fid,"%d",&debut); fscanf(fid,"%s",&id); while (!feof(fid)) { fscanf(fid,"%s",&chaine); if (strcmp(chaine,id)==0) *count=*count-2; *count=*count+1; } *count=*count-1; *begin=debut; fclose(fid); } /* ########################################## */ int lecture_temp (char *file_name,int *tab) { FILE *fid; char chaine[20],s[20]; char id[20]; int debut,time; int i=0; if ((fid = fopen(file_name,"r" )) == NULL) { printf("\n Error: Impossible to read the file" ); } fscanf(fid,"%d",&debut); fscanf(fid,"%s",&id); while (!feof(fid)) { fscanf(fid,"%s",&chaine); if (strcmp(chaine,id)==0) i--; else { tab[i]=atoi(chaine); i++; } } fclose(fid); return i-1; } /* ######################################*/ int lecture_data ( char *name,int sampl, int *temp,int begin_temp,int end,int *time_flag,float *OUT ) { FILE *fid; int z1,z2,z3,z4,j=0,time,NB,data,NB_BLOCS=0,NB_DATA=0,MISS=0,NB_TOT=0; float value,max,min,sum; char chaine[20],id[8],s[20]; long pos; int k=0; int indice=0,i=0,rest; int m=0; if ((fid = fopen(name,"r" )) == NULL) printf("\n Error: Impossible to read the file" ); printf(" \n reading file %s \n",name ); z1=fscanf(fid,"%d",&time); z2=fscanf(fid,"%s",&id); if (z1!= 1||z2!= 1) printf("\n format error (start of the file)" ); rewind(fid); if (begin_temp<time) /* i.e -> temperature starts at first */ { i=((time-begin_temp)*4)/(4*sampl)+1; k=i*sampl*4;/* -> nb of data to skip */ } if (begin_temp>time) /* i.e -> data starts at first */ { k=0;i=0; } OUT[0]=k;m++; while (!feof(fid)) { NB=0; sum=0; while ((NB<4*sampl)&&(!feof(fid))) { pos=ftell(fid); z3=fscanf(fid,"%d",&data); if (z3==0) /*i.e. its a id number*/ { fseek(fid,pos,SEEK_SET); fscanf(fid,"%s",&chaine); if(strcmp(chaine,id)==0) NB_BLOCS++; } if (z3!=0 && data<time) /*its a data*/ { NB_DATA++;NB_TOT++; if (data<0) value=convert(-data,temp[k]); if (data>=0) value=convert(data,temp[k]); k++;NB++; sum+=value; if (k>end) break; } } OUT[m]=sum/(NB); i++;m++; if(k>end) break; } fclose(fid); printf("\n number of read data %d ",NB_TOT -1); printf("\n re-sampling rate : %d",sampl ); printf("\n number of written data: %d",i ); printf("\n number of data in the last block: %d",NB -1); return m-1; } void writing ( char *name, int *time_flag, float *data,int stop,int sampl ) { FILE *fout; int i,k=(int)data[0]/(4*sampl); char s[50]; if ((fout = fopen(name,"w" )) == NULL) printf("\n Error: Impossible to read the file" ); for (i=1;i<=stop;i++) { sprintf(s,"%d\t%f\n",time_flag[k+i]+sampl/2,data[i]); fprintf(fout,"%s",s); } fclose(fout); } /* ###########################################*/ int main (int argc, char **argv) { printf ("\n %s %s",argv [1],argv [2]); char id[7],chaine[20]; int time,data,i; int sampl=300; int z1,z2,z3,z4; /*for files name */ char name_temp[150],E_upp[150],E_nidur[150],N_upp[150],N_nidur[150]; char Eu_out[50],En_out[50],Nu_out[50],Nn_out[50]; /*Creation of the strings where files names are strored */ sprintf(E_upp,"%s/E_upp",argv[1]); sprintf(E_nidur,"%s/E_nidur",argv[1]); sprintf(N_upp,"%s/N_upp",argv[1]); sprintf(N_nidur,"%s/N_nidur",argv[1]); sprintf(name_temp,"%s/Hiti_tilts",argv[1]); /*Creation of strings for outputs name */ sprintf(Eu_out,"%s_E_upp",argv[2]); sprintf(En_out,"%s_E_nidur",argv[2]); sprintf(Nu_out,"%s_N_upp",argv[2]); sprintf(Nn_out,"%s_N_nidur",argv[2]); /* for files information */ int begin_temp=0, size_temp=0; lecture (name_temp,&size_temp,&begin_temp); printf ("\n temp : %d %d", begin_temp,size_temp ); /*for temperature data*/ int *temp; temp= (int*)malloc(size_temp*sizeof(int)+1); int *time_flag; time_flag= (int*)malloc((size_temp/sampl+1)*sizeof(int)+1); /*temperature reading */ int end,truc=0; end=lecture_temp(name_temp,temp); i=1; time_flag[0]=begin_temp; while (truc<size_temp) {time_flag[i]=time_flag[i-1]+sampl; truc=i*sampl; i++; } float *E_u; E_u= (float*)malloc((truc+sampl)*4*sizeof(float)); float *E_n; E_n= (float*)malloc((truc+sampl)*4*sizeof(float)); float *N_u; N_u= (float*)malloc((truc+sampl)*4*sizeof(float)); float *N_n; N_n= (float*)malloc((truc+sampl)*4*sizeof(float)); int a,b,c,d; /* ###### end of declarations ##### */ if (size_temp>0) { a=lecture_data (E_upp,sampl,temp,begin_temp,end,time_flag, E_u ); b=lecture_data (E_nidur,sampl,temp,begin_temp,end,time_flag, E_n ); c=lecture_data (N_upp,sampl,temp,begin_temp,end,time_flag, N_u ); d=lecture_data (N_nidur,sampl,temp,begin_temp,end,time_flag, N_n ); writing(Eu_out,time_flag,E_u,a,sampl); writing(En_out,time_flag,E_n,b,sampl); writing(Nu_out,time_flag,N_u,c,sampl); writing(Nn_out,time_flag,N_n,d,sampl); } /* ########################################### */ }
|