File: coda-netcdf-type.c

package info (click to toggle)
coda 2.21.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,320 kB
  • sloc: ansic: 125,785; javascript: 6,732; java: 2,391; yacc: 1,007; python: 872; makefile: 615; lex: 204; sh: 99; fortran: 60; xml: 5
file content (204 lines) | stat: -rw-r--r-- 7,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright (C) 2007-2020 S[&]T, The Netherlands.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "coda-netcdf-internal.h"

#include <assert.h>
#include <stdlib.h>

void coda_netcdf_type_delete(coda_dynamic_type *type)
{
    assert(type != NULL);
    assert(type->backend == coda_backend_netcdf);

    if (type->definition->type_class == coda_array_class)
    {
        if (((coda_netcdf_array *)type)->base_type != NULL)
        {
            coda_dynamic_type_delete((coda_dynamic_type *)((coda_netcdf_array *)type)->base_type);
        }
    }
    if (((coda_netcdf_type *)type)->attributes != NULL)
    {
        coda_dynamic_type_delete((coda_dynamic_type *)((coda_netcdf_type *)type)->attributes);
    }
    if (type->definition != NULL)
    {
        coda_type_release((coda_type *)type->definition);
    }
    free(type);
}

coda_netcdf_array *coda_netcdf_array_new(int num_dims, long dim[CODA_MAX_NUM_DIMS], coda_netcdf_basic_type *base_type)
{
    coda_netcdf_array *type;
    int i;

    type = malloc(sizeof(coda_netcdf_array));
    if (type == NULL)
    {
        coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
                       (long)sizeof(coda_netcdf_array), __FILE__, __LINE__);
        return NULL;
    }
    type->backend = coda_backend_netcdf;
    type->definition = NULL;
    type->attributes = NULL;
    type->base_type = NULL;

    type->definition = coda_type_array_new(coda_format_netcdf);
    if (type->definition == NULL)
    {
        coda_dynamic_type_delete((coda_dynamic_type *)type);
        return NULL;
    }
    if (coda_type_array_set_base_type(type->definition, base_type->definition) != 0)
    {
        coda_dynamic_type_delete((coda_dynamic_type *)type);
        return NULL;
    }
    for (i = 0; i < num_dims; i++)
    {
        if (coda_type_array_add_fixed_dimension(type->definition, dim[i]) != 0)
        {
            coda_dynamic_type_delete((coda_dynamic_type *)type);
            return NULL;
        }
    }

    type->base_type = base_type;

    return type;
}

int coda_netcdf_array_set_attributes(coda_netcdf_array *type, coda_mem_record *attributes)
{
    assert(type->attributes == NULL);
    if (coda_type_set_attributes((coda_type *)type->definition, attributes->definition) != 0)
    {
        return -1;
    }
    type->attributes = attributes;

    return 0;
}

coda_netcdf_basic_type *coda_netcdf_basic_type_new(int nc_type, int64_t offset, int record_var, int64_t length)
{
    coda_netcdf_basic_type *type;
    coda_native_type read_type;
    int64_t byte_size;

    type = malloc(sizeof(coda_netcdf_basic_type));
    if (type == NULL)
    {
        coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
                       (long)sizeof(coda_netcdf_basic_type), __FILE__, __LINE__);
        return NULL;
    }
    type->backend = coda_backend_netcdf;
    type->definition = NULL;
    type->attributes = NULL;
    type->offset = offset;
    type->record_var = record_var;

    switch (nc_type)
    {
        case 1:
            read_type = coda_native_type_int8;
            byte_size = 1;
            type->definition = (coda_type *)coda_type_number_new(coda_format_netcdf, coda_integer_class);
            break;
        case 2:
            read_type = (length > 1) ? coda_native_type_string : coda_native_type_char;
            byte_size = length;
            type->definition = (coda_type *)coda_type_text_new(coda_format_netcdf);
            break;
        case 3:
            read_type = coda_native_type_int16;
            byte_size = 2;
            type->definition = (coda_type *)coda_type_number_new(coda_format_netcdf, coda_integer_class);
            break;
        case 4:
            read_type = coda_native_type_int32;
            byte_size = 4;
            type->definition = (coda_type *)coda_type_number_new(coda_format_netcdf, coda_integer_class);
            break;
        case 5:
            read_type = coda_native_type_float;
            byte_size = 4;
            type->definition = (coda_type *)coda_type_number_new(coda_format_netcdf, coda_real_class);
            break;
        case 6:
            read_type = coda_native_type_double;
            byte_size = 8;
            type->definition = (coda_type *)coda_type_number_new(coda_format_netcdf, coda_real_class);
            break;
        default:
            assert(0);
            exit(1);
    }
    if (type->definition == NULL)
    {
        coda_dynamic_type_delete((coda_dynamic_type *)type);
        return NULL;
    }
    if (coda_type_set_read_type(type->definition, read_type) != 0)
    {
        coda_dynamic_type_delete((coda_dynamic_type *)type);
        return NULL;
    }
    if (coda_type_set_byte_size(type->definition, byte_size) != 0)
    {
        coda_dynamic_type_delete((coda_dynamic_type *)type);
        return NULL;
    }

    return type;
}

int coda_netcdf_basic_type_set_conversion(coda_netcdf_basic_type *type, coda_conversion *conversion)
{
    assert(type->definition->type_class == coda_integer_class || type->definition->type_class == coda_real_class);
    return coda_type_number_set_conversion((coda_type_number *)type->definition, conversion);
}

int coda_netcdf_basic_type_set_attributes(coda_netcdf_basic_type *type, coda_mem_record *attributes)
{
    assert(type->attributes == NULL);
    if (coda_type_set_attributes(type->definition, attributes->definition) != 0)
    {
        return -1;
    }
    type->attributes = attributes;

    return 0;
}