Index: trunk/tools/validation_sim.py
===================================================================
--- trunk/tools/validation_sim.py	(revision 337)
+++ trunk/tools/validation_sim.py	(revision 338)
@@ -2,5 +2,5 @@
 # L. Fita, LMD-Jussieu. February 2015
 ## e.g. sfcEneAvigon # validation_sim.py -d X@west_east@None,Y@south_north@None,T@Time@time -D X@XLONG@longitude,Y@XLAT@latitude,T@time@time -k single-station -l 4.878773,43.915876,12. -o /home/lluis/DATA/obs/HyMeX/IOP15/sfcEnergyBalance_Avignon/OBSnetcdf.nc -s /home/lluis/PY/wrfout_d01_2012-10-18_00:00:00.tests -v HFX@H,LH@LE,GRDFLX@G
-## e.g. AIREP # validation_sim.py -d X@west_east@lon2D,Y@south_north@lat2D,Z@bottom_top@z2D,T@Time@time -D X@XLONG@longitude,Y@XLAT@latitude,Z@WRFz@alti,T@time@time -k trajectory -o /home/lluis/DATA/obs/HyMeX/IOP15/AIREP/2012/10/AIREP_121018.nc -s /home/lluis/PY/wrfout_d01_2012-10-18_00:00:00.tests -v WRFT@t
+## e.g. AIREP # validation_sim.py -d X@west_east@lon2D,Y@south_north@lat2D,Z@bottom_top@z2D,T@Time@time -D X@XLONG@longitude,Y@XLAT@latitude,Z@WRFz@alti,T@time@time -k trajectory -o /home/lluis/DATA/obs/HyMeX/IOP15/AIREP/2012/10/AIREP_121018.nc -s /home/lluis/PY/wrfout_d01_2012-10-18_00:00:00.tests -v WRFt@t
 ## e.g. ATRCore # validation_sim.py -d X@west_east@lon2D,Y@south_north@lat2D,Z@bottom_top@z2D,T@Time@CFtime -D X@XLONG@longitude,Y@XLAT@latitude,Z@WRFz@altitude,T@time@time -k trajectory -o /home/lluis/DATA/obs/HyMeX/IOP15/ATRCore/V3/ATR_1Hz-HYMEXBDD-SOP1-v3_20121018_as120051.nc -s /home/lluis/PY/wrfout_d01_2012-10-18_00:00:00.tests -v WRFT@air_temperature@subc@273.15
 ## e.g. BAMED # validation_sim.py -d X@west_east@lon2D,Y@south_north@lat2D,Z@bottom_top@z2D,T@Time@CFtime -D X@XLONG@longitude,Y@XLAT@latitude,Z@WRFz@altitude,T@time@time -k trajectory -o /home/lluis/DATA/obs/HyMeX/IOP15/BAMED/BAMED_SOP1_B12_TOT5.nc -s /home/lluis/PY/wrfout_d01_2012-10-18_00:00:00.tests -v WRFT@tas_north
@@ -664,6 +664,4 @@
                 quit(-1)
 
-            print type(ncobj.variables)
-    
             self.dimensions = dimensions
             self.shape = shape
@@ -672,5 +670,4 @@
     def __getitem__(self,elem):
         return self.__values[elem]
-
 
 ####### ###### ##### #### ### ## #
@@ -956,5 +953,5 @@
                         stationpos[0] = stsimpos[iid]
                     iid = iid + 1
-                if stationpos[0] == 0 and stationpos[1] == 0: notfound[it] = 1
+                if stationpos[0] == 0 or stationpos[1] == 0: notfound[it] = 1
 
                 trajpos[0,it] = stationspos[0]
@@ -1105,8 +1102,8 @@
         if dims.has_key('Z'):
             for it in range(Ncoindt):
-                if notfound[it] == 0:
-                    ito = int(coindtvalues[it,1])
-                    trajpos[2,ito] = index_mat(valdimsim['Z'][it,:,trajpos[1,ito],   \
-                     trajpos[0,ito]], valdimobs['Z'][ito])
+                ito = int(coindtvalues[it,1])
+                if notfound[ito] == 0:
+                    trajpos[2,ito] = index_mat(valdimsim['Z'][coindtvalues[it,0],:,  \
+                      trajpos[1,ito],trajpos[0,ito]], valdimobs['Z'][ito])
                     slicev = dims['X'][0]+':'+str(trajpos[0,ito]) + '|' +            \
                       dims['Y'][0]+':'+str(trajpos[1,ito]) + '|' +                   \
@@ -1115,12 +1112,24 @@
                     slicevar, dimslice = slice_variable(ovsim, slicev)
                     simobsvalues.append([ slicevar, ovobs[int(ito)]])
-                    slicev = dims['X'][0] + ':' + str(trajpos[0,ito]-Ngrid) + '@' +  \
-                      str(trajpos[0,ito]+Ngrid+1) + '|' + dims['Y'][0] + ':' +       \
-                      str(trajpos[1,ito]-Ngrid) + '@' + str(trajpos[1,ito]+Ngrid+1)+ \
-                      '|' + dims['Z'][0] + ':' + str(trajpos[2,ito]-Ngrid) + '@' +   \
-                      str(trajpos[2,ito]+Ngrid+1) + '|' + dims['T'][0] + ':' +       \
-                      str(int(coindtvalues[it,0]))
+                    minx = np.max([trajpos[0,ito]-Ngrid,0])
+                    maxx = np.min([trajpos[0,ito]+Ngrid+1,ovsim.shape[3]])
+                    miny = np.max([trajpos[1,ito]-Ngrid,0])
+                    maxy = np.min([trajpos[1,ito]+Ngrid+1,ovsim.shape[2]])
+                    minz = np.max([trajpos[2,ito]-Ngrid,0])
+                    maxz = np.min([trajpos[2,ito]+Ngrid+1,ovsim.shape[1]])
+
+                    slicev = dims['X'][0] + ':' + str(minx) + '@' + str(maxx) + '|' +\
+                      dims['Y'][0] + ':' + str(miny) + '@' + str(maxy) + '|' +       \
+                      dims['Z'][0] + ':' + str(minz) + '@' + str(maxz) + '|' +       \
+                      dims['T'][0] + ':' + str(int(coindtvalues[it,0]))
                     slicevar, dimslice = slice_variable(ovsim, slicev)
-                    simobsSvalues[it,:,:,:] = slicevar
+
+                    sliceS = []
+                    sliceS.append(it)
+                    sliceS.append(slice(0,maxz-minz))
+                    sliceS.append(slice(0,maxy-miny))
+                    sliceS.append(slice(0,maxx-minx))
+                    
+                    simobsSvalues[tuple(sliceS)] = slicevar
                     if ivar == 0:
                         trjsim[0,it] = trajpos[0,ito]
@@ -1174,8 +1183,8 @@
     if dims.has_key('Z'):
         newdim = onewnc.createDimension('zaround',Ngrid*2+1)
-        newvar = onewnc.createVariable( valvars[ivar][0] + 'around', 'f',            \
+        newvar = onewnc.createVariable(valvars[ivar][0] + 'around', 'f',            \
           ('time','zaround','yaround','xaround'), fill_value=fillValueF)
     else:
-        newvar = onewnc.createVariable( valvars[ivar][0] + 'around', 'f',            \
+        newvar = onewnc.createVariable(valvars[ivar][0] + 'around', 'f',            \
           ('time','yaround','xaround'), fill_value=fillValueF)
 
@@ -1185,6 +1194,6 @@
 
 # Statistics
-    newvar = onewnc.createVariable(varsimobs + 'staround', 'f', ('time','stats'),   \
-      fill_value=fillValueF)
+    newvar = onewnc.createVariable(valvars[ivar][0] + 'staround', 'f',               \
+      ('time','stats'), fill_value=fillValueF)
     descvar = 'around simulated statisitcs: ' + valvars[ivar][0]
     basicvardef(newvar, varsimobs + 'staround', descvar, ovobs.getncattr('units'))
