Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / 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 ntlworld.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 #ifdef DEBUG
129         if( Vij[w][h] == 0.0)
130           zeros++;
131         if( Vij[w][h] == 1.0)
132           ones++;
133 #endif
134         if( fwrite((void *) &Ex,sizeof(double), 1, Ex_bin_fp) != 1)
135           exit_with_msg_and_exit_code("Error#1: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
136
137         if( fwrite((void *) &Ey,sizeof(double), 1, Ey_bin_fp) != 1)
138           exit_with_msg_and_exit_code("Error#2: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
139
140         if( fwrite((void *) &E,sizeof(double), 1, E_bin_fp) != 1)
141           exit_with_msg_and_exit_code("Error#3: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
142
143         if( fwrite((void *) &V,sizeof(double), 1, V_bin_fp) != 1)
144           exit_with_msg_and_exit_code("Error#4: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
145
146         if( fwrite((void *) &U,sizeof(double), 1, U_bin_fp) != 1)
147           exit_with_msg_and_exit_code("Error#5: Failed to write binary file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
148
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_two_conductor_lines.c",WRITE_FAILURE);
151       }
152     }
153   } /* end of writing binary files for 2 conductor lines */
154
155   if(data.write_bitmap_field_imagesQ==TRUE)
156   {
157     find_maximum_values(&(maximum_values), ZERO_ELEMENTS_FIRST); /* sets stucture maximum_values */
158
159     /* Allocate ram to store the bitmaps before they are written to disk */
160     image_data_Ex=ustring(0L,(long) size);
161     image_data_Ey=ustring(0L,(long) size);
162     image_data_E=ustring(0L,(long) size);
163     image_data_V=ustring(0L,(long) size);
164     image_data_Er=ustring(0L,(long) size);
165     image_data_U=ustring(0L,(long) size);
166 #ifdef WRITE_ODDITY_DATA
167     image_data_oddity=ustring(0L,(long) size);
168 #endif
169
170     Ex_bmp_fp=get_file_pointer_with_right_filename(filename,".Ex.bmp");
171     Ey_bmp_fp=get_file_pointer_with_right_filename(filename,".Ey.bmp");
172     E_bmp_fp=get_file_pointer_with_right_filename(filename,".E.bmp");
173     V_bmp_fp=get_file_pointer_with_right_filename(filename,".V.bmp");
174     U_bmp_fp=get_file_pointer_with_right_filename(filename,".U.bmp");
175     permittivity_bmp_fp=get_file_pointer_with_right_filename(filename,".Er.bmp");
176 #ifdef WRITE_ODDITY_DATA
177     oddity_bmp_fp=get_file_pointer_with_right_filename(filename,".oddity.bmp");
178 #endif
179
180     if( fwrite(bmp_buff,0x36,1,Ex_bmp_fp) != 1)
181       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);
182     if( fwrite(bmp_buff,0x36,1,Ey_bmp_fp) != 1)
183       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);
184     if( fwrite(bmp_buff,0x36,1,E_bmp_fp) != 1)
185       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);
186     if( fwrite(bmp_buff,0x36,1,V_bmp_fp) != 1)
187       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);
188     if( fwrite(bmp_buff,0x36,1,U_bmp_fp) != 1)
189       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);
190     if( fwrite(bmp_buff,0x36,1,permittivity_bmp_fp) != 1)
191       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);
192
193 #ifdef WRITE_ODDITY_DATA
194     if( fwrite(bmp_buff,0x36,1,oddity_bmp_fp) != 1)
195       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);
196 #endif 
197
198     offset=-3;
199     for(h=height-1;h>=0;h--)
200     {
201       for(w=0;w<width;++w)
202       {
203         offset+=3;
204         if((w==0) && (offset%4!=0) && (h!=0)) 
205           offset+=(4-offset%4);
206         Ex=find_Ex(w,h);
207         Ey=find_Ey(w,h);
208         E=find_E(w,h); 
209         U=find_energy_per_metre(w,h);
210         calculate_colour_data(Ex, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ex, COLOUR,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
211         calculate_colour_data(Ey, maximum_values.Ex_or_Ey_max, w, h, offset,image_data_Ey, COLOUR,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
212         calculate_colour_data(E, maximum_values.E_max, w, h, offset,image_data_E, MONOCHROME,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
213         calculate_colour_data(U, maximum_values.U_max, w, h, offset,image_data_U, MONOCHROME,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
214         calculate_colour_data(Vij[w][h], maximum_values.V_max, w, h, offset,image_data_V, COLOUR,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
215         calculate_colour_data(Er[w][h], MAX_ER, w, h, offset,image_data_Er, MIXED,&r,&g,&b,IMAGE_FIDDLE_FACTOR);
216 #ifdef WRITE_ODDITY_DATA
217         odd=(double) oddity[w][h];
218         calculate_colour_data(odd, 255 , w, h, offset,image_data_oddity, MONOCHROME,&r,&g,&b,1.0);
219 #endif
220
221       }
222     }
223
224     /* if( fwrite((void *) &(image_data_Ex[0]),size, 1, Ex_bmp_fp) != 1) */
225     if( fwrite((void *) image_data_Ex,size, 1, Ex_bmp_fp) != 1)
226       exit_with_msg_and_exit_code("Error#7: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
227     if( fclose(Ex_bmp_fp) != 0)
228       exit_with_msg_and_exit_code("Error#8: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
229
230     /* if( fwrite((void *) &(image_data_Ey[0]),size, 1, Ey_bmp_fp) != 1) */
231     if( fwrite((void *) image_data_Ey,size, 1, Ey_bmp_fp) != 1)
232       exit_with_msg_and_exit_code("Error#9: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
233     if( fclose(Ey_bmp_fp) != 0)
234       exit_with_msg_and_exit_code("Error#10: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
235
236     if( fwrite((void *) &(image_data_E[0]),size, 1, E_bmp_fp) != 1)
237       exit_with_msg_and_exit_code("Error#11: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
238     if( fclose(E_bmp_fp) != 0)
239       exit_with_msg_and_exit_code("Error#12: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
240
241     if( fwrite((void *) &(image_data_U[0]),size, 1, U_bmp_fp) != 1)
242       exit_with_msg_and_exit_code("Error#13: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
243     if( fclose(U_bmp_fp) != 0)
244       exit_with_msg_and_exit_code("Error#14: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
245
246     if( fwrite((void *) &(image_data_V[0]),size, 1, V_bmp_fp) != 1)
247       exit_with_msg_and_exit_code("Error#15: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
248     if( fclose(V_bmp_fp) != 0)
249       exit_with_msg_and_exit_code("Error#16: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
250
251     if( fwrite((void *) &(image_data_Er[0]),size, 1, permittivity_bmp_fp) != 1)
252       exit_with_msg_and_exit_code("Error#17: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
253     if( fclose(permittivity_bmp_fp) != 0)
254       exit_with_msg_and_exit_code("Error#18: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
255
256 #ifdef WRITE_ODDITY_DATA
257     if( fwrite((void *) &(image_data_oddity[0]),size, 1, oddity_bmp_fp) != 1)
258       exit_with_msg_and_exit_code("Error#19: Failed to write bitmap file in write_fields_for_two_conductor_lines.c",WRITE_FAILURE);
259     if( fclose(oddity_bmp_fp) != 0)
260       exit_with_msg_and_exit_code("Error#20: Unable to close file in write_fields_for_two_conductor_lines.c",CANT_CLOSE_FILE);
261 #endif
262
263     free_ustring(image_data_Ex,0L,(long)size);
264     free_ustring(image_data_Ey,0L,(long)size);
265     free_ustring(image_data_E,0L,(long)size);
266     free_ustring(image_data_V,0L,(long)size);
267     free_ustring(image_data_U,0L,(long)size);
268     free_ustring(image_data_Er,0L,(long)size);
269 #ifdef WRITE_ODDITY_DATA
270     free_ustring(image_data_oddity,0L,(long)size);
271 #endif
272   }
273 #ifdef DEBUG
274   printf("zeros=%d ones=%d\n",zeros,ones);
275 #endif 
276 }