/* #include <stdio.h> */
/* #include <string.h> */

#include <malloc.h>
#include <sys/stat.h>

#ifndef FILE
#include <stdio.h>
#include <string.h>
#ifdef HPUX
#include <sys/types.h>
#else
#include <sys/stdtypes.h>
#endif
#include <unistd.h>
#include <time.h>
#endif

/*---------------------------------------------------------------------------
   @(#)fp0.h version 2.1.1.1  4/10/91
   @(#)cms0.h $Id$

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

#ifndef _fp0_h
#define _fp0_h

/*------------------------------------------------------------------------GL-*/
/* parametrisation syntaxique                                                */
/*---------------------------------------------------------------------------*/
#define BEGIN {
/* #define IDENT(m) static char SccsId1[]="@(#)fp/c version 2.1.1.1 4/10/91 GL CMS>@(#) (m)" */
#define END }
#define SWITCH(x) switch(x) {
#define END_SWITCH }
#define FOR(x) for(x) {
#define END_FOR }
#define IF(x) if(x) {
#define ELSE } else {
#define END_IF }

/*------------------------------------------------------------------------GL-*/
/* parametrisation des donnees internes                                      */
/*---------------------------------------------------------------------------*/
#define CHAR0       '\0'
#define TRUE 1
#define FALSE !TRUE
#define BASE_DJ 2433283 /* 1er janvier 1950 */

/*------------------------------------------------------------------------GL-*/
/* parametrisation implicite des champs DE                                   */
/*---------------------------------------------------------------------------*/
#define FP_AUC "METEO FRANCE"
#define FP_SER "SCEM/CMS"
#define FP_TIT "SANS TITRE"
#define FP_AUM "METEO FRANCE"
#define FP_NVE "FP C 2.1.1.1 GL"
#define FP_CSC "NSWE"

/*------------------------------------------------------------------------GL-*/
/* parametrisation du standard FIS                                           */
/*---------------------------------------------------------------------------*/

#define FP_NOE 512  /* nombre d'octets zone DE */
#define FP_NOD 512  /* nombre d'octets zone DD */
#define FP_NDA  21  /* nombre de DA */

/*------------------------------------------------------------------------GL-*/
/* definition compte-rendus                                                  */
/*---------------------------------------------------------------------------*/
#define FP_FOK    1 /* function ok */
#define FP_OLD   -1 /* open "old" */
#define FP_NEW   -2 /* open "new" */
#define FP_ORG   -3 /* organisation incorrecte */
#define FP_TYP   -4 /* type incorrect */
#define FP_ALL   -5 /* allocation memoire impossible */
#define FP_RDE   -6
#define FP_RDD   -7
#define FP_RDI   -8
#define FP_WDE   -9
#define FP_WDD  -10
#define FP_WDI  -11
#define FP_MOD  -12
#define FP_DIM  -13
#define FP_CDE  -14
#define FP_CDD  -15
#define FP_DDE  -16
#define FP_DDD  -17
#define FP_SDE  -18
#define FP_SDD  -19
#define FP_SDI  -20
#define FP_WRO  -21
#define FP_INT  -22


/*------------------------------------------------------------------------GL-*/
/* macro-definitions de fonctions                                            */
/*---------------------------------------------------------------------------*/
#define MIN(x,y) (x<y ? x : y)
#define MAX(x,y) (x>y ? x : y)
#define NINT(x)  (int)((x) + (x) - (int)(x))
#define ISE(x) ((int)((x) + (int)((x) != (float)((int)(x)))))
#define RDE(f,d) if((crd=fp0_rde(f,d))!=FP_FOK) return(crd)
#define WDE(f,d) if((crd=fp0_wde(f,d))!=FP_FOK) return(crd)
#define RDD(f,d) if((crd=fp0_rdd(f,d))!=FP_FOK) return(crd)
#define WDD(f,d) if((crd=fp0_wdd(f,d))!=FP_FOK) return(crd)
#define RDI(f,a,u,v,w,n,b) if((crd=fp0_rdi(f,a,u,v,w,n,b))!=FP_FOK) return(crd)
#define WDI(f,a,u,v,w,n,b) if((crd=fp0_wdi(f,a,u,v,w,n,b))!=FP_FOK) return(crd)
#define CDD(b,d) if((crd=fp0_cdd(b,d))!=FP_FOK) return(crd)
#define CDE(b,d) if((crd=fp0_cde(b,d))!=FP_FOK) return(crd)
#define DDD(b,d) if((crd=fp0_ddd(b,d))!=FP_FOK) return(crd)
#define DDE(b,d) if((crd=fp0_dde(b,d))!=FP_FOK) return(crd)
#define FOPEN(p,f,m,c) if((p=fopen(f,m)) == NULL) return(c)
#define FREAD(b,s,n,p,c) if(fread(b,s,n,p)!=n) return(c)
#define FWRITE(b,s,n,p,c) if(fwrite(b,s,n,p)!=n) return(c)
#define MALLOC(p,t,n) if((p=(t)malloc((unsigned)(n)))==NULL) return(FP_ALL)
#define NRE(n) ((n)>0 ? ISE(FP_NOE/(float)(n)) : 0)
#define NRD(n) ((n)>0 ? ISE(FP_NOD/(float)(n)) : 0)
#define OFR(r,n,p) (long)((r-1) * ((n) + (p)))
#define CCS(s0,c0,ns,nc,a,b) a=(nc==1 ? 0. :((ns-1)/(float)(nc-1)));\
                             b=(float) s0 - a * (float) c0
#define CSC(s0,c0,ns,nc,a,b) a=(ns==1 ? 0. :((nc-1)/(float)(ns-1)));\
                             b=(float) c0 - a * (float) s0
#define FSEEK(p,l,o,c) if(fseek(p,l,o) !=0) return(c)
#define SDE(f) FSEEK(f->pf,(long)0,0,FP_SDE)
#define SDD(f) FSEEK(f->pf,OFR(NRE(f->de.nor) +1,f->de.nor,f->de.pad),0,FP_SDD)
#define CTRL(x,n,a,b) if((x<a) || ((x+n-1) > b )|| (n<1)) return(FP_DIM)
#define SPRINTF(b,f,p,c) if(sprintf(b,f,p,c) !=b) return(c)
#define SSCANF(b,f,p,c)  if(sscanf(b,f,p) ==0) return(c)

/*------------------------------------------------------------------------GL-*/
/* enum Ord                                                                  */
/*---------------------------------------------------------------------------*/
typedef enum {PLC,PCL,LPC,LCP,CPL,CLP,UNO} Ord;

/*------------------------------------------------------------------------GL-*/
/* enum Blo                                                                  */
/*---------------------------------------------------------------------------*/
typedef enum {U_VW,UV_W,UVW_,UNBL} Blo;

/*------------------------------------------------------------------------GL-*/
/* struct Atf                                                                */
/*---------------------------------------------------------------------------*/
typedef struct {
          int nop       ; /* nombre d'octets par pixels */
          Ord ord       ; /* classe d'ordonnancement (uvw) */
          Blo blo       ; /* classe de blocage */
          int nor       ; /* nombre d'octets par records = de.nor) */
          int pad       ; /* nombre d'octets de padding pour chaque record */
          int nre       ; /* nombre de records de la zone DE */
          int nrd       ; /* nombre de records de la zone DD */
          int mxu       ; /* dimension u */
          int mxv       ; /* dimension v */
          int mxw       ; /* dimension w */
        } Atf;
#endif

/*------------------------------------------------------------------------GL-*/
/* prototypes                                                                */
/*---------------------------------------------------------------------------*/
void fp0_atf();
Blo fp0_blo();
int fp0_cdd();
int fp0_cde();
void fp0_cpy();
int fp0_cus();
int fp0_ddd();
int fp0_dde();
int fp0_gdj();
void fp0_ide();
char *fp0_ncs();
int fp0_nop();
int fp0_nor();
int fp0_nri();
long fp0_ofi();
Ord fp0_ord();
void fp0_oso();
int fp0_rdd();
int fp0_rde();
int fp0_rdi();
int fp0_wdd();
int fp0_wde();
int fp0_wdi();

/*-END-cms0.h-------------------------------------------------------------GL-*/
/*---------------------------------------------------------------------------
   @(#)fp.h version 2.1.1.1  4/10/91
   @(#)cms.h $Id$

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

#ifndef _fp_h
#define _fp_h

/*------------------------------------------------------------------------GL-*/
/* struct De                                                                 */
/*---------------------------------------------------------------------------*/

typedef struct {
        char   fil[41]; /* nom du fis */
        char   org[ 5]; /* organisation*/
        char   typ[ 5]; /* type */
        int    mxp    ; /* nombre maximal de pixels */
        int    mxl    ; /* nombre maximal de lignes */
        int    mxc    ; /* nombre maximal de canaux */
        char   auc[21]; /* auteur et programmme */
        int    djc    ; /* date julienne creation */
        char   ser[21]; /* service de creation */
        char   tit[81]; /* titre */
        char   aum[21]; /* auteur et programme */
        int    djm    ; /* date julienne */
        int    mis    ; /* code numerique de la mission */
        int    nim    ; /* numero d'ordre dans la mission */
        int    ins    ; /* code numerique */
        int    oss    ; /* numero d'orbite/de slot/de station */
        double ijr    ; /* instant julien */
        float  llp    ; /* lna/lsp/parametre physique de la station */
        char   csc[ 5]; /* sens de balayage de l'instrument */
        float  anw    ; /* latitude du coin NW */
        float  onw    ; /* longitude du coin NW */
        float  ane    ; /* latitude du coin NE */
        float  one    ; /* longitude du coin NE */
        float  ase    ; /* latitude du coin SE */
        float  ose    ; /* longitude du coin SE */
        float  asw    ; /* latitude du coin SW */
        float  osw    ; /* longitude du coin SW */
        int    npp    ; /* numero du premier point */
        int    npl    ; /* numero de la premiere ligne */
        int    ndp    ; /* numero du dernier point */
        int    ndl    ; /* numero de la derniere ligne */
        double ijd    ; /* instant julien de debut */
        double ijf    ; /* instant julien de fin */
        int    nlm    ; /* nombre de ligne(s) manquante(s) */
        int    nor    ; /* nombre d'octets (theorique) par record */
        int    nri    ; /* nombre de records donnees image */
        char   nve[13]; /* numero de version fp utilisee */
        int    nmi    ; /* nombre de missions des donnees images */
        int    nbr    ; /* nombre total de records du fis */
        int    pad    ; /* nombre d'octets de padding */
       }De;

/*------------------------------------------------------------------------GL-*/
/* struct Dd                                                                 */
/*---------------------------------------------------------------------------*/

typedef struct {
        char   cod[13]; /* code de qualification */
        int    npr    ; /* numero du premier record */
        int    ndr    ; /* numero du dernier record */
       }Dd;

/*------------------------------------------------------------------------GL-*/
/* enum Mod                                                                  */
/*---------------------------------------------------------------------------*/
typedef enum {RO,RW,UNM} Mod;

/*------------------------------------------------------------------------GL-*/
/* struct Fis                                                                */
/*---------------------------------------------------------------------------*/
typedef struct {
        FILE       *pf   ; /* pointeur de fichier */
        De  de           ; /* zone DE */
        Dd  dd[21]        ; /* zone DD */
        Mod mod          ; /* mode lecture ou ecriture */
       }Fis;
#endif

/*------------------------------------------------------------------------GL-*/
/* prototypes                                                                */
/*---------------------------------------------------------------------------*/
int fp_clo();  /* close d'un fis */
int fp_cre();  /* creation d'un fis */
int fp_opr();  /* open read d'un fis */
int fp_opw();  /* open write d'un fis */
int fp_rdi1(); /* read DI niveau 1 */
int fp_rdi2(); /* read DI niveau 2 */
int fp_rdi3(); /* read DI niveau 3 */
int fp_wdi1(); /* write DI niveau 1 */
int fp_wdi2(); /* write DI niveau 2 */
int fp_wdi3(); /* write DI niveau 3 */


/*-END--------------------------------------------------------------------GL-*/

/*  -->prototypes added for reader/inspection support: */
void fis_dump_Channel();
void fis_dump_Dd();
void fis_dump_De();
void fis_dump_Fis();
void fis_dump_Hdr();
void fis_dump_PixLatLon();
void fis_dump_Rpt();
void fis_dump_Ttl();
void fis_dump_Buf4();
void fis_dump_Atf();      /* report file attributes */

/******************************************************************************/
/******************************************************************************/
/* This is an example of a program dedicated                                  */
/* to read the 4 channel SST files                                            */
/* The main tasks are:                                                        */
/*    - opening the FIS file				                      */
/*    - allocating memory for the buffer in which the data are to be stored   */
/*    - reading the data from the file to the buffer			      */
/*    - processings ....				                      */
/*						                              */
/*    - closing the FIS file.					              */
/******************************************************************************/
/******************************************************************************/

struct stat stbuf;      /*  buffer for file status returned */

#define MAX_CH_NAME_LEN  10        /* name length */
#define MAX_CH           4         /*  set for five channels, 0 thru 4 */


/* **********************************************************************
 *                    CONDITIONAL COMPILATION DEFINITIONS
 *   NOTE: switches must be defined in Makefile that accompanies this
 *         software package.
 * -->refer to subroutine fis_PixLatLon.c for additional definitions
 * **********************************************************************/

#ifdef LWFLUX
	static char	*title_string ={"FIRE_AX_CMS_LWFLUX"};
	static char	chan_name_set[MAX_CH][MAX_CH_NAME_LEN] = {
		".C1", ".C2", ".C3", ".C4"		};
#define C_NAM_SET_
#endif
#ifdef SOLAR_DAY 
	static char	*title_string ={"FIRE_AX_CMS_SOLDAY"};
	static char	chan_name_set[MAX_CH][MAX_CH_NAME_LEN] = {
		".C1", ".C2", ".C3", ".C4"		};
#define C_NAM_SET_
#endif
#ifdef SOLAR_HR 
	static char	*title_string ={"FIRE_AX_CMS_SOLHR"};
	static char	chan_name_set[MAX_CH][MAX_CH_NAME_LEN] = {
		".C1", ".C2", ".C3", ".C4"		};
#define C_NAM_SET_
#endif
#ifdef SOLAR_MN 
	static char	*title_string ={"FIRE_AX_CMS_SOLMN"};
	static char	chan_name_set[MAX_CH][MAX_CH_NAME_LEN] = {
		".C1", ".C2", ".C3", ".C4"		};
#define C_NAM_SET_
#endif
#ifdef SOLAR_WK
	static char	*title_string ={"FIRE_AX_CMS_SOLWK"};
	static char	chan_name_set[MAX_CH][MAX_CH_NAME_LEN] = {
		".C1", ".C2", ".C3", ".C4"		};
#define C_NAM_SET_
#endif

#ifdef SST_DAY
	static char	*title_string ="FIRE_AX_CMS_SST_DAY";
	static char	chan_name_set[MAX_CH][MAX_CH_NAME_LEN] = {
		".C4", ".C5", ".TSM", ".FRT"		};
#define C_NAM_SET_
#endif
#ifdef SST_WEEK
	static char	*title_string ="FIRE_AX_CMS_SST_WEEK";
	static char	chan_name_set[MAX_CH][MAX_CH_NAME_LEN] = {
		".C4", ".C5", ".TSM", ".FRT"		};
#define C_NAM_SET_
#endif
#ifdef SST_FNTS
	static char	*title_string ="FIRE_AX_CMS_SST_FNTS";
	static char	chan_name_set[MAX_CH][MAX_CH_NAME_LEN] = {
		".C4", ".C5", ".TSM", ".FRT"		};
#define C_NAM_SET_
#endif

#ifndef C_NAM_SET_
/* ! INVALID Makefile PROJECT declaration. */
#endif

/* --> prototypes */
int ask_for_channel();


main(argc, argv)
int	argc;
char	*argv[];
{
	Fis fis_in;
	char	fil_tsm[256];
	char	fil_ter[256];
	int	nbp;	             /* number of pixels per channels */
	int	iret;                  /* error return */
	static int	BPX = NULL;       /* number of bytes/pixel */

	unsigned char	*buf4can;    /* buffer pointer */

	char	basename[80], *ptr;   /* base file name */
	char	report_file[80];
	FILE *RPT, *LOG;
	Atf  atf;                 /* file attribute structure */
	int	i;
	int     chan=0;
	int channel=-1;
        int done=FALSE;

	unsigned char	*cptr[MAX_CH];
	char	ans, selection;
	int	bytes;
	char	file_name[40];


char wsay[5];
int fixcount=0;
int mxchan=0;
int lpcnt;

#ifndef C_NAM_SET_
print("\n\n>>FATAL ERROR !  IMPROPER COMPILATION.\n");
print(">>This reader must be compiled with mission specific options to ensure data\n");
print(">>is processed correctly.  Edit the makefile \"PROJECT\" declaration in Makefile.\n");
print(">>Check the README for further information.\n");
print(">>....program exiting....\n\n");
exit(0);
#endif


/*****************************************************************************/
/* If the format of data is I2, and the number of channels is 1,             */
/* you have,for                                                              */
/* example, to define and use such a buffer:				     */
/*     short *bufi2; 							     */
/*     and then							             */
/*     bufi2=(short *)malloc((unsigned)(1 * nbp));                           */
/*****************************************************************************/


if ( argc < 2 )
{
  printf("\n\n\n");
  printf("\t*******************************************************************\n");
  printf("\t*                      CMS General Read                           *\n");
  printf("\t*             (Centre de Meteorologie Spatiale)                   *\n");
  printf("\t*                                                                 *\n");
  printf("\t* CMS READ PROGRAM:                                               *\n");
  printf("\t*    designed to interpret CMS binary raster data files.          *\n");
  printf("\t* requires:                                                       *\n");
  printf("\t*    a) compilation for correct mission (PROJECT in Makefile)     *\n");
  printf("\t*       and platform specific options.  (ARFLAGS, STD_DEFINES..)  *\n");
  printf("\t*    b) one or more command line arguments required               *\n");
  printf("\t*       arg1..n = name of input data file(s)                      *\n");
  printf("\t*                                                                 *\n");
  printf("\t*******************************************************************\n\n\n");
  exit(1);
}


/*  -->construct output file names */
#ifdef INSPECTION_
   strcpy(report_file, "INSPECT");
#else
   strcpy(report_file, title_string);
#endif

   ptr = strcat (report_file, ".LOG");
   LOG = fopen( ptr, "w" );
   strcpy(strrchr(ptr, '.'), ".RPT");
   RPT = fopen( ptr, "r" );
   if (RPT != NULL)
     {
       printf("***************************************************************************\n");
       printf("* WARNING: This filename exist -->  %s\n",ptr);
       printf("*         Do you wish to overwrite it?  'y' yes  'any other char to exit' *\n");
       printf("***************************************************************************\n");
       scanf("%s",wsay);
       if(tolower(wsay[0]) == 'y')
	 {
	   printf("will rewrite the file --->%s\n", ptr);
	 }
       else
	 {
           exit(0);
	 }
     }
     RPT = fopen( ptr, "w" );
argv++;
while ( *argv != NULL )
{
	strcpy(fil_tsm, *argv);

	strcpy(basename, fil_tsm);
	if ( (ptr = strrchr(fil_tsm, '/')) != NULL ) {
		strcpy(basename, ptr + 1);
	}

	/* -->0 good return=file exists */
	if ( (iret = stat( fil_tsm, &stbuf )) != 0 ) {
		argv++;
		continue;
	}

	iret = fp_opr(&fis_in, fil_tsm);	/* read in file header */

	if (iret  != FP_FOK)	 {
	   printf ( "error fp_opr on %s  code=%d", fil_tsm, iret);
	   fprintf(LOG, 
		    "error fp_opr on %s  code=%d", fil_tsm, iret);
	   exit( iret );
	}

		/* set number of bytes per pixel */
/* 		printf("fis_in.de.typ  %d\n",strstr(fis_in.de.typ)); */
/* 		dummy */
		if ( strstr(fis_in.de.typ, "i2") || strstr(fis_in.de.typ, "I2") ) {
#ifdef DBG_
			printf("DBG: >> file is type I2 \n");
#endif
			BPX = 2; /* set bytes/pixel for file type I2 */
		}
		if ( strstr(fis_in.de.typ, "i1") || strstr(fis_in.de.typ, "I1") ) {
#ifdef DBG_
			printf("DBG: >> file is type I1 \n");
#endif
			BPX = 1; /* set bytes/pixel for file type I1 */
		}
		if ( BPX == NULL ) {
			printf ("\nERR:%s>> file type is undefined. exiting\n", fil_tsm);
			fprintf(LOG, "ERR:>>%s: file type is undefined. exiting\n", fil_tsm);
			exit(1);
		}

		/* calculate buffer size to retain raster data  */
		nbp = fis_in.de.mxp * fis_in.de.mxl ;


/* #ifdef INSPECTION_ || DBG_ */
/*                 printf("\n\nDBG: >>processing file: %s\n", fil_tsm); */
/*  printf("\nDBG: nbp = %d  fis_in.de.mxp=%d  fis_in.de.mxl=%d, fis_in.de.mxc=%d\n", */
/*            nbp, fis_in.de.mxp, fis_in.de.mxl, fis_in.de.mxc); */
/* #endif  */

		/* allocate buffer to retain mxc number of channels data */

		buf4can = (unsigned char *)malloc( (unsigned)( fis_in.de.mxc * BPX * nbp ) );
		if (buf4can == NULL) {
			printf( "DBG: memory allocation error %d\n\n", buf4can);
			exit(FP_ALL);
		}
		cptr[0] = buf4can;
		cptr[1] = buf4can + nbp * BPX;
		cptr[2] = buf4can + 2 * nbp * BPX;
		cptr[3] = buf4can + 3 * nbp * BPX;

		/* read in "mxc" number of channel raster images from the data file */
/* -------------------------------------------------------- */
		iret = fp_rdi1(fis_in, buf4can);
/*               for (lpcnt=0; lpcnt<=10; lpcnt++) 
                  printf("buffer value %d   %d \n",lpcnt,cptr[0][lpcnt]); */

		fixcount=nbp*BPX*fis_in.de.mxc;
		fixcount=fixcount-iret;
		fixcount=fixcount/2;
                iret=1;
/* -------------------------------------------------------- */

/* #ifdef INSPECTION_ || DBG_ */
/* printf("\n\nDBG: >>header read error return:%d\n", iret); */

	        /* output abstract of file header contents for inspection only */
/* 	        fis_dump_Rpt ( RPT, basename, &fis_in, (long )stbuf.st_size, iret ); */
/* #endif */

		if (iret == FP_FOK) {
		    /* ##-->extract mxc channels of image data */
		    ptr = strcat(basename, ".       ");
		    for ( i = 0 ; i < fis_in.de.mxc ; i++) {
		           if ( i >= MAX_CH ) {
printf( "error: program configuration limited to %d channels. Header defines %d channels.\n",
MAX_CH, fis_in.de.mxc);
fprintf(LOG, "error: program configuration limited to %d channels. Header defines %d channels.\n",
MAX_CH, fis_in.de.mxc);
			      break;
			   }
#ifdef INSPECTION_ 
			   strcpy(strrchr(ptr, '.'), &chan_name_set[i][0]);
/* 			   printf("\n\nDBG: >>extracting Channel raster image to: %s\n", ptr); */
/* 			   fis_dump_Channel( cptr[i], fis_in.de.mxl, fis_in.de.mxp, BPX, ptr ); */
#endif
			}
/* 			printf("\n\nDBG: >>raster data extraction complete.\n");  */
                        fprintf(LOG, "data read in OK: fp_rdi1 on %s  code=%d\n", fil_tsm, iret);
			fp_clo(fis_in);
		} else {
/* ------------------------------------------------------------------------------------ */
/*                   special fix for this file only */
                        if(strcmp(fil_tsm,"ax_cms_solar_mn_9206")!=0)
			printf( "error fp_rdi1 on %s  code=%d\n", fil_tsm, iret);
/* ------------------------------------------------------------------------------------ */
			fprintf(LOG, 
			    "data read error: fp_rdi1 on %s  bytes=%d expected=%d\n",
			    fil_tsm, iret, (fis_in.de.mxp * fis_in.de.mxl * BPX) );
		}

#ifdef INSPECTION_ 
		/* report file name and buffer sizes */
/* 		fis_dump_Buf4( LOG, basename, &fis_in, cptr ); */
/* 		fp0_atf (&fis_in, &atf);          /* get file attributes */ */
/* 		fis_dump_Atf ( LOG, &atf );      /* report file attributes */ */
/* 		fis_dump_Fis ( LOG, &fis_in);    /* report file header contents */ */
/* 		fis_dump_De  ( LOG, &fis_in.de); /* report structure De contents */ */
/* 		fis_dump_Dd  ( LOG, &fis_in.dd); /* report structure Dd contents */ */
#else

/* -->Display the MENU selections and enter the options selected  */
    while (!done)
    {
	printf("\n\n\n");
	printf("\t*******************************************************************\n");
	printf("\t*                      CMS General Read                           *\n");
	printf("\t*              (Centre de Meteorologie Spatiale)                  *\n");
	printf("\t*                                                                 *\n");
	printf("\t*  Please make a selection from the menu.                         *\n");
	printf("\t*                                                                 *\n");
	printf("\t*  1.  DUMP HEADERS ONLY                                          *\n");
	printf("\t*  2.  DUMP HEADER AND CHANNEL DATA                               *\n");
	printf("\t*      (there are %d channel(s) of raster image data on this file) *\n",
	     fis_in.de.mxc);
	printf("\t*  3.  DUMP CHANNEL DATA ONLY                                     *\n");
	printf("\t*                                                                 *\n");
	printf("\t* Enter 'x' to exit or terminate processing.                      *\n");
	printf("\t*******************************************************************\n");
	printf("\n\n\n");
	scanf("%s", &selection );
	switch (selection)
	{
	case 'X':
	case 'x':
	    exit(0);
	    break;
	case '2':
	case '3':
	case '1':
	    done=TRUE;
	    break;
	default:
	    printf("* >>invalid selection\n");
	    continue;
	}
    }

	  switch (selection) 
	  {
	  case '1':
		fis_dump_Ttl(RPT, fil_tsm, title_string );
		fis_dump_Hdr(RPT, fil_tsm, &fis_in );
		break;
	  case '2':
	  case '3':
		fis_dump_Ttl(RPT, fil_tsm, title_string );
		if( selection == '2' ) fis_dump_Hdr(RPT, fil_tsm, &fis_in );
		channel = ask_for_channel ( fis_in.de.mxc );
		if(channel+1 == fis_in.de.mxc)
		  {
		    mxchan=1;
                  }
		fis_dump_PixLatLon(RPT, 
		    cptr[channel], &fis_in, BPX, fil_tsm, channel+1, fixcount, mxchan );

	       /* construct file name to receive raw image data */
	        strcpy(strrchr(ptr, '.'), &chan_name_set[channel][0]);

		printf("\n:>>writing channel %d image data to file: %s \n",channel+1, ptr);

	        fis_dump_Channel( 
	          cptr[channel], fis_in.de.mxl, fis_in.de.mxp, BPX, ptr );
	        break;

	  default:
		printf ( "\n\n>>Please select a valid option. \n");
		break;
	  }
#endif             /* end of inspection switch */

	argv++;     /* -->increment file list pointer */
}

printf("\n\n>>report summary written to: %s\n",report_file );

fclose( RPT );
fclose( LOG );
}

/*  ################################################################## */
/*  # --> ask operator to enter a reasonable channel number */
/*  ################################################################## */
int ask_for_channel ( mxc )
int mxc;
{
   char ans[10];
   int  i, channel=-1;
   if( mxc == 1 ) return 0;
   while ( channel==-1 ) 
   {
     printf("\t*******************************************************************\n");
     printf("\t* There are %2d channels found on this file.         \n",mxc);
     printf("\t* Select a channel number to dump... (1-%2d)         \n",mxc);
     printf("\t*                                                    \n");
     printf("\t* Channel raster file name suffixes:                 \n");
     printf("\t*   ");
     for ( i = 0 ; i < mxc ; i++) {
         printf("  *%s,", &chan_name_set[i][0] ); }
     printf("\n\t*                                                    \n");
     printf("\t* Enter 'x' to exit or terminate processing.         \n");
     printf("\t*******************************************************************\n");

     scanf("%s",ans);

     if (tolower(ans[0]) == 'x') { exit(0); }
     if ((atoi(ans) > mxc) || (atoi(ans) < 1))
     {
	printf("You have selected an invalid channel: %s\n",ans);
	printf("Please try again.\n");
     }else{
        printf("\n\t...please wait...processing\n");
	return (atoi(ans)-1);
     }
   }
}


/* *************************************************************
 * revision history: 
 *
 * $Log:	cms_read.c,v $
 * Revision 1.7  96/09/13  11:46:16  11:46:16  pscart (Paul Carter)
 * rolled headers and complete library into the original
 * reader to simplify distribution. 
 * 
 * Revision 1.6  96/09/13  11:16:08  11:16:08  pscart (Paul Carter)
 * replaced fp0.h with cms0.h
 * 
 * Revision 1.5  96/09/13  10:30:46  10:30:46  pscart (Paul Carter)
 * changed CMS_DAY/WEEK/FNTS to SST_
 * 
 * Revision 1.4  96/08/14  17:52:55  17:52:55  pscart (Paul Carter)
 * major edits; straightening out operator interaction
 * 
 * Revision 1.3  96/08/02  15:07:37  15:07:37  pscart (Paul Carter)
 * redefined project dependent macro definitions.
 * 
 * Revision 1.2  96/07/30  13:22:31  13:22:31  pscart (Paul Carter)
 * revised program to run either as an inspection or
 * reader program based on the defineINSPECTION_
 * 
 * Revision 1.1  96/07/30  11:36:48  11:36:48  pscart (Paul Carter)
 * Initial revision
 * 
 * *************************************************************/
/*-------------------------------------------------------------------------*

		     First Isccp Regional Experiment
		   application and service library source 
		for the CMS general purpose reader software
                             NASA Langley DAAC

 *-------------------------------------------------------------------------*
  version: $Id$
 *-------------------------------------------------------------------------*/

/* #ifndef FILE */
/* #include <stdio.h> */
/* #include <string.h> */
/* #ifdef HPUX */
/* #include <sys/types.h> */
/* #else */
/* #include <sys/stdtypes.h> */
/* #endif */
/* #include <unistd.h> */
/* #include <time.h> */
/* #endif */

/* #include "cms0.h" */


static char rcsID[]={"@(#)$RCSfile$ $Revision: 1.7 $ $Date: 96/09/13 11:46:16 $"};


void fis_dump_Atf( OUT, atf )
FILE *OUT;
Atf  *atf;
{
  long ofi;

  fprintf(OUT,"\n: Structure Atf contents:\n");
  fprintf(OUT,": \tatf->nop=%d nombre maximal de pixels\n",atf->nop);
  fprintf(OUT,": \tatf->ord (%d) Classe d'ordonnancement (uvw)\n",atf->ord);
/*------------------------------------------------------------------------GL-*/
/* typedef enum {PLC,PCL,LPC,LCP,CPL,CLP,UNO} Ord; */
/*   PLC: 0 :: "plc" "p/lc" "pl/c" "plc/" */
/*   PCL: 1 :: "pcl" "p/cl" "pc/l" "pcl/" */
/*   LPC: 2 :: "lpc" "l/pc" "lp/c" "lpc/" */
/*   LCP: 3 :: "lcp" "l/cp" "lc/p" "lcp/" */
/*   CPL: 4 :: "cpl" "c/pl" "cp/l" "cpl/" */
/*   CLP: 5 :: "clp" "c/lp" "cl/p" "clp/" */
/*   UNO: 6 :: organisation inconnue  */
/*---------------------------------------------------------------------------*/


  fprintf(OUT,": \tatf->Blo (%d) Classe de blocage \n",atf->blo);
/*---------------------------------------------------------------------------*/
/* organisation "originales" */
/* typedef enum {U_VW,UV_W,UVW_,UNBL}      see fp0_blo.c */
/*   U_VW: 0 :: "plc" "lpc" */
/*   UV_W: 1 :: "pcl" "lcp" "cpl" "clp" */
/*   UNO : 3 :: organisation inconnue  */
/*---------------------------------------------------------------------------*/


  fprintf(OUT,": \tatf->nor=%d nombre d\'octets par records = de.nor)\n",atf->nor);
  fprintf(OUT,": \tatf->pad=%d nombre d\'octets de padding pour chaque record\n",atf->pad);
  fprintf(OUT,": \tatf->nre=%d nombre de records de la zone DE\n",atf->nre);
  fprintf(OUT,": \tatf->nrd=%d nombre de records de la zone DD\n",atf->nrd);
  fprintf(OUT,": \tatf->mxu=%d dimension u\n",atf->mxu);
  fprintf(OUT,": \tatf->mxv=%d dimension v\n",atf->mxv);
  fprintf(OUT,": \tatf->mxw=%d dimension w\n",atf->mxw);

/* calc offset to record 2 (record 1 is the header) */
  ofi=( OFR(2, atf->nor, atf->pad) ) + atf->nop * atf->mxu;
  fprintf(OUT,"\n: \tofi=%d  byte offset to raster data (calc)\n",ofi);


}

/*------------------ inspection software utility --------------------------*
 *-->fis_dump_Buf4 - 
 *-------------------------------------------------------------------------*/

void fis_dump_Buf4( OUT, basename, fis, cptr )
FILE *OUT;
char *basename;          /* base file name */
Fis *fis;
unsigned char *cptr[];   /* raster data buffer pointer */
{
int i;
    fprintf(OUT,"\n: File: %s Header contents:\n",basename);
    for ( i=0 ; i < fis->de.mxc ; i++ )
    {
/*        fprintf(OUT,": \tChannel %1d buffer address= %d  \n",  */
/* 	     i, &cptr[i]); */
    }
}



/*------------------ inspection software utility --------------------------*
 *--> fis_dump_Channel
  function to dump a binary raster image to the OUT file
 *-------------------------------------------------------------------------*/

void fis_dump_Channel( cptr, lines, pixels, bytes, file_name )
unsigned char *cptr;
int lines;
int pixels;
int bytes;
char *file_name;
{
  int mxl;	             /* local line  counter */
  int mxp;	             /* local pixel counter */
  size_t b_o;                /* bytes output by fwrite */

  unsigned char *lcptr=cptr; /* local buffer pointer */

  FILE *OUT;

  OUT=fopen( file_name, "w" );
/* printf( */
/* "\nDBG:fis_dump_Channel:fwrite  bytes/pixel=%d  pixels=%d  lines=%d  file=%s\n\n",  */
/* bytes,pixels,lines,file_name); */

  for ( mxl=0 ; mxl < lines ; mxl++ ) { /* -->lines */
     for ( mxp=0 ; mxp < pixels ; mxp++ ) { /* -->pixels */
 if( (b_o=fwrite(lcptr,(size_t)bytes, (size_t)bytes, OUT)) != bytes )
        if( (b_o=fwrite(lcptr, 1, (size_t)bytes, OUT)) != bytes )
	{
/*            printf( */
/* "\n\nDBG:fis_dump_Channel:fwrite error: n=%d  bytes=%d  pixel=%d  line=%d  file=%s\n\n", b_o,bytes,mxp,mxl,file_name); */
	   fclose(OUT);
	   exit(1);
	}
/* printf("DBG:fis_dump_Channel:fwrite: n=%d bytes ptr address=O%o\n", */
/* b_o,lcptr); */
	lcptr+=bytes;  /* increment buffer pointer */
     }
  }
  fclose(OUT);
  return;
}


/*------------------ inspection software utility --------------------------*
 *--> fis_dump_Dd
 *-------------------------------------------------------------------------*/

void fis_dump_Dd( OUT, dd )
FILE *OUT;
Dd   *dd;
{
   fprintf(OUT,"\n: Structure Dd contents\n");
   fprintf(OUT,": \tdd->cod=\"%s\" code de qualification\n",dd->cod);
   fprintf(OUT,": \tdd->npr=%d numero du premier record\n",dd->npr);
   fprintf(OUT,": \tdd->ndr=%d numero du dernier record\n",dd->ndr);
   fprintf(OUT,"\n");
}


/*------------------ inspection software utility --------------------------*
 *--> fis_dump_De
 *-------------------------------------------------------------------------*/

void fis_dump_De( OUT, de )
FILE *OUT;
De   *de;
{
  fprintf(OUT,"\n: Structure De contents:\n");
  fprintf(OUT,": \tde->fil=\"%s\" nom du fis\n",de->fil);
  fprintf(OUT,": \tde->org=\"%s\" organisation\n",de->org);
  fprintf(OUT,": \tde->typ=\"%s\" type\n",de->typ);
  fprintf(OUT,": \tde->mxp=%d nombre maximal de pixels\n",de->mxp);
  fprintf(OUT,": \tde->mxl=%d nombre maximal de lignes\n",de->mxl);
  fprintf(OUT,": \tde->mxc=%d nombre maximal de canaux\n",de->mxc);
  fprintf(OUT,": \tde->auc=\"%s\" auteur et programmme\n",de->auc);
  fprintf(OUT,": \tde->djc=%d date julienne creation\n",de->djc);
  fprintf(OUT,": \tde->ser=\"%s\" service de creation\n",de->ser);
  fprintf(OUT,": \tde->tit=\"%s\" titre\n",de->tit);
  fprintf(OUT,": \tde->aum=\"%s\" auteur et programme\n",de->aum);
  fprintf(OUT,": \tde->djm=%d date julienne\n",de->djm);
  fprintf(OUT,": \tde->mis=%d code numerique de la mission\n",de->mis);
  fprintf(OUT,": \tde->nim=%d numero d\'ordre dans la mission\n",de->nim);
  fprintf(OUT,": \tde->ins=%d code numerique\n",de->ins);
  fprintf(OUT,": \tde->oss=%d numero d\'orbite/de slot/de station\n",de->oss);
  fprintf(OUT,": \tde->ijr=%f instant julien\n",de->ijr);
  fprintf(OUT,": \tde->llp=%f lna/lsp/parametre physique de la station\n",de->llp);
  fprintf(OUT,": \tde->csc=\"%s\" sens de balayage de l\'instrument\n",de->csc);
  fprintf(OUT,": \tde->anw=%f latitude du coin NW\n",de->anw);
  fprintf(OUT,": \tde->onw=%f longitude du coin NW\n",de->onw);
  fprintf(OUT,": \tde->ane=%f latitude du coin NE\n",de->ane);
  fprintf(OUT,": \tde->one=%f longitude du coin NE\n",de->one);
  fprintf(OUT,": \tde->ase=%f latitude du coin SE\n",de->ase);
  fprintf(OUT,": \tde->ose=%f longitude du coin SE\n",de->ose);
  fprintf(OUT,": \tde->asw=%f latitude du coin SW\n",de->asw);
  fprintf(OUT,": \tde->osw=%f longitude du coin SW\n",de->osw);
  fprintf(OUT,": \tde->npp=%d numero du premier point\n",de->npp);
  fprintf(OUT,": \tde->npl=%d numero de la premiere ligne\n",de->npl);
  fprintf(OUT,": \tde->ndp=%d numero du denier point\n",de->ndp);
  fprintf(OUT,": \tde->ndl=%d numero de la deniere ligne\n",de->ndl);
  fprintf(OUT,": \tde->ijd=%f instant julien de debut\n",de->ijd);
  fprintf(OUT,": \tde->ijf=%f instant julien de fin\n",de->ijf);
  fprintf(OUT,": \tde->nlm=%d nombre de ligne(s) manquante(s)\n",de->nlm);
  fprintf(OUT,": \tde->nor=%d nombre d\'octets (theorique) par record\n",de->nor);
  fprintf(OUT,": \tde->nri=%d nombre de records donnees image\n",de->nri);
  fprintf(OUT,": \tde->nve=\"%s\" numero de version fp utilisee\n",de->nve);
  fprintf(OUT,": \tde->nmi=%d nombre de missions de donnees images\n",de->nmi);
  fprintf(OUT,": \tde->nbr=%d nombre total de records du fis\n",de->nbr);
  fprintf(OUT,": \tde->pad=%d nombre d\'octets de padding\n",de->pad);
}



/*------------------ inspection software utility --------------------------*
 *-->fis_dump_Fis
 *-------------------------------------------------------------------------*/

void fis_dump_Fis( OUT, fis )
FILE *OUT;
Fis  *fis;
{
    static char m[3][4]={"RO","RW","UNM"};

    fprintf(OUT,"\n: Structure Fis contents\n");
    fprintf(OUT,": \tfis->FILE *pf  pointeur de fichier\n");
    fprintf(OUT,": \tfis->De de  zone DE\n");
    fprintf(OUT,": \tfis->Dd dd  zone DD\n");
    fprintf(OUT,": \tfis->Mod  mod=%s  [%d]\n", m[fis->mod], fis->mod);
    fprintf(OUT,"\n");
}


/*------------------ inspection software utility --------------------------*
 *--> fis_dump_Hdr
 *-------------------------------------------------------------------------*/

void fis_dump_Hdr( OUT, basename, fis )
FILE *OUT;
char *basename;          /* base file name */
Fis  *fis; 
{
  static char m[3][4]={"RO","RW","UNM"};

  fprintf(OUT,"\n: %s Header contents:\t\t\n",basename);
  fprintf(OUT,": \tfis->Mod  mode=%s  [%d]\n", m[fis->mod], fis->mod);
  fprintf(OUT,": \tfis->de.fil=\"%s\" originating file name\n",fis->de.fil);
  fprintf(OUT,": \tfis->de.org=\"%s\" raster data sequence\n",fis->de.org);
  fprintf(OUT,": \tfis->de.typ=\"%s\" data type, pixel\n",fis->de.typ);
  fprintf(OUT,": \tfis->de.mxp=%d maximum number of pixels\n",fis->de.mxp);
  fprintf(OUT,": \tfis->de.mxl=%d maximum number of lines\n",fis->de.mxl);
  fprintf(OUT,": \tfis->de.mxc=%d maximum number of channels \n",fis->de.mxc);
  fprintf(OUT,": \tfis->de.auc=\"%s\" author of program\n",fis->de.auc);
  fprintf(OUT,": \tfis->de.djc=%d creation date, julian\n",fis->de.djc);
  fprintf(OUT,": \tfis->de.ser=\"%s\" originating facility\n",fis->de.ser);
  fprintf(OUT,": \tfis->de.tit=\"%s\" title\n",fis->de.tit);
  fprintf(OUT,": \tfis->de.aum=\"%s\" author of program\n",fis->de.aum);
  fprintf(OUT,": \tfis->de.djm=%d current date, julian\n",fis->de.djm);
  fprintf(OUT,": \tfis->de.mis=%d mission code number\n",fis->de.mis);
  fprintf(OUT,": \tfis->de.nim=%d mission sequence number\n",fis->de.nim);
  fprintf(OUT,": \tfis->de.ins=%d numerical code \n",fis->de.ins);
  fprintf(OUT,": \tfis->de.oss=%d orbit number\n",fis->de.oss);
  fprintf(OUT,": \tfis->de.ijr=%f instant date, julian\n",fis->de.ijr);
  fprintf(OUT,": \tfis->de.llp=%f physical station number\n",fis->de.llp);
  fprintf(OUT,": \tfis->de.csc=\"%s\" instrument orientation\n",fis->de.csc);
  fprintf(OUT,": \tfis->de.anw=%f NW latitude\n",fis->de.anw);
  fprintf(OUT,": \tfis->de.onw=%f NW longitude\n",fis->de.onw);
  fprintf(OUT,": \tfis->de.ane=%f NE latitude\n",fis->de.ane);
  fprintf(OUT,": \tfis->de.one=%f NE longitude\n",fis->de.one);
  fprintf(OUT,": \tfis->de.ase=%f SE latitude\n",fis->de.ase);
  fprintf(OUT,": \tfis->de.ose=%f SE longitude\n",fis->de.ose);
  fprintf(OUT,": \tfis->de.asw=%f SW latitude\n",fis->de.asw);
  fprintf(OUT,": \tfis->de.osw=%f SW longitude\n",fis->de.osw);
  fprintf(OUT,": \tfis->de.npp=%d number of the first point\n",fis->de.npp);
  fprintf(OUT,": \tfis->de.npl=%d number of the first line\n",fis->de.npl);
  fprintf(OUT,": \tfis->de.ndp=%d number of the last point\n",fis->de.ndp);
  fprintf(OUT,": \tfis->de.ndl=%d number of the last line\n",fis->de.ndl);
  fprintf(OUT,": \tfis->de.ijd=%f start recording time, julian\n",fis->de.ijd);
  fprintf(OUT,": \tfis->de.ijf=%f end recording time, julian\n",fis->de.ijf);
  fprintf(OUT,": \tfis->de.nlm=%d number of missing line(s)\n",fis->de.nlm);
  fprintf(OUT,": \tfis->de.nor=%d number of bytes per record (theoretical)\n",fis->de.nor);
  fprintf(OUT,": \tfis->de.nri=%d number of bytes per raster line\n",fis->de.nri);
  fprintf(OUT,": \tfis->de.nve=\"%s\" file processing utility version number\n",fis->de.nve);
  fprintf(OUT,": \tfis->de.nmi=%d number of data images this mission\n",fis->de.nmi);
  fprintf(OUT,": \tfis->de.nbr=%d total number of records this file\n",fis->de.nbr);
  fprintf(OUT,": \tfis->de.pad=%d number of padding bytes\n",fis->de.pad);
  fprintf(OUT,": \tfis->dd[0].cod=\"%s\" qualification code \n",fis->dd[0].cod);
  fprintf(OUT,": \tfis->dd[0].npr=%d number of primary records\n",fis->dd[0].npr);
  fprintf(OUT,": \tfis->dd[0].ndr=%d number of fine points\n",fis->dd[0].ndr);
  fprintf(OUT,"\n");
}


/*-------------------------------------------------------------------------*
 *--> fis_dump_PixLatLon
  function to dump a raster data report to the OUT file
 *-------------------------------------------------------------------------*/

void fis_dump_PixLatLon(OUT , cptr, fis, bytes, file_name, channel, fixcount, mxchan )
unsigned char	*cptr;
Fis    *fis;
int	bytes;
char	*file_name;
int channel;
int fixcount;
int mxchan;
FILE   *OUT;
{
#define LOCALBUF 40
	char	out_file[LOCALBUF];
	int	mxl;	             /* local line  counter */
	int	mxp;	             /* local pixel counter    */
        char    cal_name[LOCALBUF];  /* name of the calculation done */
	char    unit_name[LOCALBUF]; /* units the calculation is done in */
	char data_descp[40];         /* data description */
	short	*tmp;	             /* local data buffer */
	int	i;
	int	inew;
	int     irows;
	int	count;
	float	lat, lon;
	unsigned char	*lcptr ; /* local buffer pointer */

	lcptr = cptr; /* local buffer pointer */


#ifdef SOLAR_DAY
#define RESOLUTION  0.04
#define LAT_DEB    40.01
#define LON_DEB   -29.99
strcpy(unit_name,"       flux [tenths of Joule/cm^2]"); 
strcpy(cal_name,"        DAILY SOLAR IRRADIANCE");
strcpy(data_descp,"DAILY SOLAR IRRADIANCE");
#endif
#ifdef SOLAR_HR
#define RESOLUTION   0.04
#define LAT_DEB     40.01
#define LON_DEB    -29.99
strcpy(unit_name,"       flux [tenths of Joule/cm^2]"); 
strcpy(cal_name,"        HOURLY SOLAR IRRADIANCE");
strcpy(data_descp,"HOURLY SOLAR IRRADIANCE");
#endif
#ifdef SOLAR_MN
#define RESOLUTION  0.04
#define LAT_DEB    40.01
#define LON_DEB   -29.99
strcpy(unit_name,"       flux [tenths of Joule/cm^2]"); 
strcpy(cal_name,"        MONTHLY SOLAR IRRADIANCE");
strcpy(data_descp,"MONTHLY SOLAR IRRADIANCE");
#endif
#ifdef SOLAR_WK
#define RESOLUTION  0.04
#define LAT_DEB    40.01
#define LON_DEB   -29.99
strcpy(unit_name,"       flux [tenths of Joule/cm^2]"); 
strcpy(cal_name,"        WEEKLY SOLAR IRRADIANCE");
strcpy(data_descp,"WEEKLY SOLAR IRRADIANCE");
#endif

#ifdef LWFLUX
#define RESOLUTION  0.08
#define LAT_DEB    39.99
#define LON_DEB   -29.97
strcpy(cal_name,"        LONGWAVE FLUX");
strcpy(data_descp,"LONGWAVE FLUX");
strcpy(unit_name,"       Wm^-2");
#endif

#ifdef SST_DAY
#define RESOLUTION  0.02
#define LAT_DEB    40.02
#define LON_DEB   -30.00
strcpy(cal_name,"        SST CMS DAY");
strcpy(data_descp,"SST CMS DAY");
strcpy(unit_name,"Sea Surface Temp. [deg C] offset by 10");
#endif
#ifdef SST_WEEK
#define RESOLUTION  0.02
#define LAT_DEB    40.02
#define LON_DEB   -30.00
strcpy(cal_name,"        SST CMS WEEK");
strcpy(data_descp,"SST CMS WEEK");
strcpy(unit_name,"Sea Surface Temp. [deg C] offset by 10");
#endif
#ifdef SST_FNTS
#define RESOLUTION  0.02
#define LAT_DEB    40.02
#define LON_DEB   -30.00
strcpy(cal_name,"        SST CMS FNTS");
strcpy(data_descp,"SST CMS FNTS");
strcpy(unit_name,"Front Intensity          [deg C/5 km]");
#endif

#ifndef RESOLUTION
/* ! INVALID Makefile PROJECT declaration. */
#endif
fprintf(OUT,"\n");
fprintf(OUT,"   *****************************************************************************\n");
fprintf(OUT,"                                                                                \n");
fprintf(OUT,"                    PROJECT_LEVEL/DATA_SET_LEVEL READ SOFTWARE                  \n");
fprintf(OUT,"        explicit listing of %s",data_descp);
fprintf(OUT," raster image data by geolocation\n");
fprintf(OUT,"                                                                                \n");
fprintf(OUT,"   *****************************************************************************\n");

/* turkey */
	inew  =0;
	irows =0;
	count =0;

	tmp = (char *)malloc( bytes );
	for ( mxl = 0 ; mxl < fis->de.mxl ; mxl++)  /*  -->rows */ { 
		fprintf(OUT,"\n");
		fprintf(OUT,"       These data values are extracted from:\n");
		fprintf(OUT,"       file:            %s\n",file_name );
		fprintf(OUT,"       channel number:  %d\n",channel );
		fprintf(OUT,"       bytes per pixel: %d\n\n",bytes );
		irows++;
		fprintf(OUT,"\n");
		fprintf(OUT,"Now do the processing for row %d\n", irows);
		lat = (LAT_DEB - ((irows - 1) * RESOLUTION));
		fprintf(OUT,"  Latitude :  %5.2f\n", lat);
		fprintf(OUT,"\n");
		fprintf(OUT,"pixel                                          \n");
		fprintf(OUT,"count%-30s        Longitude  in the data for row %d\n",cal_name,irows);
		fprintf(OUT,"      %-30s      \n",unit_name);
		fprintf(OUT,"------------------------------------------------------------------------------\n");
		for ( mxp = 0 ; mxp < fis->de.mxp ; mxp++) /* -->pixels */ {
			count++;
			lon = (LON_DEB + ((count - 1) * RESOLUTION));
			memcpy( (char *)tmp, (char *)lcptr, bytes);
 			switch (bytes) {
			case 1: 
                          fprintf(OUT,"%3d          %6d                        %5.2f     \n", count, lcptr[mxp], lon);
/* 				fprintf(OUT, " 1: %d  #%x  #",  */
/* 				    (unsigned char ) * tmp, (unsigned char ) * tmp); */
/*		          printf("mxp %d   lcptr %d \n",mxp,lcptr[mxp]); */
                 

				break;
			case 2: 
			if(mxchan == 1)
			  {
			    if (mxl == fis->de.mxl-1)
                              {
			        if (mxp >= fis->de.mxp- fixcount)
			          {
				    *tmp=-9999;
                                  }
                              }
                           }
     fprintf(OUT,"%3d          %6d                        %5.2f     \n", count, (int )*tmp, lon);
 /*                         lcptr += bytes;  /* increment buffer pointer */
				break;
       				}

			} /*End of the pixel loop */

                        lcptr += fis->de.mxp;  
			count=0;
		} /*End of row loop */

fprintf(OUT,"\n");
fprintf(OUT,"*************************************************************\n");
fprintf(OUT,"The program  has completed processing for file : %s\n",file_name);
fprintf(OUT,"A total of %d",irows);
fprintf(OUT," rows were processed.\n");
fprintf(OUT,"*************************************************************\n");
	free( tmp );
	return;
}
/* 			for ( i = 0 ; i < bytes ; i++)  */
/* 				fprintf(OUT, "%x", *(lcptr + i)); */
/* 			fprintf(OUT, " %7.2f\n", lon ); */
/* 			lcptr += bytes;   increment buffer pointer  */
/*           fprintf(OUT,"%3d          %3d                %5.2f     \n", count, *(lcptr + i), lon); */

				/*  pixel count: 1  address: 40006c18  2: 14  #e  #efd    2.00 */
				/*  pixel count: 2  address: 40006c1a  2: 14  #e  #efd    2.00 */
				/*  pixel count: 3  address: 40006c1c  2: 14  #e  #ef8    2.00 */

/* 			fprintf(OUT, " pixel count: %d ", pixel_count ); */


void fis_dump_Rpt( OUT, basename, fis, file_size, err )
FILE *OUT;
char *basename;          /* base file name */
Fis  *fis;
long file_size;
int err;
{
  De   *de;
  de = &fis->de;

  fprintf(OUT,"%s ",basename);
  fprintf(OUT,"%d ",file_size);

/*  Structure Fis contents: */
  fprintf(OUT," %d",de->mxp); /* max pixels */
  fprintf(OUT," %d",de->mxl); /* max lines */
  fprintf(OUT," %d",de->mxc); /* max channels */
  fprintf(OUT," %d",de->djc); /* creation date (julian) */
  fprintf(OUT," %d",de->djm); /* date (julian) */
  fprintf(OUT," %d",de->mis); /* mission number */
  fprintf(OUT," %d",de->oss); /* orbit number */
  fprintf(OUT," %f",de->ijr); /* instant date (julian) */
  fprintf(OUT," %s",de->csc); /* scanning orientation */

  fprintf(OUT," %f",de->anw); /* NW latitude */
  fprintf(OUT," %f",de->onw); /* NW longitude */
  fprintf(OUT," %f",de->ane); /* NE latitude */
  fprintf(OUT," %f",de->one); /* NE longitude */
  fprintf(OUT," %f",de->ase); /* SE latitude */
  fprintf(OUT," %f",de->ose); /* SE longitude */
  fprintf(OUT," %f",de->asw); /* SW latitude */
  fprintf(OUT," %f",de->osw); /* SW longitude */

/*   de->npp=%d numero du premier point\n",de->npp); */
/*   de->npl=%d numero de la premiere ligne\n",de->npl); */
/*   de->ndp=%d numero du denier point\n",de->ndp); */
/*   de->ndl=%d numero de la deniere ligne\n",de->ndl); */

  fprintf(OUT," %f",de->ijd); /* start date/time (julian) */
  fprintf(OUT," %f",de->ijf); /* end date/time (julian) */
  fprintf(OUT," %d",de->nlm); /* # missing lines */
  fprintf(OUT," %d",de->nor); /* # bytes/record (=max pixels) */
  fprintf(OUT," %d",de->nri); /* # of data records image ? */
  fprintf(OUT," %d",de->nmi); /* # of missions of data images ? */
  fprintf(OUT," %d",de->nbr); /* total # records this file */
  fprintf(OUT," %d\n",err);   /* file read error */
}



/*-------------------------------------------------------------------------*
 *-->fis_dump_Ttl
  function to dump a title mast to the OUT file
 *-------------------------------------------------------------------------*/

void fis_dump_Ttl(OUT, fis, title )
FILE   *OUT;
Fis    *fis;
char   *title;
{
	int len;
	char  *txtbuf;
	static time_t calptr;


    time(&calptr); /* fetch current date /time */

/*  center title - calc spaces */
    len =(78-strlen(title))/2 ;
    txtbuf=(char *) malloc( len );
    memset(txtbuf,' ',len);
    txtbuf[len]='\0';
fprintf(OUT,"\n\n");
fprintf(OUT,"   *****************************************************************************\n");
fprintf(OUT,"   *                                                                           *\n");
fprintf(OUT,"   *                               Langley DAAC                                *\n");
fprintf(OUT,"   *                                                                           *\n");
fprintf(OUT,"   *                PROJECT_LEVEL/DATA_SET_LEVEL READ SOFTWARE                 *\n");
fprintf(OUT,"   *%s%s\n",txtbuf,title);
fprintf(OUT,"   *                                                                           *\n");
fprintf(OUT,"   *  VERSION NUMBER: $Revision: 1.7 $ \n");
fprintf(OUT,"   *  Language: C                                                              *\n");
fprintf(OUT,"   *  Platforms supported:  SUN                                                *\n");
fprintf(OUT,"   *                        HP                                                 *\n");
fprintf(OUT,"   *                        SGI                                                *\n");
fprintf(OUT,"   *                        Dec Alpha                                          *\n");
fprintf(OUT,"   * Contact:  Langley DAAC user and Data Services Office                      *\n");
fprintf(OUT,"   *           Mail Stop 157D                                                  *\n");
fprintf(OUT,"   *           Hampton, VA 23681-0001                                          *\n");
fprintf(OUT,"   *           PHONE:   (757)864-8656            FAX: (757)864-8807            *\n");
fprintf(OUT,"   *                                                                           *\n");
fprintf(OUT,"   *****************************************************************************\n");
fprintf(OUT,"\n     date: %s \n\n",ctime(&calptr) );

	free( txtbuf );

}


/*---------------------------------------------------------------------------
   @(#)fp0_atf.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
void fp0_atf(fis,atf)
Fis *fis;
Atf *atf;
/*---------------------------------------------------------------------------*/
BEGIN
atf->nop=fp0_nop(fis->de.typ);
atf->nor=fis->de.nor;
atf->blo=fp0_blo(fis->de.org);
atf->ord=fp0_ord(fis->de.org);
atf->pad=fis->de.pad;
atf->nre=NRE(fis->de.nor);
atf->nrd=NRD(fis->de.nor);
fp0_oso(atf->ord,
        fis->de.mxp,fis->de.mxl,fis->de.mxc,&atf->mxu,&atf->mxv,&atf->mxw);
END


/*---------------------------------------------------------------------------
   @(#)fp0_blo.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
Blo fp0_blo(org)
char *org;
/*---------------------------------------------------------------------------*/
BEGIN
if (fp0_cus(org,"p/lc")) return (U_VW);
if (fp0_cus(org,"p/cl")) return (U_VW);
if (fp0_cus(org,"l/pc")) return (U_VW);
if (fp0_cus(org,"l/cp")) return (U_VW);
if (fp0_cus(org,"c/pl")) return (U_VW);
if (fp0_cus(org,"c/lp")) return (U_VW);

if (fp0_cus(org,"pl/c")) return (UV_W);
if (fp0_cus(org,"pc/l")) return (UV_W);
if (fp0_cus(org,"lp/c")) return (UV_W);
if (fp0_cus(org,"lc/p")) return (UV_W);
if (fp0_cus(org,"cp/l")) return (UV_W);
if (fp0_cus(org,"cl/p")) return (UV_W);

if (fp0_cus(org,"plc/")) return (UVW_);
if (fp0_cus(org,"pcl/")) return (UVW_);
if (fp0_cus(org,"lpc/")) return (UVW_);
if (fp0_cus(org,"lcp/")) return (UVW_);
if (fp0_cus(org,"cpl/")) return (UVW_);
if (fp0_cus(org,"clp/")) return (UVW_);

/* organisation "originales" */

if (fp0_cus(org,"plc")) return (U_VW);
if (fp0_cus(org,"pcl")) return (UV_W);
if (fp0_cus(org,"lpc")) return (U_VW);
if (fp0_cus(org,"lcp")) return (UV_W);
if (fp0_cus(org,"cpl")) return (UV_W);
if (fp0_cus(org,"clp")) return (UV_W);

/* organisation inconnue */

                        return (UNBL);
END


/*---------------------------------------------------------------------------
   @(#)fp0_cdd.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_cdd(buf,dd)
char buf[FP_NOD+1];
Dd dd[FP_NDA];
/*---------------------------------------------------------------------------*/
BEGIN
  int da;
  int c;

  c=0;
  FOR(da=0;da<FP_NDA;da++)
    fp0_cpy(&buf[   c],dd[da].cod,12);
    SPRINTF(&buf[c+12],"%6d" ,dd[da].npr,FP_CDD);
    SPRINTF(&buf[c+18],"%6d" ,dd[da].ndr,FP_CDD);
    c=c+24;
  END_FOR
  buf[FP_NOD]=CHAR0;

  return(FP_FOK);
END

/*---------------------------------------------------------------------------
   @(#)fp0_cde.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_cde(buf,de)
char buf[FP_NOE+1];
De *de;
/*---------------------------------------------------------------------------*/
BEGIN
  fp0_cpy(&buf[  0],de->fil,40);
  fp0_cpy(&buf[ 40],de->org,4);
  fp0_cpy(&buf[ 44],de->typ,4);
  SPRINTF(&buf[ 48],"%5d",de->mxp,FP_CDE);
  SPRINTF(&buf[ 53],"%5d",de->mxl,FP_CDE);
  SPRINTF(&buf[ 58],"%5d",de->mxc,FP_CDE);
  fp0_cpy(&buf[ 63],de->auc,20);
  SPRINTF(&buf[ 83],"%5d",de->djc,FP_CDE);
  fp0_cpy(&buf[ 88],de->ser,20);
  fp0_cpy(&buf[108],de->tit,80);
  fp0_cpy(&buf[188],de->aum,20);
  SPRINTF(&buf[208],"%5d",de->djm,FP_CDE);
  SPRINTF(&buf[213],"%2d",de->mis,FP_CDE);
  SPRINTF(&buf[215],"%2d",de->nim,FP_CDE);
  SPRINTF(&buf[217],"%2d",de->ins,FP_CDE);
  SPRINTF(&buf[219],"%5d",de->oss,FP_CDE);
  SPRINTF(&buf[224],"%14.8f",de->ijr,FP_CDE);
  SPRINTF(&buf[238],"%7.2f",de->llp,FP_CDE);
  fp0_cpy(&buf[245],de->csc,4);
  SPRINTF(&buf[249],"%7.2f",de->anw,FP_CDE);
  SPRINTF(&buf[256],"%7.2f",de->onw,FP_CDE);
  SPRINTF(&buf[263],"%7.2f",de->ane,FP_CDE);
  SPRINTF(&buf[270],"%7.2f",de->one,FP_CDE);
  SPRINTF(&buf[277],"%7.2f",de->ase,FP_CDE);
  SPRINTF(&buf[284],"%7.2f",de->ose,FP_CDE);
  SPRINTF(&buf[291],"%7.2f",de->asw,FP_CDE);
  SPRINTF(&buf[298],"%7.2f",de->osw,FP_CDE);
  SPRINTF(&buf[305],"%5d",de->npp,FP_CDE);
  SPRINTF(&buf[310],"%5d",de->npl,FP_CDE);
  SPRINTF(&buf[315],"%5d",de->ndp,FP_CDE);
  SPRINTF(&buf[320],"%5d",de->ndl,FP_CDE);
  SPRINTF(&buf[325],"%14.8f",de->ijd,FP_CDE);
  SPRINTF(&buf[339],"%14.8f",de->ijf,FP_CDE);
  SPRINTF(&buf[353],"%5d",de->nlm,FP_CDE);
  SPRINTF(&buf[358],"%5d",de->nor,FP_CDE);
  SPRINTF(&buf[363],"%6d",de->nri,FP_CDE);
  fp0_cpy(&buf[369],de->nve,12);
  SPRINTF(&buf[381],"%6d",de->nmi,FP_CDE);
  SPRINTF(&buf[387],"%6d",de->nbr,FP_CDE);
  SPRINTF(&buf[393],"%6d",de->pad,FP_CDE);
  buf[FP_NOE]=CHAR0;
  return(FP_FOK);
END


/*---------------------------------------------------------------------------
   @(#)fp0_cpy.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
void fp0_cpy(str1,str2,n)
char *str1,*str2;
int n;
/*---------------------------------------------------------------------------*/
BEGIN
  char *s,*d;
  int m;

  s=str2;
  d=str1;
  m=n;
  while (*s)      {   *d++=*s++; m--;}
  while (m-- > 0)     *d++=' ';
END

/*---------------------------------------------------------------------------
   @(#)fp0_cus.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_cus(str1,str2)
char *str1,*str2;
/*---------------------------------------------------------------------------*/
BEGIN
 char *fp0_ncs();
 char *nc1,*nc2;
 nc1=fp0_ncs(str1);nc2=fp0_ncs(str2);
 if(toupper(*nc1) != toupper(*nc2)) return (FALSE);
 if(*nc1 ==CHAR0) return (TRUE); else return(fp0_cus(nc1+1,nc2+1));
END

/*---------------------------------------------------------------------------
   @(#)fp0_ddd.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_ddd(buf,dd)
char buf[FP_NOD+1];
Dd dd[FP_NDA];
/*---------------------------------------------------------------------------*/
BEGIN
  int da;
  int c;

  c=0;
  FOR(da=0;da<FP_NDA;da++)
    SSCANF(&buf[c   ],"%12c",dd[da].cod ,FP_DDD); dd[da].cod[12]=CHAR0;
    SSCANF(&buf[c+12],"%6d" ,&dd[da].npr,FP_DDD);
    SSCANF(&buf[c+18],"%6d" ,&dd[da].ndr,FP_DDD);
    c=c+24;
  END_FOR

  return(FP_FOK);
END

/*---------------------------------------------------------------------------
   @(#)fp0_dde.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_dde(buf,de)
char buf[FP_NOE+1];
De *de;
/*---------------------------------------------------------------------------*/
BEGIN
  char apad[6];

  SSCANF(&buf[  0],"%40c",de->fil,FP_DDE);   de->typ[40]=CHAR0;
  SSCANF(&buf[ 40],"%4c",de->org,FP_DDE);    de->org[ 4]=CHAR0;
  SSCANF(&buf[ 44],"%4c",de->typ,FP_DDE);    de->typ[ 4]=CHAR0;
  SSCANF(&buf[ 48],"%5d",&de->mxp,FP_DDE);
  SSCANF(&buf[ 53],"%5d",&de->mxl,FP_DDE);
  SSCANF(&buf[ 58],"%5d",&de->mxc,FP_DDE);
  SSCANF(&buf[ 63],"%20c",de->auc,FP_DDE);   de->auc[20]=CHAR0;
  SSCANF(&buf[ 83],"%5d",&de->djc,FP_DDE);
  SSCANF(&buf[ 88],"%20c",de->ser,FP_DDE);   de->ser[20]=CHAR0;
  SSCANF(&buf[108],"%80c",de->tit,FP_DDE);   de->tit[80]=CHAR0;
  SSCANF(&buf[188],"%20c",de->aum,FP_DDE);
  SSCANF(&buf[208],"%5d",&de->djm,FP_DDE);
  SSCANF(&buf[213],"%2d",&de->mis,FP_DDE);
  SSCANF(&buf[215],"%2d",&de->nim,FP_DDE);
  SSCANF(&buf[217],"%2d",&de->ins,FP_DDE);
  SSCANF(&buf[219],"%5d",&de->oss,FP_DDE);
  SSCANF(&buf[224],"%14lf",&de->ijr,FP_DDE);
  SSCANF(&buf[238],"%7f",&de->llp,FP_DDE);
  SSCANF(&buf[245],"%4c",de->csc,FP_DDE);    de->csc[ 4]=CHAR0;
  SSCANF(&buf[249],"%7f",&de->anw,FP_DDE);
  SSCANF(&buf[256],"%7f",&de->onw,FP_DDE);
  SSCANF(&buf[263],"%7f",&de->ane,FP_DDE);
  SSCANF(&buf[270],"%7f",&de->one,FP_DDE);
  SSCANF(&buf[277],"%7f",&de->ase,FP_DDE);
  SSCANF(&buf[284],"%7f",&de->ose,FP_DDE);
  SSCANF(&buf[291],"%7f",&de->asw,FP_DDE);
  SSCANF(&buf[298],"%7f",&de->osw,FP_DDE);
  SSCANF(&buf[305],"%5d",&de->npp,FP_DDE);
  SSCANF(&buf[310],"%5d",&de->npl,FP_DDE);
  SSCANF(&buf[315],"%5d",&de->ndp,FP_DDE);
  SSCANF(&buf[320],"%5d",&de->ndl,FP_DDE);
  SSCANF(&buf[325],"%14lf",&de->ijd,FP_DDE);
  SSCANF(&buf[339],"%14lf",&de->ijf,FP_DDE);
  SSCANF(&buf[353],"%5d",&de->nlm,FP_DDE);
  SSCANF(&buf[358],"%5d",&de->nor,FP_DDE);
  SSCANF(&buf[363],"%6d",&de->nri,FP_DDE);
  SSCANF(&buf[369],"%12c",de->nve,FP_DDE);   de->nve[12]=CHAR0;
  SSCANF(&buf[381],"%6d",&de->nmi,FP_DDE);
  SSCANF(&buf[387],"%6d",&de->nbr,FP_DDE);
  SSCANF(&buf[393],"%6c",apad,FP_DDE);      apad[6]=CHAR0;
  de->pad=atoi(apad);
  return(FP_FOK);
END

/*---------------------------------------------------------------------------
   @(#)fp0_gdj.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_gdj()
/*---------------------------------------------------------------------------*/
BEGIN
  int yy,mm,dd;
  int e2,e4,e5;
  long e1,e3;
  time_t clock;
  struct tm tml;

  time(&clock);
  tml=*localtime(&clock);

  yy=1900 + tml.tm_year;
  mm=tml.tm_mon + 1;
  dd=tml.tm_mday;

  e1=yy;
  e2=mm-1;
  IF(mm < 3)
    e1=e1 -  1;
    e2=e2 + 12;
  END_IF
  e3=365.25 * (e1 + 4712);
  e4=e1 / 100;
  e5=e1 / 400;
  e1=e3 - e4 + e5;
  e2= ((367 * e2) + 5) / 12;

  return(dd + e1 + e2 - BASE_DJ);
END


/*---------------------------------------------------------------------------
   @(#)fp0_ide.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
void fp0_ide(de)
De *de;
/*---------------------------------------------------------------------------*/
BEGIN
  fp0_cpy(de->auc,FP_AUC,20);
  fp0_cpy(de->ser,FP_SER,20);
  fp0_cpy(de->tit,FP_TIT,80);
  fp0_cpy(de->aum,FP_AUM,20);
  de->mis=0;
  de->nim=0;
  de->ins=0;
  de->oss=0;
  de->ijr=0.;
  de->llp=0.;
  fp0_cpy(de->csc,FP_CSC,4);
  de->anw=de->onw=de->ane=de->one=de->ase=de->ose=de->asw=de->osw=0.;
  de->npp=de->npl=1;
  de->ndp=de->mxp;
  de->ndl=de->mxl;
  de->ijd=de->ijf=0.;
  de->nlm=0;
  fp0_cpy(de->nve,FP_NVE,12);
  de->nmi=0;
END

/*---------------------------------------------------------------------------
   @(#)fp0_ncs.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
char *fp0_ncs(str)
char *str;
/*---------------------------------------------------------------------------*/
BEGIN
return(((isprint(*str) && (*str!=' '))||(*str==CHAR0)) ?str :fp0_ncs(str+1));
END


/*---------------------------------------------------------------------------
   @(#)fp0_nop.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_nop(typ)
char *typ;
/*---------------------------------------------------------------------------*/
BEGIN
if (fp0_cus("i1",typ)) return(1);
if (fp0_cus("i2",typ)) return(2);
if (fp0_cus("i3",typ)) return(3);
if (fp0_cus("i4",typ)) return(4);
                       return(0);
END


/*---------------------------------------------------------------------------
   @(#)fp0_nor.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_nor(blo,nop,mxu,mxv,mxw)
Blo blo;
int nop;
int mxu,mxv,mxw;
/*---------------------------------------------------------------------------*/
BEGIN
  SWITCH(blo)
    case U_VW : return(nop * mxu);
    case UV_W : return(nop * mxu * mxv);
    case UVW_ : return(nop * mxu * mxv * mxw);
    case UNBL : return( 0);
    default   : return(-1);
  END_SWITCH
END

/*---------------------------------------------------------------------------
   @(#)fp0_nri.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_nri(blo,mxv,mxw)
Blo blo;
int mxv,mxw;
/*---------------------------------------------------------------------------*/
BEGIN
  SWITCH(blo)
    case U_VW : return(mxv * mxw);
    case UV_W : return(mxw);
    case UVW_ : return( 1);
    case UNBL : return( 0);
    default   : return(-1);
  END_SWITCH
END


/*---------------------------------------------------------------------------
   @(#)fp0_ofi.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
long fp0_ofi(atf,u,v,w)
Atf *atf;
int u,v,w;
/*---------------------------------------------------------------------------*/
BEGIN
  int rec;
  long pix,ofi;

  SWITCH(atf->blo)
    case U_VW : rec = atf->nre + atf->nrd + (w-1) * atf->mxv + v; break;
    case UV_W : rec = atf->nre + atf->nrd + w                   ; break;
    case UVW_ : rec = atf->nre + atf->nrd + 1                   ; break;
    case UNBL : return( 0);
    default   : return(-1);
  END_SWITCH

  SWITCH(atf->blo)
    case U_VW : pix =   (u - 1);
                break;
    case UV_W : pix =   (u - 1)
                      + (v - 1) * atf->mxu;
                break;
    case UVW_ : pix =   (u - 1)
                      + (v - 1) * atf->mxu
                      + (w - 1) * atf->mxv * atf->mxu;
                break;
    case UNBL : return( 0);
    default   : return(-1);
  END_SWITCH

  ofi=OFR(rec,atf->nor,atf->pad) + atf->nop * pix;

  return(ofi);

END


/*---------------------------------------------------------------------------
   @(#)fp0_ord.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
Ord fp0_ord(org)
char *org;
/*---------------------------------------------------------------------------*/
BEGIN
if (fp0_cus(org,"plc" )) return (PLC);
if (fp0_cus(org,"p/lc")) return (PLC);
if (fp0_cus(org,"pl/c")) return (PLC);
if (fp0_cus(org,"plc/")) return (PLC);

if (fp0_cus(org,"pcl" )) return (PCL);
if (fp0_cus(org,"p/cl")) return (PCL);
if (fp0_cus(org,"pc/l")) return (PCL);
if (fp0_cus(org,"pcl/")) return (PCL);

if (fp0_cus(org,"lpc" )) return (LPC);
if (fp0_cus(org,"l/pc")) return (LPC);
if (fp0_cus(org,"lp/c")) return (LPC);
if (fp0_cus(org,"lpc/")) return (LPC);

if (fp0_cus(org,"lcp" )) return (LCP);
if (fp0_cus(org,"l/cp")) return (LCP);
if (fp0_cus(org,"lc/p")) return (LCP);
if (fp0_cus(org,"lcp/")) return (LCP);

if (fp0_cus(org,"cpl" )) return (CPL);
if (fp0_cus(org,"c/pl")) return (CPL);
if (fp0_cus(org,"cp/l")) return (CPL);
if (fp0_cus(org,"cpl/")) return (CPL);

if (fp0_cus(org,"clp" )) return (CLP);
if (fp0_cus(org,"c/lp")) return (CLP);
if (fp0_cus(org,"cl/p")) return (CLP);
if (fp0_cus(org,"clp/")) return (CLP);
/* organisation inconnue */
                         return(UNO);
END


/*---------------------------------------------------------------------------
   @(#)fp0_oso.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
void fp0_oso(ord,p,l,c,u,v,w)
Ord ord;
int p,l,c;
int *u,*v,*w;
/*---------------------------------------------------------------------------*/
BEGIN
  SWITCH(ord)
    case PLC : *u=p;*v=l;*w=c;break;
    case PCL : *u=p;*v=c;*w=l;break;
    case LPC : *u=l;*v=p;*w=c;break;
    case LCP : *u=l;*v=c;*w=p;break;
    case CPL : *u=c;*v=p;*w=l;break;
    case CLP : *u=c;*v=l;*w=p;break;
    case UNO : *u=*v=*w= 0;break;
    default  : *u=*v=*w=-1;break;
  END_SWITCH
END


/*---------------------------------------------------------------------------
   @(#)fp0_rdd.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_rdd(fis,dd)
Fis *fis;
Dd dd[FP_NDA];
/*---------------------------------------------------------------------------*/
BEGIN
char bdd[FP_NOD+1];
int crd;

SDD(fis);
FREAD(bdd,1,FP_NOD,fis->pf,FP_RDD);
DDD(bdd,dd);

return(FP_FOK);
END


/*---------------------------------------------------------------------------
   @(#)fp0_rde.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_rde(fis,de)
Fis *fis;
De *de;
/*---------------------------------------------------------------------------*/
BEGIN
char bde[FP_NOE+1];
int crd;

SDE(fis);
FREAD(bde,1,FP_NOE,fis->pf,FP_RDE);
DDE(bde,de);

return(FP_FOK);

END


/*---------------------------------------------------------------------------
   @(#)fp0_rdi.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_rdi(fis,atf,u,v,w,nob,buf)
Fis *fis;
Atf *atf;
int u,v,w;
unsigned char *buf;
/*---------------------------------------------------------------------------*/
BEGIN
int i,n; /* DEBUG */
unsigned short *ptr; /* DEBUG */

 long ofi;

 ofi=fp0_ofi(atf,u,v,w);
 FSEEK(fis->pf,ofi,0,FP_SDI);


 if( (n=fread((char *)buf,1,nob,fis->pf)) == nob )
 {
     return(FP_FOK);
 }else{
     return(n);
 }

/*  printf( */
/*  "DBG:fp0_rdi: transferred n=%d  offset ofi=%d  u=%d,v=%d,w=%d,nob=%d\n", */
/*  n,ofi,u,v,w,nob); */

/*  screen buffer dump: */
/*  for ( i=0, ptr=(unsigned short *)buf; i< 30644 ; i++ , ptr++ ) */
/*  { */
/*    if( i==0 ) { */
/*      printf("\n%07o %6d:\t",i,i); */
/*    } else { */
/*      if( i%8==0 ) printf("\n%07o %6d:\t",i,i); */
/*    } */
/*    printf("%04x ",*ptr); */
/*  } */

/*  FREAD((char *)buf,1,nob,fis->pf,FP_RDI); */
/*  return(FP_FOK); */

END


/*---------------------------------------------------------------------------
   @(#)fp0_wdd.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_wdd(fis,dd)
Fis *fis;
Dd dd[FP_NDA];
/*---------------------------------------------------------------------------*/
BEGIN
char bdd[FP_NOD+1];
int crd;

CDD(bdd,dd);
SDD(fis);
FWRITE(bdd,1,FP_NOD,fis->pf,FP_WDD);

return(FP_FOK);

END


/*---------------------------------------------------------------------------
   @(#)fp0_wde.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_wde(fis,de)
Fis *fis;
De *de;
/*---------------------------------------------------------------------------*/
BEGIN
char bde[FP_NOE+1];
int crd;

CDE(bde,de);
SDE(fis);
FWRITE(bde,1,FP_NOE,fis->pf,FP_WDE);

return(FP_FOK);
END


/*---------------------------------------------------------------------------
   @(#)fp0_wdi.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp0_wdi(fis,atf,u,v,w,nob,buf)
Fis *fis;
Atf *atf;
int u,v,w;
unsigned char *buf;
/*---------------------------------------------------------------------------*/
BEGIN
 long ofi;

 ofi=fp0_ofi(atf,u,v,w);
 FSEEK(fis->pf,ofi,0,FP_SDI);
 FWRITE((char *)buf,1,nob,fis->pf,FP_WDI);

 return(FP_FOK);
END


/*---------------------------------------------------------------------------
   @(#)fp_clo.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp_clo(fis)
Fis *fis;
/*---------------------------------------------------------------------------*/
BEGIN
/* IDENT(fp_clo.c 2.1.1.1 4/10/91); */
int crd;

  SWITCH(fis->mod)
    case RO : fclose(fis->pf);
              break;
    case RW : fis->de.djm=fp0_gdj();
              WDE(fis,&fis->de);
              WDD(fis, fis->dd);
              fclose(fis->pf);
              break;
    default : return (FP_MOD);
  END_SWITCH

return(FP_FOK);

END


/*---------------------------------------------------------------------------
   @(#)fp_cre.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp_cre(fil,org,typ,mxp,mxl,mxc)
char *fil;
char *org;
char *typ;
int mxp,mxl,mxc;
/*---------------------------------------------------------------------------*/
BEGIN
/*   IDENT(fp_cre.c 2.1.1.1 4/10/91); */
  Fis fis;
  Atf atf;
  int crd;
  int dv,dw;
  int da;
  int i;
  int nob;
  unsigned char *buf;

  /* creation os du fichier */

  FOPEN(fis.pf,fil,"w",FP_NEW);

  fp0_cpy(fis.de.fil,fil,40);
  fp0_cpy(fis.de.org,org,4);
  fp0_cpy(fis.de.typ,typ,4);

  fis.de.mxp=mxp;
  fis.de.mxl=mxl;
  fis.de.mxc=mxc;

  fis.de.djc=fp0_gdj();
  fis.de.djm=fp0_gdj();

  /* initialisation DE */
  fp0_ide(&fis.de);

  if((atf.ord=fp0_ord(org)) == UNO)  return(FP_ORG);
  if((atf.blo=fp0_blo(org)) == UNBL) return(FP_ORG);
  if((atf.nop=fp0_nop(typ)) == 0  )  return(FP_TYP);

  fp0_oso(atf.ord,fis.de.mxp,fis.de.mxl,fis.de.mxc,&atf.mxu,&atf.mxv,&atf.mxw);

  fis.de.nor=fp0_nor(atf.blo,atf.nop,atf.mxu,atf.mxv,atf.mxw);
  atf.nor=fis.de.nor;
  fis.de.nri=fp0_nri(atf.blo,atf.mxv,atf.mxw);
  fis.de.nbr=NRE(fis.de.nor) + NRD(fis.de.nor) + fis.de.nri;
  fis.de.pad=0;
  atf.pad=fis.de.pad;

  /* initialisation dd */
  FOR(da=0;da<FP_NDA;da++)
    fp0_cpy(fis.dd[da].cod,"UNUSED",12);
    fis.dd[da].npr=fis.dd[da].ndr=0;
  END_FOR

  /* ecriture de */
  WDE(&fis,&fis.de);

  /* ecriture dd */
  WDD(&fis, fis.dd);

  /* initialisation di */
  nob= atf.nop * atf.mxu;
  MALLOC(buf,unsigned char*,nob);

  for (i=0;i<nob;i++) buf[i]=CHAR0;

  FOR(dw=0;dw<atf.mxw;dw++)
    FOR(dv=0;dv<atf.mxv;dv++)
      WDI(&fis,&atf,1,1+dv,1+dw,nob,buf);
    END_FOR
  END_FOR

  /* fermeture */
  fclose(fis.pf);

return (FP_FOK);

END


/*---------------------------------------------------------------------------
   @(#)fp_opr.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp_opr(fis,fil)
Fis *fis;
char *fil;
/*---------------------------------------------------------------------------*/
BEGIN
/*   IDENT(fp_opr.c 2.1.1.1 4/10/91); */
int crd;

/* mode */
fis->mod=RO;

/* ouverture os */
FOPEN(fis->pf,fil,"r",FP_OLD);

/* lecture DE */
RDE(fis,&fis->de);

/* lecture DD */
RDD(fis, fis->dd);

return(FP_FOK);

END

/*---------------------------------------------------------------------------
   @(#)fp_opw.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp_opw(fis,fil)
Fis *fis;
char *fil;
/*---------------------------------------------------------------------------*/
BEGIN
/* IDENT(fp_opw.c 2.1.1.1 4/10/91); */
int crd;

/* mode */
fis->mod=RW;

/* ouverture os */
FOPEN(fis->pf,fil,"r+",FP_OLD);

/* lecture DE */
RDE(fis,&fis->de);

/* lecture DD */
RDD(fis, fis->dd);

return(FP_FOK);

END


/*---------------------------------------------------------------------------
   @(#)fp_rdi1.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp_rdi1(fis,buf)
Fis *fis;
unsigned char *buf;
/*---------------------------------------------------------------------------*/
BEGIN
/*   IDENT(fp_rdi1.c 2.1.1.1 4/10/91); */
  Atf atf; /* attribut FIS */
  int nob,ob;
  int crd;
  int lpcnt;

  /* determination des attributs du FIS */
  fp0_atf(fis,&atf);


  /* pas de padding */
  IF(atf.pad == 0 )
    nob=atf.nop * atf.mxu * atf.mxv * atf.mxw;
    RDI(fis,&atf,1,1,1,nob,&buf[0]);
  /* padding */
  ELSE
    int dw,dv;
    ob=0;
    SWITCH(atf.blo)
      case U_VW :   nob=atf.nop * atf.mxu;
                    FOR(dw=0; dw<atf.mxw ; dw++ )
                      FOR(dv=0; dv<atf.mxv ; dv++)
                        RDI(fis,&atf,1,1+dv,1+dw,nob,&buf[ob]);
                        ob=ob+nob;
                      END_FOR
                    END_FOR
                    break;

      case UV_W :   nob = atf.nop * atf. mxu * atf.mxv;
                    FOR(dw=0; dw<atf.mxw ; dw++)
                      RDI(fis,&atf,1,1,1+dw,nob,&buf[ob]);
                      ob=ob+nob;
                    END_FOR
                    break;

      case UVW_ :   nob=atf.nop * atf.mxu * atf.mxv * atf.mxw;
                    RDI(fis,&atf,1,1,1,nob,&buf[0]);
                    break;

      case UNBL :   return(FP_ORG);
      default   :   return(FP_INT);
    END_SWITCH
  END_IF

return(FP_FOK);

END

/*---------------------------------------------------------------------------
   @(#)fp_rdi2.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp_rdi2(fis,p,l,c,npf,nlf,ncf,buf)
Fis *fis;
int p,l,c;
int npf,nlf,ncf;
unsigned char buf[];
/*---------------------------------------------------------------------------*/
BEGIN
/*   IDENT(fp_rdi2.c 2.1.1.1 4/10/91); */
  Atf atf;
  int ouf,ovf,owf;
  int nuf,nvf,nwf;
  int ob;
  int nof;
  int dw,dv;
  int crd;

  /* determination des attributs du FIS */
  fp0_atf(fis,&atf);

fis_dump_Atf( stdout, &atf );
/* printf("DBG:fp_rdi2:ENTfp0_oso(atf.ord,p,l,c,&ouf,&ovf,&owf)\n\t%d %d %d %d %d %d %d\n", atf.ord,p,l,c,ouf,ovf,owf);  */

  /* reorganisation selon l'ordonnancement du FIS */
  fp0_oso(atf.ord,p,l,c,&ouf,&ovf,&owf);

/* printf("DBG:fp_rdi2:RTNfp0_oso(atf.ord,p,l,c,&ouf,&ovf,&owf)\n\t%d %d %d %d %d %d %d\n", atf.ord,p,l,c,ouf,ovf,owf);  */
fis_dump_Atf( stdout, &atf );
/* printf("DBG:fp_rdi2:ENTfp0_oso(atf.ord,npf,nlf,ncf,&nuf,&nvf,&nwf)\n\t%d %d %d %d %d %d %d\n",atf.ord,npf,nlf,ncf,nuf,nvf,nwf); */

  fp0_oso(atf.ord,npf,nlf,ncf,&nuf,&nvf,&nwf);

/* printf("DBG:fp_rdi2:RTNfp0_oso(atf.ord,npf,nlf,ncf,&nuf,&nvf,&nwf)\n\t%d %d %d %d %d %d %d\n",atf.ord,npf,nlf,ncf,nuf,nvf,nwf); */
fis_dump_Atf( stdout, &atf );

  /* controle */
printf("\nCTRL(x,n,a,b) if((x<a) || ((x+n-1) > b )|| (n<1)) return(FP_DIM)\n");
/* printf("DBG:fp_rdi2:                          x\tn\ta\tb\n");  */
/* printf("DBG:fp_rdi2: CTRL(ouf,nuf,1,atf.mxu) %d\t%d\t%d\t%d\n");  */
  CTRL(ouf,nuf,1,atf.mxu);
/* printf("DBG:fp_rdi2: CTRL(ovf,nvf,1,atf.mxv) %d\t%d\t%d\t%d\n");  */
  CTRL(ovf,nvf,1,atf.mxv);
/* printf("DBG:fp_rdi2: CTRL(owf,nwf,1,atf.mxw) %d\t%d\t%d\t%d\n");  */
  CTRL(owf,nwf,1,atf.mxw);

  /* nombre d'octets a lire */
  nof=atf.nop * nuf;

  ob=0;
  FOR(dw=0;dw<nwf;dw++)
    FOR(dv=0;dv<nvf;dv++)
      RDI(fis,&atf,ouf,ovf+dv,owf+dw,nof,&buf[ob]);
      ob=ob+nof;
    END_FOR
  END_FOR

return(FP_FOK);

END


/*---------------------------------------------------------------------------
   @(#)fp_rdi3.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
fp_rdi3(fis,p,l,c,npf,nlf,ncf,buf,npb,nlb,ncb)
Fis *fis;
int p,l,c;
int npf,nlf,ncf;
unsigned char buf[];
int npb,nlb,ncb;
/*---------------------------------------------------------------------------*/
BEGIN
/*   IDENT(fp_rdi3.c 2.1.1.1 4/10/91); */
  Atf atf;
  int ouf,ovf,owf;
  int nuf,nvf,nwf;
  int nub,nvb,nwb;
  int nof;
  unsigned char *ubu;
  float aw,bw,av,bv,au,bu;

  int dub,dvb,dwb;
  int uf,vf,wf;
  int *tof;
  int ob,of;
  int doc;
  int crd;

  /* determination des attributs du FIS */
  fp0_atf(fis,&atf);

  /* re-ordonnancement selon ordonnancement du FIS */
  fp0_oso(atf.ord,p,l,c,&ouf,&ovf,&owf);
  fp0_oso(atf.ord,npf,nlf,ncf,&nuf,&nvf,&nwf);
  fp0_oso(atf.ord,npb,nlb,ncb,&nub,&nvb,&nwb);

  /* controle */
  CTRL(ouf,nuf,1,atf.mxu);
  CTRL(ovf,nvf,1,atf.mxv);
  CTRL(owf,nwf,1,atf.mxw);

  /* calcul des coefficients d'echantillonage */
  CCS(ouf,0,nuf,nub,au,bu);
  CCS(ovf,0,nvf,nvb,av,bv);
  CCS(owf,0,nwf,nwb,aw,bw);

  /* allocation dynamique */
  nof=atf.nop * nuf;


  MALLOC(ubu,unsigned char *,nof);
  MALLOC(tof,int *,2 * nub);

  /* pre-calcul des adresses u */
  FOR(dub=0;dub<nub;dub++)
    uf=NINT(au * dub + bu);
    tof[dub]=atf.nop * (uf-ouf);
  END_FOR

  ob=0;
  FOR(dwb=0;dwb<nwb;dwb++)
    wf=NINT(aw * dwb + bw);
    FOR(dvb=0;dvb<nvb;dvb++)
      vf=NINT(av * dvb + bv);
      RDI(fis,&atf,ouf,vf,wf,nof,ubu);
      FOR(dub=0;dub<nub;dub++)
        of=tof[dub];
        for (doc=0;doc<atf.nop;doc++)  buf[ob++]=ubu[of++];
      END_FOR
    END_FOR
  END_FOR

free((char *)ubu); free((char *)tof);
return(FP_FOK);

END

/*---------------------------------------------------------------------------
   @(#)fp_wdi1.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp_wdi1(fis,buf)
Fis *fis;
unsigned char *buf;
/*---------------------------------------------------------------------------*/
BEGIN
/*   IDENT(fp_wdi1.c 2.1.1.1 4/10/91); */
  Atf atf; /* attribut FIS */
  int nob,ob;
  int crd;

  if(fis->mod != RW) return(FP_WRO);

  /* determination des attributs du FIS */
  fp0_atf(fis,&atf);

  /* pas de padding */
  IF(atf.pad == 0 )
    nob=atf.nop * atf.mxu * atf.mxv * atf.mxw;
    WDI(fis,&atf,1,1,1,nob,&buf[0]);

  ELSE
  /* padding */
    int dw,dv;
    ob=0;
    SWITCH(atf.blo)
      case U_VW :   nob=atf.nop * atf.mxu;
                    FOR(dw=0; dw<atf.mxw ; dw++ )
                      FOR(dv=0; dv<atf.mxv ; dv++)
                        WDI(fis,&atf,1,1+dv,1+dw,nob,&buf[ob]);
                        ob=ob+nob;
                      END_FOR
                    END_FOR
                    break;

      case UV_W :   nob = atf.nop * atf. mxu * atf.mxv;
                    FOR(dw=0; dw<atf.mxw ; dw++)
                      WDI(fis,&atf,1,1,1+dw,nob,&buf[ob]);
                      ob=ob+nob;
                    END_FOR
                    break;

      case UVW_ :   nob=atf.nop * atf.mxu * atf.mxv * atf.mxw;
                    WDI(fis,&atf,1,1,1,nob,&buf[0]);
                    break;

      case UNBL :   return(FP_ORG);
      default   :   return(FP_INT);
    END_SWITCH
  END_IF

return(FP_FOK);

END

/*---------------------------------------------------------------------------
   @(#)fp_wdi2.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int fp_wdi2(fis,p,l,c,npf,nlf,ncf,buf)
Fis *fis;
int p,l,c;
int npf,nlf,ncf;
unsigned char buf[];
/*---------------------------------------------------------------------------*/
BEGIN
/*   IDENT(fp_wdi2.c 2.1.1.1 4/10/91); */
  Atf atf;
  int ouf,ovf,owf;
  int nuf,nvf,nwf;
  int ob;
  int nof;
  int dw,dv;
  int crd;

  if(fis->mod != RW) return (FP_WRO);
 
  /* determination des attributs du FIS */
  fp0_atf(fis,&atf);

  /* reorganisation selon l'ordonnancement du FIS */
  fp0_oso(atf.ord,p,l,c,&ouf,&ovf,&owf);
  fp0_oso(atf.ord,npf,nlf,ncf,&nuf,&nvf,&nwf);

  /* controle */
  CTRL(ouf,nuf,1,atf.mxu);
  CTRL(ovf,nvf,1,atf.mxv);
  CTRL(owf,nwf,1,atf.mxw);

  /* nombre d'octets a ecrire */
  nof=atf.nop * nuf;

  ob=0;
  FOR(dw=0;dw<nwf;dw++)
    FOR(dv=0;dv<nvf;dv++)
      WDI(fis,&atf,ouf,ovf+dv,owf+dw,nof,&buf[ob]);
      ob=ob+nof;
    END_FOR
  END_FOR

return(FP_FOK);

END

/*---------------------------------------------------------------------------
   @(#)fp_wdi3.c version 2.1.1.1  4/10/91

   Gerard LEGENDRE  SCEM/CMS  B.P.147  F-22302 LANNION CEDEX
  ---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
fp_wdi3(fis,p,l,c,npf,nlf,ncf,buf,npb,nlb,ncb)
Fis *fis;
int p,l,c;
int npf,nlf,ncf;
unsigned char buf[];
int npb,nlb,ncb;
/*---------------------------------------------------------------------------*/
BEGIN
/*   IDENT(fp_wdi3.c 2.1.1.1 4/10/91); */
  Atf atf;
  int ouf,ovf,owf;
  int nuf,nvf,nwf;
  int nub,nvb,nwb;
  int nof;
  unsigned char *ubu;
  float aw,bw,av,bv,au,bu;
  int duf,dvf,dwf;
  int ub,vb,wb;
  int uf,vf,wf;
  int *dob;
  int ob,of;
  int obv;
  int doc;
  int crd;

  if(fis->mod != RW) return(FP_WRO);

  /* determination des attributs du FIS */
  fp0_atf(fis,&atf);

  /* re-ordonnancement selon ordonnancement du FIS */
  fp0_oso(atf.ord,p,l,c,&ouf,&ovf,&owf);
  fp0_oso(atf.ord,npf,nlf,ncf,&nuf,&nvf,&nwf);
  fp0_oso(atf.ord,npb,nlb,ncb,&nub,&nvb,&nwb);

  /* controle */
  CTRL(ouf,nuf,1,atf.mxu);
  CTRL(ovf,nvf,1,atf.mxv);
  CTRL(owf,nwf,1,atf.mxw);

  /* calcul des coefficients d'echantillonage */
  CCS(0,ouf,nub,nuf,au,bu);
  CCS(0,ovf,nvb,nvf,av,bv);
  CCS(0,owf,nwb,nwf,aw,bw);

  /* nombre d'octets a ecrire */
  nof=atf.nop * nuf;

  /* allocation dynamique */
  MALLOC(ubu,unsigned char *,nof);
  MALLOC(dob,int *,2 * nuf);

  /* calcul des adresses u */
  FOR(duf=0;duf<nuf;duf++)
    uf=ouf + duf;
    ub=NINT(au * uf + bu);
    dob[duf]=atf.nop * ub;
  END_FOR

  FOR(dwf=0;dwf<nwf;dwf++)
    wf=owf + dwf;
    wb=NINT(aw * wf + bw);
    FOR(dvf=0;dvf<nvf;dvf++)
      vf=ovf + dvf;
      vb=NINT(av * vf + bv);
      of=0;
      obv= ((wb * nub * nvb) + (vb * nub)) * atf.nop;
      FOR(duf=0;duf<nuf;duf++)
        ob=obv + dob[duf];
        for (doc=0;doc<atf.nop;doc++) ubu[of++]=buf[ob++];
      END_FOR
      WDI(fis,&atf,ouf,vf,wf,nof,ubu);
    END_FOR
  END_FOR

free((char *)ubu); free((char *)dob);
return(FP_FOK);

END
