Changeset 6eaf89ea in sasview


Ignore:
Timestamp:
Aug 5, 2016 5:49:33 AM (8 years ago)
Author:
lewis
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
Message:

Implement reading BSL/OTOKO header file

Location:
src/sas/sascalc/file_converter
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/file_converter/bsl_loader.py

    re5308b6 r6eaf89ea  
    11from sas.sascalc.file_converter.core.bsl_loader import CLoader 
     2from copy import deepcopy 
     3import os 
    24import numpy as np 
    35 
     
    68    # TODO: Change to __init__(self, filename, frame) 
    79    # 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 = "" 
    1015 
    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']) 
    1260 
    1361    def __setattr__(self, name, value): 
     
    2068        elif name == 'n_rasters': 
    2169            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) 
    2373 
    2474    def __getattr__(self, name): 
     
    3181        elif name == 'n_rasters': 
    3282            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) 
    3486 
    3587    def create_arr(self): 
  • src/sas/sascalc/file_converter/c_ext/bsl_loader.c

    r28e2b3a r6eaf89ea  
    2424    const int n_pixels; 
    2525    const int n_rasters; 
     26    const int swap_bytes; 
    2627    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)) 
    2829            Py_RETURN_NONE; 
    2930        if (!(self->params.filename = malloc(strlen(filename) + 1))) 
     
    3334        self->params.n_pixels = n_pixels; 
    3435        self->params.n_rasters = n_rasters; 
     36        self->params.swap_bytes = swap_bytes; 
    3537    } 
    3638 
     
    4648    char str[100]; 
    4749    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", 
    4951        self->params.filename, 
    5052        self->params.frame, 
    5153        self->params.n_pixels, 
    52         self->params.n_rasters); 
     54        self->params.n_rasters, 
     55        self->params.swap_bytes); 
    5356    return Py_BuildValue("s", str); 
    5457} 
     
    104107 
    105108    return Py_BuildValue("i", self->params.n_rasters); 
     109} 
     110 
     111static PyObject *get_swap_bytes(CLoader *self, PyObject *args) { 
     112    return Py_BuildValue("i", self->params.swap_bytes); 
     113} 
     114 
     115static 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 
     124float 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; 
    106135} 
    107136 
     
    138167        for (pixel = 0; pixel < self->params.n_pixels; pixel++) { 
    139168            fread(&cur_val, sizeof(float), 1, input_file); 
     169            if (self->params.swap_bytes == 0) 
     170                cur_val = reverse_float(cur_val); 
    140171            PyArray_SETITEM(data, PyArray_GETPTR2(data, raster, pixel), PyFloat_FromDouble(cur_val)); 
    141172            read_val = PyArray_GETITEM(data, PyArray_GETPTR2(data, raster, pixel)); 
     
    161192    { "get_n_rasters", (PyCFunction)get_n_rasters, METH_VARARGS, "Get n_rasters" }, 
    162193    { "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" }, 
    163196    { "load_data", (PyCFunction)load_data, METH_VARARGS, "Load the data into a numpy array" }, 
    164197    {NULL} 
  • src/sas/sascalc/file_converter/c_ext/bsl_loader.h

    r18e7309 r6eaf89ea  
    1111    // Number of rasters in the file 
    1212    int n_rasters; 
     13    // Whether or not the bytes are in reverse order 
     14    int swap_bytes; 
    1315} CLoader_params; 
    1416 
Note: See TracChangeset for help on using the changeset viewer.