Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / write_fields_for_directional_couplers.c
1 /* atlc - arbitrary transmission line calculator, for the analysis of
2 transmission lines are directional couplers. 
3
4 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either package_version 2
9 of the License, or (at your option) any later package_version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
19 USA.
20
21 Dr. David Kirkby, e-mail drkirkby at ntlworld.com 
22
23 */
24
25 #include "config.h"
26
27 #include "definitions.h"
28
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36
37 #ifdef HAVE_SYS_STAT_H
38 #include <sys/stat.h>
39 #endif
40
41 #include "exit_codes.h"
42
43 /* Write the following files, assuming an input of example.bmp 
44
45 example.E.Even.bmp   Grayscale Bitmap of |E-field|, normallised to 1,
46                      for even mode, but corrected for Gamma
47 example.Ex.Even.bmp  Colour Bitmap of x-directed E-field, normallised to 1, 
48                      for even mode, but corrected for Gamma
49 example.Ey.Even.bmp  Colour Bitmap of y-directed E-field, normallised to 1, 
50                      for even mode, but corrected for Gamma
51 example.V.Even.bmp   Colour Bitmap of Voltage field, normallised to 1,
52                      for even mode, corrected for Gamma
53 example.U.Even.bmp   Grayscale bitmap, with just the energy (U=CV^2).
54                      for even mode, corrected for Gamma
55
56
57 example.E.Odd.bmp    Grayscale Bitmap of |E-field|, normallised to 1,
58                      for odd mode, but corrected for Gamma
59 example.Ex.Odd.bmp   Colour Bitmap of x-directed E-field, normallised to 1, 
60                      for odd mode, but corrected for Gamma
61 example.Ey.Odd.bmp   Colour Bitmap of y-directed E-field, normallised to 1, 
62                      for odd mode, but corrected for Gamma
63 example.V.Odd.bmp    Colour Bitmap of Voltage field, normallised to 1,
64                      for odd mode, corrected for Gamma
65 example.U.Odd.bmp    Grayscale bitmap, with just the energy (U=CV^2).
66                      for odd mode, corrected for Gamma
67
68
69 example.Er.bmp       Bitmap, showing dielectric constant as on grayscale 
70                      and conductors as red,green and blue. This is *not* 
71                      specifically for the any one mode. 
72
73
74
75
76 example.E.Odd.bin    Binary file of |E-field|, normallised to for odd mode.
77 example.Ex.Odd.bin   Colour binary file of x-directed E-field, for odd mode.
78 example.Ey.Odd.bin   Colour binary file of y-directed E-field, for odd mode.
79 example.V.Odd.bin    Colour binary file of Voltage field, normallised to 1, for odd mode.
80 example.U.Odd.bin    Bitmap, with just the energy (U=CV^2).  for odd mode.
81
82 example.E.Even.bin   Binary file of |E-field|, for even mode.
83 example.Ex.Even.bin  Colour binary file of x-directed E-field for even mode.
84 example.Ey.Even.bin  Colour binary file of y-directed E-field for even mode.
85 example.V.Even.bin   Colour binary file of Voltage field, for even mode.
86 example.U.Even.bin   Bitmap, with just the energy (U=CV^2) for even mode.
87
88
89 example.Er.bin       binary file, showing dielectric constant as on grayscale 
90                      and conductors as red,green and blue. This is *not* 
91                      specifically for the any one mode. 
92
93
94 */
95
96 extern double **Vij;
97 extern double **Er;
98 extern unsigned char *bmp_buff;
99 extern int width, height;
100
101 void write_fields_for_directional_couplers(char * filename, struct transmission_line_properties data, size_t size, int odd_or_even)
102 {
103   FILE *Ex_even_bin_fp, *Ey_even_bin_fp, *E_even_bin_fp, *V_even_bin_fp, *U_even_bin_fp;
104   FILE *Ex_odd_bin_fp, *Ey_odd_bin_fp, *E_odd_bin_fp, *V_odd_bin_fp, *U_odd_bin_fp;
105   FILE *Ex_even_bmp_fp, *Ey_even_bmp_fp, *E_even_bmp_fp, *V_even_bmp_fp, *U_even_bmp_fp;
106   FILE *Ex_odd_bmp_fp, *Ey_odd_bmp_fp, *E_odd_bmp_fp, *V_odd_bmp_fp, *U_odd_bmp_fp;
107   FILE *permittivity_bin_fp, *permittivity_bmp_fp;
108 #ifdef DEBUG
109   FILE *fpOddEx, *fpOddEy;
110   FILE *fpEvenEx, *fpEvenEy;
111 #endif
112   unsigned char *image_data_Ex=NULL; 
113   unsigned char *image_data_Ey=NULL;
114   unsigned char *image_data_E=NULL;
115   unsigned char *image_data_U=NULL; 
116   unsigned char *image_data_V=NULL;
117   unsigned char *image_data_Er=NULL;
118
119   unsigned char r, g, b;
120
121   static struct max_values maximum_values;
122   int offset=-3, w, h; 
123   size_t memory_location;
124   double E, Ex, Ey, U;
125
126   if(data.write_binary_field_imagesQ==TRUE && odd_or_even == ODD)
127   {
128     Ex_odd_bin_fp=get_file_pointer_with_right_filename(filename,".Ex.odd.bin");
129     Ey_odd_bin_fp=get_file_pointer_with_right_filename(filename,".Ey.odd.bin");
130     E_odd_bin_fp=get_file_pointer_with_right_filename(filename,".E.odd.bin");
131     V_odd_bin_fp=get_file_pointer_with_right_filename(filename,".V.odd.bin");
132     U_odd_bin_fp=get_file_pointer_with_right_filename(filename,".U.odd.bin");
133     permittivity_bin_fp=get_file_pointer_with_right_filename(filename,".Er.bin");
134
135     for(h=height-1;h>=0;h--)
136     {
137       for(w=0;w<width;++w)
138       {
139         Ex=find_Ex(w,h);
140         Ey=find_Ey(w,h);
141         E=find_E(w,h);
142         U=find_energy_per_metre(w,h);
143         if( fwrite((void *) &Ex,sizeof(double), 1, Ex_odd_bin_fp) != 1)
144           exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
145         if( fwrite((void *) &Ey,sizeof(double), 1, Ey_odd_bin_fp) != 1)
146           exit_with_msg_and_exit_code("Error#2: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
147         if( fwrite((void *) &E,sizeof(double), 1, E_odd_bin_fp) != 1)
148           exit_with_msg_and_exit_code("Error#3: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
149         if( fwrite((void *) &U,sizeof(double), 1, U_odd_bin_fp) != 1)
150           exit_with_msg_and_exit_code("Error#4: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
151         if( fwrite((void *) &Vij[w][h],sizeof(double), 1, V_odd_bin_fp) != 1)
152           exit_with_msg_and_exit_code("Error#5: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
153         if( fwrite((void *) &Er[w][h],sizeof(double), 1, permittivity_bin_fp) != 1)
154           exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
155       }
156     }
157     if( fclose(Ex_odd_bin_fp) != 0)
158       exit_with_msg_and_exit_code("Error#7: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
159     if( fclose(Ey_odd_bin_fp) != 0)
160       exit_with_msg_and_exit_code("Error#8: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
161     if( fclose(E_odd_bin_fp) != 0)
162       exit_with_msg_and_exit_code("Error#9: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
163     if( fclose(U_odd_bin_fp) != 0)
164       exit_with_msg_and_exit_code("Error#10: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
165     if( fclose(V_odd_bin_fp) != 0)
166       exit_with_msg_and_exit_code("Error#11: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
167     if( fclose(permittivity_bin_fp) != 0)
168       exit_with_msg_and_exit_code("Error#12: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
169   } /* End of writing odd binary files */
170
171   if(data.write_bitmap_field_imagesQ==TRUE && odd_or_even == ODD)
172   {
173     /* Allocate ram to store the bitmaps before they are written to disk */
174     image_data_Ex=ustring(0L,(long) size);
175     image_data_Ey=ustring(0L,(long) size);
176     image_data_E=ustring(0L,(long) size);
177     image_data_V=ustring(0L,(long) size);
178     image_data_Er=ustring(0L,(long) size);
179     image_data_U=ustring(0L,(long) size);
180     for(memory_location=0; memory_location < size; ++memory_location)
181     {
182       image_data_Ex[memory_location]=0;
183       image_data_Ey[memory_location]=0;
184       image_data_E[memory_location]=0;
185       image_data_U[memory_location]=0;
186       image_data_V[memory_location]=0;
187       image_data_Er[memory_location]=0;
188     }
189     /* Find maximum of the parameters */
190     find_maximum_values(&(maximum_values),ZERO_ELEMENTS_FIRST); /* sets stucture maximum_values */
191     Ex_odd_bmp_fp=get_file_pointer_with_right_filename(filename,".Ex.odd.bmp");
192     Ey_odd_bmp_fp=get_file_pointer_with_right_filename(filename,".Ey.odd.bmp");
193     E_odd_bmp_fp=get_file_pointer_with_right_filename(filename,".E.odd.bmp");
194     V_odd_bmp_fp=get_file_pointer_with_right_filename(filename,".V.odd.bmp");
195     U_odd_bmp_fp=get_file_pointer_with_right_filename(filename,".U.odd.bmp");
196     permittivity_bmp_fp=get_file_pointer_with_right_filename(filename,".Er.bmp");
197
198     /* Permittivity images are written along with the odd images. It
199     makes no difference whey they are written, since they don't change */
200
201     permittivity_bmp_fp=get_file_pointer_with_right_filename(filename,".Er.bmp");
202
203     if( fwrite(bmp_buff,0x36,1,Ex_odd_bmp_fp) != 1)
204       exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
205     if( fwrite(bmp_buff,0x36,1,Ey_odd_bmp_fp) != 1)
206       exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
207     if( fwrite(bmp_buff,0x36,1,E_odd_bmp_fp) != 1)
208       exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
209     if( fwrite(bmp_buff,0x36,1,U_odd_bmp_fp) != 1)
210       exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
211     if( fwrite(bmp_buff,0x36,1,V_odd_bmp_fp) != 1)
212       exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
213     if( fwrite(bmp_buff,0x36,1,permittivity_bmp_fp) != 1)
214       exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
215     offset=-3;
216     for(h=height-1;h>=0;h--)
217     {
218       for(w=0;w<width;++w)
219       {
220         offset+=3;
221         if((w==0) && (offset%4!=0) && (h!=0)) 
222           offset+=(4-offset%4);
223         Ex=find_Ex(w,h);
224         Ey=find_Ey(w,h);
225         E=find_Ex(w,h);
226         U=find_energy_per_metre(w,h);
227
228         calculate_colour_data(Ex, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ex, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
229         calculate_colour_data(Ey, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ey, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
230         calculate_colour_data(E, maximum_values.E_max, w, h, offset,image_data_E, MONOCHROME,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
231         calculate_colour_data(U, maximum_values.U_max, w, h, offset,image_data_U, MONOCHROME,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
232         calculate_colour_data(Vij[w][h], maximum_values.V_max, w, h, offset,image_data_V, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
233         calculate_colour_data(Er[w][h], MAX_ER, w, h, offset,image_data_Er, MIXED,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
234       }
235     } 
236     if( fwrite((void *) image_data_Ex,size, 1, Ex_odd_bmp_fp) != 1)
237       exit_with_msg_and_exit_code("Error#25: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
238     if( fwrite((void *) &(image_data_Ey[0]),size, 1, Ey_odd_bmp_fp) != 1)
239       exit_with_msg_and_exit_code("Error#26: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
240     if( fwrite((void *) &(image_data_E[0]),size, 1, E_odd_bmp_fp) != 1)
241       exit_with_msg_and_exit_code("Error#27: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
242     if( fwrite((void *) &(image_data_V[0]),size, 1, V_odd_bmp_fp) != 1)
243       exit_with_msg_and_exit_code("Error#28: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
244     if( fwrite((void *) &(image_data_U[0]),size, 1, U_odd_bmp_fp) != 1)
245       exit_with_msg_and_exit_code("Error#29: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
246     if( fwrite((void *) &(image_data_Er[0]),size, 1, permittivity_bmp_fp) != 1)
247       exit_with_msg_and_exit_code("Error#29: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
248
249     if( fclose(Ex_odd_bmp_fp) != 0)
250       exit_with_msg_and_exit_code("Error#30: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
251     if( fclose(Ey_odd_bmp_fp) != 0)
252       exit_with_msg_and_exit_code("Error#31: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
253     if( fclose(E_odd_bmp_fp) != 0)
254       exit_with_msg_and_exit_code("Error#32: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
255     if( fclose(V_odd_bmp_fp) != 0)
256       exit_with_msg_and_exit_code("Error#33: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
257     if( fclose(U_odd_bmp_fp) != 0)
258       exit_with_msg_and_exit_code("Error#34: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
259     if( fclose(permittivity_bmp_fp) != 0)
260       exit_with_msg_and_exit_code("Error#35: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
261     free_ustring(image_data_Ex,0L,(long) size);
262     free_ustring(image_data_Ey,0L,(long) size);
263     free_ustring(image_data_E,0L,(long) size);
264     free_ustring(image_data_V,0L,(long) size);
265     free_ustring(image_data_U,0L,(long) size);
266     free_ustring(image_data_Er,0L,(long)size);
267   } /* End of writing odd bitmap files  and therefore *all* odd files */
268
269   if(data.write_binary_field_imagesQ==TRUE && odd_or_even == EVEN)
270   {
271     Ex_even_bin_fp=get_file_pointer_with_right_filename(filename,".Ex.even.bin");
272     Ey_even_bin_fp=get_file_pointer_with_right_filename(filename,".Ey.even.bin");
273     E_even_bin_fp=get_file_pointer_with_right_filename(filename,".E.even.bin");
274     V_even_bin_fp=get_file_pointer_with_right_filename(filename,".V.even.bin");
275     U_even_bin_fp=get_file_pointer_with_right_filename(filename,".U.even.bin");
276     permittivity_bin_fp=get_file_pointer_with_right_filename(filename,".Er.bin");
277
278     for(h=height-1;h>=0;h--)
279     {
280       for(w=0;w<width;++w)
281       {
282         Ex=find_Ex(w,h);
283         Ey=find_Ey(w,h);
284         E=find_E(w,h);
285         U=find_energy_per_metre(w,h);
286
287         if( fwrite((void *) &Ex,sizeof(double), 1, Ex_even_bin_fp) != 1)
288           exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
289         if( fwrite((void *) &Ey,sizeof(double), 1, Ey_even_bin_fp) != 1)
290           exit_with_msg_and_exit_code("Error#2: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
291         if( fwrite((void *) &E,sizeof(double), 1, E_even_bin_fp) != 1)
292           exit_with_msg_and_exit_code("Error#3: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
293         if( fwrite((void *) &U,sizeof(double), 1, U_even_bin_fp) != 1)
294           exit_with_msg_and_exit_code("Error#4: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
295         if( fwrite((void *) &Vij[w][h],sizeof(double), 1, V_even_bin_fp) != 1)
296           exit_with_msg_and_exit_code("Error#5: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
297         if( fwrite((void *) &Er[w][h],sizeof(double), 1, permittivity_bin_fp) != 1)
298           exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
299       }
300     }
301     if( fclose(Ex_even_bin_fp) != 0)
302       exit_with_msg_and_exit_code("Error#7: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
303     if( fclose(Ey_even_bin_fp) != 0)
304       exit_with_msg_and_exit_code("Error#8: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
305     if( fclose(E_even_bin_fp) != 0)
306       exit_with_msg_and_exit_code("Error#9: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
307     if( fclose(U_even_bin_fp) != 0)
308       exit_with_msg_and_exit_code("Error#10: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
309     if( fclose(V_even_bin_fp) != 0)
310       exit_with_msg_and_exit_code("Error#11: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
311     if( fclose(permittivity_bin_fp) != 0)
312       exit_with_msg_and_exit_code("Error#12: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
313   } /* End of writing even binary files */
314
315   if(data.write_bitmap_field_imagesQ==TRUE &&  odd_or_even == EVEN)
316   {
317     /* Allocate ram to store the bitmaps before they are written to disk */
318     image_data_Ex=ustring(0L,(long) size);
319     image_data_Ey=ustring(0L,(long) size);
320     image_data_E=ustring(0L,(long) size);
321     image_data_V=ustring(0L,(long) size);
322     image_data_Er=ustring(0L,(long) size);
323     image_data_U=ustring(0L,(long) size);
324 #ifdef HAVE_MEMSET
325     (void) memset((void *) image_data_Ex,0,size);
326     (void) memset((void *) image_data_Ey,0,size);
327     (void) memset((void *) image_data_E,0,size);
328     (void) memset((void *) image_data_U,0,size);
329     (void) memset((void *) image_data_V,0,size);
330     (void) memset((void *) image_data_Er,0,size);
331 #else
332     for(memory_location=0; memory_location < size; ++memory_location)
333     {
334       image_data_Ex[memory_location]=0;
335       image_data_Ey[memory_location]=0;
336       image_data_E[memory_location]=0;
337       image_data_U[memory_location]=0;
338       image_data_V[memory_location]=0;
339       image_data_Er[memory_location]=0;
340     }
341 #endif /* End of #ifdef HAVE_MEMSET */
342     /* Find maximum of the parameters */
343     find_maximum_values(&(maximum_values),FALSE); /* sets stucture maximum_values */
344
345     Ex_even_bmp_fp=get_file_pointer_with_right_filename(filename,".Ex.even.bmp");
346     Ey_even_bmp_fp=get_file_pointer_with_right_filename(filename,".Ey.even.bmp");
347     E_even_bmp_fp=get_file_pointer_with_right_filename(filename,".E.even.bmp");
348     V_even_bmp_fp=get_file_pointer_with_right_filename(filename,".V.even.bmp");
349     U_even_bmp_fp=get_file_pointer_with_right_filename(filename,".U.even.bmp");
350     permittivity_bmp_fp=get_file_pointer_with_right_filename(filename,".Er.bmp");
351
352     /* Permittivity images are written along with the even images. It
353     makes no difference whey they are written, since they don't change */
354
355     permittivity_bmp_fp=get_file_pointer_with_right_filename(filename,".Er.bmp");
356
357     if( fwrite(bmp_buff,0x36,1,Ex_even_bmp_fp) != 1)
358       exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
359     if( fwrite(bmp_buff,0x36,1,Ey_even_bmp_fp) != 1)
360       exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
361     if( fwrite(bmp_buff,0x36,1,E_even_bmp_fp) != 1)
362       exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
363     if( fwrite(bmp_buff,0x36,1,U_even_bmp_fp) != 1)
364       exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
365     if( fwrite(bmp_buff,0x36,1,V_even_bmp_fp) != 1)
366       exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
367     if( fwrite(bmp_buff,0x36,1,permittivity_bmp_fp) != 1)
368       exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
369     offset=-3;
370 #ifdef DEBUG
371     fpEvenEx=fopen("Ex.even.txt","w");
372     fpEvenEy=fopen("Ey.even.txt","w");
373 #endif
374     for(h=height-1;h>=0;h--)
375     {
376       for(w=0;w<width;++w)
377       {
378         offset+=3;
379         if((w==0) && (offset%4!=0) && (h!=0)) 
380           offset+=(4-offset%4);
381         Ex=find_Ex(w,h);
382         Ey=find_Ey(w,h);
383         E=find_Ex(w,h);
384         U=find_energy_per_metre(w,h);
385
386         calculate_colour_data(Ex, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ex, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
387         calculate_colour_data(Ey, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ey, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
388         calculate_colour_data(E, maximum_values.E_max, w, h, offset,image_data_E, MONOCHROME,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
389         calculate_colour_data(U, maximum_values.U_max, w, h, offset,image_data_U, MONOCHROME,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
390         calculate_colour_data(Vij[w][h], maximum_values.V_max, w, h, offset,image_data_V, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
391         calculate_colour_data(Er[w][h], MAX_ER, w, h, offset,image_data_Er, MIXED,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
392       }
393     } 
394     if( fwrite((void *) &(image_data_Ex[0]),size, 1, Ex_even_bmp_fp) != 1)
395       exit_with_msg_and_exit_code("Error#25: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
396     if( fwrite((void *) &(image_data_Ey[0]),size, 1, Ey_even_bmp_fp) != 1)
397       exit_with_msg_and_exit_code("Error#26: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
398     if( fwrite((void *) &(image_data_E[0]),size, 1, E_even_bmp_fp) != 1)
399       exit_with_msg_and_exit_code("Error#27: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
400     if( fwrite((void *) &(image_data_V[0]),size, 1, V_even_bmp_fp) != 1)
401       exit_with_msg_and_exit_code("Error#28: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
402     if( fwrite((void *) &(image_data_U[0]),size, 1, U_even_bmp_fp) != 1)
403       exit_with_msg_and_exit_code("Error#29: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
404     if( fwrite((void *) &(image_data_Er[0]),size, 1, permittivity_bmp_fp) != 1)
405       exit_with_msg_and_exit_code("Error#29: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
406
407     if( fclose(Ex_even_bmp_fp) != 0)
408       exit_with_msg_and_exit_code("Error#30: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
409     if( fclose(Ey_even_bmp_fp) != 0)
410       exit_with_msg_and_exit_code("Error#31: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
411     if( fclose(E_even_bmp_fp) != 0)
412       exit_with_msg_and_exit_code("Error#32: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
413     if( fclose(V_even_bmp_fp) != 0)
414       exit_with_msg_and_exit_code("Error#33: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
415     if( fclose(U_even_bmp_fp) != 0)
416       exit_with_msg_and_exit_code("Error#34: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
417     if( fclose(permittivity_bmp_fp) != 0)
418       exit_with_msg_and_exit_code("Error#35: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
419
420     /* Free ram used to store the bitmaps before they were written to disk */
421     free_ustring(image_data_Ex,0L,(long) size);
422     free_ustring(image_data_Ey,0L,(long) size);
423     free_ustring(image_data_E,0L,(long) size);
424     free_ustring(image_data_V,0L,(long) size);
425     free_ustring(image_data_U,0L,(long) size);
426     free_ustring(image_data_Er,0L,(long) size);
427   } /* End of writing even bitmap files */
428 }