CDIwrite.c 3.67 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

  Copyright (C) 2003-2012 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
  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.
*/


#include <cdi.h>
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"


void *CDIwrite(void *argument)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
  int nvars = 1, nlevs = 0, ntimesteps = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
29
  int operatorID;
  int streamID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
  int tsID, varID, levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
31
  int gridsize, i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32
  int rval, rstart, rinc;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
34
35
  int vlistID;
  int gridID = -1, zaxisID, taxisID;
  int vdate, vtime, julday;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
  unsigned int seed = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
  const char *gridfile;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38
39
  double *levels = NULL;
  double ***vars = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
41
42

  cdoInitialize(argument);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
44
45
  operatorInputArg("grid, <nlevs, <ntimesteps, <nvars>>>");
  if ( operatorArgc() < 1 ) cdoAbort("Too few arguments!");
  if ( operatorArgc() > 4 ) cdoAbort("Too many arguments!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46

Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
48
49
50
51
  gridfile = operatorArgv()[0];
  gridID   = cdoDefineGrid(gridfile);
  if ( operatorArgc() >= 2 ) nlevs = atol(operatorArgv()[1]);
  if ( operatorArgc() >= 3 ) ntimesteps = atol(operatorArgv()[2]);
  if ( operatorArgc() >= 4 ) nvars = atol(operatorArgv()[3]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
52

Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
  srand(seed);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54

Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
  gridsize = gridInqSize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56

Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
  zaxisID = zaxisCreate(ZAXIS_SURFACE, 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58

Uwe Schulzweida's avatar
Uwe Schulzweida committed
59
  if ( cdoVerbose )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
62
63
64
65
66
67
68
69
70
71
72
      cdoPrint("gridsize   : %d", gridInqSize);
      cdoPrint("nlevs      : %d", nlevs);
      cdoPrint("ntimesteps : %d", ntimesteps);
      cdoPrint("nvars      : %d", nvars);
    } 

  if ( nlevs <= 0 ) nlevs = 1;
  if ( ntimesteps <= 0 ) ntimesteps = 1;
  if ( nvars <= 0 ) nvars = 1;

  vars = (double ***) malloc(nvars*sizeof(double **));
  for ( varID = 0; varID < nvars; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
75
76
77
78
79
80
      vars[varID] = (double **) malloc(nlevs*sizeof(double *));
      for ( levelID = 0; levelID < nlevs; levelID++ )
	{
	  vars[varID][levelID] = (double *) malloc(gridsize*sizeof(double));
	  for ( i = 0; i < gridsize; ++i )
	    vars[varID][levelID][i] = varID + rand()/(RAND_MAX+1.0);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
82
83
84
    }

  vlistID = vlistCreate();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
  for ( i = 0; i < nvars; ++i )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
88
89
      varID = vlistDefVar(vlistID, gridID, zaxisID, TIME_VARIABLE);
      vlistDefVarCode(vlistID, varID, varID+1);
      //    vlistDefVarName(vlistID, varID, );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
91
92
93
94
    }

  taxisID = taxisCreate(TAXIS_RELATIVE);
  vlistDefTaxis(vlistID, taxisID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
  // vlistDefNtsteps(vlistID, 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

  streamID = streamOpenWrite(cdoStreamName(0), cdoFiletype());

  streamDefVlist(streamID, vlistID);

  julday = date_to_julday(CALENDAR_PROLEPTIC, 10101);

  for ( tsID = 0; tsID < ntimesteps; tsID++ )
    {
      rval  = rstart + rinc*tsID;
      vdate = julday_to_date(CALENDAR_PROLEPTIC, julday + tsID);
      vtime = 0;
      taxisDefVdate(taxisID, vdate);
      taxisDefVtime(taxisID, vtime);
      streamDefTimestep(streamID, tsID);

      for ( varID = 0; varID < nvars; varID++ )
        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
          for ( levelID = 0; levelID < nlevs; levelID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
116
            {
              streamDefRecord(streamID, varID, levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
              streamWriteRecord(streamID, vars[varID][levelID], 0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
120
121
122
123
124
125
            }
        }
    }

  streamClose(streamID);

  vlistDestroy(vlistID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127
128
129
130
131
  for ( varID = 0; varID < nvars; varID++ )
    {
      free(vars[varID]);
      for ( levelID = 0; levelID < nlevs; levelID++ ) free(vars[varID][levelID]);
    }
  free(vars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
133
134
135
136

  cdoFinish();

  return (0);
}