numass-framework/numass-main/src/main/java/inr/numass/NumassIO.java

128 lines
4.3 KiB
Java
Raw Normal View History

2015-12-18 16:20:47 +03:00
/*
* Copyright 2015 Alexander Nozik.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package inr.numass;
import hep.dataforge.io.BasicIOManager;
import hep.dataforge.meta.Meta;
import hep.dataforge.names.Name;
import inr.numass.data.NumassDataReader;
import inr.numass.data.NumassPawReader;
import inr.numass.data.RawNMFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2016-03-21 15:29:31 +03:00
import org.apache.commons.io.FilenameUtils;
2015-12-18 16:20:47 +03:00
import org.apache.commons.io.output.TeeOutputStream;
/**
*
* @author Darksnake
*/
public class NumassIO extends BasicIOManager {
public static final String NUMASS_OUTPUT_CONTEXT_KEY = "numass.outputDir";
2016-03-21 15:29:31 +03:00
2015-12-18 16:20:47 +03:00
@Override
public OutputStream out(Name stage, Name name) {
List<String> tokens = new ArrayList<>();
if (getContext().hasValue("numass.path")) {
String path = getContext().getString("numass.path");
if (path.contains(".")) {
tokens.addAll(Arrays.asList(path.split(".")));
} else {
tokens.add(path);
}
}
if (stage != null) {
tokens.addAll(Arrays.asList(stage.asArray()));
}
String dirName = String.join(File.separator, tokens);
String fileName = name.removeNameSpace().toString() + ".out";
return buildOut(getOutputDir(), dirName, fileName);
}
private File getOutputDir() {
String outputDirPath = getContext().getString(NUMASS_OUTPUT_CONTEXT_KEY, ".dataforge");
File res = new File(getRootDirectory(), outputDirPath);
if (!res.exists()) {
res.mkdir();
}
return res;
}
protected OutputStream buildOut(File parentDir, String dirName, String fileName) {
File outputFile;
if (!parentDir.exists()) {
throw new RuntimeException("Working directory does not exist");
}
if (dirName != null && !dirName.isEmpty()) {
parentDir = new File(parentDir, dirName);
if (!parentDir.exists()) {
parentDir.mkdirs();
}
}
// String output = source.meta().getString("output", this.meta().getString("output", fileName + ".out"));
outputFile = new File(parentDir, fileName);
try {
if (getContext().getBoolean("numass.consoleOutput", false)) {
return new TeeOutputStream(new FileOutputStream(outputFile), System.out);
} else {
return new FileOutputStream(outputFile);
}
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
}
}
2016-03-22 11:31:54 +03:00
public static RawNMFile readAsDat(File source, Meta config) throws IOException {
return new NumassDataReader(source, config).read();
2015-12-18 16:20:47 +03:00
}
2016-03-22 11:31:54 +03:00
public static RawNMFile readAsPaw(File source) throws FileNotFoundException {
return new NumassPawReader().readPaw(source, source.getName());
2015-12-18 16:20:47 +03:00
}
2016-03-22 11:31:54 +03:00
public static RawNMFile getNumassData(File file, Meta config) {
try {
RawNMFile dataFile;
String extension = FilenameUtils.getExtension(file.getName()).toLowerCase();
switch (extension) {
case "paw":
dataFile = readAsPaw(file);
break;
case "dat":
dataFile = readAsDat(file, config);
break;
default:
throw new RuntimeException("Wrong file format");
}
return dataFile;
} catch (IOException ex) {
throw new RuntimeException(ex);
2015-12-18 16:20:47 +03:00
}
}
}