Skip to content

Commit

Permalink
Merge pull request #14 from coprigent/feature/readDataType
Browse files Browse the repository at this point in the history
Data type reading in .npy files
  • Loading branch information
Algiane authored Feb 29, 2024
2 parents f3834fd + 71f0b95 commit c4e63cb
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/inout.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
#include <string.h>
#include <math.h>

#define H2T_HEX_LINE_FEED 0x0a
#define H2T_HEX_APOS 0x27
#define H2T_HEX_LEFTPAR 0x28
#define H2T_HEX_RIGHTPAR 0x29
#define H2T_HEX_COMMA 0x2c
#define H2T_HEX_ZERO 0x30
#define H2T_HEX_NINE 0x39

static inline
MMG5_int H2T_npy_point_index(int i,int j,int k, int *n) {
return (n[1]+1)*(n[2]+1)*i + (n[2]+1)*j + k + 1;
Expand All @@ -23,8 +31,9 @@ MMG5_int H2T_npy_point_index(int i,int j,int k, int *n) {
int H2T_loadNpy(MMG5_pMesh mmgMesh, int** tabhex, char* filename) {

FILE* inm;
unsigned char buffer = 0x00;
char buffer = 0;
char* str = NULL;
size_t dataSize = 0;
int pos1, pos2, dim = 0, t[3];
MMG5_int nhex, np, ne, i, j, k, ref, pos;

Expand All @@ -37,18 +46,18 @@ int H2T_loadNpy(MMG5_pMesh mmgMesh, int** tabhex, char* filename) {
fprintf(stdout," %%%% %s OPENED\n",mmgMesh->namein);

/* Reach beginning of dimension specification in header */
while (!(buffer == 0x28)) {
while (!(buffer == H2T_HEX_LEFTPAR)) {
fread(&buffer,sizeof(buffer),1,inm);
}

/* Read array sizes */
while (!(buffer == 0x29)) {
while (!(buffer == H2T_HEX_RIGHTPAR)) {

pos1 = ftell(inm);
do {
fread(&buffer,sizeof(buffer),1,inm);
} while (!(buffer == 0x2c || buffer == 0x29));
} while (!(buffer == H2T_HEX_COMMA || buffer == H2T_HEX_RIGHTPAR));

pos2 = ftell(inm);

H2T_SAFE_CALLOC(str,pos2-pos1-1,char,return 0);
Expand All @@ -66,8 +75,29 @@ int H2T_loadNpy(MMG5_pMesh mmgMesh, int** tabhex, char* filename) {
return -1;
}

/* Read data type : look for key 'descr' in header dictionnary */
fseek(inm,0,0);
H2T_SAFE_CALLOC(str,5,char,return 0);

while (dataSize == 0) {
while (!(buffer == H2T_HEX_APOS)) {
fread(&buffer,sizeof(buffer),1,inm);
}

fread(&str[i],sizeof(char),5,inm);
if (!strcmp(str,"descr")) {
/* read header until type-specifying integers are met */
while (!((buffer >= H2T_HEX_ZERO) && (buffer <= H2T_HEX_NINE))) {
fread(&buffer,sizeof(buffer),1,inm);
}
/* read data size */
sscanf(&buffer, "%lu", &dataSize);
H2T_SAFE_FREE(str);
}
}

/* Reach end of header */
while (!(buffer == 0x0a)) {
while (!(buffer == H2T_HEX_LINE_FEED)) {
fread(&buffer,sizeof buffer,1,inm);
}

Expand Down Expand Up @@ -114,7 +144,7 @@ int H2T_loadNpy(MMG5_pMesh mmgMesh, int** tabhex, char* filename) {
(*tabhex)[iadr+7] = H2T_npy_point_index(i ,j+1,k+1,t);

/* Hexa references */
fread(&(*tabhex)[iadr+8],sizeof(uint32_t),1,inm);
fread(&(*tabhex)[iadr+8],dataSize,1,inm);
++pos;
}
}
Expand Down

0 comments on commit c4e63cb

Please sign in to comment.