missed one!
[debian/atlc] / src / 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 gmail.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   unsigned char *image_data_Ex=NULL; 
109   unsigned char *image_data_Ey=NULL;
110   unsigned char *image_data_E=NULL;
111   unsigned char *image_data_U=NULL; 
112   unsigned char *image_data_V=NULL;
113   unsigned char *image_data_Er=NULL;
114
115   unsigned char r, g, b;
116
117   static struct max_values maximum_values;
118   int offset=-3, w, h; 
119   size_t memory_location;
120   double E, Ex, Ey, U;
121
122   if(data.write_binary_field_imagesQ==TRUE && odd_or_even == ODD)
123   {
124     Ex_odd_bin_fp=get_file_pointer_with_right_filename(filename,".Ex.odd.bin");
125     Ey_odd_bin_fp=get_file_pointer_with_right_filename(filename,".Ey.odd.bin");
126     E_odd_bin_fp=get_file_pointer_with_right_filename(filename,".E.odd.bin");
127     V_odd_bin_fp=get_file_pointer_with_right_filename(filename,".V.odd.bin");
128     U_odd_bin_fp=get_file_pointer_with_right_filename(filename,".U.odd.bin");
129     permittivity_bin_fp=get_file_pointer_with_right_filename(filename,".Er.bin");
130
131     for(h=height-1;h>=0;h--)
132     {
133       for(w=0;w<width;++w)
134       {
135         Ex=find_Ex(w,h);
136         Ey=find_Ey(w,h);
137         E=find_E(w,h);
138         U=find_energy_per_metre(w,h);
139         if( fwrite((void *) &Ex,sizeof(double), 1, Ex_odd_bin_fp) != 1)
140           exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
141         if( fwrite((void *) &Ey,sizeof(double), 1, Ey_odd_bin_fp) != 1)
142           exit_with_msg_and_exit_code("Error#2: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
143         if( fwrite((void *) &E,sizeof(double), 1, E_odd_bin_fp) != 1)
144           exit_with_msg_and_exit_code("Error#3: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
145         if( fwrite((void *) &U,sizeof(double), 1, U_odd_bin_fp) != 1)
146           exit_with_msg_and_exit_code("Error#4: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
147         if( fwrite((void *) &Vij[w][h],sizeof(double), 1, V_odd_bin_fp) != 1)
148           exit_with_msg_and_exit_code("Error#5: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
149         if( fwrite((void *) &Er[w][h],sizeof(double), 1, permittivity_bin_fp) != 1)
150           exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
151       }
152     }
153     if( fclose(Ex_odd_bin_fp) != 0)
154       exit_with_msg_and_exit_code("Error#7: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
155     if( fclose(Ey_odd_bin_fp) != 0)
156       exit_with_msg_and_exit_code("Error#8: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
157     if( fclose(E_odd_bin_fp) != 0)
158       exit_with_msg_and_exit_code("Error#9: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
159     if( fclose(U_odd_bin_fp) != 0)
160       exit_with_msg_and_exit_code("Error#10: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
161     if( fclose(V_odd_bin_fp) != 0)
162       exit_with_msg_and_exit_code("Error#11: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
163     if( fclose(permittivity_bin_fp) != 0)
164       exit_with_msg_and_exit_code("Error#12: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
165   } /* End of writing odd binary files */
166
167   if(data.write_bitmap_field_imagesQ==TRUE && odd_or_even == ODD)
168   {
169     /* Allocate ram to store the bitmaps before they are written to disk */
170     image_data_Ex=ustring(0L,(long) size);
171     image_data_Ey=ustring(0L,(long) size);
172     image_data_E=ustring(0L,(long) size);
173     image_data_V=ustring(0L,(long) size);
174     image_data_Er=ustring(0L,(long) size);
175     image_data_U=ustring(0L,(long) size);
176     for(memory_location=0; memory_location < size; ++memory_location)
177     {
178       image_data_Ex[memory_location]=0;
179       image_data_Ey[memory_location]=0;
180       image_data_E[memory_location]=0;
181       image_data_U[memory_location]=0;
182       image_data_V[memory_location]=0;
183       image_data_Er[memory_location]=0;
184     }
185     /* Find maximum of the parameters */
186     find_maximum_values(&(maximum_values),ZERO_ELEMENTS_FIRST); /* sets stucture maximum_values */
187     Ex_odd_bmp_fp=get_file_pointer_with_right_filename(filename,".Ex.odd.bmp");
188     Ey_odd_bmp_fp=get_file_pointer_with_right_filename(filename,".Ey.odd.bmp");
189     E_odd_bmp_fp=get_file_pointer_with_right_filename(filename,".E.odd.bmp");
190     V_odd_bmp_fp=get_file_pointer_with_right_filename(filename,".V.odd.bmp");
191     U_odd_bmp_fp=get_file_pointer_with_right_filename(filename,".U.odd.bmp");
192     permittivity_bmp_fp=get_file_pointer_with_right_filename(filename,".Er.bmp");
193
194     /* Permittivity images are written along with the odd images. It
195     makes no difference whey they are written, since they don't change */
196
197     permittivity_bmp_fp=get_file_pointer_with_right_filename(filename,".Er.bmp");
198
199     if( fwrite(bmp_buff,0x36,1,Ex_odd_bmp_fp) != 1)
200       exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
201     if( fwrite(bmp_buff,0x36,1,Ey_odd_bmp_fp) != 1)
202       exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
203     if( fwrite(bmp_buff,0x36,1,E_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,U_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,V_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,permittivity_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     offset=-3;
212     for(h=height-1;h>=0;h--)
213     {
214       for(w=0;w<width;++w)
215       {
216         offset+=3;
217         if((w==0) && (offset%4!=0) && (h!=0)) 
218           offset+=(4-offset%4);
219         Ex=find_Ex(w,h);
220         Ey=find_Ey(w,h);
221         E=find_Ex(w,h);
222         U=find_energy_per_metre(w,h);
223
224         calculate_colour_data(Ex, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ex, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
225         calculate_colour_data(Ey, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ey, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
226         calculate_colour_data(E, maximum_values.E_max, w, h, offset,image_data_E, MONOCHROME,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
227         calculate_colour_data(U, maximum_values.U_max, w, h, offset,image_data_U, MONOCHROME,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
228         calculate_colour_data(Vij[w][h], maximum_values.V_max, w, h, offset,image_data_V, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
229         calculate_colour_data(Er[w][h], MAX_ER, w, h, offset,image_data_Er, MIXED,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
230       }
231     } 
232     if( fwrite((void *) image_data_Ex,size, 1, Ex_odd_bmp_fp) != 1)
233       exit_with_msg_and_exit_code("Error#25: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
234     if( fwrite((void *) &(image_data_Ey[0]),size, 1, Ey_odd_bmp_fp) != 1)
235       exit_with_msg_and_exit_code("Error#26: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
236     if( fwrite((void *) &(image_data_E[0]),size, 1, E_odd_bmp_fp) != 1)
237       exit_with_msg_and_exit_code("Error#27: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
238     if( fwrite((void *) &(image_data_V[0]),size, 1, V_odd_bmp_fp) != 1)
239       exit_with_msg_and_exit_code("Error#28: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
240     if( fwrite((void *) &(image_data_U[0]),size, 1, U_odd_bmp_fp) != 1)
241       exit_with_msg_and_exit_code("Error#29: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
242     if( fwrite((void *) &(image_data_Er[0]),size, 1, permittivity_bmp_fp) != 1)
243       exit_with_msg_and_exit_code("Error#29: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
244
245     if( fclose(Ex_odd_bmp_fp) != 0)
246       exit_with_msg_and_exit_code("Error#30: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
247     if( fclose(Ey_odd_bmp_fp) != 0)
248       exit_with_msg_and_exit_code("Error#31: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
249     if( fclose(E_odd_bmp_fp) != 0)
250       exit_with_msg_and_exit_code("Error#32: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
251     if( fclose(V_odd_bmp_fp) != 0)
252       exit_with_msg_and_exit_code("Error#33: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
253     if( fclose(U_odd_bmp_fp) != 0)
254       exit_with_msg_and_exit_code("Error#34: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
255     if( fclose(permittivity_bmp_fp) != 0)
256       exit_with_msg_and_exit_code("Error#35: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
257     free_ustring(image_data_Ex,0L,(long) size);
258     free_ustring(image_data_Ey,0L,(long) size);
259     free_ustring(image_data_E,0L,(long) size);
260     free_ustring(image_data_V,0L,(long) size);
261     free_ustring(image_data_U,0L,(long) size);
262     free_ustring(image_data_Er,0L,(long)size);
263   } /* End of writing odd bitmap files  and therefore *all* odd files */
264
265   if(data.write_binary_field_imagesQ==TRUE && odd_or_even == EVEN)
266   {
267     Ex_even_bin_fp=get_file_pointer_with_right_filename(filename,".Ex.even.bin");
268     Ey_even_bin_fp=get_file_pointer_with_right_filename(filename,".Ey.even.bin");
269     E_even_bin_fp=get_file_pointer_with_right_filename(filename,".E.even.bin");
270     V_even_bin_fp=get_file_pointer_with_right_filename(filename,".V.even.bin");
271     U_even_bin_fp=get_file_pointer_with_right_filename(filename,".U.even.bin");
272     permittivity_bin_fp=get_file_pointer_with_right_filename(filename,".Er.bin");
273
274     for(h=height-1;h>=0;h--)
275     {
276       for(w=0;w<width;++w)
277       {
278         Ex=find_Ex(w,h);
279         Ey=find_Ey(w,h);
280         E=find_E(w,h);
281         U=find_energy_per_metre(w,h);
282
283         if( fwrite((void *) &Ex,sizeof(double), 1, Ex_even_bin_fp) != 1)
284           exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
285         if( fwrite((void *) &Ey,sizeof(double), 1, Ey_even_bin_fp) != 1)
286           exit_with_msg_and_exit_code("Error#2: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
287         if( fwrite((void *) &E,sizeof(double), 1, E_even_bin_fp) != 1)
288           exit_with_msg_and_exit_code("Error#3: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
289         if( fwrite((void *) &U,sizeof(double), 1, U_even_bin_fp) != 1)
290           exit_with_msg_and_exit_code("Error#4: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
291         if( fwrite((void *) &Vij[w][h],sizeof(double), 1, V_even_bin_fp) != 1)
292           exit_with_msg_and_exit_code("Error#5: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
293         if( fwrite((void *) &Er[w][h],sizeof(double), 1, permittivity_bin_fp) != 1)
294           exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
295       }
296     }
297     if( fclose(Ex_even_bin_fp) != 0)
298       exit_with_msg_and_exit_code("Error#7: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
299     if( fclose(Ey_even_bin_fp) != 0)
300       exit_with_msg_and_exit_code("Error#8: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
301     if( fclose(E_even_bin_fp) != 0)
302       exit_with_msg_and_exit_code("Error#9: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
303     if( fclose(U_even_bin_fp) != 0)
304       exit_with_msg_and_exit_code("Error#10: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
305     if( fclose(V_even_bin_fp) != 0)
306       exit_with_msg_and_exit_code("Error#11: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
307     if( fclose(permittivity_bin_fp) != 0)
308       exit_with_msg_and_exit_code("Error#12: can't close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
309   } /* End of writing even binary files */
310
311   if(data.write_bitmap_field_imagesQ==TRUE &&  odd_or_even == EVEN)
312   {
313     /* Allocate ram to store the bitmaps before they are written to disk */
314     image_data_Ex=ustring(0L,(long) size);
315     image_data_Ey=ustring(0L,(long) size);
316     image_data_E=ustring(0L,(long) size);
317     image_data_V=ustring(0L,(long) size);
318     image_data_Er=ustring(0L,(long) size);
319     image_data_U=ustring(0L,(long) size);
320 #ifdef HAVE_MEMSET
321     (void) memset((void *) image_data_Ex,0,size);
322     (void) memset((void *) image_data_Ey,0,size);
323     (void) memset((void *) image_data_E,0,size);
324     (void) memset((void *) image_data_U,0,size);
325     (void) memset((void *) image_data_V,0,size);
326     (void) memset((void *) image_data_Er,0,size);
327 #else
328     for(memory_location=0; memory_location < size; ++memory_location)
329     {
330       image_data_Ex[memory_location]=0;
331       image_data_Ey[memory_location]=0;
332       image_data_E[memory_location]=0;
333       image_data_U[memory_location]=0;
334       image_data_V[memory_location]=0;
335       image_data_Er[memory_location]=0;
336     }
337 #endif /* End of #ifdef HAVE_MEMSET */
338     /* Find maximum of the parameters */
339     find_maximum_values(&(maximum_values),FALSE); /* sets stucture maximum_values */
340
341     Ex_even_bmp_fp=get_file_pointer_with_right_filename(filename,".Ex.even.bmp");
342     Ey_even_bmp_fp=get_file_pointer_with_right_filename(filename,".Ey.even.bmp");
343     E_even_bmp_fp=get_file_pointer_with_right_filename(filename,".E.even.bmp");
344     V_even_bmp_fp=get_file_pointer_with_right_filename(filename,".V.even.bmp");
345     U_even_bmp_fp=get_file_pointer_with_right_filename(filename,".U.even.bmp");
346     permittivity_bmp_fp=get_file_pointer_with_right_filename(filename,".Er.bmp");
347
348     /* Permittivity images are written along with the even images. It
349     makes no difference whey they are written, since they don't change */
350
351     permittivity_bmp_fp=get_file_pointer_with_right_filename(filename,".Er.bmp");
352
353     if( fwrite(bmp_buff,0x36,1,Ex_even_bmp_fp) != 1)
354       exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
355     if( fwrite(bmp_buff,0x36,1,Ey_even_bmp_fp) != 1)
356       exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
357     if( fwrite(bmp_buff,0x36,1,E_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,U_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,V_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,permittivity_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     offset=-3;
366     for(h=height-1;h>=0;h--)
367     {
368       for(w=0;w<width;++w)
369       {
370         offset+=3;
371         if((w==0) && (offset%4!=0) && (h!=0)) 
372           offset+=(4-offset%4);
373         Ex=find_Ex(w,h);
374         Ey=find_Ey(w,h);
375         E=find_Ex(w,h);
376         U=find_energy_per_metre(w,h);
377
378         calculate_colour_data(Ex, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ex, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
379         calculate_colour_data(Ey, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ey, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
380         calculate_colour_data(E, maximum_values.E_max, w, h, offset,image_data_E, MONOCHROME,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
381         calculate_colour_data(U, maximum_values.U_max, w, h, offset,image_data_U, MONOCHROME,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
382         calculate_colour_data(Vij[w][h], maximum_values.V_max, w, h, offset,image_data_V, COLOUR,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
383         calculate_colour_data(Er[w][h], MAX_ER, w, h, offset,image_data_Er, MIXED,&r,&g,&b, IMAGE_FIDDLE_FACTOR);
384       }
385     } 
386     if( fwrite((void *) &(image_data_Ex[0]),size, 1, Ex_even_bmp_fp) != 1)
387       exit_with_msg_and_exit_code("Error#25: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
388     if( fwrite((void *) &(image_data_Ey[0]),size, 1, Ey_even_bmp_fp) != 1)
389       exit_with_msg_and_exit_code("Error#26: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
390     if( fwrite((void *) &(image_data_E[0]),size, 1, E_even_bmp_fp) != 1)
391       exit_with_msg_and_exit_code("Error#27: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
392     if( fwrite((void *) &(image_data_V[0]),size, 1, V_even_bmp_fp) != 1)
393       exit_with_msg_and_exit_code("Error#28: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
394     if( fwrite((void *) &(image_data_U[0]),size, 1, U_even_bmp_fp) != 1)
395       exit_with_msg_and_exit_code("Error#29: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
396     if( fwrite((void *) &(image_data_Er[0]),size, 1, permittivity_bmp_fp) != 1)
397       exit_with_msg_and_exit_code("Error#29: Failed to write bitmap file in write_fields_for_directional_couplers.c",WRITE_FAILURE);
398
399     if( fclose(Ex_even_bmp_fp) != 0)
400       exit_with_msg_and_exit_code("Error#30: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
401     if( fclose(Ey_even_bmp_fp) != 0)
402       exit_with_msg_and_exit_code("Error#31: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
403     if( fclose(E_even_bmp_fp) != 0)
404       exit_with_msg_and_exit_code("Error#32: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
405     if( fclose(V_even_bmp_fp) != 0)
406       exit_with_msg_and_exit_code("Error#33: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
407     if( fclose(U_even_bmp_fp) != 0)
408       exit_with_msg_and_exit_code("Error#34: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
409     if( fclose(permittivity_bmp_fp) != 0)
410       exit_with_msg_and_exit_code("Error#35: Unable to close file in write_fields_for_directional_couplers.c",CANT_CLOSE_FILE);
411
412     /* Free ram used to store the bitmaps before they were written to disk */
413     free_ustring(image_data_Ex,0L,(long) size);
414     free_ustring(image_data_Ey,0L,(long) size);
415     free_ustring(image_data_E,0L,(long) size);
416     free_ustring(image_data_V,0L,(long) size);
417     free_ustring(image_data_U,0L,(long) size);
418     free_ustring(image_data_Er,0L,(long) size);
419   } /* End of writing even bitmap files */
420 }