', '>', $description); fputs($f,""); fputs($f,htmlentities($description)); fputs($f,"\n"); } private function WriteField($f,$field,$refgroupid) { if (!strstr($field['id'], 'xyz')){ $str = "WriteDescription($f,$field['field_des']); fputs($f,"\n"); fputs($f,"\n"); } //TODO delete else after integration by LATMOS vecteurs in VOtable else { if ($field['comps_label'] == "X;Y;Z") $attributes = explode(';', $field['comps_label']); else $attributes = preg_split ("/\s+/", $field['comps_label']); $a = 1; foreach($attributes as $attr){ $str = "WriteDescription($f,$field['field_des']); fputs($f,"\n"); fputs($f,"\n"); $a++; } } } private function WriteInfoGroup($f,$field) { if (!isset($field['array_size'])) return NULL; if (($field['array_size'] == '') || ($field['array_size'] == '*')) return NULL; $array_size = intval($field['array_size']); if ($array_size < 2) return NULL; // TODO delete after VOTable group implementation by LATMOS and Co if (strstr($field['id'], 'xyz')) return NULL; // end delete $group_id = "info_".$field['id']; $str = "\n"; //"param_".$field['id']."_"info $str .= " $iso, "srclen" => $srclen); } private function WriteDataFromASCII($f,$data_file,$timeFormat,$milli,$fields) { fputs($f,"\n"); $in = fopen($data_file['path'],"r"); if (!$in) return array('success' => false, 'message' => $data_file['path']);//"Cannot load input file"); while ($line = fgets($in)) { $l = trim($line); if ((strlen($l) <= 0) || $l[0] == '#') continue; fputs($f,""); $time = $this->getTimeValue($line,$timeFormat,$milli); if ($time['iso'] == '') continue; //cannot load time fputs($f,"".$time['iso'].""); $l = substr($l,$time['srclen'],strlen($l)-$time['srclen']); foreach($fields as $field) { if (isset($field['xtype']) && (strcmp($field['xtype'],'dateTime') == 0) && isset($field['ucd']) && (strcmp($field['ucd'],'time.epoch') == 0)) continue; //skip time field if (!isset($field['array_size'])) $size = 1; else $size = $field['array_size']; fputs($f,""); for ($i = 0; $i < $size; $i++) { $l = trim($l); if ($l == '') { // $l = fgets($in); $l = trim($l); } sscanf($l,"%s",$str_value); if ($i > 0) if (!strstr($field['id'], 'xyz')) fputs($f," "); else { fputs($f,""); // $str_value = floatval($str_value) * 3393.00; } fputs($f,$str_value); $l = substr($l,strlen($str_value),strlen($l)-strlen($str_value)); } fputs($f,""); } fputs($f,"\n"); } fclose($in); fputs($f,"\n"); return array('success' => true); } private function WriteTable($f,$input_params) { fputs($f,"\n"); foreach($input_params['fields'] as $field) { $refgroupid = $this->WriteInfoGroup($f,$field); $this->WriteField($f,$field,$refgroupid); } $res = $this->WriteDataFromASCII($f,$input_params['data_file'],$input_params['time_format'],$input_params['time_milli'],$input_params['fields']); fputs($f,"
\n"); return $res; } private function WriteResource($f,$input_params) { fputs($f,"\n"); $this->WriteDescription($f,$input_params['ressource_des']); $res = $this->WriteTable($f,$input_params); fputs($f,"\n"); return $res; } function WriteVOTable($input_params,$output_file) { $out = fopen($output_file,"w"); chmod ($output_file, 0664); if (!$out) return array('success' => false, 'message' => "Cannot create output file"); fputs($out,"\n"); fputs($out,"\n"); $this->WriteDescription($out,$input_params['general_des']); $res = $this->WriteResource($out,$input_params); fputs($out,"\n"); fclose($out); return $res; } function pushParamFields(&$fields, $param) { $infoMgr = new ParamsInfoMgr(); $infos = $infoMgr->GetParamInfo($param); if (!$infos['success']) return $infos; switch ($infos['type']) { case 'derived' : array_push($fields, array( 'field_des' => $infos['infos']['buildchain'], 'data_type' => 'float', 'name' => $infos['infos']['name'], 'id' => $param, 'ucd' => "", 'fill_val' => "NaN", 'unit' => $infos['infos']['units'] )); break; case 'local' : { if (!$infos['withcode']) { if ($infos['codeinfos']['valuetype']) switch($infos['valuetype']) { case 'FLOAT' : $valueType = 'float'; break; case 'INT' : $valueType = 'int'; break; default : $valueType = 'float'; } $ucd = $infos['ddinfos']['parameter']['ucd']; if ($infos['ddinfos']['parameter']['size'] != '') $size = $infos['ddinfos']['parameter']['size']; else $size = $infos['codeinfos']['size']; $isarray = ($size > 1); if ($infos['ddinfos']['parameter']['name'] != '') $name = $infos['ddinfos']['parameter']['name']; else $name = $infos['codeinfos']['title']; if ($isarray) if ($infos['ddinfos']['parameter']['comps_label'] != '') $comps_label = $infos['ddinfos']['parameter']['comps_label']; else $comps_label = $infos['codeinfos']['legend']; $desc = $infoMgr->ParamInfosToString($infos); $fillval = $infos['codeinfos']['fillvalue']; if ($infos['ddinfos']['parameter']['id'] != '') $id = $infos['ddinfos']['parameter']['id']; else $id = $param; } else { $valueType = 'float'; $ucd = $infos['ddinfos']['parameter']['ucd']; if ($infos['ddinfos']['parameter']['size'] != '') $size = $infos['ddinfos']['parameter']['size']; else $size = $infos['codeinfos']['size']; $isarray = ($size > 1); if ($infos['ddinfos']['parameter']['name'] != '') $name = $infos['ddinfos']['parameter']['name']; else $name = $infos['codeinfos']['title']; $comps_label = ''; if ($isarray) { for ($i = 0; $i < $size; $i++) { if ($i != 0) $comps_label .= ' '; $comps_label .= $name.'_'.$i; } } $desc = $infoMgr->ParamInfosToString($infos); $fillval = 'NaN'; $id = $param; } if ($infos['ddinfos']['parameter']['units'] != '') $units = $infos['ddinfos']['parameter']['units']; else $units = $infos['codeinfos']['units']; // $units = $infos['ddinfos']['parameter']['units']; if ($isarray) { array_push($fields, array( 'field_des' => $desc, 'data_type' => $valueType, 'array_size'=> $size, 'name' => $name, 'id' => $param, 'ucd' => $ucd, 'fill_val' => $fillval, 'unit' => $units, 'comps_label' => $comps_label )); } else { array_push($fields, array( 'field_des' => $desc, 'data_type' => $valueType, 'name' => $name, 'id' => $id, 'ucd' => $ucd, 'fill_val' => $fillval, 'unit' => $units )); } break; } case 'mydata' : { $valueType = $infos['type']; $size = $infos['size']; $name = $infos['name']; $units = $infos['units']; $fillval = $infos['fillvalue']; $id = $infos['realvar']; $ucd = $infos['mask']; array_push($fields, array( 'field_des' => $valueType, 'data_type' => $valueType, 'name' => $name, 'id' => $id, 'ucd' => $ucd, 'fill_val' => $fillval, 'unit' => $units )); break; } default : return array('success' => false, 'message' => 'Unknown parameter type : '.$infos['type']); } return array('success' => true); } function getTimeField() { return array( 'param_name' => "Time", 'field_des' => "Time", 'data_type' => "char", 'name' => "Time", 'id' => "Time", 'ucd' => "time.epoch", 'xtype' => "dateTime", 'fill_val' => "NaN", 'array_size' => "*" ); } function downloadResultToVOTable($id,$newName,$opts,$inputCompressed = true,$canBeAlreadyVOTable = false) { $dir = USERWORKINGDIR.$id.'_/'; if ($inputCompressed) { //test if the archive file exist if (strncmp($opts['compression'],'zip',3) === 0) $downFile = $dir.$newName.".zip"; else $downFile = $dir.$newName.".tar.gz"; if (!file_exists($downFile)) return array('success' => false, 'message' => "Download file ".$downId." doesn't exist"); } // $params = explode(' ',$opts['params']); $files = array(); $isVOT = $canBeAlreadyVOTable && (strcmp($opts['fileformat'],'vot') === 0); switch ($opts['structure']) { case '0' : //All In One File case '1' : //One File Per Time Interval //get information about fileds (field in VOTable = parameter in AMDA) $fields = array(); //push time field (the first field is the time field) array_push($fields, $this->getTimeField()); //push parameters fields foreach($params as $param) { $res = $this->pushParamFields($fields, $param); if (!$res['success']) return $res; } //extract the archive chdir($dir); if ($isVOT) $allfile = $newName.".xml"; else $allfile = "ALL.txt"; if ($inputCompressed) { if (strncmp($opts['compression'],'zip',3) === 0) exec("unzip ".$downFile." ".$allfile); else exec("tar -xvzf ".$downFile." ".$allfile); } if (!file_exists($allfile)) return array('success' => false, 'message' => "Error to extract data (".$allfile.")"); //write VO Table $data_file = array( 'path' => $allfile); $input_params = array('general_des' => "Generated by CDPP/AMDA", 'ressource_des' => $id, 'table_name' => $id, 'fields' => $fields, 'data_file' => $data_file, 'time_format' => $opts['timeformat'], 'time_milli' => $opts['milli']); if (!$isVOT) { $res = $this->WriteVOTable($input_params,$dir.$newName.'.xml'); unlink($allfile); } else $res = array('success' => true); if (!$res['success']) return $res; array_push($files,USERWORKINGDIR.$id.'_/'.$newName.'.xml'); break; case '2' : //One File Per Param/Interval chdir($dir); foreach($params as $param) { //get information about fileds $fields = array(); //push time field array_push($fields, $this->getTimeField()); $res = $this->pushParamFields($fields, $param); if (!$res['success']) return $res; //extract the archive chdir($dir); if ($isVOT) $filename = $newName."_".$param.'.xml'; else $filename = strtoupper($param).".txt"; if ($inputCompressed) { if (strncmp($opts['compression'],'zip',3) === 0) exec('unzip '.$downFile.' "'.$filename.'"'); else exec('tar -xvzf '.$downFile.' "'.$filename.'"'); } if (!file_exists($filename)) return array('success' => false, 'message' => "Error to extract data (".$filename.")"); //write VO Table $data_file = array( 'path' => $filename); $input_params = array('general_des' => "Imported from AMDA", 'ressource_des' => $id."_".$param, 'table_name' => $id."_".$param, 'fields' => $fields, 'data_file' => $data_file, 'time_format' => $opts['timeformat'], 'time_milli' => $opts['milli']); if (!$isVOT) { $res = $this->WriteVOTable($input_params,$dir.$newName."_".$param.'.xml'); unlink($filename); } else $res = array('success' => true); if (!$res['success']) return $res; /*$votableMgr = new VOTableMgr(); $votableMgr->load($dir.$newName."_".$param.'.xml'); $votableMgr->isValidSchema();*/ array_push($files,USERWORKINGDIR.$id.'_/'.$newName."_".$param.'.xml'); } break; // case '3' : // Argument by argument // // break; default : return array('success' => false, 'message' => "Request type unknown"); } return array('success' => true, 'files' => $files); } } ?>