Octopus
io_csv.c
Go to the documentation of this file.
1/*
2 Copyright (C) 2006 octopus team
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18
19*/
20
21/*
22The functions in this file read an array from an ascii matrix (csv) file.
23Format with values "valueXYZ" as follows
24
25File values.csv:
26--------
27value111 value211 value311 value411
28value121 value221 value321 value421
29value131 value231 value331 value431
30
31value112 value212 value312 value412
32value122 value222 value322 value422
33value132 value232 value332 value432
34
35value113 value213 value313 value413
36value123 value223 value323 value423
37value133 value233 value333 value433
38--------
39
40That is, every line scans the x-coordinate, every XY-plane as a table of values
41and all XY-planes separated by an empty row.
42
43The given matrix is interpolated/stretched to fit the calculation
44box defined in input file.
46Note that there must not be any empty line at the end of the file.
47
48Calculation box shape must be "parallelepiped".
49
50The delimiter can be a tab, a comma or a space.
51*/
52
53#include <assert.h>
54#include <config.h>
55#include <errno.h>
56#include <fcntl.h>
57#include <math.h>
58#include <stdint.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <sys/stat.h>
63#include <sys/types.h>
64#include <unistd.h>
65
66#include "io_binary.h"
67
68static const int size_of[6] = {4, 8, 8, 16, 4, 8};
69
70void read_csv(int64_t *np, void *f, int *output_type,
71 int *ierr, char* filename) {
72 int i;
73 FILE *fd;
74 char *buf = NULL;
75 char *c;
76 const char sep[] = "\t\n ,";
77 size_t buf_size = 0;
78
79 fd = fopen(filename, "r");
80
81 *ierr = 0;
82 if (fd == NULL) {
83 *ierr = 2;
84 return;
85 }
86
87 if ((*output_type) == TYPE_DOUBLE) {
88 double d;
89 i = 0;
90 while (getline(&buf, &buf_size, fd) != -1) {
91 c = strtok(buf, sep);
92 while (c != NULL) {
93 assert(i / 8 < *np);
94 d = strtod(c, (char **)NULL);
95 memcpy(f + i, &d, size_of[(*output_type)]);
96 c = (char *)strtok((char *)NULL, sep);
97 i += size_of[(*output_type)];
98 }
99 }
100 }
101
102 free(buf);
103 fclose(fd);
104}
105
106void get_info_csv(int64_t *dims, int *ierr, char *filename) {
107 char *buf = NULL;
108 char *c;
109 FILE *fd;
110 size_t buf_size = 0;
111 const char sep[] = "\n\t ,";
112
113 int64_t curr_dims[3] = {0, 0, 0};
114 int64_t prev_dims[2] = {0, 0};
115
116 fd = fopen(filename, "r");
117
118 *ierr = 0;
119 if (fd == NULL) {
120 *ierr = 2;
121 return;
122 }
123
124 while (getline(&buf, &buf_size, fd) != -1) {
125 c = strtok(buf, sep);
126
127 prev_dims[0] = curr_dims[0];
128
129 curr_dims[0] = 0;
130
132 while (c != NULL) {
133 curr_dims[0]++;
134 c = (char *)strtok((char *)NULL, sep);
135 }
136
140 if (prev_dims[0] > 0 && curr_dims[0] > 0)
141 assert(curr_dims[0] == prev_dims[0]);
142
145 if (prev_dims[0] == 0 && curr_dims[0] != 0) {
146 prev_dims[1] = curr_dims[1];
147 curr_dims[1] = 0;
148 curr_dims[2]++;
149
152 if (prev_dims[1] > 0 && curr_dims[1] > 0)
153 assert(prev_dims[1] == curr_dims[1]);
154 }
155
158 if (curr_dims[0] > 0)
159 curr_dims[1]++;
160 }
161
162 dims[0] = curr_dims[0];
163 dims[1] = curr_dims[1];
164 dims[2] = curr_dims[2];
165
166 if (dims[0]*dims[1]*dims[2] == 0) *ierr=1;
167
169 free(buf);
void * memcpy(void *__restrict __dest, const void *__restrict __src, size_t __n) __attribute__((__nothrow__
__int64_t int64_t
Definition: io_binary.c:1328
void read_csv(int64_t *np, void *f, int *output_type, int *ierr, char *filename)
Definition: io_csv.c:4613
int fclose(FILE *__stream)
void get_info_csv(int64_t *dims, int *ierr, char *filename)
Definition: io_csv.c:4685
integer, parameter fd
Definition: run.F90:170
ptrdiff_t i
static double f(double w, void *p)