1 | #!/usr/bin/env cdat |
---|
2 | # -*- coding: iso-8859-15 -*- |
---|
3 | |
---|
4 | import os, sys, getopt, time |
---|
5 | import cdms, cdutil, MV |
---|
6 | from cdms.axis import TransientAxis |
---|
7 | from cdms.coord import TransientVirtualAxis |
---|
8 | |
---|
9 | from dataproc.utils import cdat |
---|
10 | |
---|
11 | import string |
---|
12 | |
---|
13 | from Numeric import ones |
---|
14 | |
---|
15 | import fonctions_orchidee |
---|
16 | |
---|
17 | import vcs |
---|
18 | |
---|
19 | def usage(): |
---|
20 | """convert a restart file in CF convention.""" |
---|
21 | |
---|
22 | man = """ |
---|
23 | Usage : creation of a CF_'restart_file'.nc |
---|
24 | prompt> CF_restart.py restart_file.nc |
---|
25 | |
---|
26 | exemples : CF_restart.py sechiba_rest_out.nc |
---|
27 | """ |
---|
28 | print man |
---|
29 | |
---|
30 | options = [ |
---|
31 | "help", |
---|
32 | "version" ] |
---|
33 | |
---|
34 | check_test = 0 |
---|
35 | |
---|
36 | |
---|
37 | # Extract arguments from the command line |
---|
38 | try: |
---|
39 | myopts, myargs = getopt.getopt(sys.argv[1:], 'Vh', longopts = options) |
---|
40 | except getopt.GetoptError: |
---|
41 | print 'getopt error' |
---|
42 | usage() |
---|
43 | sys.exit(1) |
---|
44 | |
---|
45 | if (len(myargs) < 1): |
---|
46 | print 'I need at least 1 file.' |
---|
47 | usage() |
---|
48 | sys.exit(1) |
---|
49 | else: |
---|
50 | fich = myargs[0] |
---|
51 | if (os.path.exists(fich)==False): |
---|
52 | print 'file ',fich,' doesn''t exist !' |
---|
53 | sys.exit(1) |
---|
54 | |
---|
55 | # Affichage des variables : fixe la limite max |
---|
56 | MV.set_print_limit(320) |
---|
57 | |
---|
58 | # travail sur le fichier d'entrée |
---|
59 | f=cdms.open(fich) |
---|
60 | listev = f.listvariables() |
---|
61 | print "Liste des variables de "+fich,listev |
---|
62 | |
---|
63 | |
---|
64 | # Write result file |
---|
65 | file_out = "CF_"+fich |
---|
66 | print '\nWriting to file : ', file_out |
---|
67 | fout = cdms.open(file_out, 'w') |
---|
68 | |
---|
69 | # recuperation des axes : |
---|
70 | # lon et lat |
---|
71 | VarLon=f('nav_lon') |
---|
72 | VarLat=f('nav_lat') |
---|
73 | axisLon = VarLon.getAxis(0) |
---|
74 | axisLat = VarLon.getAxis(1) |
---|
75 | |
---|
76 | # tailles des axes |
---|
77 | jjm = len(axisLon) |
---|
78 | iim = len(axisLat) |
---|
79 | print "Tailles (iim,jjm) = ",iim,jjm |
---|
80 | |
---|
81 | lon=VarLon[0,:] |
---|
82 | lat=VarLat[:,0] |
---|
83 | |
---|
84 | att_lon = cdat.getAttributes(VarLon) |
---|
85 | att_lon['name']='lon' |
---|
86 | ax_lon = TransientAxis(lon, bounds=None, id='lon', attributes=att_lon, copy=1) |
---|
87 | |
---|
88 | att_lat = cdat.getAttributes(VarLat) |
---|
89 | att_lat['name']='lat' |
---|
90 | ax_lat = TransientAxis(lat, bounds=None, id='lat', attributes=att_lat, copy=1) |
---|
91 | if (check_test): print "Axes lon, lat : \n", ax_lon,"\n", ax_lat |
---|
92 | |
---|
93 | level=f('nav_lev') |
---|
94 | att_lev = cdat.getAttributes(level) |
---|
95 | ax_lev = TransientAxis(level, bounds=None, id='lev', attributes=att_lev, copy=1) |
---|
96 | |
---|
97 | # Boucle sur les variables |
---|
98 | for var in listev: |
---|
99 | print "*=*==*=*=*=*=*=*=*=*=*=*=*=*=*=**==*" |
---|
100 | print "Variable : ",var |
---|
101 | var0=f(var) |
---|
102 | |
---|
103 | copyattributes = cdat.getAttributes(var0) |
---|
104 | if (check_test): print var,' ; attributs : ',copyattributes |
---|
105 | v0shape = var0.shape |
---|
106 | if (check_test): print "Tailles de "+var+" : ",v0shape |
---|
107 | if (check_test): print "(min / max) de ",var," = ", cdms.MV.minimum(var0),cdms.MV.maximum(var0) |
---|
108 | |
---|
109 | |
---|
110 | axes = var0.getAxisList() |
---|
111 | nl = len(axes) |
---|
112 | |
---|
113 | |
---|
114 | if (check_test): print "Liste des axes de la variable "+var+" : " |
---|
115 | if (check_test): print axes |
---|
116 | |
---|
117 | listeaxes=[] |
---|
118 | for ax in axes: |
---|
119 | if (check_test): print ax |
---|
120 | if (check_test): print "Attributes de ax : ",ax.attributes |
---|
121 | if (ax.id == 'x'): |
---|
122 | listeaxes.append(ax_lon) |
---|
123 | elif (ax.id == 'y'): |
---|
124 | listeaxes.append(ax_lat) |
---|
125 | elif (ax.id == 'z' or ax.id == 'lev' or ax.id == 'level'): |
---|
126 | listeaxes.append(ax_lev) |
---|
127 | else: |
---|
128 | listeaxes.append(ax) |
---|
129 | |
---|
130 | if (check_test): print "liste des axes : ",listeaxes |
---|
131 | |
---|
132 | wvar = cdms.createVariable(var0, |
---|
133 | axes = listeaxes) |
---|
134 | fout.write(wvar) |
---|
135 | fout.sync() |
---|
136 | print '..... fin du traitement de ',var |
---|
137 | |
---|
138 | |
---|
139 | # # div=MV.where(var0_mask<1e-15,1,var0_mask) |
---|
140 | |
---|
141 | f.close() |
---|
142 | fout.close() |
---|
143 | print '...............finished writing.' |
---|
144 | # Plus à faire ! |
---|
145 | # fonctions_orchidee.filtre_dump(file_out) |
---|
146 | print '...............finished rewriting.' |
---|
147 | |
---|