Ignore:
Timestamp:
Aug 5, 2016 3: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/c_ext
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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.