npp/02.read_uint16_ASCII.c.x
author Forrest Hoffman <forrest@climatemodeling.org>
Thu, 26 Mar 2009 14:02:21 -0400
changeset 1 4be95183fbcd
permissions -rw-r--r--
Modifications to scoring and graphics production for the final version of code for the C-LAMP paper in GCB.
forrest@0
     1
/* purpose: conversion of binary data with little-endian to ASCII
forrest@0
     2
   Sep. 8, 2006 by Maosheng Zhao */
forrest@0
     3
forrest@0
     4
/* I also accounts for the possible different CPU on different platforms,
forrest@0
     5
   and therefore, it should work on different machines without problem */
forrest@0
     6
forrest@0
     7
#include<stdio.h>
forrest@0
     8
#define COLS 7200
forrest@0
     9
#define ROWS 3600
forrest@0
    10
#define DATA_TYPE unsigned short
forrest@0
    11
forrest@0
    12
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
forrest@0
    13
#define IN_FILE_NAME  "Npp_0.05deg_mean.int16"
forrest@0
    14
#define OUT_FILE_NAME "Npp_0.05deg_mean.ASCII2"
forrest@0
    15
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
forrest@0
    16
int main()
forrest@0
    17
{
forrest@0
    18
    /* definiation of variables will be used */
forrest@0
    19
    FILE *in_f, *out_f;
forrest@0
    20
    static DATA_TYPE data_in[ROWS][COLS];
forrest@0
    21
    int i,j;
forrest@0
    22
forrest@0
    23
    /* the follow definition is for determination the type of CPU */
forrest@0
    24
    int  is_big_endian=0;
forrest@0
    25
    short one=1;
forrest@0
    26
    char *cp;
forrest@0
    27
forrest@0
    28
    /* define a buffer pointer to swap bytes if it is big-endian CPU */
forrest@0
    29
    char temp_char[sizeof(DATA_TYPE)];
forrest@0
    30
    DATA_TYPE *buffer_data; 
forrest@0
    31
    int m;
forrest@0
    32
forrest@0
    33
    /*################################################################*/
forrest@0
    34
    /* main program starts */
forrest@0
    35
    /* open the data file and read all the data */
forrest@0
    36
    in_f = fopen(IN_FILE_NAME,"rb");
forrest@0
    37
    if(in_f == NULL)
forrest@0
    38
    {
forrest@0
    39
	printf("cannot open %s, exit...\n",IN_FILE_NAME);
forrest@0
    40
	exit(0);
forrest@0
    41
    }
forrest@0
    42
    else  /* reading the data */
forrest@0
    43
    {
forrest@0
    44
	/* tell the type of CPU */
forrest@0
    45
	cp = (char*)&one;
forrest@0
    46
	if(*cp == 0)
forrest@0
    47
	   is_big_endian = 1;  /* yes, this is a big-endian CPU */
forrest@0
    48
	else
forrest@0
    49
	   is_big_endian = 0;  /* this is a little-endian CPU */
forrest@0
    50
forrest@0
    51
	if(is_big_endian == 0)
forrest@0
    52
	{
forrest@0
    53
	   printf("This is a little-endian CPU, no need to swap bytes\n");
forrest@0
    54
	   printf("reading data now, please waiting for...\n");
forrest@0
    55
	   fread(data_in,sizeof(DATA_TYPE),COLS*ROWS,in_f);
forrest@0
    56
	}
forrest@0
    57
	else
forrest@0
    58
	{
forrest@0
    59
	   printf("This is a big-endian CPU, which needs to swap bytes\n");
forrest@0
    60
	   printf("reading data now, please waiting for...\n");
forrest@0
    61
	   for(i=0;i<ROWS;i++)
forrest@0
    62
		for(j=0;j<COLS;j++)
forrest@0
    63
		{
forrest@0
    64
	   	   for(m=0;m<sizeof(DATA_TYPE);m++)
forrest@0
    65
			fread(&temp_char[sizeof(DATA_TYPE)-1-m],1,1,in_f);
forrest@0
    66
forrest@0
    67
		   /* swap the byte order */
forrest@0
    68
		   buffer_data = (DATA_TYPE*)temp_char;
forrest@0
    69
		   data_in[i][j] = *buffer_data;
forrest@0
    70
		} /* end of nested loops for i and j */
forrest@0
    71
	}
forrest@0
    72
    }
forrest@0
    73
forrest@0
    74
    /* close the input file */
forrest@0
    75
    fclose(in_f);
forrest@0
    76
forrest@0
    77
forrest@0
    78
    /* write the ASCII to a new file */
forrest@0
    79
    out_f = fopen(OUT_FILE_NAME,"w");
forrest@0
    80
    printf("Please waiting for writing data to %s\n",OUT_FILE_NAME);
forrest@0
    81
    for(i=0;i<ROWS;i++)
forrest@0
    82
	for(j=0;j<COLS;j++)
forrest@0
    83
	    fprintf(out_f,"%6d\n",data_in[i][j]);
forrest@0
    84
forrest@0
    85
    fclose(out_f);
forrest@0
    86
forrest@0
    87
    printf("Finished conversion of binary to ASCII\n");
forrest@0
    88
forrest@0
    89
    return(0);
forrest@0
    90
}