<?php
//foreach(file("series.csv") as $line
$line = "2013.10.27;-72;AR 1883";
$params = split(";",$line);
echo "Serie found";
print_r($params);
$codes_url = "http://jsoc.stanford.edu/cgi-bin/ajax/jsoc_info?ds=hmi.sharp_cea_720s%5B%5D%5B${params[0]}_00%3A00%3A00_TAI%5D&op=rs_list&key=HARPNUM%2CNOAA_ARS&seg=**NONE**&link=**NONE**&R=1&userhandle=20141208_00069_1418066312075";
echo "Looking for NOAA_ARS code of serie ".$params[3]."\n";
echo $codes_url."\n\n";
$c = '{"recinfo":[{"name":"hmi.sharp_cea_720s[3286][2013.10.27_00:00:00_TAI]","online":0},{"name":"hmi.sharp_cea_720s[3288][2013.10.27_00:00:00_TAI]","online":0},{"name":"hmi.sharp_cea_720s[3291][2013.10.27_00:00:00_TAI]","online":0},{"name":"hmi.sharp_cea_720s[3295][2013.10.27_00:00:00_TAI]","online":0},{"name":"hmi.sharp_cea_720s[3306][2013.10.27_00:00:00_TAI]","online":0},{"name":"hmi.sharp_cea_720s[3309][2013.10.27_00:00:00_TAI]","online":0},{"name":"hmi.sharp_cea_720s[3311][2013.10.27_00:00:00_TAI]","online":0},{"name":"hmi.sharp_cea_720s[3313][2013.10.27_00:00:00_TAI]","online":0},{"name":"hmi.sharp_cea_720s[3316][2013.10.27_00:00:00_TAI]","online":0},{"name":"hmi.sharp_cea_720s[3320][2013.10.27_00:00:00_TAI]","online":0},{"name":"hmi.sharp_cea_720s[3321][2013.10.27_00:00:00_TAI]","online":0}],"keywords":[{"name":"HARPNUM","values":["3286","3288","3291","3295","3306","3309","3311","3313","3316","3320","3321"]},{"name":"NOAA_ARS","values":["11872","11873,11880","11875","11877,11879","MISSING","11881","11882","MISSING","MISSING","11883","11884,11885"]}],"segments":[],"links":[],"count":11,"runtime":0.132,"status":0}';
//$c = get_file_contents($codes_url);
$codes = json_decode($c, true);
//print_r($codes);
$index_of_code = array_search("1".substr($params[2],3),$codes['keywords'][1]['values']);
$serie_code = $codes['recinfo'][$index_of_code]['name'];
$task_launch_url = "http://jsoc.stanford.edu/cgi-bin/ajax/jsoc_fetch?op=exp_request&ds=${serie_code}{magnetogram,Bp,Bt,Br}&sizeratio=1&process=n=0|no_op&requestor=solarmail¬ify=solarmail&method=url&filenamefmt=hmi.sharp_cea_720s.{HARPNUM}.{T_REC:A}.{segment}&format=json&protocol=FITS,compress%20Rice";
echo "Going to ask archive for serie ".serie_code." data\n";
echo $task_launch_url."\n\n\n";
$c = '{"status":2,"requestid":"JSOC_20141208_324","method":"url","protocol":"FITS,compress Rice","wait":10,"rcount":1,"size":1}';
//$c = get_file_contents($task_launch_url);
$task = json_decode($c, true);
echo "Save task "+$task['requestid']+" for further monitoring\n\n";
//file_put_contents($c,$task['requestid']); //dump task
//}//close FOR loop with read series lines
//listener.php:
$dumped_tasks_list = array('JSOC_20141208_324');
//$dumped_tasks_list = glob("*");
foreach ( $dumped_tasks_list as $taskid) {
echo "Check task ".$taskid." status\n\n";
$c = '{"version":"0.5","requestid":"JSOC_20141208_324","method":"url","protocol":"fits","count":"4","size":"1","exptime":"2014.12.08_20:55:17_UT","dir":"\/SUM9\/D639406970\/S00000","status":"0","keywords":"**IN FITS FILES**","data":[{"record":"hmi.sharp_cea_720s[3320][2013.10.27_00:00:00_TAI]","filename":"hmi.sharp_cea_720s.3320.20131027_000000_TAI.magnetogram.fits"},{"record":"hmi.sharp_cea_720s[3320][2013.10.27_00:00:00_TAI]","filename":"hmi.sharp_cea_720s.3320.20131027_000000_TAI.Bp.fits"},{"record":"hmi.sharp_cea_720s[3320][2013.10.27_00:00:00_TAI]","filename":"hmi.sharp_cea_720s.3320.20131027_000000_TAI.Bt.fits"},{"record":"hmi.sharp_cea_720s[3320][2013.10.27_00:00:00_TAI]","filename":"hmi.sharp_cea_720s.3320.20131027_000000_TAI.Br.fits"}]}';
//$c = file_get_contents("http://jsoc.stanford.edu/cgi-bin/ajax/jsoc_fetch?op=exp_status&requestid=${task_id}")
$task_info = json_decode($c, true);
if($task_info['status']==='0') {
foreach($task_info['data'] as $file) {
$url = "http://jsoc.stanford.edu".$task_info['dir']."/".$file['filename'];
echo "Downloading ".$url."\n";
//$c = file_get_contents('');
//file_put_contents()
//rm($taskid)
}
}
}
?>