Changeset 4519
- Timestamp:
- May 30, 2007, 1:54:07 PM (18 years ago)
- Location:
- anuga_core/source/anuga/shallow_water
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/shallow_water/data_manager.py
r4500 r4519 5189 5189 def store_parameters(verbose=False,**kwargs): 5190 5190 """ 5191 Store "kwargs" into a temp csv file, if "completed" is a kwargs csv file is 5192 kwargs[file_name] else it is kwargs[output_dir] + details_temp.csv 5193 5191 5194 Must have a file_name keyword arg, this is what is writing to. 5192 5195 might be a better way to do this using CSV module Writer and writeDict … … 5194 5197 writes file to "output_dir" unless "completed" is in kwargs, then it writes to 5195 5198 "file_name" kwargs 5196 Returns a object which is a subset of the original 5197 and the data points and attributes in this new object refer to 5198 the indices provided 5199 5200 Input 5201 indices- a list of integers that represent the new object 5202 Output 5203 New geospatial data object representing points specified by 5204 the indices 5199 5205 5200 """ 5206 5201 import types … … 5211 5206 raise TypeError 5212 5207 5208 #is completed is kwargs? 5213 5209 try: 5214 5210 kwargs['completed'] … … 5220 5216 if completed: 5221 5217 try: 5222 file = str(kwargs .pop('file_name'))5218 file = str(kwargs['file_name']) 5223 5219 except: 5224 5220 raise 'kwargs must have file_name' 5225 5221 else: 5222 #write temp file in output directory 5226 5223 try: 5227 file = str(kwargs .pop('output_dir'))+'detail_temp.csv'5224 file = str(kwargs['output_dir'])+'detail_temp.csv' 5228 5225 except: 5229 5226 raise 'kwargs must have output_dir' … … 5236 5233 keys.sort() 5237 5234 5238 # for k in kwargs.keys():5239 5235 #used the sorted keys to create the header and line data 5240 5236 for k in keys: 5241 print "%s = %s" %(k, kwargs[k])5237 # print "%s = %s" %(k, kwargs[k]) 5242 5238 header = header+str(k) 5243 5239 line = line+str(kwargs[k]) … … 5246 5242 header = header+',' 5247 5243 line = line+',' 5248 5244 header+='\n' 5245 line+='\n' 5249 5246 5250 5247 # checks the header info, if the same, then write, if not create a new file 5251 5248 #try to open! 5252 # print'file name',file5253 5249 try: 5254 5250 fid = open(file,"r") … … 5263 5259 try: 5264 5260 fid = open(file,"w") 5265 fid.write lines(header+'\n')5261 fid.write(header) 5266 5262 fid.close() 5267 5263 file_header=header … … 5271 5267 5272 5268 #if header is same or this is a new file 5273 if file_header .strip('\n')==str(header):5269 if file_header==str(header): 5274 5270 fid=open(file,"a") 5275 5271 #write new line 5276 fid.write lines(line+'\n')5272 fid.write(line) 5277 5273 fid.close() 5278 5274 else: … … 5281 5277 file = str(kwargs['output_dir'])+'detail_temp.csv' 5282 5278 fid=open(file,"a") 5283 fid.write lines(header+'\n')5284 fid.write lines(line+'\n')5279 fid.write(header) 5280 fid.write(line) 5285 5281 fid.close() 5286 print 'file',file_header.strip('\n') 5287 print 'head',header.strip('\n') 5282 if verbose: print 'file',file_header.strip('\n') 5283 if verbose: print 'head',header.strip('\n') 5284 if file_header.strip('\n')==str(header): print 'they equal' 5288 5285 msg = 'WARNING: File header does not match input info, the input variables have changed, suggest to change file name' 5289 5286 print msg -
anuga_core/source/anuga/shallow_water/test_data_manager.py
r4500 r4519 6744 6744 assert allclose(x[:,0], [1.3, 0.0,4.5, 1.0]) 6745 6745 6746 def xxxtest_store_parameters(self):6746 def test_store_parameters(self): 6747 6747 6748 6748 from os import sep, getenv 6749 6749 6750 home = getenv('INUNDATIONHOME') 6751 output_dir=home+sep+'data' 6752 6753 kwargs = {'file_name':'new2.txt','output_dir':output_dir,'who':'me', 'what':'detail', 'how':2, 'why':241,'Completed':'yes'} 6754 # {'data_origin': data_georef.get_origin(), 6755 # 'mesh_origin': mesh_georef.get_origin(), 6756 # 'alpha': alpha, 6757 # 'verbose': verbose} 6758 6759 # fileName = tempfile.mktemp(".csv") 6760 # store_parameters(kwargs,fileName) 6761 # file_name='temp.csv' 6762 store_parameters(**kwargs) 6763 # store_parameters(file_name=file_name, scenario_name='dampier',\ 6764 # who="me", time_thining=12,tide=2.6) 6765 6766 6767 fid = open(str(kwargs.pop('file_name'))) 6768 header = fid.readline() 6769 line = fid.readline() 6770 fid.close() 6771 print'header',header,'lines',line 6750 output_dir='' 6751 file_name='details.csv' 6752 6753 kwargs = {'file_name':'new2.txt', 6754 'output_dir':output_dir, 6755 'file_name':file_name, 6756 'who':'me', 6757 'what':'detail', 6758 'how':2, 6759 'why':241, 6760 # 'completed':345 6761 } 6762 store_parameters(verbose=False,**kwargs) 6763 6764 temp='detail_temp.csv' 6765 fid = open(temp) 6766 file_header = fid.readline() 6767 file_line = fid.readline() 6768 fid.close() 6769 6770 6771 keys = kwargs.keys() 6772 keys.sort() 6773 line='' 6774 header='' 6775 count=0 6776 #used the sorted keys to create the header and line data 6777 for k in keys: 6778 # print "%s = %s" %(k, kwargs[k]) 6779 header = header+str(k) 6780 line = line+str(kwargs[k]) 6781 count+=1 6782 if count <len(kwargs): 6783 header = header+',' 6784 line = line+',' 6785 header+='\n' 6786 line+='\n' 6787 6772 6788 6773 6789 #file exists 6774 # assert access(file_name,F_OK) 6775 a=['who,time'] 6776 # assert allclose(header, a) 6777 # assert allclose(header, [who,time]) 6778 # assert header=='who,time1 6779 # assert allclose(line,['me',12]) 6790 assert access(temp,F_OK) 6791 assert header == file_header 6792 assert line == file_line 6793 6794 os.remove(temp) 6795 6796 def test_store_parameters1(self): 6797 6798 from os import sep, getenv 6799 6800 output_dir='' 6801 file_name='details.csv' 6802 6803 kwargs = {'file_name':'new2.txt', 6804 'output_dir':output_dir, 6805 'file_name':file_name, 6806 'who':'me', 6807 'what':'detail', 6808 'how':2, 6809 'why':241, 6810 # 'completed':345 6811 } 6812 store_parameters(verbose=False,**kwargs) 6813 6814 kwargs['how']=55 6815 kwargs['completed']=345 6816 6817 keys = kwargs.keys() 6818 keys.sort() 6819 line='' 6820 header='' 6821 count=0 6822 #used the sorted keys to create the header and line data 6823 for k in keys: 6824 # print "%s = %s" %(k, kwargs[k]) 6825 header = header+str(k) 6826 line = line+str(kwargs[k]) 6827 count+=1 6828 if count <len(kwargs): 6829 header = header+',' 6830 line = line+',' 6831 header+='\n' 6832 line+='\n' 6833 6834 kwargs['how']=55 6835 kwargs['completed']=345 6836 6837 store_parameters(verbose=False,**kwargs) 6838 6839 # temp='detail_temp.csv' 6840 fid = open(file_name) 6841 file_header = fid.readline() 6842 file_line1 = fid.readline() 6843 file_line2 = fid.readline() 6844 fid.close() 6845 6846 6847 #file exists 6848 # print 'header',header,'line',line 6849 # print 'file_header',file_header,'file_line1',file_line1,'file_line2',file_line2 6850 assert access(file_name,F_OK) 6851 assert header == file_header 6852 assert line == file_line1 6853 6854 temp='detail_temp.csv' 6855 os.remove(temp) 6856 os.remove(file_name) 6857 6858 def test_store_parameters2(self): 6859 6860 from os import sep, getenv 6861 6862 output_dir='' 6863 file_name='details.csv' 6864 6865 kwargs = {'file_name':'new2.txt', 6866 'output_dir':output_dir, 6867 'file_name':file_name, 6868 'who':'me', 6869 'what':'detail', 6870 'how':2, 6871 'why':241, 6872 'completed':345 6873 } 6874 store_parameters(verbose=False,**kwargs) 6875 6876 kwargs['how']=55 6877 kwargs['completed']=23.54532 6878 6879 store_parameters(verbose=False,**kwargs) 6880 6881 keys = kwargs.keys() 6882 keys.sort() 6883 line='' 6884 header='' 6885 count=0 6886 #used the sorted keys to create the header and line data 6887 for k in keys: 6888 # print "%s = %s" %(k, kwargs[k]) 6889 header = header+str(k) 6890 line = line+str(kwargs[k]) 6891 count+=1 6892 if count <len(kwargs): 6893 header = header+',' 6894 line = line+',' 6895 header+='\n' 6896 line+='\n' 6897 6898 fid = open(file_name) 6899 file_header = fid.readline() 6900 file_line1 = fid.readline() 6901 file_line2 = fid.readline() 6902 fid.close() 6903 6904 assert access(file_name,F_OK) 6905 assert header == file_header 6906 assert line == file_line2 6907 6908 os.remove(file_name) 6909 6780 6910 6781 6911 … … 6786 6916 #suite = unittest.makeSuite(Test_Data_Manager,'test_sww_header') 6787 6917 #suite = unittest.makeSuite(Test_Data_Manager,'test_export_grid_parallel') 6788 #suite = unittest.makeSuite(Test_Data_Manager,'test_ export_grid')6918 #suite = unittest.makeSuite(Test_Data_Manager,'test_store_parameters') 6789 6919 if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V': 6790 6920 Test_Data_Manager.verbose=True
Note: See TracChangeset
for help on using the changeset viewer.