File: post_utils.c

package info (click to toggle)
libgpiv 0.6.1-7.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 4,404 kB
  • sloc: ansic: 17,832; sh: 10,248; makefile: 114
file content (555 lines) | stat: -rw-r--r-- 16,186 bytes parent folder | download | duplicates (6)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */

/*
   libgpiv - library for Particle Image Velocimetry

   Copyright (C) 2007, 2008 Gerber van der Graaf

   This file is part of libgpiv.
   Libgpiv 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  



-------------------------------------------------------------------------------
FILENAME:                utils.c
LIBRARY:                 libgpiv:
EXTERNAL FUNCTIONS:
                         gpiv_null_scdata
			 gpiv_alloc_scdata
			 gpiv_free_scdata

                         gpiv_null_bindata
			 gpiv_alloc_bindata
			 gpiv_free_bindata

LAST MODIFICATION DATE:  $Id: post_utils.c,v 1.2 2007-12-19 08:46:46 gerber Exp $
 --------------------------------------------------------------------------- */

#include <gpiv.h>


void
gpiv_null_scdata (GpivScalarData *scal_data
                  )
/*-----------------------------------------------------------------------------
 * DESCRIPTION:
 *      Sets all elements of scal_data structure to NULL
 *---------------------------------------------------------------------------*/
{
    scal_data->point_x = NULL;
    scal_data->point_y = NULL;
    scal_data->scalar = NULL;
    scal_data->flag = NULL;
}



GpivScalarData *
gpiv_alloc_scdata (const gint nx,
                   const gint ny
                   )
/*-----------------------------------------------------------------------------
 * DESCRIPTION:
 *      Allocates memory for GpivScalarData
 *
 * INPUTS:
 *      scal_data:      .nx and .ny members of GpivScalarData structure
 *
 * OUTPUTS:
 *      scal_data:       point_x, point_y, scalar, flag of GpivScalarData
 *
 * RETURNS:
 *---------------------------------------------------------------------------*/
{
    GpivScalarData *scal_data = g_new0 (GpivScalarData, 1);
    gint i, j;


    if (ny <= 0 || nx <= 0) {
        gpiv_warning ("gpiv_alloc_scdata: nx = %d ny = %d",
                      nx, ny);
        return NULL;
    }
    scal_data->nx = nx;
    scal_data->ny = ny;

    scal_data->point_x = gpiv_matrix (scal_data->ny, scal_data->nx);
    scal_data->point_y = gpiv_matrix (scal_data->ny, scal_data->nx);
    scal_data->scalar = gpiv_matrix (scal_data->ny, scal_data->nx);
    scal_data->flag = gpiv_imatrix (scal_data->ny, scal_data->nx);
        
    for (i = 0; i < scal_data->ny; i++) {
        for (j = 0; j < scal_data->nx; j++) {
            scal_data->point_x[i][j] = 0.0;
            scal_data->point_y[i][j] = 0.0;
            scal_data->scalar[i][j] = 0.0;
            scal_data->flag[i][j] = 0;
        }
    }

    scal_data->scale = FALSE;
    scal_data->scale__set = FALSE;
    scal_data->comment = NULL;


    return scal_data;
}



void 
gpiv_free_scdata (GpivScalarData *scal_data
                  )
/*-----------------------------------------------------------------------------
 * DESCRIPTION:
 *      Frees memory for GpivScalarData
 *
 * INPUTS:
 *      scal_data:      scalar data structure
 *
 * OUTPUTS:
 *      scal_data:       NULL pointer to point_x, point_y, scalar, flag
 *
 * RETURNS:
 *---------------------------------------------------------------------------*/
{

    g_return_if_fail (scal_data->point_x != NULL); /* gpiv_error ("scal_data->point_x not allocated"); */
    g_return_if_fail (scal_data->point_y != NULL); /* gpiv_error ("scal_data->point_y not allocated"); */
    g_return_if_fail (scal_data->scalar != NULL); /* gpiv_error ("scal_data->scalar not allocated"); */
    g_return_if_fail (scal_data->flag != NULL); /* gpiv_error ("scal_data->flag not allocated"); */
    
    gpiv_free_matrix (scal_data->point_x);
    gpiv_free_matrix (scal_data->point_y);
    gpiv_free_matrix (scal_data->scalar);
    gpiv_free_imatrix (scal_data->flag);
    
    g_free (scal_data->comment);
    gpiv_null_scdata (scal_data);
}


gchar *
gpiv_scalar_gnuplot (const gchar * fname_out, 
                     const gchar *title, 
                     const gchar *GNUPLOT_DISPLAY_COLOR, 
                     const gint GNUPLOT_DISPLAY_SIZE
                     )
/*-----------------------------------------------------------------------------
 * DESCRIPTION:
 *      Graphical output with gnuplot for scalar data
 *
 * INPUTS:
 *      fname_out:     file name containing plot
 *      title:         title of plot
 *      GNUPLOT_DISPLAY_COLOR:  display color of window containing graph
 *      GNUPLOT_DISPLAY_SIZE:   display size of window containing graph
 *
 * OUTPUTS:
 *
 * RETURNS:
 *     NULL on success or *err_msg on failure
 *---------------------------------------------------------------------------*/
{
    gchar *err_msg = NULL;
    FILE *fp_cmd;
    const gchar *tmp_dir = g_get_tmp_dir ();
    gchar *fname_loc = "gpiv_gnuplot.cmd";
    gchar fname_cmd[GPIV_MAX_CHARS];
    gchar command[GPIV_MAX_CHARS];


    snprintf (fname_cmd, GPIV_MAX_CHARS, "%s/%s", tmp_dir, fname_loc);
    if ((fp_cmd = fopen (fname_cmd, "w")) == NULL) {
        err_msg = "gpiv_scalar_gnuplot: Failure opening %s for output";
        gpiv_warning ("%s", err_msg);
        return err_msg;
    }

    fprintf (fp_cmd, "set xlabel \"x (pixels)\"");
    fprintf (fp_cmd, "\nset ylabel \"y (pixels)\"");
    fprintf (fp_cmd, "\nset contour; set nosurface");
    fprintf (fp_cmd, "\nset view 0, 0");
    fprintf (fp_cmd,
	     "\nsplot \"%s\" title \"%s\" with lines",
	     fname_out, title);
    fprintf (fp_cmd, "\npause -1 \"Hit return to exit\"");
    fprintf (fp_cmd, "\nquit");
    fclose (fp_cmd);

    snprintf (command, GPIV_MAX_CHARS, "gnuplot -bg %s -geometry %dx%d %s",
	      GNUPLOT_DISPLAY_COLOR, GNUPLOT_DISPLAY_SIZE, 
	      GNUPLOT_DISPLAY_SIZE, fname_cmd);

    if (system (command) != 0) {
        err_msg = "gpiv_scalar_gnuplot: could not exec shell command";
        gpiv_warning ("%s", err_msg);
        return err_msg;
    }


    return err_msg;
}


void
gpiv_null_bindata (GpivBinData *bin_data
                   )
/*-----------------------------------------------------------------------------
 * DESCRIPTION:
 *      Sets all elements of bin_data structure to NULL
 *
 * INPUTS:
 *      bin_data:      Input Bin data structure
 *
 * OUTPUTS:
 *      bin_data:      Output Bin data structure
 *
 * RETURNS:
 *---------------------------------------------------------------------------*/
{
    bin_data->count = NULL;
    bin_data->bound = NULL;
    bin_data->centre = NULL;
}



GpivBinData *
gpiv_alloc_bindata (const guint nbins
                    )
/*-----------------------------------------------------------------------------
 * DESCRIPTION:
 *      Allocates memory for GpivScalarData
 *
 * INPUTS:
 *      bin_data:       nbins of GpivScalarData
 *
 * OUTPUTS:
 *      bin_data:       count, bound, centre of GpivScalarData
 *
 * RETURNS:
 *---------------------------------------------------------------------------*/
{ 
    GpivBinData *bin_data = g_new0 (GpivBinData, 1);
    gint i;

    
    if (nbins == 0) {
        gpiv_warning ("GPIV_ALLOC_BINDATA: nbins = %d", nbins);
        return NULL;
    }
    bin_data->nbins = nbins;

    /* BUGFIX */
    /*     g_warning("gpiv_alloc_bindata:: 4/4 nbins = %d", bin_data->nbins); */
    bin_data->count = gpiv_ivector (bin_data->nbins);
    bin_data->bound = gpiv_vector (bin_data->nbins);
    bin_data->centre = gpiv_vector (bin_data->nbins);
        
    for (i = 0; i < bin_data->nbins; i++) {
        bin_data->count[i] = 0;
        bin_data->bound[i] = 0.0;
        bin_data->centre[i] = 0.0;
    }

    bin_data->min = 914.6e9;
    bin_data->max = -914.6e9;
    bin_data->comment = NULL;


    return bin_data;
}      



void 
gpiv_free_bindata (GpivBinData *bin_data
                   )
/*-----------------------------------------------------------------------------
 * DESCRIPTION:
 *      Frees memory for GpivScalarData
 *
 * INPUTS:
 *      bin_data:       data of bins
 *
 * OUTPUTS:
 *      bin_data:       NULL pointer to count, bound, centre
 *
 * RETURNS:
 *---------------------------------------------------------------------------*/
{

    g_return_if_fail (bin_data->count != NULL); /* gpiv_error ("bin_data->count not allocated"); */
    g_return_if_fail (bin_data->bound != NULL); /* gpiv_error ("bin_data->bound not allocated"); */
    g_return_if_fail (bin_data->centre != NULL); /* gpiv_error ("bin_data->centre not allocated"); */
    
    /*     g_warning ("gpiv_free_bindata:: 0 nbins = %d", bin_data->nbins); */
    gpiv_free_ivector (bin_data->count);

    gpiv_free_vector (bin_data->centre);
    gpiv_free_vector (bin_data->bound);
    
    bin_data->bound = NULL;
    bin_data->centre = NULL;
    bin_data->count = NULL;
}


void 
gpiv_histo (const GpivPivData *data, 
            GpivBinData *klass
            )
/*-----------------------------------------------------------------------------
 * DESCRIPTION:
 *      Calculates histogram from GpivPivData (NOT from ScalarData!!)
 *
 * INPUTS:
 *      data:           Input data
 *
 * OUTPUTS:
 *      klass:          Output data
 *
 * RETURNS:
 *---------------------------------------------------------------------------*/
{
    gint i, j, k;
    gint nx = data->nx, ny = data->ny, **peak_no = data->peak_no;
    float **snr = data->snr;
    float delta;
    float *bound = klass->bound, *centre = klass->centre;
    gint *count = klass->count, nbins = klass->nbins;
    

    g_return_if_fail (data->point_x != NULL); /* gpiv_error ("data->point_x not allocated"); */
    g_return_if_fail (data->point_y != NULL); /* gpiv_error ("data->point_y not allocated"); */
    g_return_if_fail (data->dx != NULL); /* gpiv_error ("data->dx not allocated"); */
    g_return_if_fail (data->dy != NULL); /* gpiv_error ("data->dy not allocated"); */
    g_return_if_fail (data->snr != NULL); /* gpiv_error ("data->snr not allocated"); */
    g_return_if_fail (data->peak_no != NULL); /* gpiv_error ("ata->peak_no not allocated"); */
    
    g_return_if_fail (klass->count != NULL); /* gpiv_error ("klass->count not allocated"); */
    g_return_if_fail (klass->bound != NULL); /* gpiv_error ("klass->bound not allocated"); */
    g_return_if_fail (klass->centre != NULL); /* gpiv_error ("klass->centre not allocated"); */
    
    klass->min = 10.0e+9, klass->max = -10.0e+9;
    /*
     * find min and max value
     */
    for (i = 0; i < ny; i++) {
	for (j = 0; j < nx; j++) {
	    if (peak_no[i][j] != -1) {

		if (snr[i][j] < klass->min)
		    klass->min = snr[i][j];
		if (snr[i][j] >= klass->max)
		    klass->max = snr[i][j];

	    }
	}
    }



    /*
     * Calculating boundaries of bins
     */
    delta = (klass->max - klass->min) / nbins;
    for (i = 0; i < nbins; i++) {
	centre[i] = klass->min + delta / 2.0 + (float) i *delta;
	count[i] = 0;
    }

    for (i = 0; i < nbins; i++) {
	bound[i] = klass->min + (float) i * delta;
    }



    /*
     * Sorting of snr data in bins
     */
    for (i = 0; i < ny; i++) {
	for (j = 0; j < nx; j++) {
	    if (peak_no[i][j] != -1) {

		for (k = 0; k < nbins; k++) {
		    if ((snr[i][j] > bound[k])
			&& (snr[i][j] <= bound[k] + delta)) {
			count[k] = count[k] + 1;
		    }

		}
	    }
	}
    }
}



void 
gpiv_cumhisto (const GpivPivData *data, 
               GpivBinData *klass
               )
/*-----------------------------------------------------------------------------
 * DESCRIPTION:
 *      Calculates cumulative histogram from GpivPivData (NOT from ScalarData!!)
 *
 * INPUTS:
 *      data:           Input data
 *
 * OUTPUTS:
 *      klass:          Output data
 *
 * RETURNS:
 *---------------------------------------------------------------------------*/
{
    gint i, j, k;
    gint nx = data->nx, ny = data->ny, **peak_no = data->peak_no;
    float **snr = data->snr;
    float delta;
    float *bound = klass->bound, *centre = klass->centre;
    gint *count = klass->count, nbins = klass->nbins;


    g_return_if_fail (data->point_x != NULL); /* gpiv_error ("data->point_x not allocated"); */
    g_return_if_fail (data->point_y != NULL); /* gpiv_error ("data->point_y not allocated"); */
    g_return_if_fail (data->dx != NULL); /* gpiv_error ("data->dx not allocated"); */
    g_return_if_fail (data->dy != NULL); /* gpiv_error ("data->dy not allocated"); */
    g_return_if_fail (data->snr != NULL); /* gpiv_error ("data->snr not allocated"); */
    g_return_if_fail (data->peak_no != NULL); /* gpiv_error ("ata->peak_no not allocated"); */
    
    g_return_if_fail (klass->count != NULL); /* gpiv_error ("klass->count not allocated"); */
    g_return_if_fail (klass->bound != NULL); /* gpiv_error ("klass->bound not allocated"); */
    g_return_if_fail (klass->centre != NULL); /* gpiv_error ("klass->centre not allocated"); */
    
    klass->min = 10e+9, klass->max = -10e+9;
    /*
     * find min and max value
     */
    for (i = 0; i < ny; i++) {
	for (j = 0; j < nx; j++) {
	    if (peak_no[i][j] != -1) {

		if (snr[i][j] < klass->min)
		    klass->min = snr[i][j];
		if (snr[i][j] >= klass->max)
		    klass->max = snr[i][j];

	    }
	}
    }


    /*
     * Calculating boundaries of bins
     */
    delta = (klass->max - klass->min) / nbins;
    for (i = 0; i < nbins; i++) {
	centre[i] = klass->min + delta / 2.0 + (float) i *delta;
	count[i] = 0;
    }

    for (i = 0; i < nbins; i++) {
	bound[i] = klass->min + (float) i * delta;
    }


    /*
     * Sorting of snr data in bins
     */
    for (i = 0; i < ny; i++) {
	for (j = 0; j < nx; j++) {
	    if (peak_no[i][j] != -1) {

		for (k = 0; k < nbins; k++) {
		    if (snr[i][j] <= bound[k] + delta) {
			count[k] = count[k] + 1;
		    }

		}
	    }
	}
    }
}



gchar *
gpiv_histo_gnuplot (const gchar *fname_out, 
                    const gchar *title, 
                    const gchar *GNUPLOT_DISPLAY_COLOR,
                    const gint GNUPLOT_DISPLAY_SIZE
                    )
/*-----------------------------------------------------------------------------
 * DESCRIPTION:
 *      Plots histogram on screen with gnuplot
 *
 * INPUTS:
 *      fname_out:	output filename
 *      title:	        plot title
 *      GNUPLOT_DISPLAY_COLOR:  display color of window containing graph
 *      GNUPLOT_DISPLAY_SIZE:   display size of window containing graph
 *
 * OUTPUTS:
 *
 * RETURNS:
 *     NULL on success or *err_msg on failure
 *---------------------------------------------------------------------------*/
{
    gchar *err_msg = NULL;
    FILE *fp_cmd;
    const gchar *tmp_dir = g_get_tmp_dir ();
    gchar *fname_loc = "gpiv_gnuplot.cmd";
    gchar *function_name = "gpiv_histo_gnuplot";
    gchar fname_cmd[GPIV_MAX_CHARS];
    gchar command[GPIV_MAX_CHARS];

    
    snprintf (fname_cmd, GPIV_MAX_CHARS, "%s/%s", tmp_dir, fname_loc);
    if ((fp_cmd = fopen (fname_cmd,"w")) == NULL) { 
        err_msg = "GPIV_HISTO_GNUPLOT: Failure opening for output";
        gpiv_warning ("%s", err_msg);
        return err_msg;
    }

    fprintf (fp_cmd, "set xlabel \"residu (pixels)\"");
    fprintf (fp_cmd, "\nset ylabel \"frequency\"");
    fprintf (fp_cmd, "\nplot \"%s\" title \"%s\" with boxes", 
             fname_out, title);
    
    fprintf (fp_cmd, "\npause -1 \"Hit return to exit\"");
    fprintf (fp_cmd, "\nquit");
    
    fclose (fp_cmd);
  
    
    snprintf (command, GPIV_MAX_CHARS, "gnuplot -bg %s -geometry %dx%d %s",
              GNUPLOT_DISPLAY_COLOR, GNUPLOT_DISPLAY_SIZE, 
              GNUPLOT_DISPLAY_SIZE, fname_cmd);
    
    if (system (command) != 0) {
        g_warning ("%s:%s could not exec shell command", 
                   LIBNAME, function_name);

        exit (1);
    }

    
    return err_msg;
}