Changeset 4387


Ignore:
Timestamp:
Apr 17, 2007, 5:07:33 PM (18 years ago)
Author:
duncan
Message:

small modifications to geo-reffing

Location:
anuga_core/source/anuga
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/coordinate_transforms/geo_reference.py

    r4180 r4387  
    1616TITLE = '#geo reference' + "\n" #this title is referred to in the .xya format
    1717
     18DEFAULT_PROJECTION = 'UTM'
     19DEFAULT_DATUM = 'wgs84'
     20DEFAULT_UNITS = 'm'
     21DEFAULT_FALSE_EASTING = 500000
     22DEFAULT_FALSE_NORTHING = 10000000 #Default for southern hemisphere
     23
    1824class Geo_reference:
    1925    """
     
    2430                 xllcorner = 0.0,
    2531                 yllcorner = 0.0,
    26                  datum = 'wgs84',
    27                  projection = 'UTM',                 units = 'm',
    28                  false_easting = 500000,
    29                  false_northing = 10000000, #Default for southern hemisphere
     32                 datum = DEFAULT_DATUM,
     33                 projection = DEFAULT_PROJECTION,
     34                 units = DEFAULT_UNITS,
     35                 false_easting = DEFAULT_FALSE_EASTING,
     36                 false_northing = DEFAULT_FALSE_NORTHING,
    3037                 NetCDFObject=None,
    3138                 ASCIIFile=None,
     
    3946         it can't unread it, so this info has to be passed.
    4047         If you know of a way to unread this info, then tell us.
    41         """
     48
     49         Note, the text file only saves a sub set of the info the
     50         points file does.  Currently the info not written in text
     51         must be the default info, since ANUGA assumes it isn't
     52         changing.
     53         """
    4254
    4355        self.false_easting = false_easting
     
    8294        self.zone = infile.zone[0]
    8395
     96        self.false_easting = infile.false_easting[0]
     97        self.false_northing = infile.false_northing[0]
     98       
     99        self.datum = infile.datum       
     100        self.projection = infile.projection
     101        self.units = infile.units
     102       
    84103        # Fix some assertion failures
    85104        if type(self.zone) == ArrayType and self.zone.shape == ():
     
    95114                type(self.yllcorner) == types.IntType)
    96115        assert (type(self.zone) == types.IntType)
    97 
    98         #FIXME (Ole) Read in the rest...
     116       
     117        assert (self.false_easting == DEFAULT_FALSE_EASTING)
     118        assert (self.false_northing == DEFAULT_FALSE_NORTHING)
     119
     120        assert(self.datum == DEFAULT_DATUM)
     121        assert(self.projection == DEFAULT_PROJECTION)
     122        assert (self.units == DEFAULT_UNITS)
    99123       
    100124       
     
    132156        assert (type(self.zone) == types.IntType)
    133157       
    134         #false_easting = infile.false_easting[0]
    135         #false_northing = infile.false_northing[0]
    136158       
    137159    def change_points_geo_ref(self, points,
     
    267289            cmp = 1
    268290        return cmp
    269        
     291
     292def write_NetCDF_georeference(origin, outfile):
     293    """
     294    Write georeferrence info to a netcdf file, usually sww.
     295
     296    The origin can be a georef instance or parrameters for a geo_ref instance
     297
     298    outfile is the name of the file to be written to.
     299    """
     300    if isinstance(origin, Geo_reference):
     301        geo_ref = origin
     302    else:
     303        geo_ref = apply(Geo_reference,origin)
     304    geo_ref.write_NetCDF(outfile)
     305    return geo_ref
     306   
    270307#-----------------------------------------------------------------------
    271308
  • anuga_core/source/anuga/shallow_water/data_manager.py

    r4382 r4387  
    7272from anuga.coordinate_transforms.redfearn import redfearn, \
    7373     convert_from_latlon_to_utm
    74 from anuga.coordinate_transforms.geo_reference import Geo_reference
     74from anuga.coordinate_transforms.geo_reference import Geo_reference, \
     75     write_NetCDF_georeference
    7576from anuga.geospatial_data.geospatial_data import Geospatial_data,\
    7677     ensure_absolute
     
    26452646    outfile = NetCDFFile(swwname, 'w')
    26462647
    2647     #Create new file
    2648     outfile.institution = 'Geoscience Australia'
    2649     outfile.description = 'Converted from Ferret files: %s, %s, %s, %s'\
    2650                           %(basename_in + '_ha.nc',
    2651                             basename_in + '_ua.nc',
    2652                             basename_in + '_va.nc',
    2653                             basename_in + '_e.nc')
    2654 
    2655 
    2656     #For sww compatibility
    2657     outfile.smoothing = 'Yes'
    2658     outfile.order = 1
    2659 
    2660     #Start time in seconds since the epoch (midnight 1/1/1970)
    2661     outfile.starttime = starttime = times[0]
    2662     times = times - starttime  #Store relative times
    2663 
    2664     # dimension definitions
    2665     outfile.createDimension('number_of_volumes', number_of_volumes)
    2666 
    2667     outfile.createDimension('number_of_vertices', 3)
    2668     outfile.createDimension('number_of_points', number_of_points)
    2669 
    2670 
    2671     #outfile.createDimension('number_of_timesteps', len(times))
    2672     outfile.createDimension('number_of_timesteps', len(times))
    2673 
    2674     # variable definitions
    2675     outfile.createVariable('x', precision, ('number_of_points',))
    2676     outfile.createVariable('y', precision, ('number_of_points',))
    2677     outfile.createVariable('elevation', precision, ('number_of_points',))
    2678 
    2679     #FIXME: Backwards compatibility
    2680     outfile.createVariable('z', precision, ('number_of_points',))
    2681     #################################
    2682 
    2683     outfile.createVariable('volumes', Int, ('number_of_volumes',
    2684                                             'number_of_vertices'))
    2685 
    2686     outfile.createVariable('time', precision,
    2687                            ('number_of_timesteps',))
    2688 
    2689     outfile.createVariable('stage', precision,
    2690                            ('number_of_timesteps',
    2691                             'number_of_points'))
    2692 
    2693     outfile.createVariable('xmomentum', precision,
    2694                            ('number_of_timesteps',
    2695                             'number_of_points'))
    2696 
    2697     outfile.createVariable('ymomentum', precision,
    2698                            ('number_of_timesteps',
    2699                             'number_of_points'))
    2700 
     2648    description = 'Converted from Ferret files: %s, %s, %s, %s'\
     2649                  %(basename_in + '_ha.nc',
     2650                    basename_in + '_ua.nc',
     2651                    basename_in + '_va.nc',
     2652                    basename_in + '_e.nc')
     2653    write_sww_header(outfile, times, number_of_volumes,
     2654                     number_of_points, description=description)
     2655#     #Create new file
     2656#     outfile.institution = 'Geoscience Australia'
     2657#     outfile.description = 'Converted from Ferret files: %s, %s, %s, %s'\
     2658#                           %(basename_in + '_ha.nc',
     2659#                             basename_in + '_ua.nc',
     2660#                             basename_in + '_va.nc',
     2661#                             basename_in + '_e.nc')
     2662
     2663
     2664#     #For sww compatibility
     2665#     outfile.smoothing = 'Yes'
     2666#     outfile.order = 1
     2667
     2668#     #Start time in seconds since the epoch (midnight 1/1/1970)
     2669#     outfile.starttime = starttime = times[0]
     2670#     times = times - starttime  #Store relative times
     2671
     2672#     # dimension definitions
     2673#     outfile.createDimension('number_of_volumes', number_of_volumes)
     2674
     2675#     outfile.createDimension('number_of_vertices', 3)
     2676#     outfile.createDimension('number_of_points', number_of_points)
     2677
     2678
     2679#     #outfile.createDimension('number_of_timesteps', len(times))
     2680#     outfile.createDimension('number_of_timesteps', len(times))
     2681
     2682#     # variable definitions
     2683#     outfile.createVariable('x', precision, ('number_of_points',))
     2684#     outfile.createVariable('y', precision, ('number_of_points',))
     2685#     outfile.createVariable('elevation', precision, ('number_of_points',))
     2686
     2687#     #FIXME: Backwards compatibility
     2688#     outfile.createVariable('z', precision, ('number_of_points',))
     2689#     #################################
     2690
     2691#     outfile.createVariable('volumes', Int, ('number_of_volumes',
     2692#                                             'number_of_vertices'))
     2693
     2694#     outfile.createVariable('time', precision,
     2695#                            ('number_of_timesteps',))
     2696
     2697#     outfile.createVariable('stage', precision,
     2698#                            ('number_of_timesteps',
     2699#                             'number_of_points'))
     2700
     2701#     outfile.createVariable('xmomentum', precision,
     2702#                            ('number_of_timesteps',
     2703#                             'number_of_points'))
     2704
     2705#     outfile.createVariable('ymomentum', precision,
     2706#                            ('number_of_timesteps',
     2707#                             'number_of_points'))
     2708
     2709#     outfile.variables['time'][:] = times   #Store time relative
    27012710
    27022711    #Store
     
    27412750    volumes = array(volumes)
    27422751
    2743     if origin == None:
    2744         zone = refzone
    2745         xllcorner = min(x)
    2746         yllcorner = min(y)
    2747     else:
    2748         zone = origin[0]
    2749         xllcorner = origin[1]
    2750         yllcorner = origin[2]
    2751 
    2752 
    2753     outfile.xllcorner = xllcorner
    2754     outfile.yllcorner = yllcorner
    2755     outfile.zone = zone
     2752    if origin is None:
     2753        origin = Geo_reference(refzone,min(x),min(y))
     2754    geo_ref = write_NetCDF_georeference(origin, outfile)
     2755   
     2756#     if origin == None:
     2757#         zone = refzone
     2758#         xllcorner = min(x)
     2759#         yllcorner = min(y)
     2760#     else:
     2761#         zone = origin[0]
     2762#         xllcorner = origin[1]
     2763#         yllcorner = origin[2]
     2764
     2765
     2766#     outfile.xllcorner = xllcorner
     2767#     outfile.yllcorner = yllcorner
     2768#     outfile.zone = zone
    27562769
    27572770
     
    27672780    from Numeric import resize
    27682781    z = resize(z,outfile.variables['z'][:].shape)
    2769     outfile.variables['x'][:] = x - xllcorner
    2770     outfile.variables['y'][:] = y - yllcorner
     2782    outfile.variables['x'][:] = x - geo_ref.get_xllcorner()
     2783    outfile.variables['y'][:] = y - geo_ref.get_yllcorner()
    27712784    outfile.variables['z'][:] = z             #FIXME HACK for bacwards compat.
    27722785    outfile.variables['elevation'][:] = z
    2773     outfile.variables['time'][:] = times   #Store time relative
    27742786    outfile.variables['volumes'][:] = volumes.astype(Int32) #For Opteron 64
    27752787
     
    28082820        print '  Reference:'
    28092821        print '    Lower left corner: [%f, %f]'\
    2810               %(xllcorner, yllcorner)
     2822              %(geo_ref.get_xllcorner(), geo_ref.get_yllcorner())
    28112823        print '    Start time: %f' %starttime
    28122824        print '  Extent:'
     
    45734585    return mux_times_start_i, mux_times_fin_i
    45744586
    4575 def write_sww_header(outfile, times, number_of_volumes, number_of_points ):
     4587def write_sww_header(outfile, times, number_of_volumes, number_of_points, description='Converted from XXX'):
    45764588    """
    45774589    outfile - the name of the file that will be written
     
    45794591    number_of_volumes - the number of triangles
    45804592    """
     4593    times = ensure_numeric(times)
     4594   
    45814595    number_of_times = len(times)
    45824596   
    45834597    outfile.institution = 'Geoscience Australia'
    45844598    outfile.description = 'Converted from XXX'
    4585 
    45864599
    45874600    #For sww compatibility
     
    45944607    else:
    45954608        outfile.starttime = starttime = times[0]
    4596 
     4609    times = times - starttime  #Store relative times
    45974610
    45984611    # dimension definitions
    45994612    outfile.createDimension('number_of_volumes', number_of_volumes)
    4600 
    46014613    outfile.createDimension('number_of_vertices', 3)
    46024614    outfile.createDimension('number_of_points', number_of_points)
     
    46304642                            'number_of_points'))
    46314643   
    4632     outfile.variables['time'][:] = times   
     4644    outfile.variables['time'][:] = times    #Store time relative
    46334645
    46344646
     
    46394651    volumes = array(volumes)
    46404652
    4641     if origin is not None:
    4642         if isinstance(origin, Geo_reference):
    4643             geo_ref = origin
    4644         else:
    4645             geo_ref = apply(Geo_reference,origin)
    4646     else:
    4647         geo_ref = Geo_reference(zone,min(points_utm[:,0]),min(points_utm[:,1]))
    4648     #geo_ref = Geo_reference(zone,0,0)
    4649     geo_ref.write_NetCDF(outfile)
     4653    if origin is None:
     4654        origin = Geo_reference(zone,min(points_utm[:,0]),min(points_utm[:,1]))
     4655    geo_ref = write_NetCDF_georeference(origin, outfile)
     4656   
     4657#     if origin is not None:
     4658#         if isinstance(origin, Geo_reference):
     4659#             geo_ref = origin
     4660#         else:
     4661#             geo_ref = apply(Geo_reference,origin)
     4662#     else:
     4663#         geo_ref = Geo_reference(zone,min(points_utm[:,0]),min(points_utm[:,1]))
     4664#     #geo_ref = Geo_reference(zone,0,0)
     4665#     geo_ref.write_NetCDF(outfile)
     4666   
    46504667
    46514668    # This will put the geo ref in the middle
  • anuga_core/source/anuga/shallow_water/test_data_manager.py

    r4382 r4387  
    57245724        times = fid.variables['time'][:]
    57255725       
    5726         times_actual = [200,300,400,500]
     5726        times_actual = [0,100,200,300]
    57275727       
    57285728        assert allclose(ensure_numeric(times),
     
    59745974#-------------------------------------------------------------
    59755975if __name__ == "__main__":
    5976     #suite = unittest.makeSuite(Test_Data_Manager,'test_urs2sww')
     5976    #suite = unittest.makeSuite(Test_Data_Manager,'test_urs2sww_origin')
    59775977    #suite = unittest.makeSuite(Test_Data_Manager,'cache_test_URS_points_needed_and_urs_ungridded2sww')
    59785978    #suite = unittest.makeSuite(Test_Data_Manager,'test_urs_ungridded_hole')
Note: See TracChangeset for help on using the changeset viewer.