/*************************************************************************
  Program:     readlonlat.c
  --------
  Description:
  -----------
  Read longitude and latitude from a file in a given line.

 ***************************************************************************
  Written for application to China by:  G. Mo       
  Last update: Sept. 2008
*****************************************************************************/
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include "beps.h"

void readlonlat(line,lon,lat)
int long line;		/* Line number */
double *lon,*lat; 
{
	FILE *ptr_lon,*ptr_lat; 
	float *lon0,*lat0;
	int ptr, size, nitems, pix;
	int bytes=4;		/* Number of bytes for a unsigned int data type */

	lon0=(float*)malloc(npixels*sizeof(float));
	lat0=(float*)malloc(npixels*sizeof(float));
/*	Open files */
	if ((ptr_lon= fopen(lonf,"rb")) ==NULL) {
          printf("\n Unable to open file<%s>,  exitting program ...\n\n",
							lonf);
		exit(0);
	}
	if ((ptr_lat= fopen(latf,"rb")) ==NULL) {
          printf("\n Unable to open file<%s>,  exitting program ...\n\n",
							latf);
		exit(0);
     }

/*      Set pointer to start of line data */
        ptr=bytes*(line*npixels);

/*      Seek to and Read data from delta row and col files */
		fseek(ptr_lon,ptr,0);     
        fread(&lon0[0],size=bytes,nitems=npixels,ptr_lon);

		fseek(ptr_lat,ptr,0);     
        fread(&lat0[0],size=bytes,nitems=npixels,ptr_lat);

		for (pix=0;pix<npixels;pix++)  { 
			lon[pix]=(double)lon0[pix];
			lat[pix]=(double)lat0[pix];
		}

/*	    Close input file and memory */
        fclose(ptr_lon);
        fclose(ptr_lat);

		return;
}
