Cat.c 3.68 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
  Copyright (C) 2003-2014 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
  See COPYING 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 General Public License as published by
  the Free Software Foundation; version 2 of the License.

  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 General Public License for more details.
*/

18
19
20
21
22
23
/*
   This module contains the following operators:

      Copy       cat             Concatenate datasets
*/

Ralf Mueller's avatar
Ralf Mueller committed
24
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
27
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"
28
#include "util.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
30
31
32
33
34
35


void *Cat(void *argument)
{
  int streamID1, streamID2 = CDI_UNDEFID;
  int nrecs;
  int tsID1, tsID2 = 0, recID, varID, levelID;
36
  int vlistID1, vlistID2 = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
40
41
  int streamCnt, nfiles, indf;
  int taxisID1, taxisID2 = CDI_UNDEFID;
  int lcopy = FALSE;
  int gridsize;
  int nmiss;
42
  int ntsteps, nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
45
46
47
48
49
50
51
52
53
  double *array = NULL;

  cdoInitialize(argument);

  if ( UNCHANGED_RECORD ) lcopy = TRUE;

  streamCnt = cdoStreamCnt();
  nfiles = streamCnt - 1;

  for ( indf = 0; indf < nfiles; indf++ )
    {
54
      if ( cdoVerbose ) cdoPrint("Process file: %s", cdoStreamName(indf)->args);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55

Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
57
58
59
60
61
62
      streamID1 = streamOpenRead(cdoStreamName(indf));

      vlistID1 = streamInqVlist(streamID1);
      taxisID1 = vlistInqTaxis(vlistID1);

      if ( indf == 0 )
	{
63
	  if ( fileExists(cdoStreamName(nfiles)->args) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
65
66
67
68
69
	    {
	      streamID2 = streamOpenAppend(cdoStreamName(nfiles));

	      vlistID2 = streamInqVlist(streamID2);
	      taxisID2 = vlistInqTaxis(vlistID2);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
70
	      vlistCompare(vlistID1, vlistID2, CMP_ALL);
71

Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
	      tsID2 = vlistNtsteps(vlistID2);
73
	      if ( tsID2 == 0 ) tsID2 = 1; /* bug fix for time constant data only */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
75
76
77
	    }
	  else
	    {
	      if ( cdoVerbose )
78
		cdoPrint("Output file doesn't exist, creating: %s", cdoStreamName(nfiles)->args);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80
81
82
83
84
85

	      streamID2 = streamOpenWrite(cdoStreamName(nfiles), cdoFiletype());

	      vlistID2 = vlistDuplicate(vlistID1);
	      taxisID2 = taxisDuplicate(taxisID1);
	      vlistDefTaxis(vlistID2, taxisID2);
	  
86
	      ntsteps = vlistNtsteps(vlistID1);
87
88
89
	      nvars   = vlistNvars(vlistID1);
	      
	      if ( ntsteps == 1 )
90
		{
91
		  for ( varID = 0; varID < nvars; ++varID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92
		    if ( vlistInqVarTsteptype(vlistID1, varID) != TSTEP_CONSTANT ) break;
93
		  
94
95
96
97
98
		  if ( varID == nvars ) ntsteps = 0;
		}

	      if ( ntsteps == 0 && nfiles > 1 )
		{		  
99
		  for ( varID = 0; varID < nvars; ++varID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
		    vlistDefVarTsteptype(vlistID2, varID, TSTEP_INSTANT);
101
102
		}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
104
105
106
107
108
109
110
111
	      streamDefVlist(streamID2, vlistID2);
	    }

	  if ( ! lcopy )
	    {
	      gridsize = vlistGridsizeMax(vlistID1);
	      array = (double *) malloc(gridsize*sizeof(double));
	    }
	}
112
113
      else
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
	  vlistCompare(vlistID1, vlistID2, CMP_ALL);
115
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
117
118
119
120
121
122
123
124
125
126
127

      tsID1 = 0;
      while ( (nrecs = streamInqTimestep(streamID1, tsID1)) )
	{
	  taxisCopyTimestep(taxisID2, taxisID1);

	  streamDefTimestep(streamID2, tsID2);
	       
	  for ( recID = 0; recID < nrecs; recID++ )
	    {
	      streamInqRecord(streamID1, &varID, &levelID);
	      streamDefRecord(streamID2,  varID,  levelID);
128

Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	      if ( lcopy )
		{
		  streamCopyRecord(streamID2, streamID1); 
		}
	      else
		{
		  streamReadRecord(streamID1, array, &nmiss);
		  streamWriteRecord(streamID2, array, nmiss);
		}
	    }
	  tsID1++;
	  tsID2++;
	}
      streamClose(streamID1);
    }

  streamClose(streamID2);
 
147
  if ( array ) free(array);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149
150
151
152

  cdoFinish();

  return (0);
}