source: trunk/include/ncutil.cpp @ 7

Last change on this file since 7 was 7, checked in by jbrlod, 13 years ago

rst write

  • Property svn:eol-style set to native
File size: 14.2 KB
Line 
1#include "../include/ncutil.h"//netcdf
2
3//--------------------- fonction d'erreur -------------------------------------
4using namespace std;
5
6inline void     handle_error (int status, char *txt)
7{
8        if (status != NC_NOERR)
9        { printf ("netcdf error: %s: netcdf statut: ", txt);
10        cout << nc_strerror (status) << endl;
11        exit (-1);
12        }
13}
14
15int Ouvre_nc_write(char const *var_file)
16{
17  ///* ouvre un fichier nc pour écriture et renvoie son id var_file_id
18  int var_file_id;
19  int status = nc_create(var_file, NC_CLOBBER, &var_file_id);
20  if (status != NC_NOERR) {
21  char file_error[100];
22    sprintf(file_error,"open %s",var_file);
23    handle_error (status, file_error);
24  }
25  return(var_file_id);
26}
27
28int Ouvre_nc(char const *var_file ) 
29{
30///* Ouvre un fichier nc et renvoie son id var_file_id
31  int var_file_id;
32  int status = nc_open (var_file, NC_NOWRITE | NC_SHARE,&var_file_id);
33  if (status != NC_NOERR){
34    char file_error[100];
35    sprintf(file_error,"open %s",var_file);
36    handle_error (status, file_error);
37  }
38  return(var_file_id);
39}
40
41int Var_id(int  var_file_id,char* varn) //initial traceurs actifs
42{// avoir l id d un variable a partir de l id var_file_id
43  int var_id;
44  int status = nc_inq_varid (var_file_id,varn,&var_id);
45  if (status != NC_NOERR){
46    char var_id_error[100];
47    sprintf(var_id_error,"%s_id error",varn);
48    handle_error (status, var_id_error);
49  }
50  return(var_id);
51}
52
53void voncwrite(int t,int znbval,int ynbval, int xnbval, char const *var_name,int ncid,int dimids[], YREAL vect[])
54{ //Ecrrire le volume 3D au temps t
55  //le parametre t commence de 0.
56  // ecrit la variable var_name aux points (t, 1:znbval, 1:ynbval, 1:xnbval)
57  int status;
58
59  int var_id;
60  int local_dimids[4];
61  static size_t start[] = {0,0,0,0}; //3D+t
62  static size_t count[] = {1,1,1,1}; //3D+t
63  count[0]= (size_t) t+1;
64  count[1]= (size_t) znbval;
65  count[2]= (size_t) ynbval;
66  count[3]= (size_t) xnbval;
67  start[0]=t; //temps commence de 0;
68
69  local_dimids[0]=dimids[0]; //t
70  local_dimids[1]=dimids[1]; //z
71  local_dimids[2]=dimids[2]; //y
72  local_dimids[3]=dimids[3]; //x
73
74  //Define the netCDF variable
75  status=nc_redef(ncid);
76if (status != NC_NOERR)
77    {
78      handle_error (status, "Erreur change to def mode NetCDF ::voncwrite");
79    }
80 
81 status=nc_def_var(ncid,var_name,NC_REAL,4,local_dimids,&var_id);
82 
83  if (status != NC_NOERR)
84    {
85      printf("variable %s:",var_name);
86      handle_error (status, "Erreur define NetCDF variable ::voncwrite");
87    }
88 nc_enddef(ncid);
89  status=nc_put_vara_real(ncid,var_id,start,count,vect);
90 if (status != NC_NOERR)
91    {
92      handle_error (status, "Erreur writing NetCDF variable ::voncwrite");
93    }
94}
95
96void vonclire(int t,int znbval, int ynbval, int xnbval, int ncid, int varid, double vect[])
97{// lire volume 3D au temps t
98  // le parametre t commence de 0 et pas de 1
99  // (t,znbval,ynbval,xnbval,ncid, varid):
100  // lit la variable varid aux points (t,1:znbval,1:ynbval,1:xnbval) et
101  // renvoie un vecteur de dim znbval*ynbval*xnbval
102 
103  int           status;
104  static size_t   start[] = { 0, 0, 0, 0 };  // 3D+t
105  static size_t   count[] = { 1, 1, 1, 1 }; // 3D+t
106  count[0] =(size_t)  t+1;
107  count[1] =(size_t)  znbval;
108  count[2] =(size_t)  ynbval;
109  count[3] =(size_t)  xnbval;
110  start[0] = (size_t) t;   // temps commence de 0:
111 
112  // Read an array of values
113  status = nc_get_vara_double (ncid, varid, start, count, vect);
114 
115 if (status != NC_NOERR)
116    {
117      handle_error (status, "Erreur lecture fichier NetCDF::vonclire");
118    }
119}
120
121void soncwrite(int t,int ynbval, int xnbval, char const *var_name,int ncid,int dimids[], YREAL vect[])
122{ //Ecrrire le volume 2D au temps t
123  //le parametre t commence de 0.
124  // ecrit la variable var_name aux points (t, 1:ynbval, 1:xnbval)
125  int status;
126
127  int var_id;
128  int local_dimids[3];
129  static size_t start[] = {0,0,0}; //2D+t
130  static size_t count[] = {1,1,1}; //2D+t
131  count[0]= (size_t) t+1;
132  count[1]= (size_t) ynbval;
133  count[2]= (size_t) xnbval;
134  start[0]=t; //temps commence de 0;
135
136  local_dimids[0]=dimids[0]; //t
137  local_dimids[1]=dimids[2]; //y
138  local_dimids[2]=dimids[3]; //x
139
140  //Define the netCDF variable
141  status=nc_redef(ncid);
142if (status != NC_NOERR)
143    {
144      handle_error (status, "Erreur change to def mode NetCDF ::voncwrite");
145    }
146 
147 status=nc_def_var(ncid,var_name,NC_REAL,3,local_dimids,&var_id);
148 
149  if (status != NC_NOERR)
150    {
151      printf("variable %s:",var_name);
152      handle_error (status, "Erreur define NetCDF variable ::voncwrite");
153    }
154 nc_enddef(ncid);
155  status=nc_put_vara_real(ncid,var_id,start,count,vect);
156 if (status != NC_NOERR)
157    {
158      handle_error (status, "Erreur writing NetCDF variable ::voncwrite");
159    }
160}
161
162
163void sonclire(int t,int ynbval, int xnbval, int ncid, int varid, double vect[])
164{// lire surface 2D au temps t
165  // le parametre t commence de 0 et pas de 1
166  // (t,ynbval,xnbval,ncid, varid):
167  // lit la variable varid aux points (t,1:ynbval,1:xnbval) et
168  // renvoie un vecteur de dim ynbval*xnbval
169 
170  int           status;
171  static size_t   start[] = { 0, 0, 0 };  // 2D+t
172  static size_t   count[] = { 1, 1, 1 }; // 2D+t
173  count[0] =(size_t)  t+1;
174  count[1] =(size_t)  ynbval;
175  count[2] =(size_t)  xnbval;
176  start[0] = (size_t) t;   // temps commence de 0:
177 
178  // Read an array of values
179  status = nc_get_vara_double (ncid, varid, start, count, vect);
180 
181 if (status != NC_NOERR)
182    {
183      handle_error (status, "Erreur lecture fichier NetCDF::sonclire");
184    }
185}
186
187void sonclire(int t,int ynbval, int xnbval, int ncid, int varid, float vect[])
188{// lire surface 2D au temps t
189  // le parametre t commence de 0 et pas de 1
190  // (t,ynbval,xnbval,ncid, varid):
191  // lit la variable varid aux points (t,1:ynbval,1:xnbval) et
192  // renvoie un vecteur de dim ynbval*xnbval
193 
194  int           status;
195  static size_t   start[] = { 0, 0, 0 };  // 2D+t
196  static size_t   count[] = { 1, 1, 1 }; // 2D+t
197  count[0] =(size_t)  t+1;
198  count[1] =(size_t)  ynbval;
199  count[2] =(size_t)  xnbval;
200  start[0] = (size_t) t;   // temps commence de 0:
201 
202  // Read an array of values
203  status = nc_get_vara_float (ncid, varid, start, count, vect);
204 
205 if (status != NC_NOERR)
206    {
207      handle_error (status, "Erreur lecture fichier NetCDF::sonclire");
208    }
209}
210
211void snclire(int ynbval, int xnbval, int ncid, int varid, double vect[])
212{// lire surface 2D
213  // (ynbval,xnbval,ncid, varid):
214  // lit la variable varid aux points (1:ynbval,1:xnbval) et
215  // renvoie un vecteur de dim ynbval*xnbval
216 
217  int           status;
218  static size_t   start[] = { 0, 0 };  // 2D
219  static size_t   count[] = { 1, 1 }; // 2D
220  count[0] =(size_t)  ynbval;
221  count[1] =(size_t)  xnbval;
222 
223  // Read an array of values
224  status = nc_get_vara_double (ncid, varid, start, count, vect);
225 
226 if (status != NC_NOERR)
227    {
228      handle_error (status, "Erreur lecture fichier NetCDF::snclire");
229    }
230}
231
232void snclire(int ynbval, int xnbval, int ncid, int varid, float vect[])
233{// lire surface 2D
234  // (ynbval,xnbval,ncid, varid):
235  // lit la variable varid aux points (1:ynbval,1:xnbval) et
236  // renvoie un vecteur de dim ynbval*xnbval
237 
238  int           status;
239  static size_t   start[] = { 0, 0 };  // 2D
240  static size_t   count[] = { 1, 1 }; // 2D
241  count[0] =(size_t)  ynbval;
242  count[1] =(size_t)  xnbval;
243 
244  // Read an array of values
245  status = nc_get_vara_float (ncid, varid, start, count, vect);
246 
247 if (status != NC_NOERR)
248    {
249      handle_error (status, "Erreur lecture fichier NetCDF::snclire");
250    }
251}
252
253void zoncwrite(int t,int znbval,char const *var_name,int ncid,int dimids[], YREAL vect[])
254{ //Ecrrire la variable 1D au temps t
255  //le parametre t commence de 0.
256  // ecrit la variable var_name aux points (t, 1:znbval)
257  int status;
258
259  int var_id;
260  int local_dimids[2];
261  static size_t start[] = {0,0}; //1D+t
262  static size_t count[] = {1,1}; //1D+t
263  count[0]= (size_t) t+1;
264  count[1]= (size_t) znbval;
265 
266  start[0]=t; //temps commence de 0;
267
268  local_dimids[0]=dimids[0]; //t
269  local_dimids[1]=dimids[1]; //z
270 
271
272  //Define the netCDF variable
273  status=nc_redef(ncid);
274if (status != NC_NOERR)
275    {
276      handle_error (status, "Erreur change to def mode NetCDF ::voncwrite");
277    }
278 
279 status=nc_def_var(ncid,var_name,NC_REAL,2,local_dimids,&var_id);
280 
281  if (status != NC_NOERR)
282    {
283      printf("variable %s:",var_name);
284      handle_error (status, "Erreur define NetCDF variable ::voncwrite");
285    }
286 nc_enddef(ncid);
287  status=nc_put_vara_real(ncid,var_id,start,count,vect);
288 if (status != NC_NOERR)
289    {
290      handle_error (status, "Erreur writing NetCDF variable ::voncwrite");
291    }
292}
293
294
295void zonclire(int t,int znbval, int ncid, int varid, double vect[])
296{// lire surface 1D au temps t
297  // le parametre t commence de 0 et pas de 1
298  // (t,znbval,ncid, varid):
299  // lit la variable varid aux points (t,1:znbval) et
300  // renvoie un vecteur de dim znbval
301 
302  int           status;
303  static size_t   start[] = { 0, 0 };  // t+1D
304  static size_t   count[] = { 1, 1 }; // t+1D
305  count[0] =(size_t)  t+1;
306  count[1] =(size_t)  znbval;
307  start[0] = (size_t) t;   // temps commence de 0:
308 
309  // Read an array of values
310  status = nc_get_vara_double (ncid, varid, start, count, vect);
311 
312 if (status != NC_NOERR)
313    {
314      handle_error (status, "Erreur lecture fichier NetCDF::zonclire");
315    }
316}
317
318
319void vonclire(int t,int znbval, int ynbval, int xnbval, int ncid, int varid, int vect[])
320{// lire volume 3D au temps t
321  // le parametre t commence de 0 et pas de 1
322  // (t,znbval,ynbval,xnbval,ncid, varid):
323  // lit la variable varid aux points (t,1:znbval,1:ynbval,1:xnbval) et
324  // renvoie un vecteur de dim znbval*ynbval*xnbval
325 
326  int           status;
327  static size_t   start[] = { 0, 0, 0, 0 };  // 3D+t
328  static size_t   count[] = { 1, 1, 1, 1 }; // 3D+t
329  count[0] =(size_t)  t+1;
330  count[1] =(size_t)  znbval;
331  count[2] =(size_t)  ynbval;
332  count[3] =(size_t)  xnbval;
333  start[0] = (size_t) t;   // temps commence de 0:
334 
335  // Read an array of values
336  status = nc_get_vara_int (ncid, varid, start, count, vect);
337 
338 if (status != NC_NOERR)
339    {
340      handle_error (status, "Erreur lecture fichier NetCDF::vonclire");
341    }
342}
343
344void sonclire(int t,int ynbval, int xnbval, int ncid, int varid, int vect[])
345{// lire surface 2D au temps t
346  // le parametre t commence de 0 et pas de 1
347  // (t,ynbval,xnbval,ncid, varid):
348  // lit la variable varid aux points (t,1:ynbval,1:xnbval) et
349  // renvoie un vecteur de dim ynbval*xnbval
350 
351  int           status;
352  static size_t   start[] = { 0, 0, 0 };  // 2D+t
353  static size_t   count[] = { 1, 1, 1 }; // 2D+t
354  count[0] =(size_t)  t+1;
355  count[1] =(size_t)  ynbval;
356  count[2] =(size_t)  xnbval;
357  start[0] = (size_t) t;   // temps commence de 0:
358 
359  // Read an array of values
360  status = nc_get_vara_int (ncid, varid, start, count, vect);
361 
362 if (status != NC_NOERR)
363    {
364      handle_error (status, "Erreur lecture fichier NetCDF::sonclire");
365    }
366}
367
368void zonclire(int t,int znbval, int ncid, int varid, int vect[])
369{// lire surface 1D au temps t
370  // le parametre t commence de 0 et pas de 1
371  // (t,znbval,ncid, varid):
372  // lit la variable varid aux points (t,1:znbval) et
373  // renvoie un vecteur de dim znbval
374 
375  int           status;
376  static size_t   start[] = { 0, 0 };  // t+1D
377  static size_t   count[] = { 1, 1 }; // t+1D
378  count[0] =(size_t)  t+1;
379  count[1] =(size_t)  znbval;
380  start[0] = (size_t) t;   // temps commence de 0:
381 
382  // Read an array of values
383  status = nc_get_vara_int (ncid, varid, start, count, vect);
384 
385 if (status != NC_NOERR)
386    {
387      handle_error (status, "Erreur lecture fichier NetCDF::zonclire");
388    }
389}
390
391void znclire(int znbval, int ncid, int varid, double vect[])
392{// lire surface 1D
393  // (znbval,ncid, varid):
394  // lit la variable varid aux points (1:znbval) et
395  // renvoie un vecteur de dim znbval
396 
397  int           status;
398  static size_t   start[] = { 0 };  //1D
399  static size_t   count[] = { 1 }; // 1D
400  count[0] =(size_t)  znbval;
401 
402 
403  // Read an array of values
404  status = nc_get_vara_double (ncid, varid, start, count, vect);
405 
406 if (status != NC_NOERR)
407    {
408      handle_error (status, "Erreur lecture fichier NetCDF::zonclire");
409    }
410}
411
412
413void znclire(int znbval, int ncid, int varid, float vect[])
414{// lire surface 1D
415  // (znbval,ncid, varid):
416  // lit la variable varid aux points (1:znbval) et
417  // renvoie un vecteur de dim znbval
418 
419  int           status;
420  static size_t   start[] = { 0 };  //1D
421  static size_t   count[] = { 1 }; // 1D
422  count[0] =(size_t)  znbval;
423 
424 
425  // Read an array of values
426  status = nc_get_vara_float (ncid, varid, start, count, vect);
427 
428 if (status != NC_NOERR)
429    {
430      handle_error (status, "Erreur lecture fichier NetCDF::zonclire");
431    }
432}
433
434void sncwrite(int ynbval, int xnbval, char const *var_name,int ncid,int dimids[], YREAL vect[])
435{ //Ecrrire le volume 2D
436  // ecrit la variable var_name aux points (1:ynbval, 1:xnbval)
437  int status;
438
439  int var_id;
440  int local_dimids[2];
441  static size_t start[] = {0,0}; //2D
442  static size_t count[] = {1,1}; //2D
443  count[0]= (size_t) ynbval;
444  count[1]= (size_t) xnbval;
445 
446  local_dimids[0]=dimids[2]; //y
447  local_dimids[1]=dimids[3]; //x
448
449  //Define the netCDF variable
450  status=nc_redef(ncid);
451if (status != NC_NOERR)
452    {
453      handle_error (status, "Erreur change to def mode NetCDF ::voncwrite");
454    }
455 
456 status=nc_def_var(ncid,var_name,NC_REAL,2,local_dimids,&var_id);
457 
458  if (status != NC_NOERR)
459    {
460      printf("variable %s:",var_name);
461      handle_error (status, "Erreur define NetCDF variable ::voncwrite");
462    }
463 nc_enddef(ncid);
464  status=nc_put_vara_real(ncid,var_id,start,count,vect);
465 if (status != NC_NOERR)
466    {
467      handle_error (status, "Erreur writing NetCDF variable ::voncwrite");
468    }
469}
470
471
472void zncwrite(int znbval,char const *var_name,int ncid,int dimids[], YREAL vect[])
473{ //Ecrrire la variable 1D
474  // ecrit la variable var_name aux points (1:znbval)
475  int status;
476
477  int var_id;
478  int local_dimids[1];
479  static size_t start[] = {0}; //1D
480  static size_t count[] = {1}; //1D
481
482  count[0]= (size_t) znbval;
483 
484
485  local_dimids[0]=dimids[1]; //z
486 
487
488  //Define the netCDF variable
489  status=nc_redef(ncid);
490if (status != NC_NOERR)
491    {
492      handle_error (status, "Erreur change to def mode NetCDF ::voncwrite");
493    }
494 
495 status=nc_def_var(ncid,var_name,NC_REAL,1,local_dimids,&var_id);
496 
497  if (status != NC_NOERR)
498    {
499      printf("variable %s:",var_name);
500      handle_error (status, "Erreur define NetCDF variable ::voncwrite");
501    }
502 nc_enddef(ncid);
503  status=nc_put_vara_real(ncid,var_id,start,count,vect);
504 if (status != NC_NOERR)
505    {
506      handle_error (status, "Erreur writing NetCDF variable ::voncwrite");
507    }
508}
Note: See TracBrowser for help on using the repository browser.