npp/02.read_uint16_ASCII.c.x
changeset 0 0c6405ab2ff4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/npp/02.read_uint16_ASCII.c.x	Mon Jan 26 22:08:20 2009 -0500
     1.3 @@ -0,0 +1,90 @@
     1.4 +/* purpose: conversion of binary data with little-endian to ASCII
     1.5 +   Sep. 8, 2006 by Maosheng Zhao */
     1.6 +
     1.7 +/* I also accounts for the possible different CPU on different platforms,
     1.8 +   and therefore, it should work on different machines without problem */
     1.9 +
    1.10 +#include<stdio.h>
    1.11 +#define COLS 7200
    1.12 +#define ROWS 3600
    1.13 +#define DATA_TYPE unsigned short
    1.14 +
    1.15 +/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
    1.16 +#define IN_FILE_NAME  "Npp_0.05deg_mean.int16"
    1.17 +#define OUT_FILE_NAME "Npp_0.05deg_mean.ASCII2"
    1.18 +/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
    1.19 +int main()
    1.20 +{
    1.21 +    /* definiation of variables will be used */
    1.22 +    FILE *in_f, *out_f;
    1.23 +    static DATA_TYPE data_in[ROWS][COLS];
    1.24 +    int i,j;
    1.25 +
    1.26 +    /* the follow definition is for determination the type of CPU */
    1.27 +    int  is_big_endian=0;
    1.28 +    short one=1;
    1.29 +    char *cp;
    1.30 +
    1.31 +    /* define a buffer pointer to swap bytes if it is big-endian CPU */
    1.32 +    char temp_char[sizeof(DATA_TYPE)];
    1.33 +    DATA_TYPE *buffer_data; 
    1.34 +    int m;
    1.35 +
    1.36 +    /*################################################################*/
    1.37 +    /* main program starts */
    1.38 +    /* open the data file and read all the data */
    1.39 +    in_f = fopen(IN_FILE_NAME,"rb");
    1.40 +    if(in_f == NULL)
    1.41 +    {
    1.42 +	printf("cannot open %s, exit...\n",IN_FILE_NAME);
    1.43 +	exit(0);
    1.44 +    }
    1.45 +    else  /* reading the data */
    1.46 +    {
    1.47 +	/* tell the type of CPU */
    1.48 +	cp = (char*)&one;
    1.49 +	if(*cp == 0)
    1.50 +	   is_big_endian = 1;  /* yes, this is a big-endian CPU */
    1.51 +	else
    1.52 +	   is_big_endian = 0;  /* this is a little-endian CPU */
    1.53 +
    1.54 +	if(is_big_endian == 0)
    1.55 +	{
    1.56 +	   printf("This is a little-endian CPU, no need to swap bytes\n");
    1.57 +	   printf("reading data now, please waiting for...\n");
    1.58 +	   fread(data_in,sizeof(DATA_TYPE),COLS*ROWS,in_f);
    1.59 +	}
    1.60 +	else
    1.61 +	{
    1.62 +	   printf("This is a big-endian CPU, which needs to swap bytes\n");
    1.63 +	   printf("reading data now, please waiting for...\n");
    1.64 +	   for(i=0;i<ROWS;i++)
    1.65 +		for(j=0;j<COLS;j++)
    1.66 +		{
    1.67 +	   	   for(m=0;m<sizeof(DATA_TYPE);m++)
    1.68 +			fread(&temp_char[sizeof(DATA_TYPE)-1-m],1,1,in_f);
    1.69 +
    1.70 +		   /* swap the byte order */
    1.71 +		   buffer_data = (DATA_TYPE*)temp_char;
    1.72 +		   data_in[i][j] = *buffer_data;
    1.73 +		} /* end of nested loops for i and j */
    1.74 +	}
    1.75 +    }
    1.76 +
    1.77 +    /* close the input file */
    1.78 +    fclose(in_f);
    1.79 +
    1.80 +
    1.81 +    /* write the ASCII to a new file */
    1.82 +    out_f = fopen(OUT_FILE_NAME,"w");
    1.83 +    printf("Please waiting for writing data to %s\n",OUT_FILE_NAME);
    1.84 +    for(i=0;i<ROWS;i++)
    1.85 +	for(j=0;j<COLS;j++)
    1.86 +	    fprintf(out_f,"%6d\n",data_in[i][j]);
    1.87 +
    1.88 +    fclose(out_f);
    1.89 +
    1.90 +    printf("Finished conversion of binary to ASCII\n");
    1.91 +
    1.92 +    return(0);
    1.93 +}