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_DOUBLE) {
94 double d;
95 i = 0;
96 while (fgets(buf, buf_size * sizeof(char), fd) != NULL) {
97 c = strtok(buf, sep);
98 while (c != NULL) {
99 assert(i / 8 < *np);
100 d = strtod(c, (char **)NULL);
101 memcpy(f + i, &d, size_of[(*output_type)]);
102 c = (char *)strtok((char *)NULL, sep);
103 i += size_of[(*output_type)];
104 }
105 }
106 }
107
108 free(buf);
109 fclose(fd);
110}
111
112void FC_FUNC_(get_info_csv, GET_INFO_CSV)(unsigned long *dims, int *ierr,
113 STR_F_TYPE fname STR_ARG1) {
114 char *filename;
115 char *buf;
116 char *c;
117 FILE *fd;
118 int buf_size = 65536;
119 const char sep[] = "\n\t ,";
120
121 unsigned long curr_dims[3] = {0, 0, 0};
122 unsigned long prev_dims[2] = {0, 0};
123
124 TO_C_STR1(fname, filename);
125 fd = fopen(filename, "r");
126 free(filename);
127
128 *ierr = 0;
129 if (fd == NULL) {
130 *ierr = 2;
131 return;
132 }
133
134 buf = (char *)malloc(buf_size * sizeof(char));
135 assert(buf != NULL);
136 while (fgets(buf, buf_size * sizeof(char), fd) != NULL) {
137 c = strtok(buf, sep);
138
139 prev_dims[0] = curr_dims[0];
140
141 curr_dims[0] = 0;
142
144 while (c != NULL) {
145 curr_dims[0]++;
146 c = (char *)strtok((char *)NULL, sep);
147 }
148
152 if (prev_dims[0] > 0 && curr_dims[0] > 0)
153 assert(curr_dims[0] == prev_dims[0]);
154
157 if (prev_dims[0] == 0 && curr_dims[0] != 0) {
158 prev_dims[1] = curr_dims[1];
159 curr_dims[1] = 0;
160 curr_dims[2]++;
161
164 if (prev_dims[1] > 0 && curr_dims[1] > 0)
165 assert(prev_dims[1] == curr_dims[1]);
166 }
167
170 if (curr_dims[0] > 0)
171 curr_dims[1]++;
174 dims[0] = curr_dims[0];
175 dims[1] = curr_dims[1];
176 dims[2] = curr_dims[2];
177
178 free(buf);
180}
void * memcpy(void *__restrict __dest, const void *__restrict __src, size_t __n) __attribute__((__nothrow__
int fclose(FILE *__stream)
integer, parameter fd
Definition: run.F90:167
ptrdiff_t i
Definition: operate_inc.c:12