Changeset 2344


Ignore:
Timestamp:
Feb 7, 2006, 1:20:39 PM (19 years ago)
Author:
ole
Message:

Moved obsolete stuff out from data_manager

Location:
inundation/pyvolution
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/data_manager.py

    r2305 r2344  
    28342834
    28352835
    2836 
    2837 
    2838 
    2839 
    2840 
    2841 
    2842 
    2843 
    2844 ###############################################
    2845 #OBSOLETE STUFF
    2846 #Native checkpoint format.
    2847 #Information needed to recreate a state is preserved
    2848 #FIXME: Rethink and maybe use netcdf format
    2849 def cpt_variable_writer(filename, t, v0, v1, v2):
    2850     """Store all conserved quantities to file
    2851     """
    2852 
    2853     M, N = v0.shape
    2854 
    2855     FN = create_filename(filename, 'cpt', M, t)
    2856     #print 'Writing to %s' %FN
    2857 
    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 file
    2873     """
    2874 
    2875     M, N = v0.shape
    2876 
    2877     FN = create_filename(filename, 'cpt', M, t)
    2878     #print 'Reading from %s' %FN
    2879 
    2880     fid = open(FN)
    2881 
    2882 
    2883     for i in range(M):
    2884         values = fid.readline().split() #Get one line
    2885 
    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 bed
    2895     elevation.
    2896     FIXME: Not in use pt
    2897     """
    2898 
    2899     M, N = v0.shape
    2900 
    2901 
    2902     print X0
    2903     import sys; sys.exit()
    2904     FN = create_filename(filename, 'cpt', M)
    2905     print 'Writing to %s' %FN
    2906 
    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, y
    2911         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, y
    2916         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, y
    2921         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. visualisation
    2930 #FIXME: Do we want this?
    2931 #FIXME: Not done yet for this version
    2932 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.shape
    2937 
    2938     FN = create_filename(filename, 'dat', M)
    2939     #print 'Writing to %s' %FN
    2940 
    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, y
    2945         fid.write('%f ' %v0[i,0])       #z
    2946 
    2947         for j in range(2):
    2948             fid.write('%f ' %X1[i,j])   #x, y
    2949         fid.write('%f ' %v1[i,0])       #z
    2950 
    2951         for j in range(2):
    2952             fid.write('%f ' %X2[i,j])   #x, y
    2953         fid.write('%f ' %v2[i,0])       #z
    2954 
    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 file
    2962     """
    2963 
    2964     M, N = v0.shape
    2965 
    2966     FN = create_filename(filename, 'dat', M, t)
    2967     #print 'Writing to %s' %FN
    2968 
    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 simulation
    2981 
    2982     The integer array volumes is of shape Nx3 where N is the number of
    2983     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 dimensions
    2988     number_of_timesteps, number_of_points.
    2989 
    2990     The momentum is not always stored.
    2991 
    2992     """
    2993     from Scientific.IO.NetCDF import NetCDFFile
    2994     print 'Reading from ', filename
    2995     fid = NetCDFFile(filename, 'r')    #Open existing file for read
    2996 #latitude, longitude
    2997     # Get the variables as Numeric arrays
    2998     x = fid.variables['x']             #x-coordinates of vertices
    2999     y = fid.variables['y']             #y-coordinates of vertices
    3000     z = fid.variables['elevation']     #Elevation
    3001     time = fid.variables['time']       #Timesteps
    3002     stage = fid.variables['stage']     #Water level
    3003     #xmomentum = fid.variables['xmomentum']   #Momentum in the x-direction
    3004     #ymomentum = fid.variables['ymomentum']   #Momentum in the y-direction
    3005 
    3006     volumes = fid.variables['volumes'] #Connectivity
    3007 
    3008     #FIXME (Ole): What is this?
    3009     #             Why isn't anything returned?
    3010     #             Where's the unit test?
    3011 
    30122836def decimate_dem(basename_in, stencil, cellsize_new, basename_out=None,
    30132837                 verbose=False):
     
    31382962
    31392963
    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         3121
    3153     nrows         1800
    3154     xllcorner     722000
    3155     yllcorner     5893000
    3156     cellsize      25
    3157     NODATA_value  -9999
    3158     138.3698 137.4194 136.5062 135.5558 ..........
    3159 
    3160     Also write accompanying file with same basename_in but extension .prj
    3161     used to fix the UTM zone, datum, false northings and eastings.
    3162 
    3163     The prj format is assumed to be as
    3164 
    3165     Projection    UTM
    3166     Zone          56
    3167     Datum         WGS84
    3168     Zunits        NO
    3169     Units         METERS
    3170     Spheroid      WGS84
    3171     Xshift        0.0000000000
    3172     Yshift        10000000.0000000000
    3173     Parameters
    3174 
    3175 
    3176     if quantity is given, out values from quantity otherwise default to
    3177     elevation
    3178 
    3179     if timestep (an index) is given, output quantity at that timestep
    3180 
    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          sometrue
    3186     from utilities.polygon import inside_polygon
    3187 
    3188     #FIXME: Should be variable
    3189     datum = 'WGS84'
    3190     false_easting = 500000
    3191     false_northing = 10000000
    3192 
    3193     if quantity is None:
    3194         quantity = 'elevation'
    3195 
    3196     if reduction is None:
    3197         reduction = max
    3198 
    3199     if basename_out is None:
    3200         basename_out = basename_in + '_%s' %quantity
    3201 
    3202     swwfile = basename_in + '.sww'
    3203     ascfile = basename_out + '.asc'
    3204     prjfile = basename_out + '.prj'
    3205 
    3206 
    3207     if verbose: print 'Reading from %s' %swwfile
    3208     #Read sww file
    3209     from Scientific.IO.NetCDF import NetCDFFile
    3210     fid = NetCDFFile(swwfile)
    3211 
    3212     #Get extent and reference
    3213     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_reference
    3225         #sww files don't have to have a geo_ref
    3226         try:
    3227             geo_reference = Geo_reference(NetCDFObject=fid)
    3228         except AttributeError, e:
    3229             geo_reference = Geo_reference() #Default georef object
    3230 
    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 applicable
    3241     if verbose: print 'Reading quantity %s' %quantity
    3242 
    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' %quantity
    3251         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_reduced
    3257 
    3258     #Now q has dimension: number_of_points
    3259 
    3260     #Create grid and update xll/yll corner and x,y
    3261     if verbose: print 'Creating grid'
    3262     ncols = int((xmax-xmin)/cellsize)+1
    3263     nrows = int((ymax-ymin)/cellsize)+1
    3264 
    3265     newxllcorner = xmin+xllcorner
    3266     newyllcorner = ymin+yllcorner
    3267 
    3268     x = x+xllcorner-newxllcorner
    3269     y = y+yllcorner-newyllcorner
    3270 
    3271     vertex_points = concatenate ((x[:, NewAxis] ,y[:, NewAxis]), axis = 1)
    3272     assert len(vertex_points.shape) == 2
    3273 
    3274 
    3275     from Numeric import zeros, Float
    3276     grid_points = zeros ( (ncols*nrows, 2), Float )
    3277 
    3278 
    3279     for i in xrange(nrows):
    3280         yg = i*cellsize
    3281         for j in xrange(ncols):
    3282             xg = j*cellsize
    3283             k = i*ncols + j
    3284 
    3285             grid_points[k,0] = xg
    3286             grid_points[k,1] = yg
    3287 
    3288     #Interpolate
    3289     from least_squares import Interpolation
    3290 
    3291 
    3292     #FIXME: This should be done with precrop = True, otherwise it'll
    3293     #take forever. With expand_search  set to False, some grid points might
    3294     #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 values
    3301     if verbose: print 'Interpolating'
    3302     grid_values = interp.interpolate(q).flat
    3303 
    3304     #Write
    3305     #Write prj file
    3306     if verbose: print 'Writing %s' %prjfile
    3307     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' %ascfile
    3322     NODATA_value = -9999
    3323 
    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 mesh
    3335     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+j
    3344 
    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     #Close
    3353     ascid.close()
    3354     fid.close()
    3355 
    3356 #********************
    3357 #*** END OF OBSOLETE FUNCTIONS
    3358 #***************
    33592964
    33602965
Note: See TracChangeset for help on using the changeset viewer.