PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/geofunc/perlscripts/timeserenu_nord.pl

https://gitlab.com/gpskings/gpslibrary
Perl | 348 lines | 186 code | 55 blank | 107 comment | 18 complexity | 2acd8b6ec450273f55057972eac6004d MD5 | raw file
  1. #!/usr/bin/perl -w
  2. #
  3. #
  4. #
  5. #
  6. #-------------------------------------------------------
  7. #
  8. use strict;
  9. use PDL;
  10. use PDL::NiceSlice;
  11. #use PDL::Core ':Internal';
  12. use PDL::Slatec qw( matinv polyfit polfit );
  13. use PDL::LinearAlgebra qw(mnorm);
  14. use PDL::MatrixOps qw( inv );
  15. use PDL::Fit::Polynomial;
  16. use File::Basename qw(dirname basename fileparse);
  17. use Date::Pcalc qw(Days_in_Year);
  18. use Geo::Functions qw(rad_deg deg_rad);
  19. use Geo::Constants qw(PI);
  20. require "geo.pm";
  21. #defining an initialising parameters
  22. my $scale = '18/8';
  23. #my $outdir = "kara/psfiles/itrf05/";
  24. #my $outdir = "nord/psfiles/";
  25. my $outdir = "/home/bgo/verkefni/gps_timar/disp/nord/psfiles/";
  26. my ($paxis,$saxis, $area);
  27. my ($year,$month,$day, $doy,$long,$lat,$height,$station,$N,$sigmamed,$ressq,$oversigma,$sc,$unctemp,$ind);
  28. my ($filbase, $path,$suff,$psfileb,$psend,$psfile );
  29. my ($start, $stop,$pdata,$coeff, $coeffuns, $label,$title,$nuvvel);
  30. my (@start,@stop,@minmax,@minmaxt,@tmp,@int,@coeff,@coeffuns,@VDVA,@VDVB,@data,@xyz,@MEAN,@RMS);
  31. my ($mean,$prms,$median,$min,$max,$adev,$rms,$days);
  32. # scaling to mm or whatever
  33. my $scaling = 1000;
  34. my @comp = qw/East North Up/;
  35. my @xlabel = ("","","Year");
  36. my @pos = ("0.8 3.6", "5.6 0.6" );
  37. my @sc = (5,4,3);
  38. foreach my $ARG (@ARGV) {
  39. print $ARG,"\n";
  40. ($filbase, $path,$suff ) = &fileparse($ARG,qw{enu.disp});
  41. $title = $filbase; #Station name
  42. #$psfileb = "test"; # psfile prefix
  43. #$psend = "\.ps";
  44. #$psfile = ${psfileb}.${psend}; # psfile for gmt output
  45. $psfile = "${outdir}${filbase}enu\.ps"; # psfile for gmt output ####CHECK enu and enu.disp make more general########3
  46. #changing variables through loop @tmp @minmax
  47. #initiating plot
  48. system ("echo '0 0' | pstext -JX${scale} -R-1/1.5/-1/3 -Y29.1 -P -K << EOF > ${psfile}\n0.3 -0.8 16 0 4 TC ${title}\nEOF\n");
  49. #reading in the data
  50. @data = rcols $ARG;
  51. #getting station information
  52. open FIL, $ARG;
  53. while (<FIL>) {
  54. if (/^#{2}\s/) {
  55. chomp ((undef,undef,$xyz[0],$xyz[1],$xyz[2]) = split );
  56. }
  57. if (/^#{3}/) {
  58. chomp ((undef, $station, $long, $lat, $height ) = split);
  59. last;
  60. }
  61. }
  62. close FIL;
  63. my $interv = 0.01918;
  64. $start = &min($data[0]);
  65. $stop = &max($data[0]);
  66. $int[0] = which( $data[0] < 2007.0);
  67. $int[1] = setops( which( 2008.0 < $data[0]), 'AND', which( 2010.0 > $data[0]) );
  68. $int[2] = which( $data[0] > 2009.0);
  69. print $#int,"\n";
  70. #Fixing the Eurasian plate----------------------
  71. #Altamimi et al 2007 Absolute rotation pole fo Eurasia in ITRF2005 (56.330 deg,-95.979 deg,0.261 deg/my)
  72. #Altamimi et al 2007 Absolute rotation pole fo N. Amerika in ITRF2005 (-4.291 deg,-87.385 deg,0.192 deg/my)
  73. my @eurpole = map &rad_deg($_),(56.330,-95.979,0.261/1e6);
  74. # my @nAmerpole = map &rad_deg($_),(-4.291,-87.385,0.192/1e6);
  75. @eurpole = @{&ellecc([$eurpole[0],$eurpole[1]],[0,0,$eurpole[2]])};
  76. # @nAmerpole = @{&ellecc([$nAmerpole[0],$nAmerpole[1]],[0,0,$nAmerpole[2]])};
  77. # print join(" ",@eurpole),"\n";
  78. # print matinv(&xyz2enumatr(\@eurpole,1)) x ;
  79. $nuvvel = &eccell(&xyzell(&gdatum,\@xyz),&rotpole("",\@xyz,\@eurpole),1);
  80. print $nuvvel*1000;
  81. # my $namvel = &eccell(&xyzell(&gdatum,\@xyz),&rotpole("",\@xyz,\@nAmerpole),1);
  82. # print "-----------------\n";
  83. #$nuvvel = &eccell(&xyzell(&gdatum,\@xyz),&rotpole("EURA",\@xyz,),1);
  84. # my $vel=(pdl($namvel-$nuvvel)*$scaling)->squeeze ;
  85. #print $vel,"\n";
  86. #print mnorm($vel),"\n";
  87. #my $angle = -(atan2($vel(0),$vel(1)))->sclr + &PI/2;
  88. #print 360+deg_rad($angle),"\n";
  89. #print "-----------------\n";
  90. $data[1] .= $data[1] - $nuvvel(0,1)*($data[0]-$start);
  91. $data[3] .= $data[3] - $nuvvel(0,0)*($data[0]-$start);
  92. #-------------------------------------------------------------------
  93. for (my $i=0;$i<=2;$i++) {
  94. # subtracting the average of first few days"
  95. If plate is passed
  96. Calculate the nnr-nuvela velocity of a given plate in geocentric coordinates.
  97. If plate is not passed
  98. Calculate the velocity in a given point in geocentric coordinates xyz with for a given rotation pole wxwywz
  99. """
  100. if plate != None:
  101. pass
  102. else:
  103. if xyz != None and wxwywz != None:
  104. $days=0.002739726*7;
  105. $ind = which($data[0] < $data[0]->at(0)+$days);
  106. ($mean,$prms,$median,$min,$max,$adev,$rms) =
  107. stats($data[2*$i+1]->index($ind),1/($data[2*$i+2]->index($ind))**2);
  108. $data[2*$i+1]=$data[2*$i+1]-$mean;
  109. #the scale of axis
  110. $paxis = "pa1.0f0.1:\"$xlabel[$i]\":/a20f5:\"$comp[$i]\"::.\"\":WSen"; #labels for axis
  111. # $saxis = "sa1of0.1"; #labels for axis
  112. #data range
  113. @minmaxt = ($data[0]->at(0)-0.2,$data[0]->at(-1)+0.2);
  114. @minmax = (min($data[2*$i+1])*1000-5,max($data[2*$i+1])*1000+10);
  115. $area = "$minmaxt[0]\/$minmaxt[1]\/$minmax[0]\/$minmax[1]"; #range
  116. #----------------------------------------------------------------------------------------------
  117. #scaling of unsertainties----------------------------
  118. #Fitting
  119. for (my $j =0 ; $j <= $#int ; $j++) { #fitting each year separetly
  120. #$unctemp = $data[2*$i+2];
  121. if ($j == 0) {
  122. $data[2*$i+2] = $data[2*$i+2] * $sc[$i];
  123. }
  124. ($coeff[$j], $coeffuns[$j]) = &smplsq($data[0]->index($int[$j]),
  125. $data[2*$i+1]->index($int[$j]),
  126. $data[2*$i+2]->index($int[$j]));
  127. ($coeff[$j], $coeffuns[$j]) = ($coeff[$j]*${scaling},
  128. $coeffuns[$j]*${scaling}); #changing to mm
  129. push @VDVB, ($coeff[$j],$coeffuns[$j]);
  130. #scaling of unsertainties----------------------------
  131. #if ($j == 0) {
  132. #($N) = dims($data[2*$i+1]->index(pdl[1..50]));
  133. #$sigmamed = sumover($data[2*$i+2]->index(pdl[1..50])*${scaling})/$N;
  134. #$ressq = ($data[2*$i+1]->index(pdl[1..50])*${scaling}-($data[0]->index(pdl[1..50])*($coeff[$j]->slice(1))+$coeff[$j]->slice(0)) )**2;
  135. #$oversigma = 1/(${scaling}*$data[2*$i+2]->index(pdl[1..50]));
  136. # print $sigmamed, " ", $ressq," ",$oversigma,"\n";
  137. #$sc = sqrt($N*sumover($ressq*$oversigma)/(($N-1)*sumover($oversigma)))/sqrt($sigmamed);
  138. #$unctemp = $data[2*$i+2];
  139. #$data[2*$i+2] = $unctemp * $sc;
  140. #print $sc,"\n";
  141. # print sqrt($ressq),"\n";
  142. # print $data[2*$i+2]*${scaling},"\n";
  143. # }
  144. #fiting again-----------------------------------------------------------------
  145. # ($coeff[$j], $coeffuns[$j]) = &smplsq($data[0]->index($int[$j]),
  146. # $data[2*$i+1]->index($int[$j]),
  147. # $data[2*$i+2]->index($int[$j]));
  148. # ($coeff[$j], $coeffuns[$j]) = ($coeff[$j]*${scaling},
  149. # $coeffuns[$j]*${scaling});
  150. }
  151. #fitting everything
  152. my $cut = intersect(which($data[0] > 2007.1), which( $data[0] < 2007.7));
  153. my $use = setops( which( 2010.3 > $data[0]), 'XOR' , $cut);
  154. #my $use = which( 2010.3 > $data[0]);
  155. ($coeff, $coeffuns) = &smplsq($data[0]->index($use),
  156. $data[2*$i+1]->index($use),
  157. $data[2*$i+2]->index($use));
  158. ($coeff, $coeffuns) = ($coeff*${scaling},
  159. $coeffuns*${scaling}); #changing to mm
  160. push @VDVA, ($coeff,$coeffuns);
  161. #saving the average values of the last campaign
  162. $ind = which($data[0] > 2008.0);
  163. ($mean,$prms,$median,$min,$max,$adev,$rms) =
  164. stats($data[2*$i+1]->index($ind),1/($data[2*$i+2]->index($ind))**2);
  165. push @MEAN, ($mean*${scaling},$rms*${scaling});
  166. # preparing for ploting
  167. @tmp = ([list $data[0]], [list $data[2*$i+1]], [list $data[2*$i+2]]);
  168. $pdata="";
  169. for (my $i=0;$i<=$#{$tmp[0]};$i++) { #input for psxy
  170. $pdata .= sprintf "%s %s %s\n", ${$tmp[0]}[$i],
  171. ${$tmp[1]}[$i]*${scaling},
  172. ${$tmp[2]}[$i]*${scaling};
  173. }
  174. #ploting time series for each component
  175. system ("psxy -JX$scale -R$area -B$paxis -Sc0.05 -W0.1/0 -G60 -Ey0.25c/2 -Y-9.01 -P -O -K << END >> ${psfile}\n${pdata}\nEND\n") == 0 or die "system failed: $?";
  176. #plot the zero line
  177. system("psxy -JX${scale} -R${area} -W2/0 -P -O -K << END >> ${psfile}\n$minmaxt[0] 0\n$minmaxt[1] 0\nEND\n");
  178. #For plotting the fit
  179. for (my $j=0;$j<= $#int ;$j++) {
  180. @start = ($start-0.1, $coeff[$j]->at(0,0)+($start-0.1)*$coeff[$j]->at(1,0) );
  181. @stop = ($stop+0.1, $coeff[$j]->at(0,0)+($stop+0.1)*$coeff[$j]->at(1,0) );
  182. $label = sprintf( "%2.1f\261%2.1f mm/yr",$coeff[$j]->at(1,0),$coeffuns[$j]->at(1,0));
  183. #plot the fitt lines
  184. #if ($j == 0 ) {
  185. # system("psxy -JX${scale} -R${area} -W2/0 -P -O -K << END >> ${psfile}\n$start[0] $start[1]\n$stop[0] $stop[1]\nEND\n");
  186. #put the fitt parameters on labels
  187. # system("pstext -JX${scale} -R0/8/0/4 -P -O -K << END >> ${psfile}\n$pos[$j] 13 0 0 5 $label\nEND\n") == 0 or die "system failed: $?";
  188. # }
  189. }
  190. }
  191. #finish the plot
  192. system "echo '0 0' | psxy -JX -R -Ss0.1 -G255 -P -O >> ${psfile}";
  193. my $file1 = "NORD0509enu.vel";
  194. #open for appending
  195. open FIL1, '>>' , $file1;
  196. # the fit for the hole period
  197. printf FIL1 "%2.6f %2.6f\t%2.4f %2.4f\t%2.4f %2.4f\t%2.4f %2.4f\t%s\n",
  198. $long,$lat,
  199. $VDVA[0]->at(1,0),$VDVA[1]->at(1,0),
  200. $VDVA[2]->at(1,0),$VDVA[3]->at(1,0),
  201. $VDVA[4]->at(1,0),$VDVA[5]->at(1,0),
  202. $station;
  203. #open for appending
  204. close FIL1;
  205. my $file2 = "NORD0506enu.vel";
  206. #open for appending
  207. open FIL2, '>>' , $file2;
  208. #printing the first part of the period
  209. printf FIL2 "%2.6f %2.6f\t%2.4f %2.4f\t%2.4f %2.4f\t%2.4f %2.4f\t%s\n",
  210. $long,$lat,
  211. $VDVB[0]->at(1,0),$VDVB[1]->at(1,0),
  212. $VDVB[6]->at(1,0),$VDVB[7]->at(1,0),
  213. $VDVB[12]->at(1,0),$VDVB[13]->at(1,0),
  214. $station;
  215. close FIL2;
  216. my $file3 = "NORD0809enu.vel";
  217. #open for appending
  218. open FIL3, '>>' , $file3;
  219. #printing the last part of the period
  220. printf FIL3 "%2.6f %2.6f\t%2.4f %2.4f\t%2.4f %2.4f\t%2.4f %2.4f\t%s\n",
  221. $long,$lat,
  222. $VDVB[2]->at(1,0),$VDVB[3]->at(1,0),
  223. $VDVB[8]->at(1,0),$VDVB[9]->at(1,0),
  224. $VDVB[14]->at(1,0),$VDVB[15]->at(1,0),
  225. $station;
  226. my $file4 = "NORD0910enu.vel";
  227. #open for appending
  228. open FIL4, '>>' , $file4;
  229. #printing the last part of the period
  230. printf FIL4 "%2.6f %2.6f\t%2.4f %2.4f\t%2.4f %2.4f\t%2.4f %2.4f\t%s\n",
  231. $long,$lat,
  232. $VDVB[4]->at(1,0),$VDVB[5]->at(1,0),
  233. $VDVB[10]->at(1,0),$VDVB[11]->at(1,0),
  234. $VDVB[16]->at(1,0),$VDVB[17]->at(1,0),
  235. $station;
  236. $year = 2007;
  237. $doy = 243;
  238. # $day = $year+$doy/Days_in_Year($year,12); #calculating the difference at day $day
  239. $day = $stop; #calculating the difference at day $day
  240. my $Dday = ($day - $start)/2;
  241. print join(" ",@MEAN),"\n";
  242. print $MEAN[4]-($VDVB[8]->(0)+$VDVB[8]->(1)*$day),"\n";
  243. $file4 = "NORDenu.dsp";
  244. #open for appending
  245. open FIL4, '>>' , $file4;
  246. #printing the last part of the period
  247. printf FIL4 "%2.6f %2.6f\t%2.4f %2.4f\t%2.4f %2.4f\t%2.4f %2.4f\t%s\n",
  248. $long,$lat,
  249. $MEAN[0],$MEAN[1],
  250. $MEAN[2],$MEAN[3],
  251. list($MEAN[4]-($VDVB[8]->(0)+$VDVB[8]->(1)*$day)),
  252. list( sqrt(($MEAN[5] )**2 + ( $VDVB[9]->(1)*$Dday )**2 )),
  253. $station;
  254. # printf FIL4 "%2.6f %2.6f\t%2.4f %2.4f\t%2.4f %2.4f\t%2.4f %2.4f\t%s\n",
  255. # $long,$lat,
  256. # list(($VDVB[2]->(0)+$VDVB[2]->(1)*$day)-($VDVB[0]->(0)+$VDVB[0]->(1)*$day)),
  257. # list( sqrt( ($VDVB[3]->(1)*$Dday)**2 + ( $VDVB[1]->(1)*$Dday)**2 )),
  258. # list( ($VDVB[6]->(0)+$VDVB[6]->(1)*$day)-($VDVB[4]->(0)+$VDVB[4]->(1)*$day)),
  259. # list(sqrt( ( $VDVB[7]->(1)*$Dday)**2 + ( $VDVB[5]->(1)*$Dday)**2 )),
  260. # list(($VDVB[10]->(0)+$VDVB[10]->(1)*$day)-($VDVB[8]->(0)+$VDVB[8]->(1)*$day)),
  261. # list( sqrt(($VDVB[11]->(1)*$Dday )**2 + ( $VDVB[9]->(1)*$Dday )**2 )),
  262. # $station;
  263. close FIL4;
  264. @MEAN = ();
  265. @VDVB = ();
  266. @VDVA = ();
  267. }
  268. # subroutines ------------------------------------------------------------
  269. #----------------------------------------------------------------
  270. #
  271. # Least squere fit
  272. #
  273. #
  274. #
  275. #------------------------------------------------------------------
  276. sub smplsq {
  277. my $M = zeroes(2,2);
  278. my $V = zeroes(1,2);
  279. my $X = shift();
  280. my $Y = shift();
  281. my $chi = shift();
  282. $M(0,0) .= sumover($chi**-2);
  283. $M(0,1) .= sumover($X*$chi**-2);
  284. $M(1,0) .= $M(0,1);
  285. $M(1,1) .= sumover($X**2*$chi**-2);
  286. $V(0,0) .= sumover($Y*$chi**-2);
  287. $V(0,1) .= sumover($Y*$X*$chi**-2);
  288. if ($M(0,0)*$M(1,1)-$M(0,1)*$M(1,0)) {
  289. return ( transpose(matinv($M) x $V), sqrt(matinv($M)->diagonal(0,1)) );
  290. } else {
  291. return pdl(0 , 0), pdl(0,0);
  292. }
  293. }