-
Notifications
You must be signed in to change notification settings - Fork 1
/
datput.c
654 lines (569 loc) · 16.8 KB
/
datput.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#if HAVE_CONFIG_H
# include <config.h>
#endif
/*+DATPUT.C-*/
/* Include files */
#include <stdio.h> /* stdio for sprintf() prototype */
#include "ems.h" /* EMS error reporting routines */
#include "hds1.h" /* Global definitions for HDS */
#include "rec.h" /* Public rec_ definitions */
#include "str.h" /* Character string import/export macros */
#include "dat1.h" /* Internal dat_ definitions */
#include "dat_err.h" /* DAT__ error code definitions */
#include "hds.h"
/* Control Blocks */
/* Function prototype. */
/*======================*/
/* DAT_PUT - Write data */
/*======================*/
int
datPut( const HDSLoc *locator,
const char *type_str,
int ndim,
const HDS_PTYPE dims[],
const void *values,
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_PUT_ERR"
#define context_message\
"DAT_PUT: Error writing value(s) to an HDS primitive."
struct DSC type;
struct LCP *lcp;
struct LCP_DATA *data;
struct LCP_STATE *state;
char typbuf[DAT__SZTYP];
HDS_PTYPE axis[DAT__MXDIM];
struct PDD *app;
struct PDD *obj;
unsigned char *dom;
int naxes;
INT_BIG objlen;
INT_BIG objoff;
int i;
int nbad;
unsigned char *buf; /* Pointer to temporary alignment buffer */
int fixalign; /* Is alignment fixup needed? */
/* Enter routine. */
if (!_ok(*status))
return *status;
hds_gl_status = DAT__OK;
/* Import the type string. */
_strcsimp( &type, type_str );
/* Import the locator. */
_call(dat1_import_loc(locator, &lcp ));
data = &lcp->data;
state = &data->state;
/* Ensure that there is no currently mapped data and that the object is
primitive. Also check that the container file was not opened for read-
only access. */
if (state->mapped)
_call(DAT__PRMAP)
if (data->struc)
_call(DAT__OBJIN)
if (data->read)
_call(DAT__ACCON)
/* Determine the shape of the object and match the dimensions. */
_call( dau_get_shape( data, &naxes, axis ) )
if (ndim != naxes)
_call(DAT__DIMIN)
for (i=0; i<naxes; ++i)
if (dims[i] != axis[i])
_call(DAT__DIMIN)
/* Validate the application data type specification. */
_call( dat1_check_type( &type, typbuf ) )
/* Determine the attributes of the application data and reject the operation
if not primitive. */
_call( dat1_unpack_type( typbuf, &data->app ) )
app = &data->app;
if (app->class != DAT__PRIMITIVE)
_call(DAT__TYPIN)
/* Match the object and application data attributes and reject the operation
if the types are incompatible. */
obj = &data->obj;
_call( dau_match_types( obj, app ) )
/* Insert a pointer to the data into the PDD. */
app->body = (unsigned char*)values;
/* Calculate the length (in bytes) of the object data and determine the */
/* byte offset into the object record's dynamic domain. */
objlen = obj->length * data->size;
objoff = obj->length * data->offset;
/* If the application data type is _DOUBLE and data type or format */
/* conversion must occur, then test whether the application buffer is */
/* adequately aligned. */
fixalign = 0;
if ( ( app->dtype == DAT__D ) &&
( ( app->dtype != obj->dtype ) || ( app->format != obj->format ) ) )
{
fixalign = !_aligned_D( app->body );
}
/* If necessary, allocate a correctly aligned buffer to hold the */
/* application data and copy the data into it. Modify the application */
/* primitive data descriptor to point at this buffer. */
if ( fixalign )
{
rec_alloc_mem( app->length * data->size, (void **) &buf );
if ( _ok( hds_gl_status ) )
{
(void) memcpy( buf, values,
(size_t) ( app->length * data->size ) );
}
app->body = buf;
}
/* Scatter the object data if discontiguous. */
if ( state->broken )
{
dau_scatter_data( 1, data, &nbad );
}
/* Otherwise, locate the record's dynamic domain and translate the data */
/* from the source buffer. */
else
{
rec_locate_data( &data->han, objlen, objoff, 'W', &dom );
obj->body = dom;
dat1_cvt( 1, data->size, app, obj, &nbad );
rec_release_data( &data->han, objlen, objoff, 'W', &dom );
}
/* If a temporary buffer has been used to ensure correct data alignment, */
/* then deallocate it. */
if ( fixalign )
{
rec_deall_mem( app->length * data->size, (void **) &buf );
}
/* Check the global status value before return. */
_call( hds_gl_status )
return hds_gl_status;
}
/*===============================*/
/* DAT_PUTI - Write Integer data */
/*===============================*/
int
datPutI(const HDSLoc *locator,
int ndim,
const HDS_PTYPE dims[],
const int values[],
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_PUTI_ERR"
#define context_message\
"DAT_PUTI: Error writing integer values to an HDS primitive."
datPut(locator,
"_INTEGER",
ndim,
dims,
values,
status);
return hds_gl_status;
}
/*========================================*/
/* DAT_PUTK - Write 64-bit Integer data */
/*========================================*/
int
datPutK(const HDSLoc *locator,
int ndim,
const HDS_PTYPE dims[],
const int64_t values[],
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_PUTK_ERR"
#define context_message\
"DAT_PUTK: Error writing 64-bit integer values to an HDS primitive."
datPut(locator,
"_INT64",
ndim,
dims,
values,
status);
return hds_gl_status;
}
/*=====================================*/
/* DAT_PUTW - Write Short Integer data */
/*=====================================*/
int
datPutW(const HDSLoc *locator,
int ndim,
const HDS_PTYPE dims[],
const short values[],
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_PUTW_ERR"
#define context_message\
"DAT_PUTW: Error writing short integer values to an HDS primitive."
datPut(locator,
"_WORD",
ndim,
dims,
values,
status);
return hds_gl_status;
}
/*===============================================*/
/* DAT_PUTUW - Write Unsigned Short Integer data */
/*===============================================*/
int
datPutUW(const HDSLoc *locator,
int ndim,
const HDS_PTYPE dims[],
const unsigned short values[],
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_PUTUW_ERR"
#define context_message\
"DAT_PUTUW: Error writing unsigned short integer values to an HDS primitive."
datPut(locator,
"_UWORD",
ndim,
dims,
values,
status);
return hds_gl_status;
}
/*============================*/
/* DAT_PUTR - Write Real data */
/*============================*/
int
datPutR( const HDSLoc *locator,
int ndim,
const HDS_PTYPE dims[],
const float values[],
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_PUTR_ERR"
#define context_message\
"DAT_PUTR: Error writing real values to an HDS primitive."
datPut(locator,
"_REAL",
ndim,
dims,
values,
status);
return hds_gl_status;
}
/*========================================*/
/* DAT_PUTD - Write Double precision data */
/*========================================*/
int
datPutD( const HDSLoc *locator,
int ndim,
const HDS_PTYPE dims[],
const double values[],
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_PUTD_ERR"
#define context_message\
"DAT_PUTD: Error writing double precision value(s) to an HDS primitive."
datPut(locator,
"_DOUBLE",
ndim,
dims,
values,
status);
return hds_gl_status;
}
/*===============================*/
/* DAT_PUTL - Write Logical data */
/*===============================*/
int
datPutL( const HDSLoc *locator,
int ndim,
const HDS_PTYPE dims[],
const int values[],
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_PUTL_ERR"
#define context_message\
"DAT_PUTL: Error writing logical values to an HDS primitive."
datPut(locator,
"_LOGICAL",
ndim,
dims,
values,
status);
return hds_gl_status;
}
/*=================================*/
/* DAT_PUTC - Write Character data */
/*=================================*/
int
datPutC( const HDSLoc *locator,
int ndim,
const HDS_PTYPE dims[],
const char string[],
size_t string_length,
int *status)
{
/* Local variables */
char *string1;
char stype[DAT__SZTYP+1];
#undef context_name
#undef context_message
#define context_name "DAT_PUTC_ERR"
#define context_message\
"DAT_PUTC: Error writing character value(s) to an HDS primitive."
/* Encode the (fixed) string length into the primitive type definition */
/* before calling datPut */
/* Assume that a zero length string (eg. prefix=\"\") is a single space */
/* to make consistent with earlier HDS behaviour! */
if( string_length > 0 ) {
datCctyp( string_length, stype );
string1 = (char*)string;
} else {
strcpy( stype, "_CHAR" );
string1 = " ";
}
datPut(locator,
stype,
ndim,
dims,
string1,
status);
return hds_gl_status;
}
/* O N E - D I M P U T */
/*=================================*/
/* DAT_PUT1D - Write 1D double array */
/*=================================*/
int
datPut1D( const HDSLoc * locator,
size_t nval,
const double values[],
int * status ) {
size_t size;
hdsdim dim[1];
if ( *status != DAT__OK ) return *status;
datSize( locator, &size, status );
if ( *status == DAT__OK && size != nval ) {
*status = DAT__BOUND;
emsSeti( "IN", (int)nval );
emsSeti( "SZ", (int)size );
emsRep( "DAT_PUT1D_ERR", "Bounds mismatch: ^IN != ^SZ", status);
} else {
dim[0] = (hdsdim)size;
datPutD( locator, 1, dim, values, status );
}
return *status;
}
/*=================================*/
/* DAT_PUT1I - Write 1D int array */
/*=================================*/
int
datPut1I( const HDSLoc * locator,
size_t nval,
const int values[],
int * status ) {
size_t size;
hdsdim dim[1];
if ( *status != DAT__OK ) return *status;
datSize( locator, &size, status );
if ( *status == DAT__OK && size != nval ) {
*status = DAT__BOUND;
emsSeti( "IN", (int)nval );
emsSeti( "SZ", (int)size );
emsRep( "DAT_PUT1I_ERR", "Bounds mismatch: ^IN != ^SZ", status);
} else {
dim[0] = (hdsdim)size;
datPutI( locator, 1, dim, values, status );
}
return *status;
}
/*=================================*/
/* DAT_PUT1K - Write 1D 64-bit int array */
/*=================================*/
int
datPut1K( const HDSLoc * locator,
size_t nval,
const int64_t values[],
int * status ) {
size_t size;
hdsdim dim[1];
if ( *status != DAT__OK ) return *status;
datSize( locator, &size, status );
if ( *status == DAT__OK && size != nval ) {
*status = DAT__BOUND;
emsSeti( "IN", (int)nval );
emsSeti( "SZ", (int)size );
emsRep( "DAT_PUT1K_ERR", "Bounds mismatch: ^IN != ^SZ", status);
} else {
dim[0] = (hdsdim)size;
datPutK( locator, 1, dim, values, status );
}
return *status;
}
/*======================================*/
/* DAT_PUT1W - Write 1D short int array */
/*======================================*/
int
datPut1W( const HDSLoc * locator,
size_t nval,
const short values[],
int * status ) {
size_t size;
hdsdim dim[1];
if ( *status != DAT__OK ) return *status;
datSize( locator, &size, status );
if ( *status == DAT__OK && size != nval ) {
*status = DAT__BOUND;
emsSeti( "IN", (int)nval );
emsSeti( "SZ", (int)size );
emsRep( "DAT_PUT1W_ERR", "Bounds mismatch: ^IN != ^SZ", status);
} else {
dim[0] = (hdsdim)size;
datPutW( locator, 1, dim, values, status );
}
return *status;
}
/*================================================*/
/* DAT_PUT1UW - Write 1D unsigned short int array */
/*================================================*/
int
datPut1UW( const HDSLoc * locator,
size_t nval,
const unsigned short values[],
int * status ) {
size_t size;
hdsdim dim[1];
if ( *status != DAT__OK ) return *status;
datSize( locator, &size, status );
if ( *status == DAT__OK && size != nval ) {
*status = DAT__BOUND;
emsSeti( "IN", (int)nval );
emsSeti( "SZ", (int)size );
emsRep( "DAT_PUT1UW_ERR", "Bounds mismatch: ^IN != ^SZ", status);
} else {
dim[0] = (hdsdim)size;
datPutUW( locator, 1, dim, values, status );
}
return *status;
}
/*=================================*/
/* DAT_PUT1R - Write 1D float array */
/*=================================*/
int
datPut1R( const HDSLoc * locator,
size_t nval,
const float values[],
int * status ) {
size_t size;
hdsdim dim[1];
if ( *status != DAT__OK ) return *status;
datSize( locator, &size, status );
if ( *status == DAT__OK && size != nval ) {
*status = DAT__BOUND;
emsSeti( "IN", (int)nval );
emsSeti( "SZ", (int)size );
emsRep( "DAT_PUT1R_ERR", "Bounds mismatch: ^IN != ^SZ", status);
} else {
dim[0] = (hdsdim)size;
datPutR( locator, 1, dim, values, status );
}
return *status;
}
/*=================================*/
/* DAT_PUT1L - Write 1D logical array */
/*=================================*/
int
datPut1L( const HDSLoc * locator,
size_t nval,
const int values[],
int * status ) {
size_t size;
hdsdim dim[1];
if ( *status != DAT__OK ) return *status;
datSize( locator, &size, status );
if ( *status == DAT__OK && size != nval ) {
*status = DAT__BOUND;
emsSeti( "IN", (int)nval );
emsSeti( "SZ", (int)size );
emsRep( "DAT_PUT1L_ERR", "Bounds mismatch: ^IN != ^SZ", status);
} else {
dim[0] = (hdsdim)size;
datPutL( locator, 1, dim, values, status );
}
return *status;
}
/* V E C T O R I Z E D P U T */
/*=================================*/
/* DAT_PUTVD - Write vectorized doubles */
/*=================================*/
int
datPutVD( const HDSLoc * locator,
size_t nval,
const double values[],
int *status ) {
HDSLoc *vec = NULL;
datVec( locator, &vec, status );
datPut1D( vec, nval, values, status );
datAnnul( &vec, status );
return *status;
}
/*==================================*/
/* DAT_PUTVI - Write vectorized int */
/*==================================*/
int
datPutVI( const HDSLoc * locator,
size_t nval,
const int values[],
int *status ) {
HDSLoc *vec = NULL;
datVec( locator, &vec, status );
datPut1I( vec, nval, values, status );
datAnnul( &vec, status );
return *status;
}
/*====================================*/
/* DAT_PUTVK - Write vectorized int64 */
/*====================================*/
int
datPutVK( const HDSLoc * locator,
size_t nval,
const int64_t values[],
int *status ) {
HDSLoc *vec = NULL;
datVec( locator, &vec, status );
datPut1K( vec, nval, values, status );
datAnnul( &vec, status );
return *status;
}
/*====================================*/
/* DAT_PUTVR - Write vectorized float */
/*====================================*/
int
datPutVR( const HDSLoc * locator,
size_t nval,
const float values[],
int *status ) {
HDSLoc *vec = NULL;
datVec( locator, &vec, status );
datPut1R( vec, nval, values, status );
datAnnul( &vec, status );
return *status;
}
/*=================================*/
/* DAT_PUTVL - Write vectorized logical */
/*=================================*/
int
datPutVL( const HDSLoc * locator,
size_t nval,
const int values[],
int *status ) {
HDSLoc *vec = NULL;
datVec( locator, &vec, status );
datPut1L( vec, nval, values, status );
datAnnul( &vec, status );
return *status;
}