200 && strpos($linje, "lat =") >0){
$linje= strstr($linje, ";", true); //removes ';'
$mlat = explode(",", $linje);
$h=trim($mlat[0]);
sscanf($h,"lat = %f", $mlat[0]);
}//if
if (strlen($linje) > 200 && strpos($linje, "ime =") >0){/*fwrite($ffile, "Break\n");*/ $fstop = true; break;}
} //while
$linje= strstr($linje, ";", true); //removes ';'
$mkol = explode(",", $linje);
$farr= array();
$lin=0;
$filenum=0;
$GL=0;
$NH=1;
$SH=2;
$TR=3;
while(! feof($mfile) && !$sstop ){
$linje= fgets($mfile);
if (strpos($linje, "//") >0){/*fwrite($ffile, "ztoa\n"); */ $linje= fgets($mfile);}
if (strpos($linje, $fileName[$filenum+1]) >0){
fclose($ffile);
$filenum++;
$filn= sprintf("%s.txt", $fileName[$filenum]);
$ffile = fopen($filn, "w") or die("could not open fil");
$feq = sprintf("%s =", $fileName[$filenum]);
$lin=0;
// if($filenum==3){ $sstop=true; break;}
}
$farr = explode(",", $linje);
$alen = count($farr);
if($alen>170){
$fs =array();
$fsg =array();
$hgl=$hnh=$hsh=$htr=0;
$gc =$nc =$sc =$tc =0;
$ha = array();
$hla = array();
// average for global , NH, SH and tropical
for($s=0; $s<=$alen; $s++){
$farr[$s] = trim($farr[$s]);
/*if(strlen($farr[$s]) > 0 ) {
$hgl += $farr[$s]; $gc++;
if($mlat[$s] >=0) { $hnh += $farr[$s]; $nc++;}
if($mlat[$s] < 0) { $hsh += $farr[$s]; $sc++;}
if($mlat[$s] > -30 && $mlat[$s] < 30){ $htr += $farr[$s]; $tc++;}
}*/
$htan= 6356.8/6378.1 * tan(deg2rad($mlat[$s]));
$atan= atan($htan);
$lon = cos($atan);
//$lon = cos(deg2rad($mlat[$s]));
if(strlen($farr[$s]) > 0 ) {
$gc += $lon;
$hgl += $lon*$farr[$s];
// if($lin==0) printf("s=%d farr=%s, hgl=%s \n", $s, $farr[$s], $hgl);
// $hgl += $farr[$s]; $gc++;
if($mlat[$s] >=0) { $hnh += $lon*$farr[$s]; $nc+= $lon;}
if($mlat[$s] < 0) { $hsh += $lon*$farr[$s]; $sc+= $lon;}
if($mlat[$s] > -30 && $mlat[$s] < 30) { $htr += $lon*$farr[$s]; $tc+= $lon;}
if($mlat[$s] < -75){ $ha[0] += $lon*$farr[$s]; $hla[0] += $lon;}
if($mlat[$s] >= -75 && $mlat[$s] < -60){ $ha[1] += $lon*$farr[$s]; $hla[1] += $lon;}
if($mlat[$s] >= -60 && $mlat[$s] < -45){ $ha[2] += $lon*$farr[$s]; $hla[2] += $lon;}
if($mlat[$s] >= -45 && $mlat[$s] < -30){ $ha[3] += $lon*$farr[$s]; $hla[3] += $lon;}
if($mlat[$s] >= -30 && $mlat[$s] < -15){ $ha[4] += $lon*$farr[$s]; $hla[4] += $lon;}
if($mlat[$s] >= -15 && $mlat[$s] < 0){ $ha[5] += $lon*$farr[$s]; $hla[5] += $lon;}
if($mlat[$s] >= 0 && $mlat[$s] < 15){ $ha[6] += $lon*$farr[$s]; $hla[6] += $lon;}
if($mlat[$s] >= 15 && $mlat[$s] < 30){ $ha[7] += $lon*$farr[$s]; $hla[7] += $lon;}
if($mlat[$s] >= 30 && $mlat[$s] < 45){ $ha[8] += $lon*$farr[$s]; $hla[8] += $lon;}
if($mlat[$s] >= 45 && $mlat[$s] < 60){ $ha[9] += $lon*$farr[$s]; $hla[9] += $lon;}
if($mlat[$s] >= 60 && $mlat[$s] < 75){ $ha[10]+= $lon*$farr[$s]; $hla[10]+= $lon;}
if($mlat[$s] >= 75 ){ $ha[11]+= $lon*$farr[$s]; $hla[11]+= $lon;}
}
}
$fsg[$GL] = $hgl/$gc;
if($lin<5) printf("hgl=%s, gc=%s fsg=%s alen =%s mlatalen=%s mlat alen-1=%s, m2=%s\n", $hgl, $gc, $fsg[$GL], $alen, $mlat[$alen-3], $mlat[$alen-1], $mlat[$alen-2]);
$fsg[$NH] = $hnh/$nc;
$fsg[$SH] = $hsh/$sc;
if($tc>0) $fsg[$TR] = $htr/$tc; else printf("obs tc==0 alen=%s htr=%s\n", $alen, $htr);
//average in 15 degree latitude bands
for($s=0; $s<12; $s++){
/* $start= $s*15; $stop=($s+1)*15;
$h=0;
$hl=0;
for($m=$start; $m<$stop; $m++){
if(strlen($farr[$m]) > 0 ){
$h += $farr[$m];
$hl++;
}
} */
$fs[$s] = $ha[$s]/$hla[$s];
}
$date=date_create("2000-03-01");
$days= sprintf("%s days", $mkol[$lin]);
date_add($date,date_interval_create_from_date_string($days));
$year = date_format($date,"Y");
$mon = date_format($date,"m");
$day = date_format($date,"d");
$dyear = $year + ($mon-1)/12 + $day/365;
//$year = 2000 + $mkol[$lin]/365.25;
// $mline = sprintf("%s %s %s %s %s %s %s %s %s %s %s %s %s %s\n", $year, $mon, $day, $fs[0], $fs[1], $fs[2], $fs[3], $fs[4], $fs[5], $fs[6], $fs[7], $fs[8], $fs[9], $fs[10], $fs[11]);
$mline = sprintf("%f %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\n", $dyear, $fs[0], $fs[1], $fs[2], $fs[3], $fs[4], $fs[5], $fs[6], $fs[7], $fs[8], $fs[9], $fs[10], $fs[11],
$fsg[$GL], $fsg[$NH], $fsg[$SH], $fsg[$TR]);
$wl = fwrite($ffile, $mline);
$lin++;
}//if alen > 170
}// while
}// while
fclose($mfile);
fclose($ffile);
?>