Here you can find the source of LoadDspaceMapFile(String filename)
Parameter | Description |
---|---|
filename | The name of the binary file containing the diffractometer constants. |
public static double[] LoadDspaceMapFile(String filename)
//package com.java2s; /* //from www . ja v a2s . c om * File: FileUtil.java * * Copyright (C) 2010, Dennis Mikkelson * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * Contact : Dennis Mikkelson <mikkelsond@uwstout.edu> * Department of Mathematics, Statistics and Computer Science * University of Wisconsin-Stout * Menomonie, WI 54751, USA * * This work was supported by the Spallation Neutron Source Division * of Oak Ridge National Laboratory, Oak Ridge, TN, USA. * * Last Modified: * * $Author:$ * $Date:$ * $Revision:$ */ import java.io.*; public class Main { /** * Get the array of diffractometer constants to map from time-of-flight to * d-spacing, for each DAS ID. The file must contain constants for each * pixel ID to be mapped, as a contiguous sequence of values. The kth * double in the file is interpreted as the diffractometer constant for * DAS pixel ID k. * The file MUST be stored in little endian format as written by PYTHON * on a PC. This is NOT the form of a JAVA binary file, so this * method reverses the bytes before converting double values. * * @param filename The name of the binary file containing the * diffractometer constants. * * @return An array of doubles containing the diffractometer constants * for each pixel on the instrument. */ public static double[] LoadDspaceMapFile(String filename) { int bytes_per_record = 8; // one double per DAS ID CheckFile(filename); File map_file = new File(filename); long file_size = map_file.length(); if (file_size % bytes_per_record != 0) throw new IllegalArgumentException(filename + " is not a d-space map."); long n_ids = file_size / bytes_per_record; byte[] buffer = new byte[(int) file_size]; double[] map = new double[(int) n_ids]; try { RandomAccessFile r_file = new RandomAccessFile(filename, "r"); r_file.seek(0); long bytes_read = r_file.read(buffer); if (bytes_read != file_size) throw new IllegalArgumentException(filename + " NOT read properly."); } catch (Exception ex) { throw new IllegalArgumentException("Error loading dspace map: " + filename); } int index = 0; for (int id = 0; id < n_ids; id++) { map[id] = getDouble_64(buffer, index); index += 8; } return map; } /** * Check that the specified name is the name of a file that exists and * can be read by the user. Throw an exception if the file can't be * read. * * @param filename * * @throws IllegalArgumentException if the file name is null, the * file doesn't exist, or the file can't be read. */ public static void CheckFile(String filename) { if (filename == null) throw new IllegalArgumentException("Filename String is NULL"); File file = new File(filename); if (!file.exists()) throw new IllegalArgumentException("File doesn't exist: " + filename); if (!file.canRead()) throw new IllegalArgumentException("File can't be read: " + filename); } /** * Decode the double value stored in a sequence of eight bytes in * the buffer. The eight bytes determining the double value are * stored in the file and buffer in the sequence: b0,...,b3, with * the lowest order byte, b0, first and the the highest order byte, * b7, last. * NOTE: This method reverses the action of setDouble_64. * * @param i The index of the first byte in the buffer * * @return The double value represented by eight successive bytes from * the file. */ public static double getDouble_64(byte[] buffer, int i) { long long_val = 0; for (int shift = 0; shift < 64; shift += 8) long_val |= ((long) buffer[i++] & 0xFF) << shift; return Double.longBitsToDouble(long_val); } }