Changeset 2344
- Timestamp:
- Feb 7, 2006, 1:20:39 PM (19 years ago)
- Location:
- inundation/pyvolution
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pyvolution/data_manager.py
r2305 r2344 2834 2834 2835 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 ###############################################2845 #OBSOLETE STUFF2846 #Native checkpoint format.2847 #Information needed to recreate a state is preserved2848 #FIXME: Rethink and maybe use netcdf format2849 def cpt_variable_writer(filename, t, v0, v1, v2):2850 """Store all conserved quantities to file2851 """2852 2853 M, N = v0.shape2854 2855 FN = create_filename(filename, 'cpt', M, t)2856 #print 'Writing to %s' %FN2857 2858 fid = open(FN, 'w')2859 for i in range(M):2860 for j in range(N):2861 fid.write('%.16e ' %v0[i,j])2862 for j in range(N):2863 fid.write('%.16e ' %v1[i,j])2864 for j in range(N):2865 fid.write('%.16e ' %v2[i,j])2866 2867 fid.write('\n')2868 fid.close()2869 2870 2871 def cpt_variable_reader(filename, t, v0, v1, v2):2872 """Store all conserved quantities to file2873 """2874 2875 M, N = v0.shape2876 2877 FN = create_filename(filename, 'cpt', M, t)2878 #print 'Reading from %s' %FN2879 2880 fid = open(FN)2881 2882 2883 for i in range(M):2884 values = fid.readline().split() #Get one line2885 2886 for j in range(N):2887 v0[i,j] = float(values[j])2888 v1[i,j] = float(values[3+j])2889 v2[i,j] = float(values[6+j])2890 2891 fid.close()2892 2893 def cpt_constant_writer(filename, X0, X1, X2, v0, v1, v2):2894 """Writes x,y,z,z,z coordinates of triangles constituting the bed2895 elevation.2896 FIXME: Not in use pt2897 """2898 2899 M, N = v0.shape2900 2901 2902 print X02903 import sys; sys.exit()2904 FN = create_filename(filename, 'cpt', M)2905 print 'Writing to %s' %FN2906 2907 fid = open(FN, 'w')2908 for i in range(M):2909 for j in range(2):2910 fid.write('%.16e ' %X0[i,j]) #x, y2911 for j in range(N):2912 fid.write('%.16e ' %v0[i,j]) #z,z,z,2913 2914 for j in range(2):2915 fid.write('%.16e ' %X1[i,j]) #x, y2916 for j in range(N):2917 fid.write('%.16e ' %v1[i,j])2918 2919 for j in range(2):2920 fid.write('%.16e ' %X2[i,j]) #x, y2921 for j in range(N):2922 fid.write('%.16e ' %v2[i,j])2923 2924 fid.write('\n')2925 fid.close()2926 2927 2928 2929 #Function for storing out to e.g. visualisation2930 #FIXME: Do we want this?2931 #FIXME: Not done yet for this version2932 def dat_constant_writer(filename, X0, X1, X2, v0, v1, v2):2933 """Writes x,y,z coordinates of triangles constituting the bed elevation.2934 """2935 2936 M, N = v0.shape2937 2938 FN = create_filename(filename, 'dat', M)2939 #print 'Writing to %s' %FN2940 2941 fid = open(FN, 'w')2942 for i in range(M):2943 for j in range(2):2944 fid.write('%f ' %X0[i,j]) #x, y2945 fid.write('%f ' %v0[i,0]) #z2946 2947 for j in range(2):2948 fid.write('%f ' %X1[i,j]) #x, y2949 fid.write('%f ' %v1[i,0]) #z2950 2951 for j in range(2):2952 fid.write('%f ' %X2[i,j]) #x, y2953 fid.write('%f ' %v2[i,0]) #z2954 2955 fid.write('\n')2956 fid.close()2957 2958 2959 2960 def dat_variable_writer(filename, t, v0, v1, v2):2961 """Store water height to file2962 """2963 2964 M, N = v0.shape2965 2966 FN = create_filename(filename, 'dat', M, t)2967 #print 'Writing to %s' %FN2968 2969 fid = open(FN, 'w')2970 for i in range(M):2971 fid.write('%.4f ' %v0[i,0])2972 fid.write('%.4f ' %v1[i,0])2973 fid.write('%.4f ' %v2[i,0])2974 2975 fid.write('\n')2976 fid.close()2977 2978 2979 def read_sww(filename):2980 """Read sww Net CDF file containing Shallow Water Wave simulation2981 2982 The integer array volumes is of shape Nx3 where N is the number of2983 triangles in the mesh.2984 2985 Each entry in volumes is an index into the x,y arrays (the location).2986 2987 Quantities stage, elevation, xmomentum and ymomentum are all in arrays of dimensions2988 number_of_timesteps, number_of_points.2989 2990 The momentum is not always stored.2991 2992 """2993 from Scientific.IO.NetCDF import NetCDFFile2994 print 'Reading from ', filename2995 fid = NetCDFFile(filename, 'r') #Open existing file for read2996 #latitude, longitude2997 # Get the variables as Numeric arrays2998 x = fid.variables['x'] #x-coordinates of vertices2999 y = fid.variables['y'] #y-coordinates of vertices3000 z = fid.variables['elevation'] #Elevation3001 time = fid.variables['time'] #Timesteps3002 stage = fid.variables['stage'] #Water level3003 #xmomentum = fid.variables['xmomentum'] #Momentum in the x-direction3004 #ymomentum = fid.variables['ymomentum'] #Momentum in the y-direction3005 3006 volumes = fid.variables['volumes'] #Connectivity3007 3008 #FIXME (Ole): What is this?3009 # Why isn't anything returned?3010 # Where's the unit test?3011 3012 2836 def decimate_dem(basename_in, stencil, cellsize_new, basename_out=None, 3013 2837 verbose=False): … … 3138 2962 3139 2963 3140 3141 def sww2asc_obsolete(basename_in, basename_out = None,3142 quantity = None,3143 timestep = None,3144 reduction = None,3145 cellsize = 10,3146 verbose = False,3147 origin = None):3148 """Read SWW file and convert to Digitial Elevation model format (.asc)3149 3150 Example:3151 3152 ncols 31213153 nrows 18003154 xllcorner 7220003155 yllcorner 58930003156 cellsize 253157 NODATA_value -99993158 138.3698 137.4194 136.5062 135.5558 ..........3159 3160 Also write accompanying file with same basename_in but extension .prj3161 used to fix the UTM zone, datum, false northings and eastings.3162 3163 The prj format is assumed to be as3164 3165 Projection UTM3166 Zone 563167 Datum WGS843168 Zunits NO3169 Units METERS3170 Spheroid WGS843171 Xshift 0.00000000003172 Yshift 10000000.00000000003173 Parameters3174 3175 3176 if quantity is given, out values from quantity otherwise default to3177 elevation3178 3179 if timestep (an index) is given, output quantity at that timestep3180 3181 if reduction is given use that to reduce quantity over all timesteps.3182 3183 """3184 from Numeric import array, Float, concatenate, NewAxis, zeros,\3185 sometrue3186 from utilities.polygon import inside_polygon3187 3188 #FIXME: Should be variable3189 datum = 'WGS84'3190 false_easting = 5000003191 false_northing = 100000003192 3193 if quantity is None:3194 quantity = 'elevation'3195 3196 if reduction is None:3197 reduction = max3198 3199 if basename_out is None:3200 basename_out = basename_in + '_%s' %quantity3201 3202 swwfile = basename_in + '.sww'3203 ascfile = basename_out + '.asc'3204 prjfile = basename_out + '.prj'3205 3206 3207 if verbose: print 'Reading from %s' %swwfile3208 #Read sww file3209 from Scientific.IO.NetCDF import NetCDFFile3210 fid = NetCDFFile(swwfile)3211 3212 #Get extent and reference3213 x = fid.variables['x'][:]3214 y = fid.variables['y'][:]3215 volumes = fid.variables['volumes'][:]3216 3217 ymin = min(y); ymax = max(y)3218 xmin = min(x); xmax = max(x)3219 3220 number_of_timesteps = fid.dimensions['number_of_timesteps']3221 number_of_points = fid.dimensions['number_of_points']3222 if origin is None:3223 3224 #Get geo_reference3225 #sww files don't have to have a geo_ref3226 try:3227 geo_reference = Geo_reference(NetCDFObject=fid)3228 except AttributeError, e:3229 geo_reference = Geo_reference() #Default georef object3230 3231 xllcorner = geo_reference.get_xllcorner()3232 yllcorner = geo_reference.get_yllcorner()3233 zone = geo_reference.get_zone()3234 else:3235 zone = origin[0]3236 xllcorner = origin[1]3237 yllcorner = origin[2]3238 3239 3240 #Get quantity and reduce if applicable3241 if verbose: print 'Reading quantity %s' %quantity3242 3243 if quantity.lower() == 'depth':3244 q = fid.variables['stage'][:] - fid.variables['elevation'][:]3245 else:3246 q = fid.variables[quantity][:]3247 3248 3249 if len(q.shape) == 2:3250 if verbose: print 'Reducing quantity %s' %quantity3251 q_reduced = zeros( number_of_points, Float )3252 3253 for k in range(number_of_points):3254 q_reduced[k] = reduction( q[:,k] )3255 3256 q = q_reduced3257 3258 #Now q has dimension: number_of_points3259 3260 #Create grid and update xll/yll corner and x,y3261 if verbose: print 'Creating grid'3262 ncols = int((xmax-xmin)/cellsize)+13263 nrows = int((ymax-ymin)/cellsize)+13264 3265 newxllcorner = xmin+xllcorner3266 newyllcorner = ymin+yllcorner3267 3268 x = x+xllcorner-newxllcorner3269 y = y+yllcorner-newyllcorner3270 3271 vertex_points = concatenate ((x[:, NewAxis] ,y[:, NewAxis]), axis = 1)3272 assert len(vertex_points.shape) == 23273 3274 3275 from Numeric import zeros, Float3276 grid_points = zeros ( (ncols*nrows, 2), Float )3277 3278 3279 for i in xrange(nrows):3280 yg = i*cellsize3281 for j in xrange(ncols):3282 xg = j*cellsize3283 k = i*ncols + j3284 3285 grid_points[k,0] = xg3286 grid_points[k,1] = yg3287 3288 #Interpolate3289 from least_squares import Interpolation3290 3291 3292 #FIXME: This should be done with precrop = True, otherwise it'll3293 #take forever. With expand_search set to False, some grid points might3294 #miss out....3295 3296 interp = Interpolation(vertex_points, volumes, grid_points, alpha=0.0,3297 precrop = False, expand_search = True,3298 verbose = verbose)3299 3300 #Interpolate using quantity values3301 if verbose: print 'Interpolating'3302 grid_values = interp.interpolate(q).flat3303 3304 #Write3305 #Write prj file3306 if verbose: print 'Writing %s' %prjfile3307 prjid = open(prjfile, 'w')3308 prjid.write('Projection %s\n' %'UTM')3309 prjid.write('Zone %d\n' %zone)3310 prjid.write('Datum %s\n' %datum)3311 prjid.write('Zunits NO\n')3312 prjid.write('Units METERS\n')3313 prjid.write('Spheroid %s\n' %datum)3314 prjid.write('Xshift %d\n' %false_easting)3315 prjid.write('Yshift %d\n' %false_northing)3316 prjid.write('Parameters\n')3317 prjid.close()3318 3319 3320 3321 if verbose: print 'Writing %s' %ascfile3322 NODATA_value = -99993323 3324 ascid = open(ascfile, 'w')3325 3326 ascid.write('ncols %d\n' %ncols)3327 ascid.write('nrows %d\n' %nrows)3328 ascid.write('xllcorner %d\n' %newxllcorner)3329 ascid.write('yllcorner %d\n' %newyllcorner)3330 ascid.write('cellsize %f\n' %cellsize)3331 ascid.write('NODATA_value %d\n' %NODATA_value)3332 3333 3334 #Get bounding polygon from mesh3335 P = interp.mesh.get_boundary_polygon()3336 inside_indices = inside_polygon(grid_points, P)3337 3338 for i in range(nrows):3339 if verbose and i%((nrows+10)/10)==0:3340 print 'Doing row %d of %d' %(i, nrows)3341 3342 for j in range(ncols):3343 index = (nrows-i-1)*ncols+j3344 3345 if sometrue(inside_indices == index):3346 ascid.write('%f ' %grid_values[index])3347 else:3348 ascid.write('%d ' %NODATA_value)3349 3350 ascid.write('\n')3351 3352 #Close3353 ascid.close()3354 fid.close()3355 3356 #********************3357 #*** END OF OBSOLETE FUNCTIONS3358 #***************3359 2964 3360 2965
Note: See TracChangeset
for help on using the changeset viewer.