1 /*
2   This file is part of CDO. CDO is a collection of Operators to manipulate and analyse Climate model Data.
3 
4   Author: Uwe Schulzweida
5 
6 */
7 
8 #include <cdi.h>
9 
10 #include "process_int.h"
11 #include "varray.h"
12 
13 void *
Test(void * process)14 Test(void *process)
15 {
16   cdo_initialize(process);
17 
18   /*
19   const auto streamID1 = cdo_open_read(0);
20   const auto streamID2 = cdo_open_write(1);
21 
22   cdo_stream_close(streamID2);
23   cdo_stream_close(streamID1);
24   */
25   cdo_finish();
26 
27   return nullptr;
28 }
29 
30 void *
Test2(void * process)31 Test2(void *process)
32 {
33   cdo_initialize(process);
34 
35   /*
36   const auto streamID1 = cdo_open_read(0);
37   const auto streamID2 = cdo_open_read(1);
38   const auto streamID3 = cdo_open_write(2);
39 
40   cdo_stream_close(streamID3);
41   cdo_stream_close(streamID2);
42   cdo_stream_close(streamID1);
43   */
44   cdo_finish();
45 
46   return nullptr;
47 }
48 
49 void *
Testdata(void * process)50 Testdata(void *process)
51 {
52   cdo_initialize(process);
53 
54   operator_check_argc(0);
55 
56   const auto streamID1 = cdo_open_read(0);
57 
58   const auto vlistID1 = cdo_stream_inq_vlist(streamID1);
59   const auto taxisID1 = vlistInqTaxis(vlistID1);
60 
61   const auto streamID2 = cdo_open_write(1);
62 
63   const auto vlistID2 = vlistDuplicate(vlistID1);
64   const auto taxisID2 = taxisDuplicate(taxisID1);
65   vlistDefTaxis(vlistID2, taxisID2);
66 
67   cdo_def_vlist(streamID2, vlistID2);
68 
69   auto gridsize = vlistGridsizeMax(vlistID1);
70   Varray<double> array(gridsize);
71   Varray<float> fval(gridsize);
72   Varray<int> ival(gridsize);
73   Varray<unsigned char> cval(gridsize * 4);
74   Varray<unsigned char> cval2(gridsize * 4);
75 
76   auto fp = fopen("testdata", "w");
77 
78   int tsID2 = 0;
79   int tsID1 = 0;
80   while (true)
81     {
82       const auto nrecs = cdo_stream_inq_timestep(streamID1, tsID1);
83       if (nrecs == 0) break;
84 
85       cdo_taxis_copy_timestep(taxisID2, taxisID1);
86       cdo_def_timestep(streamID2, tsID2);
87 
88       for (int recID = 0; recID < nrecs; recID++)
89         {
90           int varID, levelID;
91           cdo_inq_record(streamID1, &varID, &levelID);
92           cdo_def_record(streamID2, varID, levelID);
93 
94           size_t nmiss;
95           cdo_read_record(streamID1, array.data(), &nmiss);
96 
97           gridsize = gridInqSize(vlistInqVarGrid(vlistID1, varID));
98           for (size_t i = 0; i < gridsize; ++i)
99             {
100               fval[i] = (float) array[i];
101 
102               memcpy(&ival[i], &fval[i], 4);
103               memcpy(&cval[i * 4], &fval[i], 4);
104 
105               cval2[i + gridsize * 0] = cval[i * 4 + 0];
106               cval2[i + gridsize * 1] = cval[i * 4 + 1];
107               cval2[i + gridsize * 2] = cval[i * 4 + 2];
108               cval2[i + gridsize * 3] = cval[i * 4 + 3];
109 
110               if (tsID1 == 0 && recID == 0)
111                 printf("%4zu %3u %3u %3u %3u %d %g\n", i, (unsigned int) cval[4 * i + 0], (unsigned int) cval[4 * i + 1],
112                        (unsigned int) cval[4 * i + 2], (unsigned int) cval[4 * i + 3], ival[i], fval[i]);
113             }
114 
115           cdo_write_record(streamID2, array.data(), nmiss);
116 
117           fwrite(cval.data(), 4, gridsize, fp);
118         }
119 
120       tsID1++;
121       tsID2++;
122     }
123 
124   fclose(fp);
125   cdo_stream_close(streamID1);
126   cdo_stream_close(streamID2);
127 
128   cdo_finish();
129 
130   return nullptr;
131 }
132