File: util.c

package info (click to toggle)
fitscut 1.4.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 924 kB
  • ctags: 465
  • sloc: ansic: 6,024; sh: 767; makefile: 132
file content (167 lines) | stat: -rw-r--r-- 4,389 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* -*- mode:C; indent-tabs-mode:nil; tab-width:8; c-basic-offset:8; -*-
 *
 * Utility functions
 *
 * Author: William Jon McCann
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: util.c,v 1.4 2004/04/21 20:13:10 mccannwj Exp $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <errno.h>
#include "getopt.h"
#include <signal.h>
#include <ctype.h>

#include <float.h>
#include <math.h>

#ifdef  STDC_HEADERS
#include <stdlib.h>
#else   /* Not STDC_HEADERS */
extern void exit ();
extern char *malloc ();
#endif  /* STDC_HEADERS */
                                                                                
#ifdef	HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif

#include "fitscut.h"
#include "util.h"

#ifdef DMALLOC
#include <dmalloc.h>
#define DMALLOC_FUNC_CHECK 1
#endif

float *
interpolate_points (long nresult, float *px, float *py, long npoints)
{
        long i, j;
        long last_index, next_index;
        float *list;
        float m, x, y;

        /* x and y are normalized to unity */
        list = (float *) malloc (sizeof (float) * nresult);
        last_index = 0;
        for (i = 0; i < npoints-1; i++) {
                next_index = ceil (nresult * px[i+1]);
                m = (py[i + 1] - py[i]) / (px[i + 1] - px[i]);
                for (j = last_index; j < next_index; j++) {
                        /* point slope formula */
                        x = j / (float) nresult;
                        y = m * (x - px[i]) + py[i];
                        list[j] = y;
                }
                last_index = next_index;
        }
        return list;
}

long
parse_coordinates (char *str, float **x, float **y)
{
        int count,i;
        char *sptr;
        char *tmpstr;
        float tx,ty;
        float *xp;
        float *yp;

        count = 0;
        for (i = 0; i < strlen (str); i++) {
                if (str[i] == ',') 
                        count++;
        } 

        xp = (float *)malloc (sizeof (float) * count);
        yp = (float *)malloc (sizeof (float) * count);
        tmpstr = strdup(str);
        for (i = 0; i < count; i++) {
                if (i == 0)
                        sptr = strtok (tmpstr, ")");
                else
                        sptr = strtok (NULL, ")");
                sscanf (sptr, "(%f,%f", &tx, &ty);
                xp[i] = tx;
                yp[i] = ty;
        }

        *x = xp;
        *y = yp;
        free (tmpstr);
        return (count);
}

#if 0

void
byte_swap_vector (void *p, int n, int size)
{
        char *a, *b, c;
  
        switch (size) {
        case 2:
                for (a = (char*)p ; n > 0; n--, a += 1) {
                        b = a + 1;
                        c = *a; *a++ = *b; *b   = c;
                }
                break;
        case 4:
                for (a = (char*)p ; n > 0; n--, a += 2) {
                        b = a + 3;
                        c = *a; *a++ = *b; *b-- = c;
                        c = *a; *a++ = *b; *b   = c;
                }
                break;
        case 8:
                for (a = (char*)p ; n > 0; n--, a += 4) {
                        b = a + 7;
                        c = *a; *a++ = *b; *b-- = c;
                        c = *a; *a++ = *b; *b-- = c;
                        c = *a; *a++ = *b; *b-- = c;
                        c = *a; *a++ = *b; *b   = c;
                }
                break;
        default:
                break;
        }
}
#endif

/*
 * Put string s in lower case, return s.
 */
char *
strlwr (char *s)
{
        char *t;
        for (t = s; *t; t++)
                *t = tolow(*t);
        return s;
}