forrest@0: /* purpose: conversion of binary data with little-endian to ASCII forrest@0: Sep. 8, 2006 by Maosheng Zhao */ forrest@0: forrest@0: /* I also accounts for the possible different CPU on different platforms, forrest@0: and therefore, it should work on different machines without problem */ forrest@0: forrest@0: #include forrest@0: #define COLS 7200 forrest@0: #define ROWS 3600 forrest@0: #define DATA_TYPE unsigned short forrest@0: forrest@0: /*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/ forrest@0: #define IN_FILE_NAME "Npp_0.05deg_mean.int16" forrest@0: #define OUT_FILE_NAME "Npp_0.05deg_mean.ASCII2" forrest@0: /*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/ forrest@0: int main() forrest@0: { forrest@0: /* definiation of variables will be used */ forrest@0: FILE *in_f, *out_f; forrest@0: static DATA_TYPE data_in[ROWS][COLS]; forrest@0: int i,j; forrest@0: forrest@0: /* the follow definition is for determination the type of CPU */ forrest@0: int is_big_endian=0; forrest@0: short one=1; forrest@0: char *cp; forrest@0: forrest@0: /* define a buffer pointer to swap bytes if it is big-endian CPU */ forrest@0: char temp_char[sizeof(DATA_TYPE)]; forrest@0: DATA_TYPE *buffer_data; forrest@0: int m; forrest@0: forrest@0: /*################################################################*/ forrest@0: /* main program starts */ forrest@0: /* open the data file and read all the data */ forrest@0: in_f = fopen(IN_FILE_NAME,"rb"); forrest@0: if(in_f == NULL) forrest@0: { forrest@0: printf("cannot open %s, exit...\n",IN_FILE_NAME); forrest@0: exit(0); forrest@0: } forrest@0: else /* reading the data */ forrest@0: { forrest@0: /* tell the type of CPU */ forrest@0: cp = (char*)&one; forrest@0: if(*cp == 0) forrest@0: is_big_endian = 1; /* yes, this is a big-endian CPU */ forrest@0: else forrest@0: is_big_endian = 0; /* this is a little-endian CPU */ forrest@0: forrest@0: if(is_big_endian == 0) forrest@0: { forrest@0: printf("This is a little-endian CPU, no need to swap bytes\n"); forrest@0: printf("reading data now, please waiting for...\n"); forrest@0: fread(data_in,sizeof(DATA_TYPE),COLS*ROWS,in_f); forrest@0: } forrest@0: else forrest@0: { forrest@0: printf("This is a big-endian CPU, which needs to swap bytes\n"); forrest@0: printf("reading data now, please waiting for...\n"); forrest@0: for(i=0;i