Changeset 6eaf89ea in sasview for src/sas/sascalc/file_converter
- Timestamp:
- Aug 5, 2016 5:49:33 AM (8 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- c3f0114
- Parents:
- e5308b6
- Location:
- src/sas/sascalc/file_converter
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/file_converter/bsl_loader.py
re5308b6 r6eaf89ea 1 1 from sas.sascalc.file_converter.core.bsl_loader import CLoader 2 from copy import deepcopy 3 import os 2 4 import numpy as np 3 5 … … 6 8 # TODO: Change to __init__(self, filename, frame) 7 9 # and parse n_(pixels/rasters) from header file 8 def __init__(self, filename, frame, n_pixels, n_rasters): 9 CLoader.__init__(self, filename, frame, n_pixels, n_rasters) 10 def __init__(self, filename, frame): 11 header_file = open(filename, 'r') 12 data_info = {} 13 is_valid = True 14 err_msg = "" 10 15 11 # See invertor.py for implementation of pickling and setters/getters 16 [folder, filename] = os.path.split(filename) 17 18 # First 2 lines are headers 19 header_file.readline() 20 header_file.readline() 21 22 while True: 23 import pdb; pdb.set_trace() 24 metadata = header_file.readline().strip() 25 metadata = metadata.split() 26 data_filename = header_file.readline().strip() 27 28 if len(metadata) != 10: 29 is_valid = False 30 err_msg = "Invalid header file: {}".format(filename) 31 break 32 # SAS data will be in file Xnn001.mdd 33 if data_filename != filename.replace('0.', '1.'): 34 continue 35 try: 36 data_info = { 37 'filename': os.path.join(folder, data_filename), 38 'pixels': int(metadata[0]), 39 'rasters': int(metadata[1]), 40 'frames': int(metadata[2]), 41 'swap_bytes': int(metadata[3]) 42 } 43 except: 44 is_valid = False 45 err_msg = "Invalid metadata in header file for {}".format(filename.replace('0.', '1.')) 46 break 47 48 if not is_valid: 49 raise Exception(err_msg) 50 51 if data_info['frames'] == 1: 52 # File is actually in OTOKO (1D) format 53 # Number of frames is 2nd indicator, 54 data_info['frames'] = data_info['rasters'] 55 data_info['rasters'] = data_info['pixels'] 56 data_info['pixels'] = 1 57 58 CLoader.__init__(self, data_info['filename'], frame, 59 data_info['pixels'], data_info['rasters'], data_info['swap_bytes']) 12 60 13 61 def __setattr__(self, name, value): … … 20 68 elif name == 'n_rasters': 21 69 return self.set_n_rasters(value) 22 return CBSLLoader.__set_attr__(self, name, value) 70 elif name == 'swap_bytes': 71 return self.set_swap_bytes(value) 72 return CLoader.__setattr__(self, name, value) 23 73 24 74 def __getattr__(self, name): … … 31 81 elif name == 'n_rasters': 32 82 return self.get_n_rasters() 33 return CBSLLoader.__getattr__(self, name) 83 elif name == 'swap_bytes': 84 return self.get_swap_bytes() 85 return CLoader.__getattr__(self, name) 34 86 35 87 def create_arr(self): -
src/sas/sascalc/file_converter/c_ext/bsl_loader.c
r28e2b3a r6eaf89ea 24 24 const int n_pixels; 25 25 const int n_rasters; 26 const int swap_bytes; 26 27 if (self != NULL) { 27 if (!PyArg_ParseTuple(args, "siii ", &filename, &frame, &n_pixels, &n_rasters))28 if (!PyArg_ParseTuple(args, "siiii", &filename, &frame, &n_pixels, &n_rasters, &swap_bytes)) 28 29 Py_RETURN_NONE; 29 30 if (!(self->params.filename = malloc(strlen(filename) + 1))) … … 33 34 self->params.n_pixels = n_pixels; 34 35 self->params.n_rasters = n_rasters; 36 self->params.swap_bytes = swap_bytes; 35 37 } 36 38 … … 46 48 char str[100]; 47 49 sprintf(str, 48 "Filename: %s\nframe: %d\nn_pixels: %d\nn_rasters: %d\n ",50 "Filename: %s\nframe: %d\nn_pixels: %d\nn_rasters: %d\nswap_bytes: %d", 49 51 self->params.filename, 50 52 self->params.frame, 51 53 self->params.n_pixels, 52 self->params.n_rasters); 54 self->params.n_rasters, 55 self->params.swap_bytes); 53 56 return Py_BuildValue("s", str); 54 57 } … … 104 107 105 108 return Py_BuildValue("i", self->params.n_rasters); 109 } 110 111 static PyObject *get_swap_bytes(CLoader *self, PyObject *args) { 112 return Py_BuildValue("i", self->params.swap_bytes); 113 } 114 115 static PyObject *set_swap_bytes(CLoader *self, PyObject *args) { 116 int new_swap; 117 if (!PyArg_ParseTuple(args, "i", &new_swap)) 118 return NULL; 119 self->params.swap_bytes = new_swap; 120 121 return Py_BuildValue("i", self->params.swap_bytes); 122 } 123 124 float reverse_float(const float in_float){ 125 float retval; 126 char *to_convert = (char *)&in_float; 127 char *return_float = (char *)&retval; 128 129 return_float[0] = to_convert[3]; 130 return_float[1] = to_convert[2]; 131 return_float[2] = to_convert[1]; 132 return_float[3] = to_convert[0]; 133 134 return retval; 106 135 } 107 136 … … 138 167 for (pixel = 0; pixel < self->params.n_pixels; pixel++) { 139 168 fread(&cur_val, sizeof(float), 1, input_file); 169 if (self->params.swap_bytes == 0) 170 cur_val = reverse_float(cur_val); 140 171 PyArray_SETITEM(data, PyArray_GETPTR2(data, raster, pixel), PyFloat_FromDouble(cur_val)); 141 172 read_val = PyArray_GETITEM(data, PyArray_GETPTR2(data, raster, pixel)); … … 161 192 { "get_n_rasters", (PyCFunction)get_n_rasters, METH_VARARGS, "Get n_rasters" }, 162 193 { "set_n_rasters", (PyCFunction)set_n_rasters, METH_VARARGS, "Set n_rasters" }, 194 { "get_swap_bytes", (PyCFunction)get_swap_bytes, METH_VARARGS, "Get swap_bytes" }, 195 { "set_swap_bytes", (PyCFunction)set_swap_bytes, METH_VARARGS, "Set swap_bytes" }, 163 196 { "load_data", (PyCFunction)load_data, METH_VARARGS, "Load the data into a numpy array" }, 164 197 {NULL} -
src/sas/sascalc/file_converter/c_ext/bsl_loader.h
r18e7309 r6eaf89ea 11 11 // Number of rasters in the file 12 12 int n_rasters; 13 // Whether or not the bytes are in reverse order 14 int swap_bytes; 13 15 } CLoader_params; 14 16
Note: See TracChangeset
for help on using the changeset viewer.