X-shooter Pipeline Reference Manual 3.8.15
test-xsh_data_image_3d.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-01-16 21:09:42 $
23 * $Revision: 1.4 $
24*/
25
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30
31/*--------------------------------------------------------------------------*/
37/*--------------------------------------------------------------------------*/
40#include <xsh_data_pre.h>
41#include <xsh_data_image_3d.h>
42#include <xsh_error.h>
43#include <xsh_msg.h>
44#include <xsh_data_instrument.h>
45#include <xsh_dfs.h>
46#include <xsh_pfits.h>
47#include <tests.h>
48
49#include <cpl.h>
50#include <math.h>
51#include <getopt.h>
52
53/*---------------------------------------------------------------------------
54 Defines
55 ---------------------------------------------------------------------------*/
56#define MODULE_ID "XSH_DATA_IMAGE_3D"
57
58enum {
60} ;
61
62static const char *Options = "?" ;
63static struct option LongOptions[] = {
64 {"nx", required_argument, 0, NX_OPT},
65 {"ny", required_argument, 0, NY_OPT},
66 {"nz", required_argument, 0, NZ_OPT},
67 {"debug", required_argument, 0, DBG_OPT},
68 {"help", 0, 0, HELP_OPT },
69 {0, 0, 0, 0}
70} ;
71
73static int Nx = 3, Ny =4, Nz = 3 ;
74
75static int Help( void ) ;
76
77static int Help( void )
78{
79 printf( "Test 3D images handling\n" ) ;
80 printf( "Options:\n" ) ;
81 printf( " --nx=<size> : X size of images (default 3)" ) ;
82 printf( " --ny=<size> : Y size of images (default 4)" ) ;
83 printf( " --nz=<size> : X size of images (default 3)" ) ;
84 printf( " --dbg=<level> : Set debug level. <level> is one of:\n" ) ;
85 printf( " none, low, high, medium.\n" ) ;
86 printf( " Default is medium.\n" ) ;
87 return 0 ;
88}
89
90static void HandleOptions( int argc, char **argv )
91{
92 int opt ;
93 int opt_idx = 0 ;
94
95 while (( opt = getopt_long( argc, argv, Options,
96 LongOptions, &opt_idx )) != EOF )
97 switch( opt ) {
98 case NX_OPT:
99 sscanf( optarg, "%64d", &Nx ) ;
100 break ;
101 case NY_OPT:
102 sscanf( optarg, "%64d", &Ny ) ;
103 break ;
104 case NZ_OPT:
105 sscanf( optarg, "%64d", &Nz ) ;
106 break ;
107 case DBG_OPT:
108 if ( strcmp( optarg, "none" ) == 0 ) dbg_level = XSH_DEBUG_LEVEL_NONE ;
109 else if ( strcmp( optarg, "low" ) == 0 )
111 else if ( strcmp( optarg, "medium" ) == 0 )
113 else if ( strcmp( optarg, "high" ) == 0 )
115 break ;
116 default:
117 Help() ;
118 exit( 0 ) ;
119 }
120}
121
122static void fill_image( cpl_image * img, int first )
123{
124 int ix, iy ;
125 int * pixels = NULL ;
126
127 XSH_ASSURE_NOT_NULL( img ) ;
128
129 check( pixels = cpl_image_get_data_int( img ) ) ;
130 xsh_msg( "fill image with %d", first ) ;
131
132 for( iy = 0 ; iy < Ny ; iy++, first++ )
133 for( ix = 0 ; ix < Nx ; ix++ ) {
134 *(pixels + ix + iy*Nx) = first ;
135 }
136 cleanup:
137 return ;
138}
139
140static int compare_img_3d( xsh_image_3d * one, xsh_image_3d * two )
141{
142 if ( one->nx != two->nx || one->ny != two->ny || one->nz != two->nz ) {
143 xsh_msg( "Sizes not equal (nx: %d-%d, ny: %d-%d, nz: %d-%d",
144 one->nx, two->nx, one->ny, two->ny, one->nz, two->nz ) ;
145 return 1 ;
146 }
147 return 0 ;
148}
149
150int main( int argc, char** argv)
151{
152 int ret = 1 ;
153 int iz ;
154 cpl_image * img = NULL ;
155 xsh_image_3d * img_3d = NULL ;
156 xsh_image_3d * new_3d = NULL ;
157 cpl_propertylist * header = NULL ;
158 const char * img_name = "TEST_3D_IMAGE.fits" ;
159
160 /* Initialize libraries */
162 cpl_msg_set_level( CPL_MSG_DEBUG);
164
165 HandleOptions( argc, argv ) ;
166
167 /* Create the 3D image */
168 check( img_3d = xsh_image_3d_new( Nx, Ny, Nz, CPL_TYPE_INT ) ) ;
169 xsh_msg( "Img 3D: axis: %dx%dx%d, type: %d", img_3d->nx, img_3d->ny,
170 img_3d->nz, img_3d->type ) ;
171
172 /* Loop over the Z axis */
173 for( iz = 0 ; iz < Nz ; iz++ ) {
174 cpl_error_code err = CPL_ERROR_NONE ;
175 char i_name[128] ;
176
177 /* Create one image */
178 check( img = cpl_image_new( Nx, Ny, CPL_TYPE_INT ) ) ;
179 /* Fill it */
180 fill_image( img, iz*100 ) ;
181 sprintf( i_name, "TEST_3D_IMG_%d.fits", iz ) ;
182 cpl_image_save( img, i_name, CPL_BPP_32_SIGNED, NULL, CPL_IO_DEFAULT ) ;
183 /* Insert the image into 3D image */
184 err = xsh_image_3d_insert( img_3d, img, iz ) ;
185 xsh_msg( " Insert Image err=%d", err ) ;
186 //xsh_free_image( &img ) ;
187 }
188
189 /* Save 3D image */
191 header, CPL_IO_DEFAULT ) ) ;
192 xsh_msg( "Image Saved" ) ;
193
194 ret = 0 ;
195
196 /* Now reload the image and compare */
197 new_3d = xsh_image_3d_load( img_name, CPL_TYPE_INT, 0 ) ;
198 if( compare_img_3d( img_3d, new_3d ) != 0 ) {
199 xsh_msg( "Finished BADLY" ) ;
200 ret = 1 ;
201 }
202 else xsh_msg( "Finished OK" ) ;
203
204 cleanup:
205 return ret ;
206}
207
int main()
Unit test of xsh_bspline_interpol.
static void HandleOptions(int argc, char **argv)
static const char * Options
static int Help(void)
static void fill_image(cpl_image *img, int first)
static int Nz
#define MODULE_ID
static int dbg_level
static int Nx
static struct option LongOptions[]
static int compare_img_3d(xsh_image_3d *one, xsh_image_3d *two)
static int Ny
static const char * img_name
cpl_error_code xsh_image_3d_insert(xsh_image_3d *img_3d, cpl_image *img, int iz)
cpl_error_code xsh_image_3d_save(xsh_image_3d *img_3d, const char *fname, cpl_propertylist *header, unsigned mode)
xsh_image_3d * xsh_image_3d_new(int nx, int ny, int nz, cpl_type type)
xsh_image_3d * xsh_image_3d_load(const char *filename, cpl_type type, int xtnum)
#define check(COMMAND)
Definition: xsh_error.h:71
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
int xsh_debug_level_set(int level)
set debug level
Definition: xsh_utils.c:3125
unsigned int first
Definition: irplib_error.c:88
#define TESTS_INIT(DRL_ID)
Definition: tests.h:105
@ XSH_DEBUG_LEVEL_HIGH
Definition: xsh_utils.h:138
@ XSH_DEBUG_LEVEL_NONE
Definition: xsh_utils.h:137
@ XSH_DEBUG_LEVEL_LOW
Definition: xsh_utils.h:137
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138