diff --git a/php/classes/FilesMgr.php b/php/classes/FilesMgr.php index 1f1d756..a275bfd 100644 --- a/php/classes/FilesMgr.php +++ b/php/classes/FilesMgr.php @@ -120,15 +120,21 @@ class FilesMgr extends AmdaObjectMgr return array('type' => $data_type, 'size' => $size, 'n_records' => $n_recs, 'units' => $units, 'fillvalue' => $fillval); } - public function getCdfSampling() - { + public function getCdfSampling() { copy($this->fileName, "temp.cdf"); exec('cdfsamplingfromdata ', $results); unlink("temp.cdf"); return $results; } + + public function getCdfSamplings() { + copy($this->fileName, "temp.cdf"); + exec('cdfsamplingsfromdata ', $results); + unlink("temp.cdf"); + return $results; + } /* * CEF format processing */ @@ -594,22 +600,30 @@ class FilesMgr extends AmdaObjectMgr } - protected function createFile($format) + protected function createFile($format, $samplingType) { $newFile = $this->contentDom->createElement('file'); $newFile->setAttribute('xml:id', $this->fileId); $newFile->setAttribute('name', $this->fileId); $newFile->setAttribute('format', $format); - + + $minSamp = $this->sampling; + $maxSamp = $this->maxSampling; + switch ($format) { case "cdf": $start_stop = explode(" ",$this->getCdfStartStop()); - $result = $this->getCdfSampling(); - //TODO process errors - $minSamp = $result[count($result)-1]; - //TODO if min & max - $maxSamp = $minSamp; + if ($minSamp < 0) { + if ($samplingType == 'constant') { + $result = $this->getCdfSampling(); + //TODO process errors + $minSamp = $result[count($result)-1]; + $maxSamp = $minSamp; + } else { + // $result = $this->getCdfSamplings(); + } + } break; case "txt": $start_stop = explode(" ",$this->reformatTxt()); @@ -621,16 +635,14 @@ class FilesMgr extends AmdaObjectMgr // Test if 'standard' meta exist in CEF $cefMetaTest = $this->getCefStartStop(); - if ($cefMetaTest != -1) - { + if ($cefMetaTest != -1) { $cefStartStop = explode("/",$cefMetaTest); $start_stop[] = strtotime($cefStartStop[0]); $start_stop[] = strtotime($cefStartStop[1]); $result = $this->getCefSampling(); $minSamp = $result[count($result)-1]; } - else - { + else { // no META data - so time processing $timeInfo = $this->getCefTimeInfo(); $timeInfoArr = explode(" ", $timeInfo); @@ -644,8 +656,10 @@ class FilesMgr extends AmdaObjectMgr case "vot" : $start_stop = explode(" ",$this->getVotFileMgr()->getStartStop()); $samplings = $this->getVotFileMgr()->getSamplings(); - $minSamp = $samplings["minSampling"]; - $maxSamp = $samplings["maxSampling"]; + if ($minSamp < 0) { + $minSamp = $samplings["minSampling"]; + $maxSamp = $samplings["maxSampling"]; + } $desc = $this->getVotFileMgr()->getDescription(); break; case "nc": @@ -655,11 +669,13 @@ class FilesMgr extends AmdaObjectMgr return $ncInfo; } $ncInfoArr = explode("#",$ncInfo); - $start_stop = explode(":",$ncInfoArr[0]); - //TODO process errors - $minSamp = $ncInfoArr[1]; - //TODO if min & max - $maxSamp = $minSamp; + $start_stop = explode(":",$ncInfoArr[0]); + if ($minSamp < 0) { + //TODO process errors + $minSamp = $ncInfoArr[1]; + //TODO if min & max + $maxSamp = $minSamp; + } break; default: } @@ -1128,20 +1144,27 @@ class FilesMgr extends AmdaObjectMgr $format = $this->getFormat(); - if ($format === 'unknown') - { + if ($format === 'unknown') { unlink($this->fileName); return array('success' => false, 'error' => 'Sorry, unknown format of '.$fileName); } + // if sampling is defined by user + if ($formats["samplingType"] == "manual") { + $this->sampling = $formats["min_sampling"]; + if ($formats["max_sampling"]) + $this->maxSampling = $formats["max_sampling"]; + else + $this->maxSampling = $this->sampling; + } + + if ($format == 'txt') { - if ($formats["timeFormat"] == "user") - { + if ($formats["timeFormat"] == "user") { $res = $this->reformatTime($formats["nonStandard"], $formats["timeLength"], $formats["doy"]); - if (!$res) - { + if (!$res) { unlink($this->fileName); return array('success' => false, 'error' => 'can\'t reformat time'); } @@ -1149,60 +1172,54 @@ class FilesMgr extends AmdaObjectMgr // check if file is not empty $vars = $this->getTxtColNumber(false); - if ($vars == -100) - { + if ($vars == -100) { return array('success' => false, 'error' => 'no such file'); } - if ($vars == -1) - { + if ($vars == -1) { unlink($this->fileName); return array('success' => false, 'error' => 'while reading file'); } - if ($vars === 0) - { + if ($vars === 0) { unlink($this->fileName); return array('success' => false, 'error' => 'file contains no data'); } + + // sampling is to be defined automatically + if ($this->sampling < 0) { + if ($formats["timeSampling"] == "constant") { + $this->sampling = $this->getTxtSampling(); + $this->maxSampling = $this->sampling; + } + else { + $samplings = $this->getTxtSamplings(); + $this->sampling = $samplings[0]; + $this->maxSampling = $samplings[1]; + } - if ($formats["timeSampling"] == "constant") - { - $this->sampling = $this->getTxtSampling(); - $this->maxSampling = $this->sampling; - } - else - { - $samplings = $this->getTxtSamplings(); - $this->sampling = $samplings[0]; - $this->maxSampling = $samplings[1]; + if ($this->sampling <= 0) { + unlink($this->fileName); + if ($this->sampling == -10) { + return array('success' => false, 'error' => 'Sorry, can\'t process'.$fileName.PHP_EOL.'. Check if there are non numeric chars in the data'); + } + return array('success' => false, 'error' => 'Sorry, can\'t process Time for '.$fileName.PHP_EOL.'. Check time format, start time (> 1970-01-01) or sampling time (>= 1s)'); + } } - - if ($this->sampling <= 0) - { - unlink($this->fileName); - if ($this->sampling == -10) - { - return array('success' => false, 'error' => 'Sorry, can\'t process'.$fileName.PHP_EOL.'. Check if there are non numeric chars in the data'); - } - return array('success' => false, 'error' => 'Sorry, can\'t process Time for '.$fileName.PHP_EOL.'. Check time format, start time (> 1970-01-01) or sampling time (>= 1s)'); - } } if ($format == 'nc') { $status = $this->reformatNcTime(); - if ($status <= 0) - { + if ($status <= 0) { unlink($this->fileName); return array('success' => false, 'error' => 'error '.$timeFormat.PHP_EOL.'Time Format problem'); } } //create new file tag with all attributes and add it to the content DOM - $newFile = $this->createFile($format); - if (is_int($newFile) && ($newFile < 0)) - { + $newFile = $this->createFile($format, $formats["timeSampling"]); + if (is_int($newFile) && ($newFile < 0)) { unlink($this->fileName); return array( 'success' => false, 'file' => $fileName); } @@ -1218,8 +1235,8 @@ class FilesMgr extends AmdaObjectMgr $mask->appendChild($newFile); $files = $mask->getElementsByTagName("file"); - foreach ($files as $file) - { + + foreach ($files as $file) { $starts[] = $file->getAttribute("start"); $stops[] = $file->getAttribute("stop"); } @@ -1230,8 +1247,7 @@ class FilesMgr extends AmdaObjectMgr } } // no corresponding masks => add to fileList - if (!$isMask) - { + if (!$isMask) { $filesList = $this->contentDom->getElementById($this->contentRootId); $filesList->appendChild($newFile); } @@ -1241,8 +1257,7 @@ class FilesMgr extends AmdaObjectMgr //if mask exists - add to data base $myBaseManager = new BaseManager(); $mask = $myBaseManager->addFile($fileName); - if ($mask != null) - { + if ($mask != null) { $startstop = $myBaseManager->getStartStop($myBaseManager->getVi($mask)); $myParamMgr = new DerivedParamMgr('myDataParam'); $myParamMgr->updateMydata($mask,$startstop); diff --git a/php/src/cdfsamplingfromdata.c b/php/src/cdfsamplingfromdata.c index e804d6d..66261e2 100644 --- a/php/src/cdfsamplingfromdata.c +++ b/php/src/cdfsamplingfromdata.c @@ -1,8 +1,5 @@ -/* $Id: timeResFromData.c,v 1.1 2010/08/31 10:32:55 budnik Exp $ */ +/* $Id: cdf constant samplig from data $ */ -/** @file timeResFromData.c -* @brief Function to get CDAWEB dataset sampling times from data files -*/ #include #include #include @@ -46,48 +43,18 @@ void cdf_handle_error(CDFstatus status) exit(1); } -void find_min(int* delta, int nRecs, int minFalse, int* minVal) -{ - int i, n, min; - - n = 1; - i = 0; - while (abs(delta[i]) <= minFalse) i++; - if (i < nRecs) { - min = abs(delta[i]); - // printf(" first MIN %d \n", min); - for (i = 1; i < nRecs; i++) { - // printf(" delta %d \n", delta[i]); - if (delta[i] == min) n++; - if (delta[i] < min && delta[i] > minFalse) { - min = delta[i]; - n = 1; - } - } - } else { - min = -10; - n = nRecs; - } - - minVal[0] = min; - minVal[1] = n; - printf(" MIN %d %d\n", min, n); -} - int main(int argc, char *argv[]) { - long RecStart = 0, RecCount, RecInt = 1; + long RecStart = 0, RecCount = 2, RecInt = 1; long indices[1] = {0}, intervals[1] = {1}, counts[1] = {1}; long varN = -1; long datatype = 0, attrN, maxEntry; CDFstatus cstatus; // CDF status code CDFid id; double *value; - int *delta; int i, minFalse = 0, numEnough; - int min[2] = {0, 0}; char attrValue[300]; - float Sampling; + int sampling; double epoch16[2], dbl_value, dbl_value_; long year, month, day, hour, minute, sec, msec, mksec, nsec, psec; long long *int_value; @@ -106,26 +73,27 @@ int main(int argc, char *argv[]) if ((cstatus = CDFlib(SELECT_, zVAR_, varN, GET_, zVAR_DATATYPE_, &datatype, NULL_)) != CDF_OK) cdf_handle_error (cstatus); - // if (datatype == CDF_EPOCH16) break; } + fprintf(stdout,"DATATYPE %d %d\n", varN, datatype); if ((cstatus = CDFlib(SELECT_, zVAR_, varN, GET_, zVAR_MAXREC_, &CDFmaxRec, NULL_)) != CDF_OK) cdf_handle_error (cstatus); fprintf(stdout,"Max Recs %d\n", CDFmaxRec+1); - if (CDFmaxRec < 10) { + + if (CDFmaxRec < 3) { fprintf(stdout," %d\n", badFile); exit (0); } if ( datatype == CDF_TIME_TT2000 ) { - int_value = (long long *) malloc (sizeof(long long) * (CDFmaxRec+1)); + int_value = (long long *) malloc (sizeof(long long) * (RecCount+1)); /******************* Get Epoch *************************/ if ((cstatus = CDFlib (SELECT_, zVAR_, varN, zVAR_RECNUMBER_, RecStart, - zVAR_RECCOUNT_, CDFmaxRec+1, + zVAR_RECCOUNT_, RecCount + 1, zVAR_RECINTERVAL_, RecInt, zVAR_DIMINDICES_, indices, zVAR_DIMCOUNTS_, counts, @@ -134,12 +102,12 @@ int main(int argc, char *argv[]) cdf_handle_error (cstatus); } else { - value = (double *)malloc(sizeof(double)* (CDFmaxRec+1)*((datatype == CDF_EPOCH16)+1)); + value = (double *)malloc(sizeof(double)* (RecCount + 1)*((datatype == CDF_EPOCH16)+1)); /******************* Get Epoch *************************/ if ((cstatus = CDFlib (SELECT_, zVAR_, varN, zVAR_RECNUMBER_, RecStart, - zVAR_RECCOUNT_, CDFmaxRec+1, + zVAR_RECCOUNT_, RecCount + 1, zVAR_RECINTERVAL_, RecInt, zVAR_DIMINDICES_, indices, zVAR_DIMCOUNTS_, counts, @@ -147,46 +115,35 @@ int main(int argc, char *argv[]) GET_, zVAR_HYPERDATA_, value, NULL_)) != CDF_OK) cdf_handle_error (cstatus); } - - - delta = (int *)malloc(sizeof(int)* CDFmaxRec); - for (i = 1; i < CDFmaxRec+1; i++) - { + if (datatype == CDF_TIME_TT2000) { - dbl_value_ = CDF_TT2000_to_UTC_EPOCH(int_value[i-1]); - dbl_value = CDF_TT2000_to_UTC_EPOCH(int_value[i]); - delta[i-1] = (int)(dbl_value - dbl_value_ + 50)/100; + dbl_value_ = CDF_TT2000_to_UTC_EPOCH(int_value[0]); + dbl_value = CDF_TT2000_to_UTC_EPOCH(int_value[1]); + sampling = (int)(dbl_value - dbl_value_ + 50)/100; } else { if (datatype == CDF_EPOCH16) { - epoch16[0] = value[2*(i-1)]; - epoch16[1] = value[2*(i-1)+1]; + epoch16[0] = value[0]; + epoch16[1] = value[1]; EPOCH16breakdown(epoch16, &year, &month, &day, &hour, &minute, &sec, &msec, &mksec, &nsec, &psec); - value[i-1] = computeEPOCH(year, month, day, hour, minute, sec, msec); - epoch16[0] = value[2*i]; - epoch16[1] = value[2*i+1]; + value[0] = computeEPOCH(year, month, day, hour, minute, sec, msec); + epoch16[0] = value[2]; + epoch16[1] = value[3]; EPOCH16breakdown(epoch16, &year, &month, &day, &hour, &minute, &sec, &msec, &mksec, &nsec, &psec); - value[i] = computeEPOCH(year, month, day, hour, minute, sec, msec); + value[1] = computeEPOCH(year, month, day, hour, minute, sec, msec); } - delta[i-1] = (int)(value[i] - value[i-1] + 50)/100; + sampling = (int)(value[1] - value[0] + 50)/100; } - } + // fprintf(stdout,"value %f\n", value[11]-value[10]); if (datatype == CDF_TIME_TT2000) free(int_value); else free(value); - // fprintf(stdout,"delta %d\n", delta[0]); - numEnough = CDFmaxRec/10; - while (min[1] < numEnough) { - fprintf(stdout,"minFalse %d\n", minFalse); - find_min(delta, CDFmaxRec, minFalse, min); - minFalse = min[0]; - } + - free(delta); if ((cstatus = CDFlib(CLOSE_, CDF_, NULL_)) != CDF_OK) cdf_handle_error (cstatus); // fprintf(stdout,"%d %f\n",min[1], (float)min[0]/100.0); - fprintf(stdout,"%f\n",(float)min[0]/10.0); + fprintf(stdout,"%f\n",(float)sampling/10.0); } diff --git a/php/uploadFile.php b/php/uploadFile.php index eee7f91..eccb04b 100644 --- a/php/uploadFile.php +++ b/php/uploadFile.php @@ -117,11 +117,23 @@ $nonStd = $_POST['nonstd'] ? $_POST['nonstd'] : null; $timeLength = $_POST['timelength'] ? $_POST['timelength'] : null; $doy = isset($_POST['doy']) ? $_POST['doy'] : null; + $sampling = isset($_POST['smpl']) ? $_POST['smpl'] : null; + $min_sampling = isset($_POST['min_manual_sampling']) ? $_POST['min_manual_sampling'] : null; + $max_sampling = isset($_POST['max_manual_sampling']) ? $_POST['max_manual_sampling'] : null; + + if ($sampling == "auto" && $timeSmplg == "variable" + && (strtolower($fileFrmt) == "cdf" || strtolower($fileFrmt) == "nc")) { + $response = array('success' => false, 'error' => " : Auto-definition of variable sampling for $fileFrmt isn't implemented yet. + Use manual sampling definition"); + die(json_encode($response)); + } + $sampData = isset($_POST['sampData']) ? $_POST['sampData'] : null; $sampFileName = isset($_POST['sampFileName']) ? $_POST['sampFileName'] : null; $allFormats = array('fileFormat' => $fileFrmt, 'timeFormat' => $timeFrmt, 'doy' => $doy, - 'timeSampling' => $timeSmplg, 'nonStandard' => $nonStd, 'timeLength' => $timeLength); + 'timeSampling' => $timeSmplg, 'nonStandard' => $nonStd, 'timeLength' => $timeLength, + 'samplingType' => $sampling, 'min_sampling' => $min_sampling, 'max_sampling' => $max_sampling); if ($_POST['filesrc'] == 'URL') $fromURL = true; else $fromURL = false; -- libgit2 0.21.2