Changeset 2718
- Timestamp:
- Apr 17, 2006, 10:47:48 PM (17 years ago)
- Location:
- inundation/pyvolution
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pyvolution/data_manager.py
r2648 r2718 48 48 49 49 """ 50 51 import exceptions 52 class DataMissingValuesError(exceptions.Exception): pass 53 class DataFileNotOpenError(exceptions.Exception): pass 54 class DataTimeError(exceptions.Exception): pass 55 class DataDomainError(exceptions.Exception): pass 56 57 58 50 59 import os 51 60 … … 156 165 157 166 if size is not None: 158 167 FN += '_size%d' %size 159 168 160 169 if time is not None: … … 199 208 200 209 self.filename = create_filename(domain.get_datadir(), 201 210 domain.get_name(), extension) 202 211 203 212 #print 'F', self.filename … … 259 268 if hasattr(domain, 'texture'): 260 269 fid.texture = domain.texture 261 270 #else: 262 271 # fid.texture = 'None' 263 272 … … 337 346 Q = domain.quantities['elevation'] 338 347 X,Y,Z,V = Q.get_vertex_values(xy=True, 339 348 precision = self.precision) 340 349 341 350 … … 387 396 if not file_open: 388 397 msg = 'File %s could not be opened for append' %self.filename 389 raise msg398 raise DataFileNotOpenError, msg 390 399 391 400 … … 473 482 474 483 #As in.... 475 476 484 #eval( name + '[i,:] = A.astype(self.precision)' ) 485 #FIXME: But we need a UNIT test for that before refactoring 477 486 478 487 … … 566 575 Q = domain.quantities['elevation'] 567 576 X,Y,Z,V = Q.get_vertex_values(xy=True, 568 577 precision = self.precision) 569 578 570 579 … … 596 605 #In that case, wait a while and try again 597 606 msg = 'Warning (store_timestep): File %s could not be opened'\ 598 607 %self.filename 599 608 msg += ' - trying again' 600 609 print msg … … 604 613 file_open = True 605 614 606 607 608 raise msg615 if not file_open: 616 msg = 'File %s could not be opened for append' %self.filename 617 raise DataFileNotOPenError, msg 609 618 610 619 … … 622 631 Q = domain.quantities[name] 623 632 A,V = Q.get_vertex_values(xy=False, 624 633 precision = self.precision) 625 634 626 635 stage[i,:] = A.astype(self.precision) … … 866 875 t = float(filename[i0:i1]) 867 876 else: 868 raise 'Hmmmm'877 raise DataTimeError, 'Hmmmm' 869 878 870 879 … … 1524 1533 origin = None, 1525 1534 datum = 'WGS84', 1526 1535 format = 'ers'): 1527 1536 1528 1537 """Read SWW file and convert to Digitial Elevation model format (.asc or .ers) … … 1735 1744 for i in xrange(nrows): 1736 1745 if format.lower() == 'asc': 1737 1738 1739 1746 yg = i*cellsize 1747 else: 1748 #this will flip the order of the y values for ers 1740 1749 yg = (nrows-i)*cellsize 1741 1750 … … 1782 1791 1783 1792 if format.lower() == 'ers': 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1793 # setup ERS header information 1794 grid_values = reshape(grid_values,(nrows, ncols)) 1795 header = {} 1796 header['datum'] = '"' + datum + '"' 1797 # FIXME The use of hardwired UTM and zone number needs to be made optional 1798 # FIXME Also need an automatic test for coordinate type (i.e. EN or LL) 1799 header['projection'] = '"UTM-' + str(zone) + '"' 1800 header['coordinatetype'] = 'EN' 1801 if header['coordinatetype'] == 'LL': 1802 header['longitude'] = str(newxllcorner) 1803 header['latitude'] = str(newyllcorner) 1804 elif header['coordinatetype'] == 'EN': 1805 header['eastings'] = str(newxllcorner) 1806 header['northings'] = str(newyllcorner) 1807 header['nullcellvalue'] = str(NODATA_value) 1808 header['xdimension'] = str(cellsize) 1809 header['ydimension'] = str(cellsize) 1810 header['value'] = '"' + quantity + '"' 1811 #header['celltype'] = 'IEEE8ByteReal' #FIXME: Breaks unit test 1812 1813 1814 #Write 1815 if verbose: print 'Writing %s' %demfile 1816 import ermapper_grids 1817 ermapper_grids.write_ermapper_grid(demfile, grid_values, header) 1818 1819 fid.close() 1811 1820 else: 1812 1821 #Write to Ascii format … … 1815 1824 prjfile = basename_out + '.prj' 1816 1825 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1826 if verbose: print 'Writing %s' %prjfile 1827 prjid = open(prjfile, 'w') 1828 prjid.write('Projection %s\n' %'UTM') 1829 prjid.write('Zone %d\n' %zone) 1830 prjid.write('Datum %s\n' %datum) 1831 prjid.write('Zunits NO\n') 1832 prjid.write('Units METERS\n') 1833 prjid.write('Spheroid %s\n' %datum) 1834 prjid.write('Xshift %d\n' %false_easting) 1835 prjid.write('Yshift %d\n' %false_northing) 1836 prjid.write('Parameters\n') 1837 prjid.close() 1838 1839 1840 1841 if verbose: print 'Writing %s' %demfile 1842 1843 ascid = open(demfile, 'w') 1844 1845 ascid.write('ncols %d\n' %ncols) 1846 ascid.write('nrows %d\n' %nrows) 1847 ascid.write('xllcorner %d\n' %newxllcorner) 1848 ascid.write('yllcorner %d\n' %newyllcorner) 1849 ascid.write('cellsize %f\n' %cellsize) 1850 ascid.write('NODATA_value %d\n' %NODATA_value) 1851 1852 1853 #Get bounding polygon from mesh 1854 #P = interp.mesh.get_boundary_polygon() 1855 #inside_indices = inside_polygon(grid_points, P) 1856 1857 for i in range(nrows): 1858 if verbose and i%((nrows+10)/10)==0: 1859 print 'Doing row %d of %d' %(i, nrows) 1851 1860 1852 1861 base_index = (nrows-i-1)*ncols … … 1858 1867 1859 1868 #print 1860 1861 1869 #for j in range(ncols): 1870 # index = base_index+j# 1862 1871 # print grid_values[index], 1863 1872 # ascid.write('%f ' %grid_values[index]) 1864 1865 1866 1867 1868 1873 #ascid.write('\n') 1874 1875 #Close 1876 ascid.close() 1877 fid.close() 1869 1878 1870 1879 #Backwards compatibility … … 1885 1894 verbose = verbose, 1886 1895 origin = origin, 1887 1888 1896 datum = 'WGS84', 1897 format = 'asc') 1889 1898 1890 1899 def sww2ers(basename_in, basename_out = None, … … 1905 1914 verbose = verbose, 1906 1915 origin = origin, 1907 1908 1916 datum = datum, 1917 format = 'ers') 1909 1918 ################################# END COMPATIBILITY ############## 1910 1919 … … 2310 2319 msg = 'NetCDFFile %s contains missing values'\ 2311 2320 %(basename_in+'_ha.nc') 2312 raise msg2321 raise DataMissingValuesError, msg 2313 2322 else: 2314 2323 amplitudes = amplitudes*(missing==0) + missing*NaN_filler … … 2319 2328 msg = 'NetCDFFile %s contains missing values'\ 2320 2329 %(basename_in+'_ua.nc') 2321 raise msg2330 raise DataMissingValuesError, msg 2322 2331 else: 2323 2332 uspeed = uspeed*(missing==0) + missing*NaN_filler … … 2328 2337 msg = 'NetCDFFile %s contains missing values'\ 2329 2338 %(basename_in+'_va.nc') 2330 raise msg2339 raise DataMissingValuesError, msg 2331 2340 else: 2332 2341 vspeed = vspeed*(missing==0) + missing*NaN_filler … … 2338 2347 msg = 'NetCDFFile %s contains missing values'\ 2339 2348 %(basename_in+'_e.nc') 2340 raise msg2349 raise DataMissingValuesError, msg 2341 2350 else: 2342 2351 elevations = elevations*(missing==0) + missing*NaN_filler … … 2516 2525 else: 2517 2526 z = elevations 2518 2527 #FIXME: z should be obtained from MOST and passed in here 2519 2528 2520 2529 from Numeric import resize … … 2623 2632 msg += ' date-time with format %s.\n' %time_format 2624 2633 msg += 'I got %s instead.' %fields[0] 2625 raise msg2634 raise DataTimeError, msg 2626 2635 2627 2636 … … 2828 2837 fid.close() 2829 2838 msg = 'Domain could not be created: %s. Perhaps use "fail_if_NaN=False and NaN_filler = ..."' %e 2830 raise msg2839 raise DataDomainError, msg 2831 2840 2832 2841 if not boundary is None: … … 2855 2864 if fail_if_NaN: 2856 2865 msg = 'quantity "%s" contains no_data entry'%quantity 2857 raise msg2866 raise DataMissingValuesError, msg 2858 2867 else: 2859 2868 data = (X<>NaN) … … 2880 2889 if fail_if_NaN: 2881 2890 msg = 'quantity "%s" contains no_data entry'%quantity 2882 raise msg2891 raise DataMissingValuesError, msg 2883 2892 else: 2884 2893 data = (X<>NaN) … … 2917 2926 %('FIXMEfilename', T[0], T[-1]) 2918 2927 msg += ' does not match model time: %s' %tau 2919 if tau < time[0]: raise msg2920 if tau > time[-1]: raise msg2928 if tau < time[0]: raise DataTimeError, msg 2929 if tau > time[-1]: raise DataTimeError, msg 2921 2930 while tau > time[index]: index += 1 2922 2931 while tau < time[index]: index -= 1 … … 3443 3452 msg = 'File %s contains missing values'\ 3444 3453 %(elevation_files[j]) 3445 raise msg3454 raise DataMissingValuesError, msg 3446 3455 else: 3447 3456 elevation_grid = elevation_grid*(missing==0) + \ -
inundation/pyvolution/test_all.py
r2516 r2718 96 96 97 97 #print regressionTest() 98 unittest.main(defaultTest='regressionTest') 98 #unittest.main(defaultTest='regressionTest') 99 100 suite = regressionTest() 101 runner = unittest.TextTestRunner(verbosity=2) 102 runner.run(suite)
Note: See TracChangeset
for help on using the changeset viewer.