WERAcalibration.html 34.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
      --><title>WERAcalibration</title><meta name="generator" content="MATLAB 7.14"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2015-10-07"><meta name="DC.source" content="WERAcalibration.m"><style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}

html { min-height:100%; margin-bottom:1px; }
html body { height:100%; margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#000; line-height:140%; background:#fff none; overflow-y:scroll; }
html body td { vertical-align:top; text-align:left; }

h1 { padding:0px; margin:0px 0px 25px; font-family:Arial, Helvetica, sans-serif; font-size:1.5em; color:#d55000; line-height:100%; font-weight:normal; }
h2 { padding:0px; margin:0px 0px 8px; font-family:Arial, Helvetica, sans-serif; font-size:1.2em; color:#000; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }
h3 { padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:1.1em; color:#000; font-weight:bold; line-height:140%; }

a { color:#005fce; text-decoration:none; }
a:hover { color:#005fce; text-decoration:underline; }
a:visited { color:#004aa0; text-decoration:none; }

p { padding:0px; margin:0px 0px 20px; }
img { padding:0px; margin:0px 0px 20px; border:none; }
p img, pre img, tt img, li img { margin-bottom:0px; } 

ul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }
ul li { padding:0px; margin:0px 0px 7px 0px; }
ul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }
ul li ol li { list-style:decimal; }
ol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }
ol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }
ol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }
ol li ol li { list-style-type:lower-alpha; }
ol li ul { padding-top:7px; }
ol li ul li { list-style:square; }

.content { font-size:1.2em; line-height:140%; padding: 20px; }

pre, tt, code { font-size:12px; }
pre { margin:0px 0px 20px; }
pre.error { color:red; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }

@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }

span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }

.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }

  </style></head><body><div class="content"><pre class="codeinput">clear <span class="string">all</span>
close <span class="string">all</span>

<span class="comment">%cd('/home/bandma01/Documents/WERAs/')</span>
cd(<span class="string">'/home/bandma01/hfr-data-processing/calibrations/'</span>)

site = <span class="string">'PAO'</span>;
<span class="comment">% site = 'PAB';</span>

cd(site)

<span class="comment">% system</span>
<span class="keyword">for</span> sys = 1:2

    <span class="keyword">if</span> sys == 1
        systm = <span class="string">'Helzel'</span>;
    <span class="keyword">elseif</span> sys == 2
        systm = <span class="string">'Flament'</span>;
    <span class="keyword">end</span>

    clear <span class="string">J</span> <span class="string">I</span> <span class="string">E</span> <span class="string">Phase</span> <span class="string">calib_file</span> <span class="string">calib_hr</span> <span class="string">calib_jd</span> <span class="string">calib_min</span> <span class="string">calib_time</span> <span class="string">calib_value</span>
    clear <span class="string">calib_year</span> <span class="string">d</span> <span class="string">data</span> <span class="string">delta_phase_obs</span> <span class="string">delta_phase_th</span> <span class="string">gps_data</span> <span class="string">index_timecalib</span>
    clear <span class="string">theta_pairs</span>

    <span class="keyword">if</span> strcmp(site,<span class="string">'PAO'</span>) == 1

        load(<span class="string">'HelzelPAO_Track_29-OCT-14 183138.mat'</span>)

    <span class="keyword">elseif</span> strcmp(site,<span class="string">'PAB'</span>) == 1

        <span class="keyword">if</span> strcmp(systm,<span class="string">'Helzel'</span>) == 1
            load(<span class="string">'HelzelPAB_Track_30-OCT-14 175446.mat'</span>)
        <span class="keyword">elseif</span> strcmp(systm,<span class="string">'Flament'</span>) == 1
            load(<span class="string">'FlamentPAB_Track_30-OCT-14 182008.mat'</span>)
        <span class="keyword">end</span>

    <span class="keyword">end</span>


    <span class="comment">% phases and time of calibration</span>
    <span class="keyword">if</span> strcmp(site,<span class="string">'PAO'</span>) == 1
        cd(<span class="string">'2014302'</span>)
    <span class="keyword">elseif</span> strcmp(site,<span class="string">'PAB'</span>) == 1
        cd(<span class="string">'2014303'</span>)
    <span class="keyword">end</span>

    <span class="comment">% list files</span>
    clear <span class="string">d</span>
    d = dir(<span class="string">'*.mat'</span>);       <span class="comment">% returns a structure with name, date, bytes, isdir, datenum</span>
    Nfiles = length(d);     <span class="comment">% nombre de fichiers a analyser</span>


    <span class="comment">% look at the names of files and extract information</span>
    <span class="keyword">for</span> c = 1:Nfiles
        calib_year(c) = str2double(d(c).name(1:4));
        calib_jd(c) = str2double(d(c).name(5:7));
        calib_hr(c) = str2double(d(c).name(8:9));
        calib_min(c) = str2double(d(c).name(10:11));

        load(d(c).name,<span class="string">'phase'</span>)

        Phase(:,c) = phase;
    <span class="keyword">end</span>
    clear <span class="string">phase</span>

    <span class="comment">% for each time stamp (column) compute phase difference between 2</span>
    <span class="comment">% consecutive antennas (row)</span>
    delta_phase_obs = modsym(diff(Phase,1),180);

    To = datenum(2014,01,01,0,0,0);

    <span class="comment">% add 20 sec to center calibration time</span>
    calib_time = To + calib_jd -1 + calib_hr./24 + calib_min/24/60 + 20/24/3600;
    <span class="comment">% datevec(calib_time)</span>

        clear <span class="string">calib_year</span> <span class="string">calib_jd</span> <span class="string">calib_hr</span> <span class="string">calib_min</span>

    <span class="keyword">if</span> strcmp(site,<span class="string">'PAO'</span>) == 1
        <span class="comment">% calib time 2min34sec late wrt GPS</span>
        calib_time = calib_time + 2/24/60 + 34/24/3600;
    <span class="keyword">end</span>
    disp([<span class="string">'calibrations started at '</span> datestr(calib_time(1)) <span class="string">' and ended at '</span> datestr(calib_time(end))])

    <span class="comment">% separate between calibrations made with Helzel's system and Flament's system</span>
    <span class="comment">% date of new calibration files</span>
    <span class="comment">% Helzel 2014-10-30 16:52 to 17:54</span>
    <span class="comment">% Flament 2014-10-30 17:55 to 18:20</span>
    T1 = datenum(2014,10,30,17,55,00);

        clear <span class="string">J</span>
        <span class="keyword">if</span> strcmp(systm,<span class="string">'Helzel'</span>) == 1
            J = find(calib_time &gt; T1);
        <span class="keyword">elseif</span> strcmp(systm,<span class="string">'Flament'</span>) == 1
            J = find(calib_time &lt; T1);
        <span class="keyword">end</span>

        <span class="comment">% remove unwanted data to avoid confusion!</span>
<span class="comment">%         calib_year(J)=[];</span>
<span class="comment">%         calib_jd(J)=[];</span>
<span class="comment">%         calib_hr(J)=[];</span>
<span class="comment">%         calib_min(J)=[];</span>
        Phase(:,J)=[];
        delta_phase_obs(:,J)=[];
        calib_time(J)=[];
        d(J)=[];
        clear <span class="string">c</span> <span class="string">Nfiles</span>

    <span class="comment">%+++++++++++++++++++++++++++++++++++++++++</span>
    <span class="comment">% for each calibration file  (series# 1)</span>
    <span class="comment">%+++++++++++++++++++++++++++++++++++++++++</span>
    <span class="keyword">for</span> calib_file = 1:length(calib_time)

        time_calib = calib_time(calib_file);

        <span class="comment">% find corresponding file in gps data</span>
        clear <span class="string">I</span>
        I = find(data(:,1) &gt; time_calib);
        <span class="comment">% take closest point for now</span>
        I = I(1);

        <span class="comment">% datevec(data(I,1))</span>
        <span class="comment">% datevec(time_calib)</span>

        index_timecalib(calib_file) = I;
    <span class="keyword">end</span>

    <span class="comment">% check</span>
    <span class="comment">% datevec(data(index_timecalib,1))</span>

    <span class="comment">% isolate gps calibration data</span>
    gps_data = data(index_timecalib,:); <span class="comment">% time lat lon</span>

    figure(1)
    hold <span class="string">on</span>
    h1=plot(gps_data(:,2),gps_data(:,3),<span class="string">'.'</span>);
    grid <span class="string">on</span>
        <span class="keyword">if</span> strcmp(systm,<span class="string">'Helzel'</span>) == 1
            set(h1,<span class="string">'marker'</span>,<span class="string">'.'</span>,<span class="string">'color'</span>,<span class="string">'b'</span>)
        <span class="keyword">elseif</span> strcmp(systm,<span class="string">'Flament'</span>) == 1
            set(h1,<span class="string">'marker'</span>,<span class="string">'x'</span>,<span class="string">'color'</span>,<span class="string">'k'</span>)
        <span class="keyword">end</span>

    <span class="comment">% load antenna coordinates</span>
    cd <span class="string">..</span>
    load([site <span class="string">'_Rx_coords.mat'</span>])

    hold <span class="string">on</span>
    plot(lon,lat,<span class="string">'x'</span>)
    grid <span class="string">on</span>
    legend(<span class="string">'gps'</span>,<span class="string">'antennas'</span>,<span class="string">'location'</span>,<span class="string">'southeast'</span>)
    xlabel(<span class="string">'longitude'</span>)
    ylabel(<span class="string">'latitude'</span>)
    title([<span class="string">'site = '</span> site ])



    <span class="comment">% compute theoretical phase values</span>
    <span class="comment">% wavelength</span>
    lambda = 3E8/16.15E6;


    <span class="comment">% initialize variables</span>
    theta_pairs = nan(11,length(gps_data));

    <span class="comment">% for each gps point</span>
    <span class="keyword">for</span> pt = 1:length(gps_data)

        <span class="comment">% compute distance between GPS and antennas</span>
        <span class="keyword">for</span> ant = 1:12
            clear <span class="string">E</span> <span class="string">W</span>
            [E,W] = lonlat2km(gps_data(pt,2),gps_data(pt,3),lon(ant),lat(ant));
            ddist = sqrt(E.^2+W.^2);
            r(ant) = ddist*1000;    <span class="comment">%meters</span>
        <span class="keyword">end</span>


        <span class="comment">% for each pair of antennas</span>
        <span class="keyword">for</span> p = 1:11

            <span class="comment">%delta_r</span>
            delta_r(p) = r(p+1) - r(p);

            <span class="comment">% theoretical phase</span>
            delta_phase_th(p,pt) = modsym((delta_r(p)*360./lambda),180);


            <span class="comment">% position of middle of pairs</span>
            clear <span class="string">lon_mid</span> <span class="string">lat_mid</span>
            lon_mid = (lon(p+1)+lon(p))/2;
            lat_mid = (lat(p+1)+lat(p))/2;

            <span class="comment">% distance between middle of pair and one of the antenna in the pair</span>
            <span class="comment">%         clear E W</span>
            <span class="comment">%         [E,W] = lonlat2km(lon_mid,lat_mid,lon(p),lat(p));</span>
            <span class="comment">%         ddist = sqrt(E.^2+W.^2);</span>
            <span class="comment">%         a(p) = ddist*1000;    %meters</span>

            <span class="comment">% distance between middle of pair and GPS pt</span>
            clear <span class="string">ddsit</span> <span class="string">phasAng</span>
            [ddist,phasAng] = sw_dist([gps_data(pt,3) lat_mid],[gps_data(pt,2) lon_mid],<span class="string">'km'</span>);
            r_mid(p) = ddist*1000;    <span class="comment">%meters</span>
            zeta(p) = phasAng;


            <span class="comment">% distance between pair of antennas</span>
            clear <span class="string">ddsit</span> <span class="string">phasAng</span>
            [ddist,phasAng] = sw_dist([lat(p) lat(p+1)],[lon(p) lon(p+1)],<span class="string">'km'</span>);
            phi(p) = phasAng;


            <span class="comment">% azimuth</span>
            theta_pairs(p,pt) = modsym(zeta(p)-phi(p)+90,180);
            <span class="comment">%rad2deg(theta_pairs)</span>
        <span class="keyword">end</span>
    <span class="keyword">end</span>



    <span class="comment">% for each pair of antenna</span>
    <span class="keyword">for</span> p = 1:11
        figure(p+1)
        h2=plot(theta_pairs(p,:),delta_phase_th(p,:),<span class="string">'x'</span>);
        hold <span class="string">on</span>
        h3=plot(theta_pairs(p,:),-delta_phase_obs(p,:),<span class="string">'or'</span>);
        grid <span class="string">on</span>
        xlabel(<span class="string">'incidence (degrees)'</span>)
        ylabel(<span class="string">'phase difference (degrees)'</span>)
        legend(<span class="string">'theoretical'</span>,<span class="string">'observed'</span>,<span class="string">'location'</span>,<span class="string">'best'</span>)
        <span class="keyword">if</span> strcmp(systm,<span class="string">'Helzel'</span>) == 1
            set(h2,<span class="string">'color'</span>,<span class="string">'b'</span>)
            set(h3,<span class="string">'color'</span>,<span class="string">'r'</span>,<span class="string">'marker'</span>,<span class="string">'o'</span>)
        <span class="keyword">elseif</span> strcmp(systm,<span class="string">'Flament'</span>) == 1
            set(h2,<span class="string">'color'</span>,<span class="string">'k'</span>,<span class="string">'marker'</span>,<span class="string">'*'</span>)
            set(h3,<span class="string">'color'</span>,<span class="string">'r'</span>,<span class="string">'marker'</span>,<span class="string">'sq'</span>,<span class="string">'markerfacecolor'</span>,<span class="string">'r'</span>)
        <span class="keyword">end</span>
        title([site <span class="string">' - antennas '</span> num2str(p) <span class="string">'-'</span> num2str(p+1)])


        <span class="comment">% compute calibration value for antennas pairs</span>
        calib_value(p) = nanmean(modsym(-delta_phase_obs(p,:)-delta_phase_th(p,:),180),2);


         <span class="comment">% compute calibration value for each antenna</span>
        phi_ant(p+1,sys) = sum(calib_value);

    <span class="keyword">end</span>

    P = ones(16,3);
    P(13:16,:) = 0;
    P(1:12,3) = phi_ant(:,1);


    <span class="comment">% save file (we only keep results from Helzel</span>
    <span class="keyword">if</span> strcmp(site,<span class="string">'PAO'</span>) == 1
        dlmwrite(<span class="string">'calibration_pao_29OCT2014.wera'</span>,P,<span class="string">'delimiter'</span>,<span class="string">'\t'</span>,<span class="string">'precision'</span>,<span class="string">'%.7f'</span>)
    <span class="keyword">elseif</span> strcmp(site,<span class="string">'PAB'</span>) == 1
        dlmwrite(<span class="string">'calibration_pab_30OCT2014.wera'</span>,P,<span class="string">'delimiter'</span>,<span class="string">'\t'</span>,<span class="string">'precision'</span>,<span class="string">'%.7f'</span>)
    <span class="keyword">end</span>


    figure(13)
    hold <span class="string">on</span>
    h4 = plot(calib_value,<span class="string">'.-'</span>);
    grid <span class="string">on</span>
    xlabel(<span class="string">'pairs of antennas'</span>)
    ylabel([<span class="string">'phase differences between theoretical'</span>,10 ,<span class="string">'and observed values (degrees)'</span>])
        <span class="keyword">if</span> strcmp(systm,<span class="string">'Helzel'</span>) == 1
            set(h4,<span class="string">'color'</span>,<span class="string">'b'</span>)
            legend(<span class="string">'Helzel'</span>)
        <span class="keyword">elseif</span> strcmp(systm,<span class="string">'Flament'</span>) == 1
            set(h4,<span class="string">'color'</span>,<span class="string">'k'</span>)
            legend(<span class="string">'Helzel'</span>,<span class="string">'Flament'</span>,<span class="string">'location'</span>,<span class="string">'southeast'</span>)
        <span class="keyword">end</span>
    title(site)

    figure(14)
    hold <span class="string">on</span>
    h5 = plot([1:12],phi_ant(:,sys),<span class="string">'r'</span>);
    grid <span class="string">on</span>
    <span class="keyword">if</span> strcmp(systm,<span class="string">'Helzel'</span>) == 1
        set(h5,<span class="string">'marker'</span>,<span class="string">'o'</span>)
        legend(<span class="string">'Helzel'</span>)
    <span class="keyword">elseif</span> strcmp(systm,<span class="string">'Flament'</span>) == 1
        set(h5,<span class="string">'marker'</span>,<span class="string">'sq'</span>,<span class="string">'markerfacecolor'</span>,<span class="string">'r'</span>)
        legend(<span class="string">'Helzel'</span>,<span class="string">'Flament'</span>,<span class="string">'location'</span>,<span class="string">'southeast'</span>)
    <span class="keyword">end</span>
    xlabel(<span class="string">'antenna number'</span>)
    ylabel(<span class="string">'calibration phase to be applied (degrees)'</span>)
    title(site)

    <span class="keyword">if</span> strcmp(site,<span class="string">'PAO'</span>) == 1
       <span class="keyword">break</span>
    <span class="keyword">end</span>

<span class="keyword">end</span>

<span class="comment">% add current calibration phases</span>
<span class="keyword">if</span> strcmp(site,<span class="string">'PAO'</span>) == 1
    load <span class="string">calibration_pao.wera</span>
    figure(14)
    plot([1:12],calibration_pao(1:12,3),<span class="string">'b.-'</span>)
    legend(<span class="string">'Helzel'</span>,<span class="string">'original calibration'</span>,<span class="string">'location'</span>,<span class="string">'northwest'</span>)
<span class="keyword">elseif</span> strcmp(site,<span class="string">'PAB'</span>) == 1
    load <span class="string">calibration_pab.wera</span>
    figure(14)
    plot([1:12],calibration_pab(1:12,3),<span class="string">'b.-'</span>)
    legend(<span class="string">'Helzel'</span>,<span class="string">'Flament'</span>,<span class="string">'original calibration'</span>,<span class="string">'location'</span>,<span class="string">'southeast'</span>)
<span class="keyword">end</span>






<span class="comment">% save figures</span>
save_flag = 1;

figdir = (<span class="string">'/home/bandma01/Dropbox/UQAR/MEOPAR_Marion/figures/WERAcalibration'</span>);
cd(figdir)

fig_hdl = figure(1);
<span class="keyword">if</span> save_flag
    fname = [<span class="string">'configuration_'</span> site <span class="string">'_calibrations'</span>];
    fig_w = 8;<span class="comment">%cm</span>
    fig_h = 5;<span class="comment">%cm</span>
    set(fig_hdl,<span class="string">'PaperUnits'</span>,<span class="string">'centimeters'</span>)
    set(fig_hdl,<span class="string">'PaperPosition'</span>,[0 0 fig_w fig_h])
    set(fig_hdl, <span class="string">'PaperSize'</span>, [fig_w fig_h]);
    print(fig_hdl,<span class="string">'-dpng'</span>,<span class="string">'-r500'</span>,fname)
    <span class="comment">%crop([fname '.png'])</span>
<span class="keyword">end</span>




<span class="keyword">for</span> i = 2:12
    fig_hdl = figure(i);
    <span class="keyword">if</span> save_flag
        fname = [<span class="string">'Phases_'</span> site <span class="string">'_pair'</span> num2str(i-1) <span class="string">'-'</span> num2str(i)];
        fig_w = 8;<span class="comment">%cm</span>
        fig_h = 5;<span class="comment">%cm</span>
        set(fig_hdl,<span class="string">'PaperUnits'</span>,<span class="string">'centimeters'</span>)
        set(fig_hdl,<span class="string">'PaperPosition'</span>,[0 0 fig_w fig_h])
        set(fig_hdl, <span class="string">'PaperSize'</span>, [fig_w fig_h]);
        print(fig_hdl,<span class="string">'-dpng'</span>,<span class="string">'-r500'</span>,fname)
        <span class="comment">%         crop([fname '.png'])</span>
    <span class="keyword">end</span>
<span class="keyword">end</span>



fig_hdl = figure(13);
<span class="keyword">if</span> save_flag
    fname = [<span class="string">'PhaseDifference_'</span> site <span class="string">'_calibrations'</span>];
    fig_w = 8;<span class="comment">%cm</span>
    fig_h = 5;<span class="comment">%cm</span>
    set(fig_hdl,<span class="string">'PaperUnits'</span>,<span class="string">'centimeters'</span>)
    set(fig_hdl,<span class="string">'PaperPosition'</span>,[0 0 fig_w fig_h])
    set(fig_hdl, <span class="string">'PaperSize'</span>, [fig_w fig_h]);
    print(fig_hdl,<span class="string">'-dpng'</span>,<span class="string">'-r500'</span>,fname)
    <span class="comment">%         crop([fname '.png'])</span>
<span class="keyword">end</span>

fig_hdl = figure(14);
<span class="keyword">if</span> save_flag
    fname = [<span class="string">'PhaseCalibration_'</span> site];
    fig_w = 8;<span class="comment">%cm</span>
    fig_h = 5;<span class="comment">%cm</span>
    set(fig_hdl,<span class="string">'PaperUnits'</span>,<span class="string">'centimeters'</span>)
    set(fig_hdl,<span class="string">'PaperPosition'</span>,[0 0 fig_w fig_h])
    set(fig_hdl, <span class="string">'PaperSize'</span>, [fig_w fig_h]);
    print(fig_hdl,<span class="string">'-dpng'</span>,<span class="string">'-r500'</span>,fname)
    <span class="comment">%         crop([fname '.png'])</span>
<span class="keyword">end</span>
</pre><pre class="codeoutput">calibrations started at 29-Oct-2014 17:48:54 and ended at 29-Oct-2014 18:25:54
</pre><img vspace="5" hspace="5" src="WERAcalibration_01.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_02.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_03.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_04.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_05.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_06.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_07.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_08.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_09.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_10.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_11.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_12.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_13.png" alt=""> <img vspace="5" hspace="5" src="WERAcalibration_14.png" alt=""> <p class="footer"><br>
      Published with MATLAB&reg; 7.14<br></p></div><!--
##### SOURCE BEGIN #####
clear all
close all

%cd('/home/bandma01/Documents/WERAs/')
cd('/home/bandma01/hfr-data-processing/calibrations/')

site = 'PAO';
% site = 'PAB';

cd(site)

% system
for sys = 1:2
    
    if sys == 1
        systm = 'Helzel';
    elseif sys == 2
        systm = 'Flament';
    end
    
    clear J I E Phase calib_file calib_hr calib_jd calib_min calib_time calib_value
    clear calib_year d data delta_phase_obs delta_phase_th gps_data index_timecalib
    clear theta_pairs 
    
    if strcmp(site,'PAO') == 1
        
        load('HelzelPAO_Track_29-OCT-14 183138.mat')
        
    elseif strcmp(site,'PAB') == 1
        
        if strcmp(systm,'Helzel') == 1
            load('HelzelPAB_Track_30-OCT-14 175446.mat')
        elseif strcmp(systm,'Flament') == 1
            load('FlamentPAB_Track_30-OCT-14 182008.mat')
        end
        
    end
    
    
    % phases and time of calibration
    if strcmp(site,'PAO') == 1
        cd('2014302')
    elseif strcmp(site,'PAB') == 1
        cd('2014303')
    end
    
    % list files
    clear d
    d = dir('*.mat');       % returns a structure with name, date, bytes, isdir, datenum
    Nfiles = length(d);     % nombre de fichiers a analyser
    
    
    % look at the names of files and extract information
    for c = 1:Nfiles
        calib_year(c) = str2double(d(c).name(1:4));
        calib_jd(c) = str2double(d(c).name(5:7));
        calib_hr(c) = str2double(d(c).name(8:9));
        calib_min(c) = str2double(d(c).name(10:11));
        
        load(d(c).name,'phase')
        
        Phase(:,c) = phase;
    end
    clear phase
    
    % for each time stamp (column) compute phase difference between 2
    % consecutive antennas (row)
    delta_phase_obs = modsym(diff(Phase,1),180);
    
    To = datenum(2014,01,01,0,0,0);
    
    % add 20 sec to center calibration time
    calib_time = To + calib_jd -1 + calib_hr./24 + calib_min/24/60 + 20/24/3600;
    % datevec(calib_time)
    
        clear calib_year calib_jd calib_hr calib_min
        
    if strcmp(site,'PAO') == 1 
        % calib time 2min34sec late wrt GPS
        calib_time = calib_time + 2/24/60 + 34/24/3600;
    end
    disp(['calibrations started at ' datestr(calib_time(1)) ' and ended at ' datestr(calib_time(end))])
    
    % separate between calibrations made with Helzel's system and Flament's system
    % date of new calibration files
    % Helzel 2014-10-30 16:52 to 17:54
    % Flament 2014-10-30 17:55 to 18:20
    T1 = datenum(2014,10,30,17,55,00);
    
        clear J
        if strcmp(systm,'Helzel') == 1
            J = find(calib_time > T1);
        elseif strcmp(systm,'Flament') == 1
            J = find(calib_time < T1);
        end
    
        % remove unwanted data to avoid confusion!
%         calib_year(J)=[];
%         calib_jd(J)=[];
%         calib_hr(J)=[];
%         calib_min(J)=[];
        Phase(:,J)=[];
        delta_phase_obs(:,J)=[];
        calib_time(J)=[];
        d(J)=[];
        clear c Nfiles
    
    %+++++++++++++++++++++++++++++++++++++++++
    % for each calibration file  (series# 1)
    %+++++++++++++++++++++++++++++++++++++++++
    for calib_file = 1:length(calib_time)
        
        time_calib = calib_time(calib_file);
        
        % find corresponding file in gps data
        clear I
        I = find(data(:,1) > time_calib);
        % take closest point for now
        I = I(1);
        
        % datevec(data(I,1))
        % datevec(time_calib)
        
        index_timecalib(calib_file) = I;
    end
    
    % check
    % datevec(data(index_timecalib,1))
    
    % isolate gps calibration data
    gps_data = data(index_timecalib,:); % time lat lon
    
    figure(1)
    hold on
    h1=plot(gps_data(:,2),gps_data(:,3),'.');
    grid on
        if strcmp(systm,'Helzel') == 1
            set(h1,'marker','.','color','b')
        elseif strcmp(systm,'Flament') == 1
            set(h1,'marker','x','color','k')
        end
        
    % load antenna coordinates
    cd ..
    load([site '_Rx_coords.mat'])
    
    hold on
    plot(lon,lat,'x')
    grid on
    legend('gps','antennas','location','southeast')
    xlabel('longitude')
    ylabel('latitude')
    title(['site = ' site ])
   
    
    
    % compute theoretical phase values
    % wavelength
    lambda = 3E8/16.15E6;
    
    
    % initialize variables
    theta_pairs = nan(11,length(gps_data));
    
    % for each gps point
    for pt = 1:length(gps_data)
        
        % compute distance between GPS and antennas
        for ant = 1:12
            clear E W
            [E,W] = lonlat2km(gps_data(pt,2),gps_data(pt,3),lon(ant),lat(ant));
            ddist = sqrt(E.^2+W.^2);
            r(ant) = ddist*1000;    %meters
        end
        

        % for each pair of antennas
        for p = 1:11
            
            %delta_r
            delta_r(p) = r(p+1) - r(p);
            
            % theoretical phase
            delta_phase_th(p,pt) = modsym((delta_r(p)*360./lambda),180);
            
            
            % position of middle of pairs
            clear lon_mid lat_mid
            lon_mid = (lon(p+1)+lon(p))/2;
            lat_mid = (lat(p+1)+lat(p))/2;
            
            % distance between middle of pair and one of the antenna in the pair
            %         clear E W
            %         [E,W] = lonlat2km(lon_mid,lat_mid,lon(p),lat(p));
            %         ddist = sqrt(E.^2+W.^2);
            %         a(p) = ddist*1000;    %meters
            
            % distance between middle of pair and GPS pt
            clear ddsit phasAng
            [ddist,phasAng] = sw_dist([gps_data(pt,3) lat_mid],[gps_data(pt,2) lon_mid],'km');
            r_mid(p) = ddist*1000;    %meters
            zeta(p) = phasAng;
            
            
            % distance between pair of antennas
            clear ddsit phasAng
            [ddist,phasAng] = sw_dist([lat(p) lat(p+1)],[lon(p) lon(p+1)],'km');
            phi(p) = phasAng;
            
            
            % azimuth
            theta_pairs(p,pt) = modsym(zeta(p)-phi(p)+90,180);
            %rad2deg(theta_pairs)
        end
    end
    
    
    
    % for each pair of antenna
    for p = 1:11
        figure(p+1)
        h2=plot(theta_pairs(p,:),delta_phase_th(p,:),'x');
        hold on
        h3=plot(theta_pairs(p,:),-delta_phase_obs(p,:),'or');
        grid on
        xlabel('incidence (degrees)')
        ylabel('phase difference (degrees)')
        legend('theoretical','observed','location','best')
        if strcmp(systm,'Helzel') == 1
            set(h2,'color','b')
            set(h3,'color','r','marker','o')
        elseif strcmp(systm,'Flament') == 1
            set(h2,'color','k','marker','*')
            set(h3,'color','r','marker','sq','markerfacecolor','r')
        end
        title([site ' - antennas ' num2str(p) '-' num2str(p+1)])
       
        
        % compute calibration value for antennas pairs
        calib_value(p) = nanmean(modsym(-delta_phase_obs(p,:)-delta_phase_th(p,:),180),2);

        
         % compute calibration value for each antenna
        phi_ant(p+1,sys) = sum(calib_value);
        
    end
    
    P = ones(16,3);
    P(13:16,:) = 0;
    P(1:12,3) = phi_ant(:,1);
    

    % save file (we only keep results from Helzel
    if strcmp(site,'PAO') == 1
        dlmwrite('calibration_pao_29OCT2014.wera',P,'delimiter','\t','precision','%.7f')
    elseif strcmp(site,'PAB') == 1
        dlmwrite('calibration_pab_30OCT2014.wera',P,'delimiter','\t','precision','%.7f')
    end
    
    
    figure(13)
    hold on
    h4 = plot(calib_value,'.-');
    grid on
    xlabel('pairs of antennas')
    ylabel(['phase differences between theoretical',10 ,'and observed values (degrees)'])
        if strcmp(systm,'Helzel') == 1
            set(h4,'color','b')
            legend('Helzel')
        elseif strcmp(systm,'Flament') == 1
            set(h4,'color','k')
            legend('Helzel','Flament','location','southeast')
        end
    title(site)
        
    figure(14)
    hold on
    h5 = plot([1:12],phi_ant(:,sys),'r');
    grid on
    if strcmp(systm,'Helzel') == 1
        set(h5,'marker','o')
        legend('Helzel')
    elseif strcmp(systm,'Flament') == 1
        set(h5,'marker','sq','markerfacecolor','r')
        legend('Helzel','Flament','location','southeast')
    end
    xlabel('antenna number')
    ylabel('calibration phase to be applied (degrees)')
    title(site)
    
    if strcmp(site,'PAO') == 1
       break
    end

end

% add current calibration phases
if strcmp(site,'PAO') == 1
    load calibration_pao.wera
    figure(14)
    plot([1:12],calibration_pao(1:12,3),'b.-')
    legend('Helzel','original calibration','location','northwest')
elseif strcmp(site,'PAB') == 1
    load calibration_pab.wera
    figure(14)
    plot([1:12],calibration_pab(1:12,3),'b.-')
    legend('Helzel','Flament','original calibration','location','southeast')
end

    




% save figures
save_flag = 1;

figdir = ('/home/bandma01/Dropbox/UQAR/MEOPAR_Marion/figures/WERAcalibration');
cd(figdir)

fig_hdl = figure(1);
if save_flag
    fname = ['configuration_' site '_calibrations'];
    fig_w = 8;%cm
    fig_h = 5;%cm
    set(fig_hdl,'PaperUnits','centimeters')
    set(fig_hdl,'PaperPosition',[0 0 fig_w fig_h])
    set(fig_hdl, 'PaperSize', [fig_w fig_h]);
    print(fig_hdl,'-dpng','-r500',fname)
    %crop([fname '.png'])
end




for i = 2:12
    fig_hdl = figure(i);
    if save_flag
        fname = ['Phases_' site '_pair' num2str(i-1) '-' num2str(i)];
        fig_w = 8;%cm
        fig_h = 5;%cm
        set(fig_hdl,'PaperUnits','centimeters')
        set(fig_hdl,'PaperPosition',[0 0 fig_w fig_h])
        set(fig_hdl, 'PaperSize', [fig_w fig_h]);
        print(fig_hdl,'-dpng','-r500',fname)
        %         crop([fname '.png'])
    end
end



fig_hdl = figure(13);
if save_flag
    fname = ['PhaseDifference_' site '_calibrations'];
    fig_w = 8;%cm
    fig_h = 5;%cm
    set(fig_hdl,'PaperUnits','centimeters')
    set(fig_hdl,'PaperPosition',[0 0 fig_w fig_h])
    set(fig_hdl, 'PaperSize', [fig_w fig_h]);
    print(fig_hdl,'-dpng','-r500',fname)
    %         crop([fname '.png'])
end

fig_hdl = figure(14);
if save_flag
    fname = ['PhaseCalibration_' site];
    fig_w = 8;%cm
    fig_h = 5;%cm
    set(fig_hdl,'PaperUnits','centimeters')
    set(fig_hdl,'PaperPosition',[0 0 fig_w fig_h])
    set(fig_hdl, 'PaperSize', [fig_w fig_h]);
    print(fig_hdl,'-dpng','-r500',fname)
    %         crop([fname '.png'])
end
##### SOURCE END #####
--></body></html>