1 /*
2  * Copyright (c) 2005 Sandia Corporation. Under the terms of Contract
3  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Governement
4  * retains certain rights in this software.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *     * Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials provided
16  *       with the distribution.
17  *
18  *     * Neither the name of Sandia Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 /*!
37 *
38 * expfrm - ex_put_coordinate_frames: write coordinate frames
39 *
40 * \param exoid          exodus file id
41 * \param nframes        number of coordinate frames in model
42 * \param cf_ids         coordinate ids
43 * \param pt_coordinates pointer to coordinates. 9 values per coordinate frame
44 * \param tags           character tag for each frame. 'r' - rectangular, 'c' - cylindrical, 's' - spherical
45 *
46 * returns -
47 *      EX_NOERR         for no error
48 *      EX_FATAL         for fatal errors
49 *      1                number frames < 0
50 *
51 *****************************************************************************/
52 
53 #include <assert.h>
54 #include <string.h>
55 #include "exodusII.h"
56 #include "exodusII_int.h"
57 
58 /* -------------------- local defines --------------------------- */
59 #define PROCNAME "ex_put_coordinate_frames"
60 /* -------------------- end of local defines -------------------- */
ex_put_coordinate_frames(int exoid,int nframes,const int cf_ids[],void * pt_coordinates,const char * tags)61 int ex_put_coordinate_frames( int exoid, int nframes, const int cf_ids[],
62                               void* pt_coordinates, const char* tags)
63 {
64   int status;
65   int dim, dim9;                   /* dimension id for nframes, nframes*9 */
66   char errmsg[MAX_ERR_LENGTH];     /* buffer for error messages      */
67   int varcoords;                   /* variable id for the coordinates */
68   int varids;                      /* variable id for the frame ids  */
69   int vartags;                     /* variable id for the frame tags */
70   int i;                           /* general indices */
71 
72   if ( exoid < 0 )
73     return exoid;
74 
75   if ( nframes == 0 ) /* write nothing */
76     return (EX_NOERR);
77 
78   if ( nframes<0 )
79     return 1;
80 
81   assert( cf_ids!=0 );
82   assert( pt_coordinates !=0 );
83   assert( tags != 0 );
84 
85   /* make the definitions */
86   /* go into define mode. define num_frames, num_frames9 */
87   if ((status = nc_redef (exoid)) != NC_NOERR) {
88     exerrval = status;
89     sprintf(errmsg,"Error: failed to place file id %d into define mode",
90             exoid);
91     ex_err(PROCNAME,errmsg,exerrval);
92     return (EX_FATAL);
93   }
94 
95   if ((status = nc_def_dim(exoid, DIM_NUM_CFRAMES, nframes, &dim)) != NC_NOERR  ||
96       (nc_def_dim(exoid, DIM_NUM_CFRAME9, nframes*9, &dim9) != NC_NOERR)) {
97     exerrval = status;
98     sprintf(errmsg,
99 	    "Error: failed to define number of coordinate frames in file id %d",
100 	    exoid);
101     ex_err(PROCNAME,errmsg,exerrval);
102     goto error_ret;
103   }
104 
105   /* define the variables. coordinates, tags and ids */
106   if (nc_def_var (exoid, VAR_FRAME_COORDS,
107 		  nc_flt_code(exoid), 1, &dim9, &varcoords) != NC_NOERR ||
108       (nc_def_var(exoid, VAR_FRAME_IDS,NC_INT, 1, &dim, &varids) != NC_NOERR) ||
109       (nc_def_var(exoid, VAR_FRAME_TAGS,NC_CHAR,1,&dim, &vartags) != NC_NOERR) ) {
110     exerrval = EX_FATAL;
111     sprintf(errmsg,
112 	    "Error:  failed to define coordinate frames in file id %d",
113 	    exoid);
114     ex_err(PROCNAME,errmsg,exerrval);
115     goto error_ret;         /* exit define mode and return */
116   }
117 
118   /* leave define mode */
119   if ((status = nc_enddef (exoid)) != NC_NOERR) {
120     exerrval = status;
121     sprintf(errmsg,
122 	    "Error: failed to complete coordinate frame definition in file id %d",
123 	    exoid);
124     ex_err(PROCNAME,errmsg,exerrval);
125     return (EX_FATAL);
126   }
127 
128   /* check variables consistency */
129   exerrval = EX_NOERR;
130   for (i=0;i<nframes;i++)
131     if ( strchr("RrCcSs",tags[i])==0 ){
132       sprintf(errmsg,"Warning: Unrecognized coordinate frame tag: '%c'.",
133 	      tags[i]);
134       exerrval=2;
135       ex_err(PROCNAME,errmsg,exerrval);
136     }
137   /* could also check vectors. Leave this up to the application */
138 
139   /* put the variables into the file */
140   if (  nc_put_var_text(exoid, vartags, tags) != NC_NOERR ||
141 	nc_put_var_int(exoid, varids, cf_ids) != NC_NOERR) {
142     exerrval = status;
143     sprintf(errmsg,
144 	    "Error: failed writing frame data in file id %d",exoid);
145     ex_err(PROCNAME,errmsg,exerrval);
146     return (EX_FATAL);
147   }
148 
149   if (ex_comp_ws(exoid) == 4) {
150     status = nc_put_var_float(exoid, varcoords, pt_coordinates);
151   } else {
152     status = nc_put_var_double(exoid, varcoords, pt_coordinates);
153   }
154 
155   if (status != NC_NOERR) {
156     exerrval = status;
157     sprintf(errmsg,
158 	    "Error: failed writing frame data in file id %d",exoid);
159     ex_err(PROCNAME,errmsg,exerrval);
160     return (EX_FATAL);
161   }
162   return (EX_NOERR);
163 
164  error_ret:
165   if (nc_enddef (exoid) != NC_NOERR) {    /* exit define mode */
166     sprintf(errmsg,
167 	    "Error: failed to complete frame definition for file id %d",
168 	    exoid);
169     ex_err(PROCNAME,errmsg,exerrval);
170   }
171   return (EX_FATAL);
172 
173 
174 }
175 
176 
177