Exemplo n.º 1
0
int _marx_get_parameters (Param_File_Type *p, _Marx_Parameter_Table_Type *table) /*{{{*/
{
   char *name;
   int status;
   char buf[PF_MAX_LINE_LEN];

   while ((name = table->name) != NULL)
     {
	switch (table->type)
	  {
	   case PF_INTEGER_TYPE:
	     status = pf_get_integer (p, name, table->value);
	     break;

	   case PF_REAL_TYPE:
	     status = pf_get_double (p, name, table->value);
	     break;

	   case PF_BOOLEAN_TYPE:
	     status = pf_get_boolean (p, name, table->value);
	     break;

	   case PF_FILE_TYPE:
	     /* drop */
	   case PF_STRING_TYPE:
	     if (table->type == PF_STRING_TYPE)
	       status = pf_get_string (p, name, buf);
	     else status = pf_get_file (p, name, buf);

	     if (status != -1)
	       {
		  char *s = (char *) SLMALLOC (strlen (buf) + 1);
		  if (s == NULL)
		    {
		       marx_error ("Malloc error.");
		       return -1;
		    }
		  strcpy (s, buf);
		  *((char **)table->value) = s;
	       }
	     break;

	   default:
	     marx_error ("Unknown type");
	     return -1;
	  }

	if (status == -1)
	  {
	     marx_error ("Requested parameter %s not found in parameter file.",
			 name);
	     return -1;
	  }

	table++;
     }
   return 0;
}
Exemplo n.º 2
0
SLFUTURE_VOID *SLdebug_malloc (unsigned long n)
{
   char *p;

   if ((p = (char *) SLMALLOC (n + 2 * Chunk)) == NULL) return NULL;

   fixup ((unsigned char *) p, n, "MALLOC");
   return (SLFUTURE_VOID *)(p + Chunk);
}
Exemplo n.º 3
0
void *_JDMmalloc (unsigned long nbytes, char *err)
{
   void *p;

   p = SLMALLOC (nbytes);
   if (p == NULL)
     {
	JDMath_Error = JDMATH_MALLOC_ERROR;
	JDMmsg_error (err);
     }

   return p;
}
Exemplo n.º 4
0
int JDMread_column_fdata (char *file, float **data, int *cindex, unsigned int n, unsigned int *num_read)
{
   char buf[512];
   unsigned int space = 1024;
   unsigned int nread = 0;
   int do_close;
   unsigned int i;
#define MAX_COLUMNS 20
   float dum[MAX_COLUMNS];
   FILE *fp;

   *num_read = 0;
   if (n > MAX_COLUMNS)
     {
	JDMath_Error = JDMATH_INVALID_PARAMETER;
	JDMmsg_error ("JDMread_column_fdata");
	return -1;
     }

   if (file == NULL)
     {
	fp = stdin;
	do_close = 0;
	file = "<stdin>";
     }
   else
     {
	if (NULL == (fp = fopen (file, "r")))
	  {
	     JDMath_Error = JDMATH_FILE_OPEN_ERROR;
	     JDMmsg_error2 ("JDMread_column_fdata", file);
	     return -1;
	  }
	do_close = 1;
     }

   /* initialize everything to NULL */
   for (i = 0; i < n; i++) data[i] = NULL;

   for (i = 0; i < n; i++)
     {
	float *ptr;
	if (cindex[i] == 0) continue;
	ptr = (float *) SLMALLOC (space * sizeof (float));
	if (ptr == NULL) goto return_error;
	data[i] = ptr;
     }

   while (NULL != fgets (buf, sizeof (buf) - 1, fp))
     {
	if ((int)n > sscanf (buf, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
			dum+0, dum+1, dum+2, dum+3, dum+4,
			dum+5, dum+6, dum+7, dum+8, dum+9,
			dum+10, dum+11, dum+12, dum+13, dum+14,
			dum+15, dum+16, dum+17, dum+18, dum+19
			))
	  continue;

	if (nread == space)
	  {
	     space += 1024;
	     for (i = 0; i < n; i++)
	       {
		  float *ptr;
		  if (cindex[i] == 0) continue;
		  ptr = (float *) SLREALLOC (data[i], space * sizeof (float));
		  if (ptr == NULL) goto return_error;
		  data[i] = ptr;
	       }
	  }

	for (i = 0; i < n; i++)
	  {
	     if (cindex[i] == 0) continue;
	     data[i][nread] = dum[i];
	  }

	nread++;
     }

   if (nread == 0)
     {
	JDMath_Error = JDMATH_CORRUPT_FILE_ERROR;
	JDMmsg_error2 ("JDMread_column_fdata", file);
	goto read_error;
     }

   if (file != NULL) fclose (fp);
   *num_read = nread;
   return 0;

   return_error:
   JDMath_Error = JDMATH_MALLOC_ERROR;

   read_error:

   if (do_close) fclose (fp);

   JDMmsg_error2 ("JDMread_column_fdata", file);

   for (i = 0; i < n; i++)
     {
	if (data[i] != NULL) SLFREE (data[i]);
     }
   return -1;
}
Exemplo n.º 5
0
int jdfits_bintable_parse_headers (JDFits_Type *ft) /*{{{*/
{
   JDFits_Header_Type *h = NULL;
   JDFits_Keyword_Type *kwt = NULL;
   JDFits_Bintable_Type *bt = NULL;
   JDFits_Bintable_Field_Type *bft = NULL;
   int tfields, i;

   /* Check to make sure this is a bintable */

   if ((ft == NULL) || (NULL == (h = ft->header)))
     {
	jdfits_error ("jdfits_bintable_parse_headers: header is NULL.");
	return -1;
     }

   if (h->type == JDFITS_EXTENSION_HEADER)
     {
	if ((h->name != NULL) && !strcmp ((char *) h->name, "BINTABLE"))
	  h->type = JDFITS_BINTABLE_HEADER;
     }

   if (h->type != JDFITS_BINTABLE_HEADER)
     {
	jdfits_error ("jdfits_bintable_parse_headers: Not a BINTABLE header.");
	return -1;
     }

   /* Check to make sure the header meets the other basic requirements. */
   if ((h->bitpix != 8)
       || (h->naxis != 2)
       || (h->gcount != 1))
     {
	jdfits_error ("jdfits_bintable_parse_headers: Header does not meet bintable requirements.");
	return -1;
     }

   if (h->ext.bintable != NULL)
     return 0;			       /* already parsed */

   /* Now look for the tfields header. */
   if ((NULL == (kwt = jdfits_parse_keyword (h, "TFIELDS", JDFITS_INT_TYPE)))
       || ((tfields = kwt->v.ival) < 0) || (tfields > 999))
     {
	jdfits_error ("jdfits_bintable_parse_headers: Bad or missing TFIELDS entry.");
	goto free_error;
     }

   if (NULL == (bt = (JDFits_Bintable_Type *) SLMALLOC (sizeof (JDFits_Bintable_Type))))
     {
	jdfits_error ("jdfits_bintable_parse_headers: Malloc Error.");
	goto free_error;
     }

   memset ((char *) bt, 0, sizeof (JDFits_Bintable_Type));

   if (0 != tfields)
     {
	bft = (JDFits_Bintable_Field_Type *) SLCALLOC (tfields, sizeof (JDFits_Bintable_Field_Type));
	if (bft == NULL)
	  {
	     jdfits_error ("jdfits_bintable_parse_headers: Calloc Error.");
	     goto free_error;
	  }
     }

   bt->finfo = bft;
   bt->tfields = tfields;

   if (NULL != (kwt = jdfits_parse_keyword (h, "EXTNAME", JDFITS_STRING_TYPE)))
     bt->extname = (char *) kwt->v.sval;

   for (i = 1; i <= tfields; i++)
     {
	char buf[9], *b;
	unsigned char ch;
	unsigned int repeat, type, size;
	char *fmt, *default_format;

	JDFits_Bintable_Field_Type *bfti = bft + (i - 1);

	sprintf (buf, "TFORM%d", i);

	kwt = jdfits_parse_keyword (h, buf, JDFITS_STRING_TYPE);
	if (kwt == NULL)
	  {
	     jdfits_error ("jdfits_bintable_parse_headers: Bad or missing %s entry.", buf);
	     goto free_error;
	  }

	b = (char *) kwt->v.sval;
	ch = *b;

	/* Some files produced by ftools use spaces like:  "  1J  " */
	while (ch == ' ')
	  {
	     b++;
	     ch = *b;
	  }

	if (isdigit (ch) == 0)
	  repeat = 1;
	else
	  {
	     repeat = 0;
	     while (((ch = (unsigned char) *b) != 0) && isdigit (ch))
	       {
		  repeat = 10 * repeat + (ch - '0');
		  b++;
	       }
	  }

	bfti->repeat = repeat;

	default_format = "% 16.9E";

	type = 0;
	pointer_type_label:

	switch (ch)
	  {
	   case 'L':
	     type |= JDFITS_BOOL_TYPE;
	     size = 8;
	     fmt = "%d";
	     break;
	   case 'X':
	     switch (repeat)
	       {
		  /* FIXME: I need to use hex formats, but that requires
		   * proper parsing of the format string in the dump function.
		   */
		case 8:
		  type |= JDFITS_BYTE_TYPE;
		  bfti->repeat = 1;
		  size = 8;
		  fmt = "%d";
		  break;
		case 16:
		  type |= JDFITS_INT16_TYPE;
		  bfti->repeat = 1;
		  size = 16;
		  fmt = JDFITS_FMT_16;
		  break;
		case 32:
		  type |= JDFITS_INT32_TYPE;
		  bfti->repeat = 1;
		  size = 32;
		  fmt = JDFITS_FMT_32;
		  break;

		default:
		  type |= JDFITS_BIT_TYPE;
		  size = 1;
		  fmt = "%X";
		  break;
	       }
	     break;
	   case 'B':
	     type |= JDFITS_BYTE_TYPE;
	     size = 8;
	     fmt = "%c";
	     break;
	   case 'I':
	     type |= JDFITS_INT16_TYPE;
	     size = 16;
	     fmt = JDFITS_FMT_16;
	     break;

	   case 'P':		       /* variable length array */
	     if (repeat > 1)
	       {
		  jdfits_error ("jdfits_bintable_parse_headers: The repeat count must be 0 or 1 for P type.");
		  goto free_error;
	       }
	     if (repeat) repeat = 2;
	     bfti->repeat = repeat;
	     b++;
	     ch = *b;
	     type = JDFITS_POINTER_FLAG;
	     goto pointer_type_label;

	   case 'J':
	     type |= JDFITS_INT32_TYPE;
	     size = 32;
	     fmt = JDFITS_FMT_32;
	     break;
	   case 'A':
	     type |= JDFITS_STRING_TYPE;
	     size = 8;
	     fmt = "%s";
	     break;
	   case 'E':
	     type |= JDFITS_FLOAT32_TYPE;
	     fmt = "% 15.7E";
	     size = 32;
	     break;
	   case 'D':
	     type |= JDFITS_FLOAT64_TYPE;
	     size = 64;
	     fmt = "% 16.9E";
	     break;

	   case 0:
	     ch = '0';
	     /* drop */
	   case 'C':		       /* complex */
	   case 'M':		       /* double precision complex */
	   default:
	     jdfits_error ("jdfits_bintable_parse_headers: Field type '%c' is not implemented. (%s)",
			 ch, buf);
	     goto free_error;
	  }
	bfti->size = size;
	bfti->type = type;

	/* Now pick up the remaining indexed keywords. */

	if (NULL == (kwt = bt_parse_index_kw (h, "TTYPE", i, JDFITS_STRING_TYPE)))
	  bfti->ttype = NULL;
	else bfti->ttype = kwt->v.sval;

	if (NULL == (kwt = bt_parse_index_kw (h, "TUNIT", i, JDFITS_STRING_TYPE)))
	  bfti->tunit = NULL;
	else bfti->tunit = kwt->v.sval;

	if (NULL == (kwt = bt_parse_index_kw (h, "TDIM", i, JDFITS_STRING_TYPE)))
	  bfti->tdim = NULL;
	else bfti->tdim = kwt->v.sval;

	if (NULL == (kwt = bt_parse_index_kw (h, "TNULL", i, JDFITS_INT_TYPE)))
	  bfti->tnull = 0;
	else bfti->tnull = kwt->v.ival;

	bfti->has_scaling = 0;
	if (NULL == (kwt = bt_parse_index_kw (h, "TSCAL", i, JDFITS_FLOAT64_TYPE)))
	  {
	     bfti->tscal = 1.0;
	  }
	else
	  {
	     bfti->tscal = kwt->v.dval;
	     bfti->has_scaling = 1;
	  }

	if (NULL == (kwt = bt_parse_index_kw (h, "TZERO", i, JDFITS_FLOAT64_TYPE)))
	  bfti->tzero = 0.0;
	else
	  {
	     bfti->tzero = kwt->v.dval;
	     bfti->has_scaling = 1;
	  }

	if (NULL == (kwt = bt_parse_index_kw (h, "TCRPX", i, JDFITS_FLOAT64_TYPE)))
	  bfti->tcrpx = 0.0;
	else
	  {
	     bfti->tcrpx = kwt->v.dval;
	     bfti->has_scaling = 1;
	  }

	if (NULL == (kwt = bt_parse_index_kw (h, "TCRVL", i, JDFITS_FLOAT64_TYPE)))
	  bfti->tcrvl = 0.0;
	else
	  {
	     bfti->tcrvl = kwt->v.dval;
	     bfti->has_scaling = 1;
	  }

	if (NULL == (kwt = bt_parse_index_kw (h, "TCDLT", i, JDFITS_FLOAT64_TYPE)))
	  bfti->tcdlt = 1.0;
	else
	  {
	     bfti->tcdlt = kwt->v.dval;
	     bfti->has_scaling = 1;
	  }
#if 0
	/* We do not yet handled the unsigned int hack.  So avoid float format */
	if (bfti->has_scaling) fmt = default_format;
#endif
	if ((NULL == (kwt = bt_parse_index_kw (h, "TDISP", i, JDFITS_STRING_TYPE)))
	    || (-1 == jdfits_ffmt_to_cfmt (kwt->v.sval, bfti->tdisp)))
	  strcpy ((char *) bfti->tdisp, fmt);
     }

   h->ext.bintable = bt;
   h->free_routine = free_bintable;

   /* Now set up the remaining fields.  Note that the positions of
    * naxis1 and naxis2 are fixed.
    */
   bt->naxis1 = h->keys[3].v.ival;
   bt->naxis2 = h->keys[4].v.ival;

   return 0;

   /* Only get here if something goes wrong.  Here all malloced items in this
    * function are freed.
    */
   free_error:
   h->ext.bintable = NULL;
   if (bt != NULL) SLFREE (bt);
   if (bft != NULL) SLFREE (bft);
   return -1;
}