Changeset 877
- Timestamp:
- Feb 14, 2005, 6:09:05 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/data_manager.py
r856 r877 996 996 997 997 998 999 1000 1001 1002 def ferret2sww(basefilename, verbose=False, 1003 minlat = None, maxlat =None, 1004 minlon = None, maxlon =None, 1005 mint = None, maxt = None, mean_stage = 0): 1006 """Convert 'Ferret' NetCDF format for wave propagation to 1007 sww format native to pyvolution. 1008 1009 Specify only basefilename and read files of the form 1010 basefilename_ha.nc, basefilename_ua.nc, basefilename_va.nc containing 1011 relative height, x-velocity and y-velocity, respectively. 1012 1013 Also convert latitude and longitude to UTM. All coordinates are 1014 assumed to be given in the GDA94 datum. 1015 1016 1017 min's and max's: If omitted - full extend is used. 1018 To include a value min may equal it, while max must exceed it. 1019 """ 1020 1021 import os 1022 from Scientific.IO.NetCDF import NetCDFFile 1023 from Numeric import Float, Int, searchsorted, zeros, array 1024 precision = Float 1025 1026 1027 #Get NetCDF data 1028 file_h = NetCDFFile(basefilename + '_ha.nc', 'r') #Wave amplitude (cm) 1029 file_u = NetCDFFile(basefilename + '_ua.nc', 'r') #Velocity (x) (cm/s) 1030 file_v = NetCDFFile(basefilename + '_va.nc', 'r') #Velocity (y) (cm/s) 1031 1032 swwname = basefilename + '.sww' 1033 1034 times = file_h.variables['TIME'] 1035 latitudes = file_h.variables['LAT'] 1036 longitudes = file_h.variables['LON'] 1037 1038 if mint == None: 1039 jmin = 0 1040 else: 1041 jmin = searchsorted(times, mint) 1042 1043 if maxt == None: 1044 jmax=len(times) 1045 else: 1046 jmax = searchsorted(times, maxt) 1047 1048 if minlat == None: 1049 kmin=0 1050 else: 1051 kmin = searchsorted(latitudes, minlat) 1052 1053 if maxlat == None: 1054 kmax = len(latitudes) 1055 else: 1056 kmax = searchsorted(latitudes, maxlat) 1057 1058 if minlon == None: 1059 lmin=0 1060 else: 1061 lmin = searchsorted(longitudes, minlon) 1062 1063 if maxlon == None: 1064 lmax = len(longitudes) 1065 else: 1066 lmax = searchsorted(longitudes, maxlon) 1067 1068 latitudes = latitudes[kmin:kmax] 1069 longitudes = longitudes[lmin:lmax] 1070 times = times[jmin:jmax] 1071 1072 amplitudes = file_h.variables['HA'][jmin:jmax, kmin:kmax, lmin:lmax] 1073 xspeed = file_u.variables['UA'][jmin:jmax, kmin:kmax, lmin:lmax] 1074 yspeed = file_v.variables['VA'][jmin:jmax, kmin:kmax, lmin:lmax] 1075 1076 number_of_latitudes = latitudes.shape[0] 1077 number_of_longitudes = longitudes.shape[0] 1078 1079 1080 #print times 1081 #print latitudes 1082 #print longitudes 1083 1084 #print 'MIN', min(min(min(amplitudes))) 1085 #print 'MAX', max(max(max(amplitudes))) 1086 1087 #print number_of_latitudes, number_of_longitudes 1088 number_of_points = number_of_latitudes*number_of_longitudes 1089 number_of_volumes = (number_of_latitudes-1)*(number_of_longitudes-1)*2 1090 1091 #print file_h.dimensions.keys() 1092 #print file_h.variables.keys() 1093 1094 if verbose: print 'Store to SWW file %s' %swwname 1095 # NetCDF file definition 1096 outfile = NetCDFFile(swwname, 'w') 1097 1098 #Create new file 1099 outfile.institution = 'Geoscience Australia' 1100 outfile.description = 'Converted from Ferret files: %s, %s, %s'\ 1101 %(basefilename + '_ha.nc', 1102 basefilename + '_ua.nc', 1103 basefilename + '_va.nc') 1104 1105 1106 #For sww compatibility 1107 outfile.smoothing = 'Yes' 1108 outfile.order = 1 1109 1110 #Start time in seconds since the epoch (midnight 1/1/1970) 1111 outfile.starttime = times[0] 1112 1113 # dimension definitions 1114 outfile.createDimension('number_of_volumes', number_of_volumes) 1115 1116 outfile.createDimension('number_of_vertices', 3) 1117 outfile.createDimension('number_of_points', number_of_points) 1118 1119 1120 #outfile.createDimension('number_of_timesteps', len(times)) 1121 outfile.createDimension('number_of_timesteps', len(times)) 1122 1123 # variable definitions 1124 outfile.createVariable('x', precision, ('number_of_points',)) 1125 outfile.createVariable('y', precision, ('number_of_points',)) 1126 outfile.createVariable('elevation', precision, ('number_of_points',)) 1127 1128 #FIXME: Backwards compatibility 1129 outfile.createVariable('z', precision, ('number_of_points',)) 1130 ################################# 1131 1132 outfile.createVariable('volumes', Int, ('number_of_volumes', 1133 'number_of_vertices')) 1134 1135 outfile.createVariable('time', precision, 1136 ('number_of_timesteps',)) 1137 1138 outfile.createVariable('stage', precision, 1139 ('number_of_timesteps', 1140 'number_of_points')) 1141 1142 outfile.createVariable('xmomentum', precision, 1143 ('number_of_timesteps', 1144 'number_of_points')) 1145 1146 outfile.createVariable('ymomentum', precision, 1147 ('number_of_timesteps', 1148 'number_of_points')) 1149 1150 1151 #Store 1152 from coordinate_transforms.redfearn import redfearn 1153 x = zeros(number_of_points, Float) #Easting 1154 y = zeros(number_of_points, Float) #Northing 1155 #volumes = zeros(number_of_volumes, Int) 1156 i = 0 1157 1158 #Check zone boundaries 1159 refzone, _, _ = redfearn(latitudes[0],longitudes[0]) 1160 1161 vertices = {} 1162 for l, lon in enumerate(longitudes): 1163 for k, lat in enumerate(latitudes): 1164 vertices[l,k] = i 1165 1166 zone, easting, northing = redfearn(lat,lon) 1167 1168 msg = 'Zone boundary crossed at longitude =', lon 1169 assert zone == refzone, msg 1170 #print '%7.2f %7.2f %8.2f %8.2f' %(lon, lat, easting, northing) 1171 x[i] = easting 1172 y[i] = northing 1173 i += 1 1174 1175 1176 #Construct 2 triangles per 'rectangular' element 1177 volumes = [] 1178 for l in range(number_of_longitudes-1): 1179 for k in range(number_of_latitudes-1): 1180 v1 = vertices[l,k+1] 1181 v2 = vertices[l,k] 1182 v3 = vertices[l+1,k+1] 1183 v4 = vertices[l+1,k] 1184 1185 volumes.append([v1,v2,v3]) #Upper element 1186 volumes.append([v4,v3,v2]) #Lower element 1187 1188 volumes = array(volumes) 1189 1190 origin = (min(x), min(y)) 1191 outfile.minx = origin[0] 1192 outfile.miny = origin[1] 1193 1194 #print x - origin[0] 1195 #print y - origin[1] 1196 1197 #print len(x) 1198 #print number_of_points 1199 1200 outfile.variables['x'][:] = x - origin[0] 1201 outfile.variables['y'][:] = y - origin[1] 1202 outfile.variables['z'][:] = 0.0 1203 outfile.variables['elevation'][:] = 0.0 1204 outfile.variables['volumes'][:] = volumes 1205 outfile.variables['time'][:] = times 1206 1207 #Time stepping 1208 stage = outfile.variables['stage'] 1209 xmomentum = outfile.variables['xmomentum'] 1210 ymomentum = outfile.variables['ymomentum'] 1211 1212 for j in range(len(times)): 1213 i = 0 1214 for l in range(number_of_longitudes): 1215 for k in range(number_of_latitudes): 1216 h = amplitudes[j,k,l]/100 + mean_stage 1217 stage[j,i] = h 1218 xmomentum[j,i] = xspeed[j,k,l]/100*h 1219 ymomentum[j,i] = yspeed[j,k,l]/100*h 1220 i += 1 1221 1222 outfile.close() 1223 1224 1225 1226 1227 998 1228 #OBSOLETE STUFF 999 1229 #Native checkpoint format.
Note: See TracChangeset
for help on using the changeset viewer.