From 940baf04d71fe5fdc76cd8c204fe6123cd4f4253 Mon Sep 17 00:00:00 2001 From: Lewis O'Driscoll Date: Mon, 25 Jul 2016 12:10:42 +0100 Subject: [PATCH] Add working C class to load 2D BSL file --- .gitignore | 5 ++++ c_ext/bsl_loader.c | 55 ++++++++++++++++++++++++++++++++++++++++++ c_ext/bsl_loader.def | 5 ++++ c_ext/bsl_loader.h | 21 ++++++++++++++++ c_ext/compile.bat | 2 ++ c_ext/try_loader.c | 20 ++++++++++++++++ load.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ load_export.py | 12 ++++++++++ load_frame.c | 48 +++++++++++++++++++++++++++++++++++++ 9 files changed, 225 insertions(+) create mode 100644 c_ext/bsl_loader.c create mode 100644 c_ext/bsl_loader.def create mode 100644 c_ext/bsl_loader.h create mode 100644 c_ext/compile.bat create mode 100644 c_ext/try_loader.c create mode 100644 load.c create mode 100644 load_export.py create mode 100644 load_frame.c diff --git a/.gitignore b/.gitignore index 5186e9d..fceb99c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ multi_data/* single_data/* *.pyc +*.dll +*.lib +*.obj +*.exe +*.exp diff --git a/c_ext/bsl_loader.c b/c_ext/bsl_loader.c new file mode 100644 index 0000000..85ea125 --- /dev/null +++ b/c_ext/bsl_loader.c @@ -0,0 +1,55 @@ +#include "bsl_loader.h" +#include +#include +#include + +void loader_dealloc(Loader_params *params) { + int i; + + for (i = 0; i < params->rasters; i++) { + free(params->data[i]); + } + free(params->data); +} + +void loader_init(Loader_params *params) { + int i; + + params->data = malloc(sizeof(float *) * params->rasters); + for (i = 0; i < params->rasters; i++) { + params->data[i] = malloc(sizeof(float) * params->pixels); + } +} + +void load_frame(Loader_params *params) { + struct record { + float x; + }; + + int frame; + int raster; + int pixel; + int frame_pos; + FILE *input_file; + struct record my_record; + + input_file = fopen(params->filename, "rb"); + + if (!input_file) { + printf("Unable to open file: %s\n", params->filename); + return; + } + + frame_pos = params->rasters * params->pixels * params->frame; + fseek(input_file, frame_pos*sizeof(struct record), SEEK_SET); + for (raster = 0; raster < params->rasters; raster++) { + for (pixel = 0; pixel < params->pixels; pixel++) { + fread(&my_record, sizeof(struct record), 1, input_file); + params->data[raster][pixel] = my_record.x; + } + } + + fclose(input_file); + + return; +} diff --git a/c_ext/bsl_loader.def b/c_ext/bsl_loader.def new file mode 100644 index 0000000..4f6d8e3 --- /dev/null +++ b/c_ext/bsl_loader.def @@ -0,0 +1,5 @@ +LIBRARY bsl_loader +EXPORTS + loader_dealloc + loader_init + load_frame diff --git a/c_ext/bsl_loader.h b/c_ext/bsl_loader.h new file mode 100644 index 0000000..8dd2e1c --- /dev/null +++ b/c_ext/bsl_loader.h @@ -0,0 +1,21 @@ +#if !defined(bsl_loader) +#define bsl_loader + +typedef struct Loader_params { + // File to load + char filename[50]; + // Frame to load + int frame; + // Number of pixels in the file + int pixels; + // Number of rasters in the file + int rasters; + // Data to be returned; + float **data; +} Loader_params; + +void loader_dealloc(Loader_params *params); +void loader_init(Loader_params *params); +void load_frame(Loader_params *params); + +#endif diff --git a/c_ext/compile.bat b/c_ext/compile.bat new file mode 100644 index 0000000..7acd7e4 --- /dev/null +++ b/c_ext/compile.bat @@ -0,0 +1,2 @@ +cl /LD bsl_loader.c /link /DEF:bsl_loader.def +cl try_loader.c /link bsl_loader.lib diff --git a/c_ext/try_loader.c b/c_ext/try_loader.c new file mode 100644 index 0000000..41a3bef --- /dev/null +++ b/c_ext/try_loader.c @@ -0,0 +1,20 @@ +#include "bsl_loader.h" +#include + +int main() { + Loader_params params; + params.pixels = 1475; + params.rasters = 1679; + params.frame = 5; + strcpy(params.filename, "../multi_data/B12001.629"); + + loader_init(¶ms); + + load_frame(¶ms); + + printf("data[766][1358] = %f\n", params.data[766][1358]); + + loader_dealloc(¶ms); + + return 1; +} diff --git a/load.c b/load.c new file mode 100644 index 0000000..fc2bc5e --- /dev/null +++ b/load.c @@ -0,0 +1,57 @@ +#include + +struct rec { + float x; +}; + +const int n_frames = 41; +const long n_pixels = 1475; +const long n_rasters = 1679; + +float frames[1475*1679*41]; + +int offset(int x, int y, int z) { + return (z * n_pixels * n_rasters) + (y * n_pixels) + x; +} + +int main() { + // float frames[1679][1475][41]; + + int frame; + int raster; + int pixel; + FILE *my_file; + struct rec my_record; + char filename[50]; + FILE *save_file; + + my_file = fopen("multi_data/B12001.629", "rb"); + + if (!my_file) { + printf("Unable to open input file"); + return 1; + } + + for (frame = 0; frame < n_frames; frame++) { + sprintf(filename, "multi_data/export/frame%d.txt", frame); + save_file = fopen(filename, "w"); + if (!save_file) { + printf("Unable to open save file: %s", filename); + return 1; + } + for (raster = 0; raster < n_rasters; raster++) { + for (pixel = 0; pixel < n_pixels; pixel++) { + fread(&my_record, sizeof(struct rec), 1, my_file); + frames[offset(raster, pixel, frame)] = my_record.x; + fprintf(save_file, "%.2e ", my_record.x); + } + fprintf(save_file, "\n"); + } + printf("%s\n", filename); + fclose(save_file); + } + + fclose(my_file); + + return 0; +} diff --git a/load_export.py b/load_export.py new file mode 100644 index 0000000..0885ee0 --- /dev/null +++ b/load_export.py @@ -0,0 +1,12 @@ +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.image as mpimg +import sys + +frame = int(sys.argv[1]) + +data = np.loadtxt("multi_data/export/frame{}.txt".format(frame), dtype=np.float32) +plt.imshow(data) +# plt.xlim([700,850]) +# plt.ylim([1275,1440]) +plt.show() diff --git a/load_frame.c b/load_frame.c new file mode 100644 index 0000000..6edc02f --- /dev/null +++ b/load_frame.c @@ -0,0 +1,48 @@ +#include + +struct rec { + float x; +}; + +const long n_pixels = 1475; +const long n_rasters = 1679; + +float data[1679][1475]; + +int main() { + int frame = 0; + int raster; + int pixel; + FILE *my_file; + struct rec my_record; + char filename[50]; + FILE *save_file; + + my_file = fopen("multi_data/B12001.629", "rb"); + + if (!my_file) { + printf("Unable to open input file"); + return 1; + } + + sprintf(filename, "multi_data/export/frame%d.txt", frame); + save_file = fopen(filename, "w"); + if (!save_file) { + printf("Unable to open save file: %s", filename); + return 1; + } + for (raster = 0; raster < n_rasters; raster++) { + for (pixel = 0; pixel < n_pixels; pixel++) { + fread(&my_record, sizeof(struct rec), 1, my_file); + data[raster][pixel] = my_record.x; + fprintf(save_file, "%.2e ", my_record.x); + } + fprintf(save_file, "\n"); + } + printf("%s\n", filename); + fclose(save_file); + + fclose(my_file); + + return 0; +}