Copy.c 4.28 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-2010 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       copy            Copy datasets
*/

Ralf Mueller's avatar
Ralf Mueller committed
24
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
#include "cdo.h"
#include "cdo_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
#include "par_io.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
29
#include "pstream.h"

30
void    vlistDefVarTime(int vlistID, int varID, int timeID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
31
32
33

void *Copy(void *argument)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
  static const char *func = "Copy";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36
  int COPY, SELALL;
  int operatorID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
  int streamID1, streamID2 = CDI_UNDEFID;
  int nrecs;
  int tsID1, tsID2, recID, varID, levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
  int lcopy = FALSE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
  int gridsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
42
  int vlistID1, vlistID2 = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
45
  int nmiss;
  int streamCnt, nfiles, indf;
  int taxisID1, taxisID2 = CDI_UNDEFID;
46
  int ntsteps;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
  double *array = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
  par_io_t parIO;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
51

  cdoInitialize(argument);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
53
54
55
56
57
58
  COPY    = cdoOperatorAdd("copy",   0, 0, NULL);
  SELALL  = cdoOperatorAdd("selall", 0, 0, NULL);

  if ( UNCHANGED_RECORD ) lcopy = TRUE;

  operatorID = cdoOperatorID();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
60
61
62
63
64
  streamCnt = cdoStreamCnt();
  nfiles = streamCnt - 1;

  tsID2 = 0;
  for ( indf = 0; indf < nfiles; indf++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
66
      if ( cdoVerbose ) cdoPrint("Process file: %s", cdoStreamName(indf));

Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
      streamID1 = streamOpenRead(cdoStreamName(indf));
      if ( streamID1 < 0 ) cdiError(streamID1, "Open failed on %s", cdoStreamName(indf));

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

      if ( indf == 0 )
	{
	  streamID2 = streamOpenWrite(cdoStreamName(nfiles), cdoFiletype());
	  if ( streamID2 < 0 ) cdiError(streamID2, "Open failed on %s", cdoStreamName(nfiles));

	  vlistID2 = vlistDuplicate(vlistID1);
	  taxisID2 = taxisDuplicate(taxisID1);
	  vlistDefTaxis(vlistID2, taxisID2);

82
83
84
85
86
87
88
89
90
	  ntsteps = vlistNtsteps(vlistID1);
	  if ( ntsteps == 0 && nfiles > 1 )
	    {
	      int nvars = vlistNvars(vlistID1);
	      
	      for ( varID = 0; varID < nvars; ++varID )
		vlistDefVarTime(vlistID2, varID, TIME_VARIABLE);
	    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
92
93
94
	  streamDefVlist(streamID2, vlistID2);

	  gridsize = vlistGridsizeMax(vlistID1);
	  array = (double *) malloc(gridsize*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
96
	  if ( cdoParIO )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
	      fprintf(stderr, "Parallel reading enabled!\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
99
100
	      parIO.array = (double *) malloc(gridsize*sizeof(double));
	      parIO.array_size = gridsize;
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
102
103
      else
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
	  vlistCompare(vlistID1, vlistID2, CMP_SFT);
	  /* vlistCompare(vlistID1, vlistID2, CMP_HRD); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
108
109
110
111
112
113
114
115

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

	  streamDefTimestep(streamID2, tsID2);
	       
	  for ( recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
	    { 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
118
	      if ( lcopy && operatorID == SELALL )
		{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
120
		  streamInqRecord(streamID1, &varID, &levelID);
		  streamDefRecord(streamID2,  varID,  levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
122
123
124
		  streamCopyRecord(streamID2, streamID1);
		}
	      else
		{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
126
127
		  if ( cdoParIO )
		    {
		      parIO.recID = recID; parIO.nrecs = nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
128
		      /* fprintf(stderr, "in1 streamID %d varID %d levelID %d\n", streamID1, varID, levelID);*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
		      parReadRecord(streamID1, &varID, &levelID, array, &nmiss, &parIO);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
		      /* fprintf(stderr, "in2 streamID %d varID %d levelID %d\n", streamID1, varID, levelID);*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
134
135
136
		    }
		  else
		    {
		      streamInqRecord(streamID1, &varID, &levelID);
		      streamReadRecord(streamID1, array, &nmiss);
		    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
140
		  /*
		  if ( cdoParIO )
		    fprintf(stderr, "out1 %d %d %d\n", streamID2,  varID,  levelID);
		  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
		  streamDefRecord(streamID2,  varID,  levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
		  streamWriteRecord(streamID2, array, nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
144
145
146
		  /*
		  if ( cdoParIO )
		    fprintf(stderr, "out2 %d %d %d\n", streamID2,  varID,  levelID);
		  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
147
		}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149
150
151
	    }
	  tsID1++;
	  tsID2++;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152

Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154
155
156
157
158
159
160
161
162
163
      streamClose(streamID1);
    }

  streamClose(streamID2);

  if ( array ) free(array);

  cdoFinish();

  return (0);
}