/* Interface routines : Satview Satellite Image (SI) format into S. */

#include <si.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>

#ifndef NULL
#define NULL 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 1
#endif

SatImageHdr *in_header;
void print_si_header();

/* Test for file existance */
void exists(path, number, result)
char **path;
long *number, *result;
{
   int i;
   struct stat buf;

   for (i = 0; i < *number; i++) {
     result[i] = (long) stat(path[i], &buf);
   }

} 

/* Open a file, find the number of rows and columns.
   Used to allocate the space within S. */
void my_open(file, nscans, samps_per_scan, day, hour, sensor, bad_value)
char **file;
long *nscans, *samps_per_scan, *day, *hour, *sensor;
float *bad_value;

{
   if(!(in_header = si_open(*file, 0)))
      {
      fprintf(stderr, "Error in SI utilities: can't open %s", *file);
      return;
      }
   *file = strstr(*file, "GOES");
   *nscans = in_header->num_scans;
   *samps_per_scan = in_header->samps_per_scan;
   *day = in_header->day;
   *hour = in_header->time;
   *sensor = in_header->parameter;
   *bad_value = in_header->bad_value;

   return;
}

void my_close()
{
   si_close(in_header);
   return;
}

void read_si_image(value, lons, lats, nrows, ncols, get_positions)
float *value, *lons, *lats;
long *nrows, *ncols, *get_positions;
{
   float *tmp_val, *tmp_lat, *tmp_lon; 
   int i;

   tmp_val = value; tmp_lat = lats; tmp_lon = lons;
   for(i = 0; i < (int)*nrows; i++) {
      if(*get_positions) {
         si_read_scanline(in_header, tmp_val, tmp_lat, tmp_lon, NULL, NULL);
         tmp_val += *ncols; tmp_lat += *ncols, tmp_lon += *ncols;
      } 
      else {
	 si_read_scanline(in_header, tmp_val, NULL, NULL, NULL, NULL);
	 tmp_val += *ncols;
      }
   }
   return;
}


void write_old_si_image(file, data, comment)
float *data;
char **file, **comment;
{
   SatImageHdr *out_header;
   int i;

/* Use old header, allowing for changes in comment and writing of floats */
   out_header = si_dup_header(in_header, NULL);
   out_header->data_type = FLOAT;
   if(strlen(*comment)) {
      out_header->comment = *comment;
      out_header->comment_len = strlen(*comment);
   }
   si_write_header(*file, out_header, FALSE);

/* Write out data (without lats and lons) */
   for (i = 0; i < out_header->num_scans; i++) {
      si_write_scanline(out_header, data, NULL, NULL, NULL, NULL);
      data += out_header->samps_per_scan;
   }
   si_close(out_header);
   return;
      
}

void write_new_si_image(file, data, lats, lons, nrows, ncols, comment, 
			bad_value)
char **file, **comment;
float *data, *lats, *lons, *bad_value;
long *nrows, *ncols;

{
   SatImageHdr *out_header;
   char *ll_file;
   int i;

/* Create header, include lat-lon file name */
   ll_file = (char *) malloc(strlen(*file)+4),
   sprintf(ll_file, "%s.ll", *file);

/* This should be done by si_create_header, but I can't get that to work */
/* Create header and fill in */
   out_header = (SatImageHdr *) malloc(sizeof(SatImageHdr));
   out_header->bad_value = *bad_value;
   out_header->num_scans = (int)*nrows;
   out_header->samps_per_scan = (int)*ncols;
   out_header->lat_lon_filename = (char *) malloc(strlen(ll_file)+1);
   strcpy(out_header->lat_lon_filename, ll_file);
   out_header->lat_lon_filename_len = strlen(ll_file);
   if(*comment){
     out_header->comment = (char *) malloc(strlen(comment)+1);
     strcpy(out_header->comment, comment);
     out_header->comment_len = strlen(comment);
   }
   out_header->data_type = FLOAT; 
   out_header->time_flag = FALSE;
   out_header->private = NULL;
   out_header->private_size = 0;
   out_header->year = 0;
   out_header->month = 0;
   out_header->day = 0;
   out_header->time = 0.0;
   for(i=0; i<NUM_DUM; i++)
     out_header->dum[i] = 0;
   out_header->id = 0;
   out_header->parameter = 0;
   out_header->min = 0.0;
   out_header->max = 0.0;
/* The end of header duplication */

   si_write_header(*file, out_header, TRUE);

/* Write out data, lats, and lons */
   for (i = 0; i < *nrows; i++) {
      si_write_scanline(out_header, data, lats, lons, NULL, NULL);
      data += *ncols;
      lats += *ncols;
      lons += *ncols;
   }
   si_close(out_header);
   return;
      
}

void print_si_header(header)
SatImageHdr *header;
{
   printf("Contents of si header:\n");
   printf("Id\t%s \tSize:\t%d\n", header->id_string, header->header_size);
   printf("Version\t%d \tSat id\t%d\n", header->version, header->id);
   printf("Time\t%f\ttimeflag %d\n", header->time, header->time_flag);
   printf("Year\t%d\tmonth\t%d\tday\t%d\n", header->year, header->month, header->day);
   printf("Parameter\t%d\tmin\t%f\tmax\t%f\n", header->parameter, header->min, header->max);
   printf("Bad value\t %f\n", header->bad_value);
   printf("Lat-lon file length\t%d\tnscans\t%d\tnsamps\t%d\n", header->lat_lon_filename_len, 
	   header->num_scans, header->samps_per_scan);
   printf("Comment length\t%d\tPrivate size\t%d\n", header->comment_len, header->private_size);
   printf("Data type\t%d\tIndex size\t%d\n", header->data_type, header->index_size);
   printf("Lat-lon file name:\t%s\n", header->lat_lon_filename);
   printf("Comment:\t%s\n", header->comment);
   return;
}
