/*++
 *  D E S C R I P T I O N
******	Header structure for picture files forming part of the Ericsson radar
*	software system taken from section 2 of manual H/SI 6239 REV 4-890209
*
*	On investigation of some of the PIC files it was found that the
*	parameter in the header that indicates the number of pixels in
*	the vertical (NV below) does not indicate how images are actually
*	stored in the PIC file. Another parameter in the header (PIC_FIL_TYP
*	below) indicates what sort of volume the PIC file is.
*	
*/
struct ewis_header_horiz
{
  SINT4	 hei_nr;         /* Height level number (-1=pseudocappi) */
  FLT4	 hei;            /* Height in km of plane */
  char   acc_start[12];  /* Accumulation start yymmddhhmmss */
  SINT4  sys_time[2];    /* Set to zero by IRIS */
  FLT4	 acc_int;        /* Accumulation time (secs) */
  FLT4	 acc_lost;	 /* Lost accumulation time */
  FLT4	 fcst_spd;	 /* Forecast speed of precip */
  FLT4	 fcst_dir;       /* Direction in which moving */
  FLT4	 fcst_cor;       /* Forecast correlation 0-1 */
  FLT4	 cappi_range;    /* Range of cappi at this level */
  FLT4	 threshold;      /* Threshold e.g. for echo top */
  UINT1	 store_type;     /* Indicates how data following header */
  /* Should be interpreted (0=fixed pt 0-255, 1=threshold numbers 1-15) */
  char   ipad57x3[3];
  FLT4   thresh[16];     /* 16 thresholds corresp to 0-15 */
};
struct ewis_header_horiz_file
{
  UINT1	 hei_nr[4];      /* (int) */
  UINT1	 hei[4];         /* (float)*/
  char   ACC_START[12]; 
  UINT1  sys_time[2][4];    
  UINT1	 acc_int[4];     /* (float)*/
  UINT1	 acc_lost[4];	 /* (float) */
  UINT1	 fcst_spd[4];	 /* (float) */
  UINT1	 fcst_dir[4];    /* (float) */
  UINT1	 fcst_cor[4];    /* (float) */
  UINT1	 cappi_range[4]; /* (float) */
  UINT1	 threshold[4];   /* (float) */
  UINT1	 store_type;     
  UINT1  thresh[16][4];  /* (float) */
};
#define EWIS_XYZ_MAX_SLICES  16  /* The maximum number of slices in vertical direction */
struct ewis_header_xyz_volume
{
  SINT4	 block_no[EWIS_XYZ_MAX_SLICES+1];   /* Block numbers for start of each level */
  FLT4	 pic_level[EWIS_XYZ_MAX_SLICES+1];  /* Height in km of each level (-1=pseudo-cappi) */
};
struct ewis_header_xyz_volume_file
{
  UINT1	 block_no[EWIS_XYZ_MAX_SLICES+1][4];   /* int */
  UINT1	 pic_level[EWIS_XYZ_MAX_SLICES+1][4];  /* float */
};

#define EWIS_POLAR_MAX_SWEEPS 20
struct ewis_header_polar_volume
{
  SINT4	 elev_uppb;            /* Number of elevation sweeps */
  FLT4	 elev[EWIS_POLAR_MAX_SWEEPS]; /* Actual elevations in degrees */
  SINT4  azim_uppb;            /* Number of azimuths in a sweep, minus 1 */
  SINT4  range_uppb;           /* Number of range bins */
  SINT4  range_lim[3];         /* Limits in pixels for different range gates. */
  FLT4	 range_siz[3];         /* Size of range gates. */
  SINT4  scan_size;            /* Size of each range scan, data follows after a header 
				  with a time stamp. */
  SINT4  elev_block_nr[EWIS_POLAR_MAX_SWEEPS]; /* Polar volume, block in file for each elev'ns.*/
};
struct ewis_header_polar_volume_file
{
  UINT1	 elev_uppb[4];         /* int */
  UINT1  elev[EWIS_POLAR_MAX_SWEEPS][4]; /* float */
  UINT1  azim_uppb[4];         /* int */
  UINT1  range_uppb[4];        /* int */
  UINT1  range_lim[3][4];      /* int */
  UINT1  range_siz[3][4];      /* float */
  UINT1  scan_size[4];         /* int  */
  UINT1  elev_block_nr[EWIS_POLAR_MAX_SWEEPS][4]; /* int*/
};


struct ewis_header_vertical
{
  SINT4 north_nr;           /* Coordinate relative radar for orthogonal section */
  SINT4 east_nr;
  FLT4  hei_level[16];	    /* height levels in km, first is -1 pseudo-cappi level,
			     * normally not used. */
  SINT4 north_nr_l;	    /* Coordinates relative radar in km for oblique section*/
  SINT4 east_nr_l; 
  SINT4	north_nr_2;
  SINT4 east_nr_2;
};

union ewis_header_type_file
{
  struct ewis_header_horiz_file        horiz;
  struct ewis_header_xyz_volume_file   xyz;
  struct ewis_header_polar_volume_file polar;
};
union ewis_header_type
{
  struct ewis_header_horiz        horiz;
  struct ewis_header_xyz_volume   xyz;
  struct ewis_header_polar_volume polar;
};

struct ewis_header_file
{
  char	ident[8];	 /* ALWAYS 'PMERAWIS' */
  UINT1 header_len[4];   /* Header length in blocks (int) */
  char  date_time[12]; 
  SINT4 sys_time[2];
  UINT1	scale; 
  UINT1 pic_type;        /* Picture type */
  UINT1 quant;           /* Data type */
  UINT1 weath;           /* Type of weather */
  char  comment1[10];    /* First part of comment (written on display) */
  char  comment2[30];    /* second part of comment */
  char  signature[2];    /* initials of operator */
  char	place[20];       /* radar site (1st 6 written on display) */
  UINT1	geo_lat[4];      /* lat of picture centre (float)*/
  UINT1 geo_lon[4];      /* Lon of picture centre (float)*/
  UINT1 radar_x[4];      /* Displacement of pic centre km (float)*/
  UINT1 radar_y[4];      /* Displacement of pic centre km (float)*/
  UINT1	radar_type;      /* Values undefined */
  UINT1 ne[4];           /* No of pixels in east direction (int) */
  UINT1 e_inc[4];        /* E pixel spacing in km (float)*/
  UINT1 nn[4];           /* No of pixels in north direction (int)*/
  UINT1 n_inc[4];        /* N pixel spacing in km (float)*/
  UINT1 nv[4];           /* No of pixels in vertical (min index -1)(int) */
  UINT1 v_inc[4];        /* Mean pixel spacing in vertical (km) (float)*/
  UINT1	pixel_cnt[4];    /* (int)*/
  UINT1	store_min[4];    /* Smallest possible stored value (int)*/
  UINT1	store_max[4];    /* Largest possible stored value (int)*/
  UINT1	store_slope[4];  /* Conversion to physical value (float)*/
  UINT1 store_ord[4];    /* Conversion to physical value (float)*/
  UINT1 store_offset[4]; /* Number to subtract to get 0 (int)*/
  UINT1	store_bits[4];   /* Number of bits per datum (int)*/
  UINT1	store_align[4];  /* Number of allocated bits per datum (int)*/
  char  store_quant[8];  /* Physical quantity */
  UINT1	compressed;      /* True if data compressed  */
  UINT1 rain_const1[4];  /* A IN Z=A*R**B (float)*/
  UINT1	rain_const2[4];  /* B IN Z=A*R**B (float)*/
  UINT1 geo_height[4];   /* (float)*/
  UINT1	map_proj[4];     /* Map projection (0=radar,1=lambert) (int)*/
  UINT1	sub_proj[4];     /* Map sub-projection (spain???) (int)*/
  UINT1	map_cen_e[4];    /* East coord in map to pic cen (float)*/
  UINT1	map_cen_n[4];    /* North coord in map to pic cen (float)*/
  UINT1 e_const[5][4];   /* E'=-E0+C1*E+C2*N + C3*E*E + C4*E*N + C5*N*N (float)*/
  UINT1	n_const[5][4];   /* N'=-N0+C1*N+C2*E + C3*N*N + C4*E*N + C5*E*E (float)*/
  UINT1 volume_params[2]; /* short */
  UINT1	fill[29];
  UINT1	pic_fil_typ;     /* Picture File Type */

  union ewis_header_type_file Type;
  char ipad480x32[32];
};

/* Longword aligned version */
struct ewis_header
{
  char	ident[8];	/* ALWAYS 'PMERAWIS' */
  SINT4 header_len;     /* Header length in blocks */
  char  date_time[12];  /* YYMMDDhhmm (time in UTC)*/
  SINT4 sys_time[2];    /* as above in system time format, set to zero from IRIS */
  UINT1	scale; 
#define EWIS_SCALE_120KM_BY1  0
#define EWIS_SCALE_240KM_BY2  1
#define EWIS_SCALE_480KM_BY2  2
#define EWIS_SCALE_240KM_BY1  3
#define EWIS_SCALE_960KM_BY4  4
#define EWIS_SCALE_1920KM_BY8 5
#define EWIS_SCALE_480KM_BY4  6
#define EWIS_SCALE_960KM_BY2  7
#define EWIS_SCALE_1920KM_BY4 8
#define EWIS_SCALE_120KM_BY05 9
#define EWIS_SCALE_60KM_BY025 10
  UINT1 pic_type;       /* Picture type */
#define EWIS_PICTYPE_NONE 0
#define EWIS_PICTYPE_CAPPI 1
#define EWIS_PICTYPE_MAX 2
#define EWIS_PICTYPE_HEIGHT_MAX 3
#define EWIS_PICTYPE_ECHO_TOP 4
#define EWIS_PICTYPE_ACCUM 5           /* User defined interval */
#define EWIS_PICTYPE_ACCUM_HOURLY 12
  /*
    !	6 ACCUM, TIME 18-24
    !	7 ACCUM, TIME 12-18
    !	8 ACCUM, TIME  6-12
    !	9 ACCUM, TIME  0- 6
    !	10 ACCUM, DAY 7-7
    !	11 ACCUM, DAY 0-0
    !	13 FORECAST
    !	14 REGIONAL COMPOSITE RAIN
    !	15 REGIONAL COMPOSITE ETOP
    !	16 NATIONAL COMPOSITE RADAR+SAT RAIN
    !	17 NATIONAL COMPOSITE RADAR+SAT ETOP
    !	18 NATIONAL COMPOSITE RADAR RAIN
    !	19 NATIONAL COMPOSITE RADAR ETOP */

  UINT1 quant;          /* Data type */
#define EWIS_DTYPE_NONE         0
#define EWIS_DTYPE_REFLECTIVITY 1
#define EWIS_DTYPE_RAINRATE     2
#define EWIS_DTYPE_VELOCITY     3
#define EWIS_DTYPE_WIDTH        4
#define EWIS_DTYPE_HEIGHT_REF   5
#define EWIS_DTYPE_HEIGHT_VEL   6
#define EWIS_DTYPE_HEIGHT_WID   7
#define EWIS_DTYPE_RAIN_ACC     8
#define EWIS_DTYPE_HEIGHT       9
#define EWIS_DTYPE_VPI          10  /* Unused */
#define EWIS_DTYPE_HORZ_REFL    11
#define EWIS_DTYPE_ZDR          12
#define EWIS_DTYPE_LIQUID       13

  UINT1 weath;          /* Type of weather */
  /*
    !	0   DRIZZLE
    !	1   RAIN
    !	2   SHOWERS
    !	3   DRY SNOW
    !	4   WET SNOW */
  char  comment1[10];   /* First part of comment (written on display) */
  char  comment2[30];   /* second part of comment */
  char  signature[2];   /* initials of operator */
  char	place[20];      /* radar site (1st 6 written on display) */
  UINT1	radar_type;     /* Type of radar */
#define EWIS_RADAR_TYPE_NONDOP  0
#define EWIS_RADAR_TYPE_DOPPLER 1
  UINT1	compressed;     /* True if data compressed  */
  FLT4	geo_lat;        /* lat of picture centre */
  FLT4	geo_lon;        /* Lon of picture centre */
  FLT4	radar_x;        /* Displacement of pic centre km */
  FLT4	radar_y;        /* Displacement of pic centre km */
  SINT4	ne;             /* No of pixels in east direction */
  FLT4	e_inc;	        /* E pixel spacing in km */
  SINT4	nn;             /* No of pixels in north direction */
  FLT4	n_inc;          /* N pixel spacing in km */
  SINT4	nv;             /* No of pixels in vertical (min index -1) */
  FLT4	v_inc;	        /* Mean pixel spacing in vertical (km) */
  SINT4	pixel_cnt;      /* No of bytes following the header, rounded up to the next 512 */
  SINT4	store_min;      /* Smallest possible stored value */
  SINT4	store_max;      /* Largest possible stored value */
  FLT4	store_slope;    /* Conversion to physical value */
  FLT4	store_ord;      /* Conversion to physical value */
  SINT4	store_offset;   /* Number to subtract to get 0 */
  SINT4	store_bits;     /* Number of bits per datum */
  SINT4	store_align;    /* Number of allocated bits per datum */
  char  store_quant[8]; /* Physical quantity */
  FLT4	rain_const1;    /* A IN Z=A*R**B */
  FLT4	rain_const2;    /* B IN Z=A*R**B */
  FLT4	geo_height;     /* Height of radar above sea level in km */
  SINT4	map_proj;       /* Map projection (0=radar,1=lambert) */
  SINT4	sub_proj;       /* Map sub-projection (spain???) */
  FLT4	map_cen_e;      /* East coord in map to pic cen */
  FLT4	map_cen_n;      /* North coord in map to pic cen */
  FLT4	e_const[5];     /* E'=-E0+C1*E+C2*N + C3*E*E + C4*E*N + C5*N*N */
  FLT4	n_const[5];     /* N'=-N0+C1*N+C2*E + C3*N*N + C4*E*N + C5*E*E */
  UINT2 volume_params;
  UINT1	fill[29];
  UINT1	pic_fil_typ;    /* Picture File Type */
#define EWIS_PIC_FIL_POLAR_VOLUME  0
#define EWIS_PIC_FIL_XYZ_VOLUME    1
#define EWIS_PIC_FIL_HORIZ_PICTURE 2
#define EWIS_PIC_FIL_VERT_PICTURE  3
#define EWIS_PIC_FIL_COLOR_IMAGE   4
#define EWIS_PIC_FIL_VERT_OBLIQUE_PICTURE 5
#define EWIS_PIC_FIL_UNKNOWN       255

  union ewis_header_type Type;
};

/*
!The following array describes what I think the number of images is per
!PIC file as at 8-APR-1991 (Peter Dini).
!A value of -1 indicates that the values of NV, as described in the
!Ericsson header above, should be used.
!
	INTEGER	ERICSSON_NUMBER_IMAGES(0:5)/
     -		  -1,	  !0 Polar volume		      1021 BLOCKS
     -		  -1,	  !1 XYZ volume			      1483   "
     -		   1,	  !2 HORIZ PICTURE		       114   "
     -		   1,	  !3 VERT PICTURE (PERPENDICULAR)       13   "
     -		   1,	  !4 COLOUR DISPLAY IMAGE		 ?   "
     -		   1/	  !5 VERT PICTURE (OBLIQUE)	         ?   "
*/

/* Table used to map both ways between IRIS sitenames and HDF5 Places.  
 * Wild cards are allowed for each input, the
 * list is searched in order.  The first match wins.
 */
struct convert_site_table
{
  char  sIrisSiteName16[20];   /* IRIS Sitename, null terminated */
  char  sEwisPlace20[24];      /* EWIS Place name, null terminated */
};


#ifdef INCLUDE_STDIO_PROTOTYPES
#ifdef SIGMET_USER_LIB_H
#define MAX_PIPE_SITES 50
/* Information from the config file to customize the convertor */
struct convert_config
{
  char sVersion[8];
  struct convert_site_table Sites[MAX_PIPE_SITES];  /* site list */
  struct sig_logging_spec logging;
  UINT4  lLatitudeFirst;              /* Flag to indicate lat/lon order */
  FLT4   fPolarRangeRes;              /* Override range resolution to this value */
  SINT4  iPolarBinCount;              /* Override range bin out count */
  FLT4   fDopPolarRangeRes;           /* Different numbers for Doppler data */
  SINT4  iDopPolarBinCount;
};
#endif  /* #ifdef SIGMET_USER_LIB_H */
#endif  /* #ifdef INCLUDE_STDIO_PROTOTYPES */

/* =============================================
 * Function prototypes for shared functions 
 */
void HeaderFromFile        /* File: FileConvert.C */
( struct ewis_header *pHeader_a,  /* Strings are byte swapped correctly */
  const struct ewis_header_file *pHeaderFile_a );

void FileFromHeader        /* File: FileConvert.C */
( struct ewis_header_file *pHeaderFile_a,
  const struct ewis_header *pHeader_a );

MESSAGE LoadSetup          /* File: Load.C */
( const char *sRemoteHostname_a,
  struct convert_config *pCSetup_a );

