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;
75 char *c;
76 const char sep[] = "\t\n ,";
77 int buf_size = 65536;
78
79 fd = fopen(filename, "r");
80
81 *ierr = 0;
82 if (fd == NULL) {
83 *ierr = 2;
84 return;
85 }
86
87 buf = (char *)malloc(buf_size * sizeof(char));
88 assert(buf != NULL);
89
90 if ((*output_type) == TYPE_DOUBLE) {
91 double d;
92 i = 0;
93 while (fgets(buf, buf_size * sizeof(char), fd) != NULL) {
94 c = strtok(buf, sep);
95 while (c != NULL) {
96 assert(i / 8 < *np);
97 d = strtod(c, (char **)NULL);
98 memcpy(f + i, &d, size_of[(*output_type)]);
99 c = (char *)strtok((char *)NULL, sep);
100 i += size_of[(*output_type)];
101 }
102 }
103 }
104
105 free(buf);
106 fclose(fd);
107}
108
109void get_info_csv(int64_t *dims, int *ierr, char *filename) {
110 char *buf;
111 char *c;
112 FILE *fd;
113 int buf_size = 65536;
114 const char sep[] = "\n\t ,";
115
116 int64_t curr_dims[3] = {0, 0, 0};
117 int64_t prev_dims[2] = {0, 0};
118
119 fd = fopen(filename, "r");
120
121 *ierr = 0;
122 if (fd == NULL) {
123 *ierr = 2;
124 return;
125 }
126
127 buf = (char *)malloc(buf_size * sizeof(char));
128 assert(buf != NULL);
129 while (fgets(buf, buf_size * sizeof(char), fd) != NULL) {
130 c = strtok(buf, sep);
131
132 prev_dims[0] = curr_dims[0];
133
134 curr_dims[0] = 0;
135
137 while (c != NULL) {
138 curr_dims[0]++;
139 c = (char *)strtok((char *)NULL, sep);
140 }
141
145 if (prev_dims[0] > 0 && curr_dims[0] > 0)
146 assert(curr_dims[0] == prev_dims[0]);
147
150 if (prev_dims[0] == 0 && curr_dims[0] != 0) {
151 prev_dims[1] = curr_dims[1];
152 curr_dims[1] = 0;
153 curr_dims[2]++;
154
157 if (prev_dims[1] > 0 && curr_dims[1] > 0)
158 assert(prev_dims[1] == curr_dims[1]);
159 }
160
163 if (curr_dims[0] > 0)
164 curr_dims[1]++;
165 }
166
167 dims[0] = curr_dims[0];
168 dims[1] = curr_dims[1];
169 dims[2] = curr_dims[2];
171 if (dims[0]*dims[1]*dims[2] == 0) *ierr=1;
174 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:4704
integer, parameter fd
Definition: run.F90:170
ptrdiff_t i
Definition: operate_inc.c:12
static double f(double w, void *p)