Skip to content

Commit

Permalink
Add working C class to load 2D BSL file
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisodriscoll committed Jul 25, 2016
1 parent b598e39 commit 940baf0
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
multi_data/*
single_data/*
*.pyc
*.dll
*.lib
*.obj
*.exe
*.exp
55 changes: 55 additions & 0 deletions c_ext/bsl_loader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "bsl_loader.h"
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>

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;
}
5 changes: 5 additions & 0 deletions c_ext/bsl_loader.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
LIBRARY bsl_loader
EXPORTS
loader_dealloc
loader_init
load_frame
21 changes: 21 additions & 0 deletions c_ext/bsl_loader.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions c_ext/compile.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cl /LD bsl_loader.c /link /DEF:bsl_loader.def
cl try_loader.c /link bsl_loader.lib
20 changes: 20 additions & 0 deletions c_ext/try_loader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "bsl_loader.h"
#include <string.h>

int main() {
Loader_params params;
params.pixels = 1475;
params.rasters = 1679;
params.frame = 5;
strcpy(params.filename, "../multi_data/B12001.629");

loader_init(&params);

load_frame(&params);

printf("data[766][1358] = %f\n", params.data[766][1358]);

loader_dealloc(&params);

return 1;
}
57 changes: 57 additions & 0 deletions load.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdio.h>

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;
}
12 changes: 12 additions & 0 deletions load_export.py
Original file line number Diff line number Diff line change
@@ -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()
48 changes: 48 additions & 0 deletions load_frame.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>

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;
}

0 comments on commit 940baf0

Please sign in to comment.