1 | #!/bin/env cdat |
---|
2 | # -*- coding: iso-8859-15 -*- |
---|
3 | |
---|
4 | # python system |
---|
5 | import os, sys, gc, getopt, time |
---|
6 | #import string |
---|
7 | import re |
---|
8 | import Numeric |
---|
9 | |
---|
10 | |
---|
11 | # CDAT/CDMS |
---|
12 | import cdms, MV |
---|
13 | #, cdutil |
---|
14 | from cdms.axis import TransientAxis |
---|
15 | from cdms.coord import TransientVirtualAxis |
---|
16 | from dataproc.utils import cdat |
---|
17 | |
---|
18 | def usage(): |
---|
19 | """Display usage information.""" |
---|
20 | |
---|
21 | man = """ |
---|
22 | This script with corrected the mask of a restart. |
---|
23 | It will get file_to_modify datas and the mask of file_to_add file |
---|
24 | and make a new file_out. The missing datas in file_to_modify will be simply |
---|
25 | replaced by the same points in file_to_add file. |
---|
26 | |
---|
27 | Usage : |
---|
28 | prompt> cdat ajoute_bords.py file_to_add file_to_modify file_out [variable] |
---|
29 | Add some points defines in file_to_add for all variables (or just one) of file_to_modify to file_out. |
---|
30 | |
---|
31 | examples : |
---|
32 | > ajoute_bords.py CM1414A_20001231_sechiba_rest.nc regrid_CF_CM5PIRC11_29901231_sechiba_rest.nc grille_regrid_CF_CM5PIRC11_29901231_sechiba_rest.nc |
---|
33 | > ajoute_bords.py CM1414A_20001231_stomate_rest.nc regrid_CF_CM5PIRC11_29901231_stomate_rest.nc grille_regrid_CF_CM5PIRC11_29901231_stomate_rest.nc |
---|
34 | > ajoute_bords.py CM5PIRC8_26601231_stomate_rest.nc regrid_CF_OOL-CTRLPI-1_9_5_SPIN_v2ORC_43_01001231_stomate_rest.nc grille_regrid_CF_OOL-CTRLPI-1_9_5_SPIN_v2ORC_43_01001231_stomate_rest.nc |
---|
35 | |
---|
36 | """ |
---|
37 | print man |
---|
38 | |
---|
39 | |
---|
40 | options = [ |
---|
41 | "help", |
---|
42 | "version" ] |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | # Extract arguments from the command line |
---|
47 | try: |
---|
48 | myopts, myargs = getopt.getopt(sys.argv[1:], 'Vh', longopts = options) |
---|
49 | except getopt.GetoptError: |
---|
50 | print 'getopt error' |
---|
51 | usage() |
---|
52 | sys.exit(1) |
---|
53 | |
---|
54 | if (len(myargs) < 3): |
---|
55 | print 'I need at least three files.' |
---|
56 | usage() |
---|
57 | sys.exit(1) |
---|
58 | else: |
---|
59 | FileIn = myargs[0] |
---|
60 | print "file_to_add : ",FileIn |
---|
61 | if (os.path.exists(FileIn)==False): |
---|
62 | print 'file ',FileIn,' doesn''t exist !' |
---|
63 | sys.exit(1) |
---|
64 | else: |
---|
65 | fin=cdms.open(FileIn, 'r') |
---|
66 | |
---|
67 | FileOut = myargs[1] |
---|
68 | print "file_to_modify : ",FileOut |
---|
69 | if (os.path.exists(FileOut)==False): |
---|
70 | print 'file ',FileOut,' doesn''t exist !' |
---|
71 | sys.exit(1) |
---|
72 | else: |
---|
73 | fout=cdms.open(FileOut, 'r') |
---|
74 | |
---|
75 | FileNew = myargs[2] |
---|
76 | print "file_out : ",FileNew |
---|
77 | fnew=cdms.open(FileNew, 'w') |
---|
78 | |
---|
79 | if (len(myargs) > 3): |
---|
80 | varlist = [myargs[3]] |
---|
81 | print 'Only one variable :',varlist |
---|
82 | else: |
---|
83 | varlist = fin.listvariables() |
---|
84 | #varlist = [varlist[0]] |
---|
85 | print "Liste des variables de "+FileIn,varlist |
---|
86 | |
---|
87 | # Affichage des variables : fixe la limite max |
---|
88 | MV.set_print_limit(1700) |
---|
89 | |
---|
90 | var0=fin(varlist[0]) |
---|
91 | missing = var0.missing_value |
---|
92 | missing_ = var0.missing_value - 1. |
---|
93 | print missing, missing_ |
---|
94 | |
---|
95 | for var in varlist: |
---|
96 | print "-----" |
---|
97 | print "Variable : ",var,"------" |
---|
98 | |
---|
99 | if (var in ["nav_lon","nav_lat","nav_lev","time_steps","time", |
---|
100 | "lat","lon","lev","day_counter", |
---|
101 | "dt_days", "routingcounter", "veget_year", "date" ]): |
---|
102 | var1=fin(var) |
---|
103 | fnew.write(var1,attributes=cdat.getAttributes(var1), axes=var1.getAxisList(), id=var1.id) |
---|
104 | print "remplace." |
---|
105 | continue |
---|
106 | if (var in ["bounds_lat","bounds_lon","routingcounter","routingarea","routetogrid","routetobasin", |
---|
107 | "basinid","topoindex","fastres","slowres","streamres","lakeres","previous_outflow", |
---|
108 | "lakeinflow","returnflow","riverflow","coastalflow","hydrographs","runoff_route", |
---|
109 | "drainage_route","evapot_route","precip_route","humrel_route","totnobio_route","vegtot_route"]): |
---|
110 | print "supprime." |
---|
111 | continue |
---|
112 | |
---|
113 | var0 = fin(var) |
---|
114 | var0m = MV.getmaskarray(var0) |
---|
115 | |
---|
116 | #var0_notmissing=(var0 < missing_) |
---|
117 | var0_notmissing=(var0m == 0) |
---|
118 | |
---|
119 | var1 = fout(var) |
---|
120 | |
---|
121 | var1m = MV.getmaskarray(var1) |
---|
122 | #var1_missing=(var1 > missing_) |
---|
123 | var1_missing=(var1m == 1) |
---|
124 | # var1or = MV.logical_or(var1,var1m) |
---|
125 | |
---|
126 | #test = MV.logical_and(var0_notmissing,var1or) |
---|
127 | test = MV.logical_and(var0_notmissing,var1_missing) |
---|
128 | # if ( var in ["temp_sol"] ): |
---|
129 | # fnew.write(var0m,attributes=cdat.getAttributes(var1), axes=var1.getAxisList(), id="var0m") |
---|
130 | # fnew.write(var0_notmissing,attributes=cdat.getAttributes(var1), axes=var1.getAxisList(), id="var0_notmissing") |
---|
131 | # fnew.write(var1_missing,attributes=cdat.getAttributes(var1), axes=var1.getAxisList(), id="var1_missing") |
---|
132 | # fnew.write(var1m,attributes=cdat.getAttributes(var1), axes=var1.getAxisList(), id="var1m") |
---|
133 | # fnew.write(var1or,attributes=cdat.getAttributes(var1), axes=var1.getAxisList(), id="var1or") |
---|
134 | # fnew.write(test,attributes=cdat.getAttributes(var1), axes=var1.getAxisList(), id="test") |
---|
135 | |
---|
136 | var1b = MV.where( test , var0, var1) |
---|
137 | |
---|
138 | # var1w = cdms.createVariable(var1b, |
---|
139 | # fill_value = missing, |
---|
140 | # attributes = cdat.getAttributes(var1), |
---|
141 | # id = var, |
---|
142 | # axes = var1.getAxisList()) |
---|
143 | # print var1w |
---|
144 | |
---|
145 | # var1w = cdms.createVariable(var1b, |
---|
146 | # attributes = cdat.getAttributes(var1), |
---|
147 | # id = var, |
---|
148 | # axes = [var1.getAxis(0), var1.getAxis(1), var1.getAxis(2)]) |
---|
149 | # print var1w,cdat.getAttributes(var1w), |
---|
150 | |
---|
151 | fnew.write(var1b,attributes=cdat.getAttributes(var1), axes=var1.getAxisList(), id=var1.id) |
---|
152 | |
---|
153 | fnew.close() |
---|