-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathx2sys.c
2095 lines (1861 loc) · 80.1 KB
/
x2sys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-----------------------------------------------------------------
*
* Copyright (c) 1999-2024 by the GMT Team (https://www.generic-mapping-tools.org/team.html)
* See LICENSE.TXT file for copying and redistribution conditions.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3 or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* Contact info: www.generic-mapping-tools.org
*--------------------------------------------------------------------*/
/* x2sys.c contains the source code for the X2SYS crossover library
* libx2sys.a. The code is copylefted under the GNU Public Library
* License.
*
* The following functions are external and user-callable form other
* programs:
*
* x2sys_set_system : Initialize X2SYS via the specified TAG
* x2sys_initialize : Reads the definition info file for current data files
* x2sys_read_file : Reads and returns the entire data matrix
* x2sys_read_gmtfile : Specifically reads an old .gmt file
* x2sys_read_mgd77file : Specifically reads an ASCII MGD77 file
* x2sys_read_mgd77ncfile : Specifically reads an netCDF MGD77+ file
* x2sys_read_ncfile : Specifically reads an COARDS netCDF file
* x2sys_read_list : Read an ASCII list of track names
* x2sys_dummytimes : Make dummy times for tracks missing times
* x2sys_n_data_cols : Gives number of data columns in this data set
* x2sys_fopen : Opening files with error message and exit
* x2sys_fclose : Closes files and gives error messages
* x2sys_skip_header : Skips the header record(s) in the open file
* x2sys_read_record : Reads and returns one record from the open file
* x2sys_pick_fields : Decodes the -F<fields> flag of desired columns
* x2sys_free_info : Frees the information structure
* x2sys_free_data : Frees the data matrix
* x2sys_read_coe_dbase : Reads into memory the entire COE ASCII database
* x2sys_free_coe_dbase : Free the array of COE structures
*------------------------------------------------------------------
* Core crossover functions are part of GMT:
* gmt_init_track : Prepares a track for crossover analysis
* gmt_crossover : Calculates crossovers for two data sets
* GMT_x_alloc : Allocate space for crossovers
* gmt_x_free : Free crossover structure
* support_ysort : Sorting routine used in x2sys_init_track [Hidden]
*------------------------------------------------------------------
* These routines are local to x2sys and used by the above routines:
*
* x2sys_set_home : Initializes X2SYS paths
*
*------------------------------------------------------------------
* Author: Paul Wessel
* Date: 20-SEP-2008
* Version: 1.1, based on the spirit of the old xsystem code
*
*/
/*
*--------------------------------------------------------------------------------
* Overview of how x2sys deals with data columns and selects those to use:
*
* The information of what data columns exist in a data set is maintained
* in the <tag>.def file set up during x2sys_init. So all x2sys programs
* reading data knows this information, which is stored in an X2SYS_INFO
* structure; call it s here. To keep things clear, let icol refer to one
* of the incoming (data file) columns while ocol refers to one of the
* chosen outgoing (e.g., printed output) columns, Thus,
* s->n_fields Total number of in-columns in file
* In addition, a few special data columns are marked via column-variables:
* s->t_col icol with time (or -1 if N/A)
* s->x_col icol with longitude|x (or -1 if N/A)
* s->y_col icol with latitude|y (or -1 if N/A)
* Of course, these columns start at 0 for first column [C-counting].
* Next, each column has an info struct, i.e., s->info[icol], in which
* information about that column is kept. Some examples:
* s->info[icol].name text string with column name, e.g., "depth".
* s->info[icol].scale factor to scale data, etc., most likely 1.
* Now, many programs just want to extract a subset of all these columns and
* the output order can be anything desired. This selection is done by giving
* an option like -Flon,lat,depth,faa
* This string is parsed by x2sys_pick_fields which determines several things:
* s->n_out_cols Number of items we selected (here 4)
* It also fills out three important arrays allocated to length s->n_fields:
* s->use_column[icol] true if column <icol> was selected for output
* s->out_order[ocol] Refers to which icol goes with each ocol
* e.g., if time is the 4th input column (icol = 3)
* and we gave -Ftime,lat then s->out_order[0] = 3.
* By looping ocol over s->out_order we find which icol to output.
* s->in_order[icol] Refers to which ocol goes with each icol;
* this is the inverse of out_order.
* So if we want to know what ocol will be used
* for a given icol we look at s->in_order[icol]
* in colsultation with use_column[icol] since if
* that is false then that columns was not requested.
* In case -F is not given then s->n_out_cols = s->n_fields, in|out_order are
* initialized to 0,1,2,... and use_column[] = true so that by default all columns
* are output in the same order as stored in the file.
*--------------------------------------------------------------------------------
*/
#include "gmt_dev.h"
#include "gmt_internals.h"
#include "mgd77/mgd77.h"
#include "x2sys.h"
/* Global variables used by X2SYS functions */
char *X2SYS_HOME;
struct MGD77_CONTROL M;
#define MAX_DATA_PATHS 32
static char *x2sys_datadir[MAX_DATA_PATHS]; /* Directories where track data may live */
static unsigned int n_x2sys_paths = 0; /* Number of these directories */
/* Here are legacy functions for old GMT MGG supplement needed in x2sys */
static char *mgg_path[10]; /* Max 10 directories for now */
static int n_mgg_paths = 0; /* Number of these directories */
GMT_LOCAL int x2sys_mggpath_func (char *leg_path, char *leg) {
int id;
char geo_path[PATH_MAX] = {""};
/* First look in current directory */
sprintf (geo_path, "%s.gmt", leg);
if (!access (geo_path, R_OK)) {
strcpy (leg_path, geo_path);
return (0);
}
/* Then look elsewhere */
for (id = 0; id < n_mgg_paths; id++) {
sprintf (geo_path, "%s/%s.gmt", mgg_path[id], leg);
if (!access (geo_path, R_OK)) {
strcpy (leg_path, geo_path);
return (0);
}
}
return(1);
}
/* x2sys_mggpath_init reads the GMT_SHAREDIR/mgg/gmtfile_paths or ~/.gmt/gmtfile_paths file and gets all
* the gmtfile directories.
*/
GMT_LOCAL void x2sys_mggpath_init (struct GMT_CTRL *GMT) {
char line[PATH_MAX] = {""};
FILE *fp = NULL;
gmt_getsharepath (GMT, "mgg", "gmtfile_paths", "", line, R_OK);
n_mgg_paths = 0;
if ((fp = fopen (line, "r")) == NULL) {
GMT_Report (GMT->parent, GMT_MSG_WARNING, "Path file %s for *.gmt files not found\n", line);
GMT_Report (GMT->parent, GMT_MSG_WARNING, "(Will only look in current directory for such files)\n");
return;
}
while (fgets (line, PATH_MAX, fp)) {
if (line[0] == '#') continue; /* Comments */
if (line[0] == ' ' || line[0] == '\0') continue; /* Blank line */
mgg_path[n_mgg_paths] = gmt_M_memory (GMT, NULL, strlen (line), char);
line[strlen (line)-1] = 0;
strcpy (mgg_path[n_mgg_paths], line);
n_mgg_paths++;
}
fclose (fp);
}
GMT_LOCAL void x2sys_mggpath_free (struct GMT_CTRL *GMT) {
int k;
for (k = 0; k < n_mgg_paths; k++)
gmt_M_free (GMT, mgg_path[k]);
n_mgg_paths = 0;
}
GMT_LOCAL int x2sys_get_first_year (struct GMT_CTRL *GMT, double t) {
/* obtain yyyy/mm/dd and return year */
int64_t rd;
double s;
struct GMT_GCAL CAL;
gmt_dt2rdc (GMT, t, &rd, &s);
gmt_gcal_from_rd (GMT, rd, &CAL);
return (CAL.year);
}
GMT_LOCAL const char *x2sys_strerror (struct GMT_CTRL *GMT, int err) {
/* Returns the error string for a given error code "err"
Passes "err" on to nc_strerror if the error code is not one we defined */
gmt_M_unused(GMT);
switch (err) {
case X2SYS_FCLOSE_ERR:
return "Error from fclose";
case X2SYS_BAD_DEF:
return "Cannot find format definition file in either current or X2SYS_HOME directories";
case X2SYS_BAD_COL:
return "Unrecognized string";
case X2SYS_TAG_NOT_SET:
return "TAG has not been set";
case X2SYS_BAD_ARG:
return "Unrecognized argument";
case X2SYS_CONFLICTING_ARGS:
return "Conflicting arguments";
case X2SYS_BIX_BAD_ROW:
return "Bad row index";
case X2SYS_BIX_BAD_COL:
return "Bad col index";
case X2SYS_BIX_BAD_INDEX:
return "Bad bin index";
default: /* default passes through to GMT error */
return GMT_strerror(err);
}
}
GMT_LOCAL int x2sys_err_pass (struct GMT_CTRL *GMT, int err, char *file) {
if (err == X2SYS_NOERROR) return (err);
/* When error code is non-zero: print error message and pass error code on */
if (file && file[0])
GMT_Report (GMT->parent, GMT_MSG_ERROR, " %s [%s]\n", x2sys_strerror(GMT, err), file);
else
GMT_Report (GMT->parent, GMT_MSG_ERROR, "%s\n", x2sys_strerror(GMT, err));
return (err);
}
int x2sys_set_home (struct GMT_CTRL *GMT) {
char *this = NULL;
if (X2SYS_HOME) return GMT_NOERROR; /* Already set elsewhere */
if ((this = getenv ("X2SYS_HOME")) != NULL) { /* Set user's default path */
X2SYS_HOME = gmt_M_memory (GMT, NULL, strlen (this) + 1, char);
strcpy (X2SYS_HOME, this);
}
else { /* Require user to set this parameters since subdirs will be created and it would be messy to just use . */
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Environmental parameter X2SYS_HOME has not been set but is a required parameter\n");
return (GMT_RUNTIME_ERROR);
}
#ifdef WIN32
gmt_dos_path_fix (X2SYS_HOME);
#endif
return GMT_NOERROR;
}
void x2sys_path (struct GMT_CTRL *GMT, char *fname, char *path) {
gmt_M_unused(GMT);
sprintf (path, "%s/%s", X2SYS_HOME, fname);
}
FILE *x2sys_fopen (struct GMT_CTRL *GMT, char *fname, char *mode) {
FILE *fp = NULL;
char file[PATH_MAX] = {""};
if (mode[0] == 'w') { /* Writing: Do this only in X2SYS_HOME */
x2sys_path (GMT, fname, file);
fp = fopen (file, mode);
}
else { /* Reading: Try both current directory and X2SYS_HOME */
if ((fp = fopen (fname, mode)) == NULL) { /* Not in current directory, try $X2SYS_HOME */
x2sys_path (GMT, fname, file);
fp = fopen (file, mode);
}
}
return (fp);
}
int x2sys_access (struct GMT_CTRL *GMT, char *fname, int mode) {
int k;
char file[PATH_MAX] = {""};
x2sys_path (GMT, fname, file);
if ((k = access (file, mode)) != 0) { /* Not in X2SYS_HOME directory */
k = access (fname, mode); /* Try in current directory */
}
return (k);
}
int x2sys_fclose (struct GMT_CTRL *GMT, char *fname, FILE *fp) {
gmt_M_unused(GMT); gmt_M_unused(fname);
if (fclose (fp)) return (X2SYS_FCLOSE_ERR);
return (X2SYS_NOERROR);
}
GMT_LOCAL int x2sys_skip_header (struct GMT_CTRL *GMT, FILE *fp, struct X2SYS_INFO *s) {
unsigned int i;
char line[GMT_BUFSIZ] = {""};
if (s->file_type == X2SYS_ASCII) { /* ASCII, skip records */
for (i = 0; i < s->skip; i++) {
if (!fgets (line, GMT_BUFSIZ, fp)) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Read error in header line %d\n", i);
return (GMT_DATA_READ_ERROR);
}
}
}
else if (s->file_type == X2SYS_BINARY) { /* Native binary, skip bytes */
if (fseek (fp, (off_t)s->skip, SEEK_CUR)) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Seed error while skipping headers\n");
return (GMT_DATA_READ_ERROR);
}
}
return (GMT_NOERROR);
}
int x2sys_initialize (struct GMT_CTRL *GMT, char *TAG, char *fname, struct GMT_IO *G, struct X2SYS_INFO **I) {
/* Reads the format definition file and sets all information variables */
unsigned int i = 0;
int is;
size_t n_alloc = GMT_TINY_CHUNK;
int c; /* Remain 4-byte integer */
FILE *fp = NULL;
struct X2SYS_INFO *X = NULL;
char line[GMT_BUFSIZ] = {""}, cardcol[80] = {""}, yes_no;
if (x2sys_set_home (GMT))
return (GMT_RUNTIME_ERROR);
X = gmt_M_memory (GMT, NULL, n_alloc, struct X2SYS_INFO);
X->TAG = strdup (TAG);
X->info = gmt_M_memory (GMT, NULL, n_alloc, struct X2SYS_DATA_INFO);
X->file_type = X2SYS_ASCII;
X->x_col = X->y_col = X->t_col = GMT_NOTSET;
X->ms_flag = '>'; /* Default multisegment header flag */
sprintf (line, "%s/%s.%s", TAG, fname, X2SYS_FMT_EXT);
X->dist_flag = 0; /* Cartesian distances */
sprintf (X->separators, "%s\n", GMT_TOKEN_SEPARATORS);
if ((fp = x2sys_fopen (GMT, line, "r")) == NULL) { /* Failed, try to deprecated extension instead */
sprintf (line, "%s/%s.%s", TAG, fname, X2SYS_FMT_EXT_OLD);
if ((fp = x2sys_fopen (GMT, line, "r")) == NULL) { /* Even that failed so out of here */
gmt_M_free (GMT, X);
return (X2SYS_BAD_DEF);
}
}
X->unit[X2SYS_DIST_SELECTION][0] = 'k'; X->unit[X2SYS_DIST_SELECTION][1] = '\0'; /* Initialize for geographic data (km and m/s) */
X->unit[X2SYS_SPEED_SELECTION][0] = GMT_MAP_DIST_UNIT; X->unit[X2SYS_SPEED_SELECTION][1] = '\0';
if (!strcmp (fname, "mgd77+")) {
X->read_file = &x2sys_read_mgd77ncfile;
X->geographic = true;
X->geodetic = GMT_IS_0_TO_P360_RANGE;
X->dist_flag = 2; /* Create circle distances */
MGD77_Init (GMT, &M); /* Initialize MGD77 Machinery */
}
else if (!strcmp (fname, "gmt") && gmt_M_compat_check (GMT, 4)) {
X->read_file = &x2sys_read_gmtfile;
X->geographic = true;
X->geodetic = GMT_IS_0_TO_P360_RANGE;
X->dist_flag = 2; /* Create circle distances */
}
else if (!strcmp (fname, "mgd77")) {
X->read_file = &x2sys_read_mgd77file;
X->geographic = true;
X->geodetic = GMT_IS_0_TO_P360_RANGE;
X->dist_flag = 2; /* Create circle distances */
MGD77_Init (GMT, &M); /* Initialize MGD77 Machinery */
}
else {
X->read_file = &x2sys_read_file;
X->dist_flag = 0; /* Cartesian distances */
X->unit[X2SYS_DIST_SELECTION][0] = 'c'; /* Reset to Cartesian */
X->unit[X2SYS_SPEED_SELECTION][0] = 'c'; /* Reset to Cartesian */
}
while (fgets (line, GMT_BUFSIZ, fp)) {
if (line[0] == '\0') continue;
if (line[0] == '#') {
if (!strncmp (line, "#SKIP", 5U)) X->skip = atoi (&line[6]);
if (!strncmp (line, "#ASCII", 6U)) X->file_type = X2SYS_ASCII;
if (!strncmp (line, "#BINARY", 7U)) X->file_type = X2SYS_BINARY;
if (!strncmp (line, "#NETCDF", 7U)) X->file_type = X2SYS_NETCDF;
if (!strncmp (line, "#GEO", 4U)) X->geographic = true;
if (!strncmp (line, "#MULTISEG", 9U)) {
X->multi_segment = true;
sscanf (line, "%*s %c", &X->ms_flag);
}
continue;
}
gmt_chop (line); /* Remove trailing CR or LF */
is = sscanf (line, "%s %c %c %lf %lf %lf %s %s", X->info[i].name, &X->info[i].intype, &yes_no, &X->info[i].nan_proxy, &X->info[i].scale, &X->info[i].offset, X->info[i].format, cardcol);
if (X->info[i].intype == 'A') { /* ASCII Card format */
sscanf (cardcol, "%d-%d", &X->info[i].start_col, &X->info[i].stop_col);
X->info[i].n_cols = X->info[i].stop_col - X->info[i].start_col + 1;
}
if (is == 6) X->info[i].format[0] = '-'; /* No custom formatting given */
c = X->info[i].intype;
if (tolower (c) == 'a') X->file_type = X2SYS_ASCII;
c = yes_no;
if (tolower (c) != 'Y') X->info[i].has_nan_proxy = true;
if (!(X->info[i].scale == 1.0 && X->info[i].offset == 0.0)) X->info[i].do_scale = true;
if (!strcmp (X->info[i].name, "x") || !strcmp (X->info[i].name, "lon")) X->x_col = i;
if (!strcmp (X->info[i].name, "y") || !strcmp (X->info[i].name, "lat")) X->y_col = i;
if (!strcmp (X->info[i].name, "t") || !strcmp (X->info[i].name, "time")) X->t_col = i;
if (!strcmp (X->info[i].name, "rtime")) X->t_col = i, X->rel_time = true;
i++;
if (i == n_alloc) {
n_alloc <<= 1;
X->info = gmt_M_memory (GMT, X->info, n_alloc, struct X2SYS_DATA_INFO);
}
}
fclose (fp);
if (X->file_type == X2SYS_NETCDF) X->read_file = &x2sys_read_ncfile;
if (i < n_alloc) X->info = gmt_M_memory (GMT, X->info, i, struct X2SYS_DATA_INFO);
X->n_fields = X->n_out_columns = i;
if (X->file_type == X2SYS_BINARY) { /* Binary mode needed */
strcpy (G->r_mode, "rb");
strcpy (G->w_mode, "wb");
strcpy (G->a_mode, "ab+");
}
X->in_order = gmt_M_memory (GMT, NULL, X->n_fields, unsigned int);
X->out_order = gmt_M_memory (GMT, NULL, X->n_fields, unsigned int);
X->use_column = gmt_M_memory (GMT, NULL, X->n_fields, bool);
for (i = is = 0; i < X->n_fields; i++, is++) { /* Default is same order and use all columns */
X->in_order[i] = X->out_order[i] = i;
X->use_column[i] = 1;
G->col_type[GMT_IN][i] = G->col_type[GMT_OUT][i] = (X->x_col == is) ? GMT_IS_LON : ((X->y_col == is) ? GMT_IS_LAT : GMT_IS_UNKNOWN);
if (X->x_col == is)
G->col_type[GMT_IN][i] = G->col_type[GMT_OUT][i] = GMT_IS_LON;
else if (X->y_col == is)
G->col_type[GMT_IN][i] = G->col_type[GMT_OUT][i] = GMT_IS_LAT;
else if (X->t_col == is) {
G->col_type[GMT_IN][i] = (X->rel_time) ? GMT_IS_RELTIME : GMT_IS_ABSTIME;
G->col_type[GMT_OUT][i] = GMT_IS_ABSTIME;
}
else
G->col_type[GMT_IN][i] = G->col_type[GMT_OUT][i] = GMT_IS_UNKNOWN;
}
X->n_data_cols = x2sys_n_data_cols (GMT, X);
X->rec_size = (8 + X->n_data_cols) * sizeof (double);
*I = X;
return (X2SYS_NOERROR);
}
void x2sys_end (struct GMT_CTRL *GMT, struct X2SYS_INFO *X) {
/* Free allocated memory */
unsigned int id;
gmt_M_free (GMT, X2SYS_HOME);
if (!X) return;
gmt_M_free (GMT, X->in_order);
gmt_M_free (GMT, X->out_order);
gmt_M_free (GMT, X->use_column);
gmt_M_str_free (X->TAG); /* free since allocated by strdup */
x2sys_free_info (GMT, X);
for (id = 0; id < n_x2sys_paths; id++) gmt_M_free (GMT, x2sys_datadir[id]);
x2sys_mggpath_free (GMT);
MGD77_end (GMT, &M);
}
unsigned int x2sys_n_data_cols (struct GMT_CTRL *GMT, struct X2SYS_INFO *s) {
unsigned int i, n = 0;
int is;
gmt_M_unused(GMT);
for (i = is = 0; i < s->n_out_columns; i++, is++) { /* Loop over all possible fields in this data set */
if (is == s->x_col) continue;
if (is == s->y_col) continue;
if (is == s->t_col) continue;
n++; /* Only count data columns */
}
return (n);
}
int x2sys_pick_fields (struct GMT_CTRL *GMT, char *string, struct X2SYS_INFO *s) {
/* Scan the -Fstring and select which columns to use and which order
* they should appear on output. Default is all columns and the same
* order as on input. Once this is set you can loop through i = 0:n_out_columns
* and use out_order[i] to get the original column number. Or you can loop
* over all in_order[i] to determine what output column they will be put in,
* provided use_column[i] is true.
*/
char line[GMT_BUFSIZ] = {""}, p[GMT_BUFSIZ] = {""};
unsigned int i = 0, j, pos = 0;
strncpy (s->fflags, string, GMT_BUFSIZ-1);
strncpy (line, string, GMT_BUFSIZ-1); /* Make copy for later use */
gmt_M_memset (s->use_column, s->n_fields, bool);
while ((gmt_strtok (line, ",", &pos, p))) {
j = 0;
while (j < s->n_fields && strcmp (p, s->info[j].name)) j++;
if (j < s->n_fields) {
s->out_order[i] = j;
s->in_order[j] = i;
s->use_column[j] = true;
}
else {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Unknown column name %s\n", p);
return (X2SYS_BAD_COL);
}
i++;
}
s->n_out_columns = i;
return (X2SYS_NOERROR);
}
void x2sys_free_info (struct GMT_CTRL *GMT, struct X2SYS_INFO *s) {
gmt_M_free (GMT, s->info);
gmt_M_free (GMT, s);
}
void x2sys_free_data (struct GMT_CTRL *GMT, double **data, unsigned int n, struct X2SYS_FILE_INFO *p) {
unsigned int i;
for (i = 0; i < n; i++) {
gmt_M_free (GMT, data[i]);
}
gmt_M_free (GMT, data);
gmt_M_free (GMT, p->ms_rec);
}
double *x2sys_dummytimes (struct GMT_CTRL *GMT, uint64_t n) {
uint64_t i;
double *t;
/* Make monotonically increasing dummy time sequence */
t = gmt_M_memory (GMT, NULL, n, double);
for (i = 0; i < n; i++) t[i] = (double)i;
return (t);
}
/*
* x2sys_data_read: Read subroutine for x2_sys data input.
* This function will read one logical record of ASCII or
* binary data from the open file, and return with a double
* array called data[] with each data value in it.
*/
GMT_LOCAL int x2sys_read_record (struct GMT_CTRL *GMT, FILE *fp, double *data, struct X2SYS_INFO *s, struct GMT_IO *G) {
bool error = false;
unsigned int j, k, i, pos;
int is;
size_t n_read = 0;
char line[GMT_BUFSIZ] = {""}, buffer[GMT_LEN64] = {""}, p[GMT_BUFSIZ] = {""}, c;
unsigned char u;
short int h;
float f;
long L;
double NaN = GMT->session.d_NaN;
for (j = 0; !error && j < s->n_fields; j++) {
switch (s->info[j].intype) {
case 'A': /* ASCII Card Record, must extract columns */
if (j == 0) {
s->ms_next = false;
if (!fgets (line, GMT_BUFSIZ, fp)) return (-1);
while (line[0] == '#' || line[0] == s->ms_flag) {
if (!fgets (line, GMT_BUFSIZ, fp)) return (-1);
if (s->multi_segment) s->ms_next = true;
}
gmt_chop (line); /* Remove trailing CR or LF */
}
strncpy (buffer, &line[s->info[j].start_col], s->info[j].n_cols);
buffer[s->info[j].n_cols] = 0;
if (gmt_scanf (GMT, buffer, G->col_type[GMT_IN][j], &data[j]) == GMT_IS_NAN) data[j] = GMT->session.d_NaN;
break;
case 'a': /* ASCII Record, get all columns directly */
k = 0;
s->ms_next = false;
if (!fgets (line, GMT_BUFSIZ, fp)) return (-1);
while (line[0] == '#' || line[0] == s->ms_flag) {
if (!fgets (line, GMT_BUFSIZ, fp)) return (-1);
if (s->multi_segment) s->ms_next = true;
}
gmt_chop (line); /* Remove trailing CR or LF */
pos = 0;
while ((gmt_strtok (line, s->separators, &pos, p)) && k < s->n_fields) {
if (gmt_scanf (GMT, p, G->col_type[GMT_IN][k], &data[k]) == GMT_IS_NAN) data[k] = GMT->session.d_NaN;
k++;
}
return ((k != s->n_fields) ? -1 : 0);
break;
case 'c': /* Binary signed 1-byte character */
n_read += fread (&c, sizeof (char), 1U, fp);
data[j] = (double)c;
break;
case 'u': /* Binary unsigned 1-byte character */
n_read += fread (&u, sizeof (unsigned char), 1U, fp);
data[j] = (double)u;
break;
case 'h': /* Binary signed 2-byte integer */
n_read += fread (&h, sizeof (short int), 1U, fp);
data[j] = (double)h;
break;
case 'i': /* Binary signed 4-byte integer */
n_read += fread (&i, sizeof (int), 1U, fp);
data[j] = (double)i;
break;
case 'l': /* Binary signed 4/8-byte integer (long) */
n_read += fread (&L, sizeof (long), 1U, fp);
data[j] = (double)L;
break;
case 'f': /* Binary signed 4-byte float */
n_read += fread (&f, sizeof (float), 1U, fp);
data[j] = (double)f;
break;
case 'd': /* Binary signed 8-byte float */
n_read += fread (&data[j], sizeof (double), 1U, fp);
break;
default:
error = true;
break;
}
}
/* Change nan-proxies to NaNs and apply any data scales and offsets */
for (i = is = 0; i < s->n_fields; i++, is++) {
if (s->info[i].has_nan_proxy && data[i] == s->info[i].nan_proxy)
data[i] = NaN;
else if (s->info[i].do_scale)
data[i] = data[i] * s->info[i].scale + s->info[i].offset;
if (gmt_M_is_dnan (data[i])) s->info[i].has_nans = true;
if (is == s->x_col && s->geographic) gmt_lon_range_adjust (s->geodetic, &data[i]);
}
return ((error || n_read != s->n_fields) ? -1 : 0);
}
int x2sys_read_file (struct GMT_CTRL *GMT, char *fname, double ***data, struct X2SYS_INFO *s, struct X2SYS_FILE_INFO *p, struct GMT_IO *G, uint64_t *n_rec) {
/* Reads the entire contents of the file given and returns the
* number of data records. The data matrix is return in the
* pointer data.
*/
int error;
uint64_t j;
unsigned int i, start = 0;
bool first = true;
size_t n_alloc;
FILE *fp = NULL;
double **z = NULL, *rec = NULL;
char path[PATH_MAX] = {""}, file[GMT_LEN32] = {""};
strncpy (file, fname, GMT_LEN32-1);
if (gmt_file_is_cache (GMT->parent, file)) { /* Must be a cache file */
if (strstr (file, s->suffix) == NULL) {strcat (file, "."); strcat (file, s->suffix); } /* Must have suffix to download */
start = gmt_download_file_if_not_found (GMT, file, 0);
}
if (x2sys_get_data_path (GMT, path, &file[start], s->suffix)) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_file : Cannot find track %s\n", &file[start]);
return (-1);
}
if ((fp = fopen (path, G->r_mode)) == NULL) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_file : Cannot open file %s\n", path);
return (-1);
}
strcpy (s->path, path);
n_alloc = GMT_CHUNK;
rec = gmt_M_memory (GMT, NULL, s->n_fields, double);
z = gmt_M_memory (GMT, NULL, s->n_fields, double *);
for (i = 0; i < s->n_fields; i++) z[i] = gmt_M_memory (GMT, NULL, n_alloc, double);
p->ms_rec = gmt_M_memory (GMT, NULL, n_alloc, uint64_t);
if ((error = x2sys_skip_header (GMT, fp, s))) {
gmt_M_free (GMT, rec);
return error;
}
p->n_segments = 0; /* So that first increment sets it to 0 */
j = 0;
while (!x2sys_read_record (GMT, fp, rec, s, G)) { /* Gets the next data record */
if (s->multi_segment && s->ms_next && !first) p->n_segments++;
for (i = 0; i < s->n_fields; i++) z[i][j] = rec[i];
p->ms_rec[j] = p->n_segments;
j++;
if (j == n_alloc) { /* Get more */
n_alloc <<= 1;
for (i = 0; i < s->n_fields; i++) z[i] = gmt_M_memory (GMT, z[i], n_alloc, double);
p->ms_rec = gmt_M_memory (GMT, p->ms_rec, n_alloc, uint64_t);
}
first = false;
}
p->n_segments++; /* To get the total number of segments 0-(n_segments-1) */
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "x2sys_read_file : File %s contained %" PRIu64 " segments\n", path, p->n_segments);
fclose (fp);
gmt_M_free (GMT, rec);
for (i = 0; i < s->n_fields; i++) z[i] = gmt_M_memory (GMT, z[i], j, double);
p->ms_rec = gmt_M_memory (GMT, p->ms_rec, j, uint64_t);
*data = z;
p->n_rows = j;
p->year = 0;
strncpy (p->name, &file[start], 31U);
*n_rec = p->n_rows;
return (X2SYS_NOERROR);
}
int x2sys_read_gmtfile (struct GMT_CTRL *GMT, char *fname, double ***data, struct X2SYS_INFO *s, struct X2SYS_FILE_INFO *p, struct GMT_IO *G, uint64_t *n_rec) {
/* Reads the entire contents of the file given and returns the
* number of data records. The data matrix is return in the
* pointer data. The input file format is the venerable GMT
* MGG format from old Lamont by Wessel and Smith.
*/
int i, year, n_records; /* These must remain 4-byte ints */
int64_t rata_day;
uint64_t j;
unsigned int first = 0;
char path[PATH_MAX] = {""}, file[GMT_LEN32] = {""};
FILE *fp = NULL;
double **z = NULL;
double NaN = GMT->session.d_NaN, t_off;
struct GMTMGG_REC record;
strncpy (file, fname, GMT_LEN32-1);
if (gmt_file_is_cache (GMT->parent, file)) { /* Must be a cache file */
if (strstr (file, s->suffix) == NULL) {strcat (file, "."); strcat (file, s->suffix); } /* Must have suffix to download */
first = gmt_download_file_if_not_found (GMT, file, 9);
}
if (n_x2sys_paths) {
if (x2sys_get_data_path (GMT, path, &file[first], s->suffix)) return (GMT_GRDIO_FILE_NOT_FOUND);
}
else {
char name[82] = {""};
if (!(s->flags & 1)) { /* Must init gmt file paths */
x2sys_mggpath_init (GMT);
s->flags |= 1;
}
strncpy (name, &file[first], 81U);
if (strstr (&file[first], ".gmt")) name[strlen(&file[first])-4] = 0; /* Name includes .gmt suffix, remove it */
if (x2sys_mggpath_func (path, name)) return (GMT_GRDIO_FILE_NOT_FOUND);
}
strcpy (s->path, path);
if ((fp = fopen (path, G->r_mode)) == NULL) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_file : Cannot open file %s\n", path);
return (-1);
}
if (fread (&year, sizeof (int), 1U, fp) != 1U) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_gmtfile: Could not read leg year from %s\n", path);
fclose (fp);
return (-1);
}
p->year = year;
rata_day = gmt_rd_from_gymd (GMT, year, 1, 1); /* Get the rata day for start of cruise year */
t_off = gmt_rdc2dt (GMT, rata_day, 0.0); /* Secs to start of day */
if (fread (&n_records, sizeof (int), 1U, fp) != 1U) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_gmtfile: Could not read n_records from %s\n", path);
fclose (fp);
return (GMT_GRDIO_READ_FAILED);
}
if (n_records <= 0 || n_records > 1e6) { /* The 1e6 is to satisfy CID 39227 (TAINTED variable) */
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_gmtfile: Got bad n_records %d\n", n_records);
fclose (fp);
return (GMT_GRDIO_READ_FAILED);
}
p->n_rows = n_records;
gmt_M_memset (p->name, 32, char);
if (fread (p->name, sizeof (char), 10U, fp) != 10U) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_gmtfile: Could not read agency from %s\n", path);
fclose (fp);
return (GMT_GRDIO_READ_FAILED);
}
/* Only return the data fields requested */
z = gmt_M_memory (GMT, NULL, s->n_out_columns, double *);
for (i = 0; i < (int)s->n_out_columns; i++) z[i] = gmt_M_memory (GMT, NULL, p->n_rows, double);
/* coverity[tainted_data] */ /* p->n_rows was checked above to be less than 1e6 */
for (j = 0; j < p->n_rows; j++) {
if (fread (&record, 18U, 1U, fp) != 1) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_gmtfile: Could not read record %" PRIu64 " from %s\n", j, path);
fclose (fp);
for (i = 0; i < (int)s->n_out_columns; i++) gmt_M_free (GMT, z[i]);
gmt_M_free (GMT, z);
return (GMT_GRDIO_READ_FAILED);
}
for (i = 0; i < (int)s->n_out_columns; i++) {
switch (s->out_order[i]) {
case 0: z[i][j] = record.time * GMT->current.setting.time_system.i_scale + t_off; break; /* To get GMT time keeping */
case 1: z[i][j] = record.lat * MDEG2DEG; break;
case 2: z[i][j] = record.lon * MDEG2DEG; break;
case 3: z[i][j] = (record.gmt[0] == GMTMGG_NODATA) ? NaN : 0.1 * record.gmt[0]; break;
case 4: z[i][j] = (record.gmt[1] == GMTMGG_NODATA) ? NaN : record.gmt[1]; break;
case 5: z[i][j] = (record.gmt[2] == GMTMGG_NODATA) ? NaN : record.gmt[2]; break;
}
}
}
fclose (fp);
p->ms_rec = NULL;
p->n_segments = 0;
*n_rec = p->n_rows;
*data = z;
return (X2SYS_NOERROR);
}
int x2sys_read_mgd77file (struct GMT_CTRL *GMT, char *fname, double ***data, struct X2SYS_INFO *s, struct X2SYS_FILE_INFO *p, struct GMT_IO *G, uint64_t *n_rec) {
uint64_t i, j;
size_t n_alloc = GMT_CHUNK;
int col[MGD77_N_DATA_EXTENDED];
unsigned int first = 0;
char path[PATH_MAX] = {""}, file[GMT_LEN32] = {""}, *tvals[MGD77_N_STRING_FIELDS];
double **z = NULL, dvals[MGD77_N_DATA_EXTENDED];
struct MGD77_HEADER H;
struct MGD77_CONTROL MC;
gmt_M_unused(G);
MGD77_Init (GMT, &MC); /* Initialize MGD77 Machinery */
strncpy (file, fname, GMT_LEN32-1);
if (gmt_file_is_cache (GMT->parent, file)) { /* Must be a cache file */
if (strstr (file, s->suffix) == NULL) {strcat (file, "."); strcat (file, s->suffix); } /* Must have suffix to download */
first = gmt_download_file_if_not_found (GMT, file, 0);
}
if (n_x2sys_paths) {
if (x2sys_get_data_path (GMT, path, &file[first], s->suffix)) return (GMT_GRDIO_FILE_NOT_FOUND);
if (MGD77_Open_File (GMT, path, &MC, 0)) return (GMT_GRDIO_OPEN_FAILED);
}
else if (MGD77_Open_File (GMT, &file[first], &MC, 0))
return (GMT_GRDIO_FILE_NOT_FOUND);
strcpy (s->path, MC.path);
if (MGD77_Read_Header_Record (GMT, &file[first], &MC, &H)) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Failure while reading header sequence for cruise %s\n", &file[first]);
return (GMT_GRDIO_READ_FAILED);
}
for (i = 0; i < MGD77_N_STRING_FIELDS; i++) tvals[i] = gmt_M_memory (GMT, NULL, 9, char);
z = gmt_M_memory (GMT, NULL, s->n_fields, double *);
for (i = 0; i < s->n_fields; i++) z[i] = gmt_M_memory (GMT, NULL, n_alloc, double);
for (i = 0; i < s->n_out_columns; i++) {
col[i] = MGD77_Get_Column (GMT, s->info[s->out_order[i]].name, &MC);
}
p->year = 0;
j = 0;
while (!MGD77_Read_Data_Record (GMT, &MC, &H, dvals, tvals)) { /* While able to read a data record */
gmt_lon_range_adjust (s->geodetic, &dvals[MGD77_LONGITUDE]);
for (i = 0; i < s->n_out_columns; i++)
z[i][j] = dvals[col[i]];
if (p->year == 0 && !gmt_M_is_dnan (dvals[0])) p->year = x2sys_get_first_year (GMT, dvals[0]);
j++;
if (j == n_alloc) {
n_alloc <<= 1;
for (i = 0; i < s->n_fields; i++) z[i] = gmt_M_memory (GMT, z[i], n_alloc, double);
}
}
MGD77_Close_File (GMT, &MC);
MGD77_Free_Header_Record (GMT, &MC, &H); /* Free up header structure */
MGD77_end (GMT, &MC);
strncpy (p->name, &file[first], 31U);
p->n_rows = j;
for (i = 0; i < s->n_fields; i++) z[i] = gmt_M_memory (GMT, z[i], p->n_rows, double);
p->ms_rec = NULL;
p->n_segments = 0;
for (i = 0; i < MGD77_N_STRING_FIELDS; i++) gmt_M_free (GMT, tvals[i]);
*data = z;
*n_rec = p->n_rows;
return (X2SYS_NOERROR);
}
int x2sys_read_mgd77ncfile (struct GMT_CTRL *GMT, char *fname, double ***data, struct X2SYS_INFO *s, struct X2SYS_FILE_INFO *p, struct GMT_IO *G, uint64_t *n_rec) {
uint64_t i;
unsigned int first = 0;
char path[PATH_MAX] = {""}, file[GMT_LEN32] = {""};
double **z = NULL;
struct MGD77_DATASET *S = NULL;
struct MGD77_CONTROL MC;
gmt_M_unused(G);
MGD77_Init (GMT, &MC); /* Initialize MGD77 Machinery */
MC.format = MGD77_FORMAT_CDF; /* Set input file's format to netCDF */
MGD77_Select_Format (GMT, MC.format); /* Only allow the specified MGD77 input format */
for (i = 0; i < s->n_out_columns; i++)
MC.desired_column[i] = strdup(s->info[s->out_order[i]].name); /* Set all the required fields */
MC.n_out_columns = s->n_out_columns;
S = MGD77_Create_Dataset (GMT); /* Get data structure w/header */
strncpy (file, fname, GMT_LEN32-1);
if (gmt_file_is_cache (GMT->parent, file)) { /* Must be a cache file */
if (strstr (file, s->suffix) == NULL) {strcat (file, "."); strcat (file, s->suffix); } /* Must have suffix to download */
first = gmt_download_file_if_not_found (GMT, file, 0);
}
if (n_x2sys_paths) {
if (x2sys_get_data_path (GMT, path, &file[first], s->suffix)) return (GMT_GRDIO_FILE_NOT_FOUND);
if (MGD77_Open_File (GMT, path, &MC, 0)) return (GMT_GRDIO_OPEN_FAILED);
}
else if (MGD77_Open_File (GMT, &file[first], &MC, 0))
return (GMT_GRDIO_FILE_NOT_FOUND);
strcpy (s->path, MC.path);
if (MGD77_Read_Header_Record (GMT, &file[first], &MC, &S->H)) { /* Returns info on all columns */
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_mgd77ncfile: Failure while reading header sequence for cruise %s\n", &file[first]);
return (GMT_GRDIO_READ_FAILED);
}
if (MGD77_Read_Data (GMT, &file[first], &MC, S)) { /* Only gets the specified columns and barfs otherwise */
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_mgd77ncfile: Failure while reading data set for cruise %s\n", &file[first]);
return (GMT_GRDIO_READ_FAILED);
}
MGD77_Close_File (GMT, &MC);
z = gmt_M_memory (GMT, NULL, MC.n_out_columns, double *);
for (i = 0; i < MC.n_out_columns; i++) z[i] = S->values[i];
strncpy (p->name, &file[first], 31U);
p->n_rows = S->H.n_records;
p->ms_rec = NULL;
p->n_segments = 0;
p->year = S->H.meta.Departure[0];
for (i = 0; i < MGD77_N_SETS; i++) gmt_M_free (GMT, S->flags[i]);
MGD77_Free_Header_Record (GMT, &MC, &(S->H)); /* Free up header structure */
gmt_M_free (GMT, S);
MGD77_end (GMT, &MC);
*data = z;
*n_rec = p->n_rows;
return (X2SYS_NOERROR);
}
int x2sys_read_ncfile (struct GMT_CTRL *GMT, char *fname, double ***data, struct X2SYS_INFO *s, struct X2SYS_FILE_INFO *p, struct GMT_IO *G, uint64_t *n_rec) {
int n_fields, ns = s->n_out_columns;
unsigned int first = 0;
uint64_t n_expect = GMT_MAX_COLUMNS;
uint64_t i, j;
char path[PATH_MAX] = {""}, file[GMT_LEN64] = {""};
double **z = NULL, *in = NULL;
FILE *fp = NULL;
gmt_M_unused(G);
strncpy (file, fname, GMT_LEN64-1);
if (gmt_file_is_cache (GMT->parent, file)) { /* Must be a cache file */
if (strstr (file, s->suffix) == NULL) {strcat (file, "."); strcat (file, s->suffix); } /* Must have suffix to download */
first = gmt_download_file_if_not_found (GMT, file, 0);
}
if (x2sys_get_data_path (GMT, path, &file[first], s->suffix)) return (GMT_GRDIO_FILE_NOT_FOUND);
strcat (path, "?"); /* Set all the required fields */
for (i = 0; i < s->n_out_columns; i++) {
if (i) strcat (path, "/");
strcat (path, s->info[s->out_order[i]].name);
}
strcpy (s->path, path);
gmt_parse_common_options (GMT, "b", 'b', "c"); /* Tell GMT this is a netCDF file */
if ((fp = gmt_fopen (GMT, path, "r")) == NULL) { /* Error in opening file */
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_ncfile: Failure while opening file %s\n", &file[first]);
return (GMT_GRDIO_READ_FAILED);
}
z = gmt_M_memory (GMT, NULL, s->n_out_columns, double *);
for (i = 0; i < s->n_out_columns; i++) z[i] = gmt_M_memory (GMT, NULL, GMT->current.io.ndim, double);
for (j = 0; j < GMT->current.io.ndim; j++) {
if ((in = GMT->current.io.input (GMT, fp, &n_expect, &n_fields)) == NULL || n_fields != ns) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "x2sys_read_ncfile: Failure while reading file %s at record %d\n", &file[first], j);
for (i = 0; i < s->n_out_columns; i++) gmt_M_free (GMT, z[i]);
gmt_M_free (GMT, z);
gmt_fclose (GMT, fp);
return (GMT_GRDIO_READ_FAILED);
}
for (i = 0; i < s->n_out_columns; i++) z[i][j] = in[i];