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 "string_f.h"
67
68#include "io_binary.h"
69
70static const int size_of[6] = {4, 8, 8, 16, 4, 8};
71
72void read_csv(int64_t *np, void *f, int *output_type,
73 int *ierr, char* filename) {
74 int i;
75 FILE *fd;
76 char *buf;
77 char *c;
78 const char sep[] = "\t\n ,";
79 int buf_size = 65536;
80
81 fd = fopen(filename, "r");
82
83 *ierr = 0;
84 if (fd == NULL) {
85 *ierr = 2;
86 return;
87 }
89 buf = (char *)malloc(buf_size * sizeof(char));
90 assert(buf != NULL);
91
92 if ((*output_type) == TYPE_DOUBLE) {
93 double d;
94 i = 0;
95 while (fgets(buf, buf_size * sizeof(char), fd) != NULL) {
96 c = strtok(buf, sep);
97 while (c != NULL) {
98 assert(i / 8 < *np);
99 d = strtod(c, (char **)NULL);
100 memcpy(f + i, &d, size_of[(*output_type)]);
101 c = (char *)strtok((char *)NULL, sep);
102 i += size_of[(*output_type)];
103 }
104 }
105 }
106
107 free(buf);
108 fclose(fd);
109}
110
111void get_info_csv(int64_t *dims, int *ierr, char *filename) {
112 char *buf;
113 char *c;
114 FILE *fd;
115 int buf_size = 65536;
116 const char sep[] = "\n\t ,";
117
118 int64_t curr_dims[3] = {0, 0, 0};
119 int64_t prev_dims[2] = {0, 0};
120
121 fd = fopen(filename, "r");
122
123 *ierr = 0;
124 if (fd == NULL) {
125 *ierr = 2;
126 return;
127 }
128
129 buf = (char *)malloc(buf_size * sizeof(char));
130 assert(buf != NULL);
131 while (fgets(buf, buf_size * sizeof(char), fd) != NULL) {
132 c = strtok(buf, sep);
133
134 prev_dims[0] = curr_dims[0];
135
136 curr_dims[0] = 0;
137
139 while (c != NULL) {
140 curr_dims[0]++;
141 c = (char *)strtok((char *)NULL, sep);
142 }
143
147 if (prev_dims[0] > 0 && curr_dims[0] > 0)
148 assert(curr_dims[0] == prev_dims[0]);
149
152 if (prev_dims[0] == 0 && curr_dims[0] != 0) {
153 prev_dims[1] = curr_dims[1];
154 curr_dims[1] = 0;
155 curr_dims[2]++;
156
159 if (prev_dims[1] > 0 && curr_dims[1] > 0)
160 assert(prev_dims[1] == curr_dims[1]);
161 }
162
165 if (curr_dims[0] > 0)
166 curr_dims[1]++;
167 }
169 dims[0] = curr_dims[0];
170 dims[1] = curr_dims[1];
171 dims[2] = curr_dims[2];
173 if (dims[0]*dims[1]*dims[2] == 0) *ierr=1;
176 free(buf);
177 fclose(fd);
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:4616
int fclose(FILE *__stream)
void get_info_csv(int64_t *dims, int *ierr, char *filename)
Definition: io_csv.c:4707
integer, parameter fd
Definition: run.F90:168
ptrdiff_t i
Definition: operate_inc.c:12
static double f(double w, void *p)