missed one!
[debian/atlc] / src / write_fields_for_two_conductor_lines.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_STRING_H
30 #include <string.h>
31 #endif
32
33 #ifdef HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
36
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40
41 #ifdef HAVE_STDIO_H
42 #include <stdio.h>
43 #endif
44
45 #include "exit_codes.h"
46
47 extern double **Vij;
48 extern double **Er;
49 extern unsigned char *bmp_buff;
50 extern int width, height, errno;
51 #ifdef WRITE_ODDITY_DATA
52 extern unsigned char **oddity;
53 #endif
54
55 /* Write the following files, assuming an input of example.bmp 
56 where example.bmp is a 2 conductor transmission lines. For
57 3 conductor transmission lines (couplers) the function
58
59
60 example.E.bmp   Grayscale Bitmap of |E-field|, normallised to 1,
61 but corrected for Gamma
62 example.Ex.bmp  Colour Bitmap of x-directed E-field, normallised to 1, 
63 but corrected for Gamma
64 example.Ey.bmp  Colour Bitmap of y-directed E-field, normallised to 1, 
65 but corrected for Gamma
66 example.V.bmp   Colour Bitmap of Voltage field, normallised to 1, but 
67 corrected for Gamma
68 eexample.U.bmp  Grayscale bitmap, with just the energy (U=CV^2).
69
70 example.Ex.bin  binary file, with just the x-directed E-field 
71 (in volts/pixel) as doubles 
72 example.Ey.bin  binary file, with just the y-directed E-field 
73 (in volts/pixel) as doubles 
74 example.E.bin   binary file, with just the E-field {sqrt(Ex^2+Ey^2)} 
75 (in volts/pixel) as doubles 
76 example.V.bin   binary file, with just the Voltage as doubles 
77 eexample.U.bin  binary file, with just the energy (U=CV^2).
78
79 */
80
81 extern double image_fiddle_factor;
82
83
84 void write_fields_for_two_conductor_lines(char * filename, struct transmission_line_properties data, size_t size)
85 {
86   FILE *Ex_bin_fp=NULL, *Ey_bin_fp=NULL;
87   FILE *E_bin_fp=NULL, *V_bin_fp, *U_bin_fp=NULL;
88   FILE *Ex_bmp_fp=NULL, *Ey_bmp_fp=NULL;
89   FILE *E_bmp_fp=NULL, *V_bmp_fp=NULL, *U_bmp_fp=NULL;
90 #ifdef WRITE_ODDITY_DATA
91   FILE *oddity_bmp_fp=NULL;
92   double odd;
93 #endif
94   unsigned char r, g, b;
95   FILE *permittivity_bin_fp=NULL, *permittivity_bmp_fp=NULL;
96   struct max_values maximum_values;
97   int offset=-3, w, h;
98   double V, E, Ex, Ey, U;
99
100   unsigned char *image_data_Ex=NULL; 
101   unsigned char *image_data_Ey=NULL;
102   unsigned char *image_data_E=NULL;
103   unsigned char *image_data_U=NULL; 
104   unsigned char *image_data_V=NULL;
105   unsigned char *image_data_Er=NULL;
106 #ifdef WRITE_ODDITY_DATA
107   unsigned char *image_data_oddity=NULL;
108 #endif 
109
110
111   if(data.write_binary_field_imagesQ==TRUE)
112   {
113     Ex_bin_fp=get_file_pointer_with_right_filename(filename,".Ex.bin");
114     Ey_bin_fp=get_file_pointer_with_right_filename(filename,".Ey.bin");
115     E_bin_fp=get_file_pointer_with_right_filename(filename,".E.bin");
116     V_bin_fp=get_file_pointer_with_right_filename(filename,".V.bin");
117     U_bin_fp=get_file_pointer_with_right_filename(filename,".U.bin");
118     permittivity_bin_fp=get_file_pointer_with_right_filename(filename,".Er.bin");
119     for(h=height-1;h>=0;h--)
120     {
121       for(w=0;w<width;++w)
122       {
123         V=Vij[w][h];
124         Ex=find_Ex(w,h);
125         Ey=find_Ey(w,h);
126         E=find_E(w,h); 
127         U=find_energy_per_metre(w,h);
128         if( fwrite((void *) &Ex,sizeof(double), 1, Ex_bin_fp) != 1)
129           exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
130
131         if( fwrite((void *) &Ey,sizeof(double), 1, Ey_bin_fp) != 1)
132           exit_with_msg_and_exit_code("Error#2: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
133
134         if( fwrite((void *) &E,sizeof(double), 1, E_bin_fp) != 1)
135           exit_with_msg_and_exit_code("Error#3: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
136
137         if( fwrite((void *) &V,sizeof(double), 1, V_bin_fp) != 1)
138           exit_with_msg_and_exit_code("Error#4: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
139
140         if( fwrite((void *) &U,sizeof(double), 1, U_bin_fp) != 1)
141           exit_with_msg_and_exit_code("Error#5: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
142
143         if( fwrite((void *) &Er[w][h],sizeof(double), 1, permittivity_bin_fp) != 1)
144           exit_with_msg_and_exit_code("Error#6: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
145       }
146     }
147   } /* end of writing binary files for 2 conductor lines */
148
149   if(data.write_bitmap_field_imagesQ==TRUE)
150   {
151     find_maximum_values(&(maximum_values), ZERO_ELEMENTS_FIRST); /* sets stucture maximum_values */
152
153     /* Allocate ram to store the bitmaps before they are written to disk */
154     image_data_Ex=ustring(0L,(long) size);
155     image_data_Ey=ustring(0L,(long) size);
156     image_data_E=ustring(0L,(long) size);
157     image_data_V=ustring(0L,(long) size);
158     image_data_Er=ustring(0L,(long) size);
159     image_data_U=ustring(0L,(long) size);
160 #ifdef WRITE_ODDITY_DATA
161     image_data_oddity=ustring(0L,(long) size);
162 #endif
163
164     Ex_bmp_fp=get_file_pointer_with_right_filename(filename,".Ex.bmp");
165     Ey_bmp_fp=get_file_pointer_with_right_filename(filename,".Ey.bmp");
166     E_bmp_fp=get_file_pointer_with_right_filename(filename,".E.bmp");
167     V_bmp_fp=get_file_pointer_with_right_filename(filename,".V.bmp");
168     U_bmp_fp=get_file_pointer_with_right_filename(filename,".U.bmp");
169     permittivity_bmp_fp=get_file_pointer_with_right_filename(filename,".Er.bmp");
170 #ifdef WRITE_ODDITY_DATA
171     oddity_bmp_fp=get_file_pointer_with_right_filename(filename,".oddity.bmp");
172 #endif
173
174     if( fwrite(bmp_buff,0x36,1,Ex_bmp_fp) != 1)
175       exit_with_msg_and_exit_code("failed to write file with the pointer Ex_bmp_fp in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
176     if( fwrite(bmp_buff,0x36,1,Ey_bmp_fp) != 1)
177       exit_with_msg_and_exit_code("failed to write file with the pointer Ey_bmp_fp in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
178     if( fwrite(bmp_buff,0x36,1,E_bmp_fp) != 1)
179       exit_with_msg_and_exit_code("failed to write file with the pointer E_bmp_fp in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
180     if( fwrite(bmp_buff,0x36,1,V_bmp_fp) != 1)
181       exit_with_msg_and_exit_code("failed to write file with the pointer V_bmp_fp in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
182     if( fwrite(bmp_buff,0x36,1,U_bmp_fp) != 1)
183       exit_with_msg_and_exit_code("failed to write file with the pointer U_bmp_fp in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
184     if( fwrite(bmp_buff,0x36,1,permittivity_bmp_fp) != 1)
185       exit_with_msg_and_exit_code("failed to write file with the pointer permittivity_bmp_fp in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
186
187 #ifdef WRITE_ODDITY_DATA
188     if( fwrite(bmp_buff,0x36,1,oddity_bmp_fp) != 1)
189       exit_with_msg_and_exit_code("failed to write file with the pointer oddity_bmp_fp in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
190 #endif 
191
192     offset=-3;
193     for(h=height-1;h>=0;h--)
194     {
195       for(w=0;w<width;++w)
196       {
197         offset+=3;
198         if((w==0) && (offset%4!=0) && (h!=0)) 
199           offset+=(4-offset%4);
200         Ex=find_Ex(w,h);
201         Ey=find_Ey(w,h);
202         E=find_E(w,h); 
203         U=find_energy_per_metre(w,h);
204         calculate_colour_data(Ex, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ex, COLOUR,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
205         calculate_colour_data(Ey, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ey, COLOUR,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
206         calculate_colour_data(E, maximum_values.E_max, w, h, offset,image_data_E, MONOCHROME,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
207         calculate_colour_data(U, maximum_values.U_max, w, h, offset,image_data_U, MONOCHROME,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
208         calculate_colour_data(Vij[w][h], maximum_values.V_max, w, h, offset,image_data_V, COLOUR,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
209         calculate_colour_data(Er[w][h], MAX_ER, w, h, offset,image_data_Er, MIXED,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
210 #ifdef WRITE_ODDITY_DATA
211         odd=(double) oddity[w][h];
212         calculate_colour_data(odd, 255 , w, h, offset,image_data_oddity, MONOCHROME,&r,&g,&b,1.0);
213 #endif
214
215       }
216     }
217
218     /* if( fwrite((void *) &(image_data_Ex[0]),size, 1, Ex_bmp_fp) != 1) */
219     if( fwrite((void *) image_data_Ex,size, 1, Ex_bmp_fp) != 1)
220       exit_with_msg_and_exit_code("Error#7: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
221     if( fclose(Ex_bmp_fp) != 0)
222       exit_with_msg_and_exit_code("Error#8: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
223
224     /* if( fwrite((void *) &(image_data_Ey[0]),size, 1, Ey_bmp_fp) != 1) */
225     if( fwrite((void *) image_data_Ey,size, 1, Ey_bmp_fp) != 1)
226       exit_with_msg_and_exit_code("Error#9: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
227     if( fclose(Ey_bmp_fp) != 0)
228       exit_with_msg_and_exit_code("Error#10: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
229
230     if( fwrite((void *) &(image_data_E[0]),size, 1, E_bmp_fp) != 1)
231       exit_with_msg_and_exit_code("Error#11: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
232     if( fclose(E_bmp_fp) != 0)
233       exit_with_msg_and_exit_code("Error#12: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
234
235     if( fwrite((void *) &(image_data_U[0]),size, 1, U_bmp_fp) != 1)
236       exit_with_msg_and_exit_code("Error#13: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
237     if( fclose(U_bmp_fp) != 0)
238       exit_with_msg_and_exit_code("Error#14: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
239
240     if( fwrite((void *) &(image_data_V[0]),size, 1, V_bmp_fp) != 1)
241       exit_with_msg_and_exit_code("Error#15: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
242     if( fclose(V_bmp_fp) != 0)
243       exit_with_msg_and_exit_code("Error#16: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
244
245     if( fwrite((void *) &(image_data_Er[0]),size, 1, permittivity_bmp_fp) != 1)
246       exit_with_msg_and_exit_code("Error#17: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
247     if( fclose(permittivity_bmp_fp) != 0)
248       exit_with_msg_and_exit_code("Error#18: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
249
250 #ifdef WRITE_ODDITY_DATA
251     if( fwrite((void *) &(image_data_oddity[0]),size, 1, oddity_bmp_fp) != 1)
252       exit_with_msg_and_exit_code("Error#19: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
253     if( fclose(oddity_bmp_fp) != 0)
254       exit_with_msg_and_exit_code("Error#20: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
255 #endif
256
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 #ifdef WRITE_ODDITY_DATA
264     free_ustring(image_data_oddity,0L,(long)size);
265 #endif
266   }
267 }