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 value112 value113
28value121 value122 value123
29value131 value132 value133
30
31value211 value212 value213
32value221 value222 value223
33value231 value232 value233
34
35value311 value312 value313
36value321 value322 value323
37value331 value332 value333
38--------
39
40That is, every XY-plane as a table of values and all XY-planes separated by an
41empty row.
42
43The given matrix is interpolated/stretched to fit the calculation
44box defined in input file.
46Calculation box shape must be "parallelepiped".
47
48The delimiter can be a tab, a comma or a space.
49*/
50
51#include <assert.h>
52#include <config.h>
53#include <errno.h>
54#include <fcntl.h>
55#include <math.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <sys/stat.h>
61#include <sys/types.h>
62#include <unistd.h>
63
64#include "string_f.h"
65
66#include "io_binary.h"
67
68static const int size_of[6] = {4, 8, 8, 16, 4, 8};
69
70void FC_FUNC_(read_csv, READ_CSV)(unsigned long *np, void *f, int *output_type,
71 int *ierr, STR_F_TYPE fname STR_ARG1) {
72 char *filename;
73 int i;
74 FILE *fd;
75 char *buf;
76 char *c;
77 const char sep[] = "\t\n ,";
78 int buf_size = 65536;
79
80 TO_C_STR1(fname, filename);
81 fd = fopen(filename, "r");
82 free(filename);
83
84 *ierr = 0;
85 if (fd == NULL) {
86 *ierr = 2;
87 return;
88 }
89
90 buf = (char *)malloc(buf_size * sizeof(char));
91 assert(buf != NULL);
92
93 if ((*output_type) == TYPE_FLOAT) {
94 i = 0;
95 while (fgets(buf, buf_size * sizeof(char), fd) != NULL) {
96 float d;
97 c = strtok(buf, sep);
98 while (c != NULL) {
99 assert(i / 8 < *np);
100 d = strtof(c, (char **)NULL);
101 c = (char *)strtok((char *)NULL, sep);
102 memcpy(f + i, &d, size_of[(*output_type)]);
103 i += size_of[(*output_type)];
104 }
105 }
106 } else if ((*output_type) == TYPE_DOUBLE) {
107 double d;
108 i = 0;
109 while (fgets(buf, buf_size * sizeof(char), fd) != NULL) {
110 c = strtok(buf, sep);
111 while (c != NULL) {
112 assert(i / 8 < *np);
113 d = strtod(c, (char **)NULL);
114 memcpy(f + i, &d, size_of[(*output_type)]);
115 c = (char *)strtok((char *)NULL, sep);
116 i += size_of[(*output_type)];
117 }
118 }
119 }
120
121 free(buf);
122 fclose(fd);
123}
124
125void FC_FUNC_(get_info_csv, GET_INFO_CSV)(unsigned long *dims, int *ierr,
126 STR_F_TYPE fname STR_ARG1) {
127 char *filename;
128 char *buf;
129 char *c;
130 FILE *fd;
131 int buf_size = 65536;
132 const char sep[] = "\n\t ,";
133
134 unsigned long curr_dims[3] = {0, 0, 0};
135 unsigned long prev_dims[2] = {0, 0};
136
137 TO_C_STR1(fname, filename);
138 fd = fopen(filename, "r");
139 free(filename);
140
141 *ierr = 0;
142 if (fd == NULL) {
143 *ierr = 2;
144 return;
145 }
146
147 buf = (char *)malloc(buf_size * sizeof(char));
148 assert(buf != NULL);
149 while (fgets(buf, buf_size * sizeof(char), fd) != NULL) {
150 c = strtok(buf, sep);
151
152 prev_dims[0] = curr_dims[0];
153
154 curr_dims[0] = 0;
155
157 while (c != NULL) {
158 curr_dims[0]++;
159 c = (char *)strtok((char *)NULL, sep);
160 }
161
165 if (prev_dims[0] > 0 && curr_dims[0] > 0)
166 assert(curr_dims[0] == prev_dims[0]);
167
170 if (prev_dims[0] == 0 && curr_dims[0] != 0) {
171 prev_dims[1] = curr_dims[1];
172 curr_dims[1] = 0;
173 curr_dims[2]++;
177 if (prev_dims[1] > 0 && curr_dims[1] > 0)
178 assert(prev_dims[1] == curr_dims[1]);
180
183 if (curr_dims[0] > 0)
184 curr_dims[1]++;
186
187 dims[0] = curr_dims[0];
188 dims[1] = curr_dims[1];
189 dims[2] = curr_dims[2];
190
191 free(buf);
192 fclose(fd);
void * memcpy(void *__restrict __dest, const void *__restrict __src, size_t __n) __attribute__((__nothrow__
int fclose(FILE *__stream)
integer, parameter fd
Definition: run.F90:165
ptrdiff_t i
Definition: operate_inc.c:12