SDDSlib
Loading...
Searching...
No Matches
atest1.c
1/*************************************************************************\
2* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3* National Laboratory.
4* Copyright (c) 2002 The Regents of the University of California, as
5* Operator of Los Alamos National Laboratory.
6* This file is distributed subject to a Software License Agreement found
7* in the file LICENSE that is included with this distribution.
8\*************************************************************************/
9
10/*
11 $Log: not supported by cvs2svn $
12 Revision 1.2 2001/08/29 18:08:40 borland
13 Added a 3-D array plus nonequispaced y values.
14
15 Revision 1.1 2001/08/29 17:56:02 borland
16 First version.
17
18 Revision 1.3 1995/12/12 04:26:45 borland
19 Added SDDS_SetNoRowCounts() routine, plus support for writing and appending
20 to files with no_row_counts=1.
21
22 * Revision 1.2 1995/09/05 21:11:28 saunders
23 * First test release of the SDDS1.5 package.
24 *
25 */
26#include "mdb.h"
27#include "SDDS.h"
28
29#define NX 40
30#define NY 200
31#define NZ 5
32
33main() {
34 long d2[2] = {NX, NY}, dx[1] = {NX}, dy[1] = {NY}, dz[1] = {NZ};
35 long d3[3] = {NX, NY, NZ};
36 double **p2, *x, *y, ***p3, *z;
37 long i, j, k;
38 SDDS_DATASET SDDS_dataset;
39
40 /* make pointer arrays that index the data--note that the data
41 * is NOT copied here. All I'm doing is setting up multi-level
42 * pointer arrays into the existing data.
43 */
44 if (!(p2 = (double **)zarray_2d(sizeof(double), d2[0], d2[1])) ||
45 !(p3 = (double ***)zarray_2d(sizeof(**p3), d3[0], d3[1])) ||
46 !(x = malloc(sizeof(double) * d2[0])) || !(y = malloc(sizeof(double) * d2[1])) ||
47 !(z = malloc(sizeof(double) * d3[2]))) {
48 SDDS_PrintErrors(stderr, 1);
49 exit(1);
50 }
51
52 for (i = 0; i < NX; i++) {
53 for (j = 0; j < NY; j++) {
54 if (!(p3[i][j] = malloc(sizeof(***p3) * NZ)))
55 bomb("memory allocation failure", NULL);
56 }
57 }
58
59 /* set up to put data into file "atest.out" */
60 if (!SDDS_InitializeOutput(&SDDS_dataset, SDDS_ASCII, 1, "description 1", "description 2", "atest.out")) {
61 SDDS_PrintErrors(stderr, 1);
62 exit(1);
63 }
64
65 /* define double arrays */
66 if (SDDS_DefineArray(&SDDS_dataset, "p2", NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0, 2, NULL) < 0 ||
67 SDDS_DefineArray(&SDDS_dataset, "p3", NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0, 3, NULL) < 0 ||
68 SDDS_DefineArray(&SDDS_dataset, "x", NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0, 1, NULL) < 0 ||
69 SDDS_DefineArray(&SDDS_dataset, "y", NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0, 1, NULL) < 0 ||
70 SDDS_DefineArray(&SDDS_dataset, "z", NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0, 1, NULL) < 0) {
71 SDDS_PrintErrors(stderr, 1);
72 exit(1);
73 }
74 /* write the layout and give SDDS the data for the first page */
75 if (!SDDS_SetNoRowCounts(&SDDS_dataset, 1)) {
76 SDDS_PrintErrors(stderr, 1);
77 exit(1);
78 }
79
80 for (i = 0; i < d2[0]; i++) {
81 for (j = 0; j < d2[1]; j++) {
82 x[i] = (1.0 * i) / d2[0];
83 y[j] = sqr(sqr((1.0 * j) / d2[1]));
84
85 p2[i][j] = x[i] + y[j] * y[j];
86 }
87 }
88
89 for (i = 0; i < d3[0]; i++) {
90 for (j = 0; j < d3[1]; j++) {
91 for (k = 0; k < d3[2]; k++) {
92 z[k] = k;
93 p3[i][j][k] = (k + 1) * (x[i] + y[j] * y[j]);
94 }
95 }
96 }
97
98 if (!SDDS_WriteLayout(&SDDS_dataset) || !SDDS_StartPage(&SDDS_dataset, 0) ||
99 !SDDS_SetArray(&SDDS_dataset, "p2", SDDS_POINTER_ARRAY, p2, d2) ||
100 !SDDS_SetArray(&SDDS_dataset, "p3", SDDS_POINTER_ARRAY, p3, d3) ||
101 !SDDS_SetArray(&SDDS_dataset, "x", SDDS_POINTER_ARRAY, x, dx) ||
102 !SDDS_SetArray(&SDDS_dataset, "y", SDDS_POINTER_ARRAY, y, dy) ||
103 !SDDS_SetArray(&SDDS_dataset, "z", SDDS_POINTER_ARRAY, z, dz)) {
104 SDDS_PrintErrors(stderr, 1);
105 exit(1);
106 }
107 /* write out the data */
108 if (!SDDS_WritePage(&SDDS_dataset) || !SDDS_Terminate(&SDDS_dataset)) {
109 SDDS_PrintErrors(stderr, 1);
110 exit(1);
111 }
112 fprintf(stderr, "No errors detected\n");
113}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetArray(SDDS_DATASET *SDDS_dataset, char *array_name, int32_t mode, void *data_pointer, int32_t *dimension)
Sets the values of an array variable in the SDDS dataset using specified dimensions.
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_DefineArray(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length, int32_t dimensions, const char *group_name)
Defines a data array within the SDDS dataset.
int32_t SDDS_SetNoRowCounts(SDDS_DATASET *SDDS_dataset, int32_t value)
Sets the flag to enable or disable row counts in the SDDS dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
void ** zarray_2d(uint64_t size, uint64_t n1, uint64_t n2)
Allocates a 2D array with specified dimensions.
Definition array.c:93
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26