]> Pileus Git - ~andy/rsl/blob - toolkit_memory_mgt.c
RSL v1.44
[~andy/rsl] / toolkit_memory_mgt.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1996, 1997
5             Mike Kolander
6             Space Applications Corporation
7             Vienna, Virginia
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU Library General Public
11     License as published by the Free Software Foundation; either
12     version 2 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17     Library General Public License for more details.
18
19     You should have received a copy of the GNU Library General Public
20     License along with this library; if not, write to the Free
21     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #ifdef HAVE_LIBTSDISTK
27 /******************************************************************
28
29   Memory management functions used for the dynamic allocation/
30   deallocation of TSDIS Toolkit GV_Level_1 structures, created when
31         reading/writing to an HDF file.
32
33         Functions herein are called by RSL_radar_to_hdf() and RSL_hdf_to_radar().
34
35 *******************************************************************/
36
37 /* TSDIS toolkit function and structure definitions. */
38 #include "IO.h"
39 #include "IO_GV.h"
40
41 /* ------ Functions defined in this file. ------- */
42 /*
43 void TKfreeGVL1(L1B_1C_GV *gvl1);
44 int8 ***TKnewParmData1byte(int nsweep, int nray, int ncell);
45 int16 ***TKnewParmData2byte(int nsweep, int nray, int ncell);
46 PARAMETER *TKnewGVL1parm(void);
47 L1B_1C_GV *TKnewGVL1(void);
48 */
49
50 /*************************************************************/
51 /*                                                           */
52 /*                       TKfreeGVL1                          */
53 /*                                                           */
54 /*************************************************************/
55 void TKfreeGVL1(L1B_1C_GV *gvl1)
56 {
57 /* Frees all memory which was (dynamically) allocated to the toolkit
58          'L1B_1C_GV' structure, and associated substructures. 
59 */
60         int pindex;
61         
62         for (pindex=0; pindex<MAX_PARM; pindex++)
63         {
64           if (gvl1->sensor.parm[pindex] != NULL)
65                 {
66                         if (gvl1->sensor.parm[pindex]->parmData1byte != NULL)
67                         {
68                                 free(gvl1->sensor.parm[pindex]->parmData1byte[0][0]);
69                                 free(gvl1->sensor.parm[pindex]->parmData1byte[0]);
70                                 free(gvl1->sensor.parm[pindex]->parmData1byte);
71                         }  /* end if (gvl1->sensor.parm[pindex]->parmData1byte != NULL) */
72                         else if (gvl1->sensor.parm[pindex]->parmData2byte != NULL)
73                         {
74                                 free(gvl1->sensor.parm[pindex]->parmData2byte[0][0]);
75                                 free(gvl1->sensor.parm[pindex]->parmData2byte[0]);
76                                 free(gvl1->sensor.parm[pindex]->parmData2byte);
77                         }  /* end if (gvl1->sensor.parm[pindex]->parmData1byte != NULL) */
78                         free(gvl1->sensor.parm[pindex]);
79                 }  /* end if (gvl1->sensor.parm[pindex] != NULL)*/
80         }  /* end for (pindex=0;*/
81
82         free(gvl1);
83 }
84
85 /*************************************************************/
86 /*                                                           */
87 /*                       TKnewParmData1byte                  */
88 /*                                                           */
89 /*************************************************************/
90 int8 ***TKnewParmData1byte(int nsweep, int nray, int ncell)
91 {
92 /* Allocates memory for indexed storage of a 3_dimensional array 
93          of 1_byte mask values associated with one parameter of the
94          toolkit 'L1B_1C_GV' structure.
95 */
96         int8 ***parmData1byte;
97         int8 **ray;
98         int8 *data;
99         int sindex, rindex;
100         
101         /* Vector for 'nsweep' sweep pointers. */
102         parmData1byte = (int8 ***)calloc(nsweep, sizeof(int8 **));
103         /* 2D array for 'nsweep*nray' ray pointers. */
104         ray = (int8 **)calloc(nsweep*nray, sizeof(int8 *));
105         /* 3D array for data. */
106         data = (int8 *)calloc(nsweep*nray*ncell, sizeof(int8));
107
108         /* Fill all the sweep and ray pointer slots created above. */
109   for (sindex=0; sindex<nsweep; sindex++)
110         {
111                 parmData1byte[sindex] = (int8 **)(ray + sindex*nray);
112                 for (rindex=0; rindex<nray; rindex++)
113                         parmData1byte[sindex][rindex] = (int8 *)(data + sindex*nray*ncell +
114                                                                                                                                                                                          rindex*ncell);
115   }
116
117         return(parmData1byte);
118 }
119
120 /*************************************************************/
121 /*                                                           */
122 /*                       TKnewParmData2byte                  */
123 /*                                                           */
124 /*************************************************************/
125 int16 ***TKnewParmData2byte(int nsweep, int nray, int ncell)
126 {
127 /* Allocates memory for storage of a 3_dimensional array of 2_byte
128          data values associated with one parameter of the toolkit 
129          'L1B_1C_GV' structure.
130 */
131         int16 ***parmData2byte;
132         int16 **ray;
133         int16 *data;    
134         int sindex, rindex;
135         
136         /* Vector for 'nsweep' sweep pointers. */
137         parmData2byte = (int16 ***)calloc(nsweep, sizeof(int16 **));
138         /* 2D array for 'nsweep*nray' ray pointers. */
139         ray = (int16 **)calloc(nsweep*nray, sizeof(int16 *));
140         /* 3D array for data. */
141         data = (int16 *)calloc(nsweep*nray*ncell, sizeof(int16));
142
143         /* Fill all the sweep and ray pointer slots created above. */
144   for (sindex=0; sindex<nsweep; sindex++)
145         {
146                 parmData2byte[sindex] = (int16 **)(ray + sindex*nray);
147                 for (rindex=0; rindex<nray; rindex++)
148                         parmData2byte[sindex][rindex] = (int16 *)(data + sindex*nray*ncell +
149                                                                                                                                                                                                 rindex*ncell);
150   }
151
152         return(parmData2byte);
153 }
154
155 /*************************************************************/
156 /*                                                           */
157 /*                       TKnewGVL1parm                       */
158 /*                                                           */
159 /*************************************************************/
160 PARAMETER *TKnewGVL1parm(void)
161 {
162 /* Allocates memory for a 'PARAMETER' structure. */
163         PARAMETER *parm;
164
165         parm = (PARAMETER *)calloc(1, sizeof(PARAMETER));
166         parm->parmData1byte = (int8 ***)NULL;
167         parm->parmData2byte = (int16 ***)NULL;
168         return(parm);
169 }
170
171 /*************************************************************/
172 /*                                                           */
173 /*                          TKnewGVL1                        */
174 /*                                                           */
175 /*************************************************************/
176 L1B_1C_GV *TKnewGVL1(void)
177 {
178 /* Allocates memory for a 'L1B_1C_GV' structure. */
179         L1B_1C_GV *gvl1;
180         int pindex;
181         
182         gvl1 = (L1B_1C_GV *)calloc(1, sizeof(L1B_1C_GV));
183         for (pindex=0; pindex<MAX_PARM; pindex++)
184           gvl1->sensor.parm[pindex] = (PARAMETER *)NULL;
185
186         return(gvl1);
187 }
188
189 #endif