1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
import math import netCDF4 from netCDF4 import Dataset import numpy as np import datetime
class StaticDriver: """ This is an example script to generate static drivers for PALM.
You can use it as a starting point for creating your setup specific driver. """
def __init__(self): """ Open the static driver as NetCDF4 file. Here, you have to give the full path to the static driver that shall be created. Existing file with same name is deleted. """ print('Opening file...') self.nc_file = Dataset("new_raster.nc", 'w', format='NETCDF4') print('Opening target file ...') self.nc_target_file = read_acs()
def write_global_attributes(self): """ Write global attributes to static driver. """ print("Writing global attributes...")
self.nc_file.origin_lon = 55.0 self.nc_file.origin_lat = 0.0 self.nc_file.origin_time = '2022-09-14 00:00:00 +00' self.nc_file.origin_x = 308124 self.nc_file.origin_y = 6098908 self.nc_file.origin_z = 0.0 self.nc_file.rotation_angle = 0.0
self.nc_file.author = '...' self.nc_file.comment = 'Miscellaneous information about the data ' \ 'or methods to produce it.' self.nc_file.creation_date = str(datetime.datetime.now()) self.nc_file.institution = 'LUH' self.nc_file.history = '' self.nc_file.palm_revision = '' self.nc_file.title = 'Static driver for E3'
def define_dimensions(self): """ Set dimensions on which variables are defined. """ print("Writing dimensions...")
self.nx = self.nc_target_file.shape[1] self.ny = self.nc_target_file.shape[0] self.nz = 250 self.dx = 90.0 self.dy = 90.0 self.dz = 4.0
self.nc_file.createDimension('x' ,self.nx+1) self.x = self.nc_file.createVariable('x', 'f8', ('x',)) self.x.units = 'm' self.x.standard_name = 'x coordinate of cell centers' self.x[:] = np.arange(0,(self.nx+1)*self.dx,self.dx)
self.nc_file.createDimension('xu', self.nx) self.xu = self.nc_file.createVariable('xu', 'f8', ('xu',)) self.xu.units = 'm' self.xu.standard_name = 'x coordinate of cell edges' self.xu[:] = np.arange(self.dx/2.,self.nx*self.dx,self.dx)
self.nc_file.createDimension('y', self.ny+1) self.y = self.nc_file.createVariable('y', 'f8', ('y',)) self.y.units = 'm' self.y.standard_name = 'y coordinate of cell centers' self.y[:] = np.arange(0,(self.ny+1)*self.dy,self.dy)
self.nc_file.createDimension('yv', self.ny) self.yv = self.nc_file.createVariable('yv', 'f8', ('yv',)) self.yv.units = 'm' self.yv.standard_name = 'y coordinate of cell edges' self.yv[:] = np.arange(self.dy/2.,self.ny*self.dy,self.dy)
z_array = np.append(0, np.arange(self.dz/2,(self.nz+1)*self.dz,self.dz)) self.nc_file.createDimension('z',self.nz+2) self.z = self.nc_file.createVariable('z', 'f8', ('z',)) self.z.units = 'm' self.z.standard_name = 'z coordinate of cell centers' self.z[:] = z_array
zw_array = np.arange(0,(self.nz+2)*self.dz,self.dz) self.nc_file.createDimension('zw', self.nz+2) self.zw = self.nc_file.createVariable('zw', 'f8', ('zw',)) self.zw.units = 'm' self.zw.standard_name = 'z coordinate of cell edges' self.zw[:] = zw_array
def add_variables(self):
print("Writing variables...") fill_val_real = -9999.9 fill_val_int8 = -127
nc_buildings_2d = self.nc_file.createVariable( 'buildings_2d', 'f4', ('y','x'), fill_value=fill_val_real) nc_buildings_2d.lod = 1 nc_buildings_2d[:,:] = fill_val_real
nc_building_id = self.nc_file.createVariable( 'building_id', 'i1', ('y','x'), fill_value=fill_val_int8) nc_building_id[:,:] = fill_val_int8
nc_building_type = self.nc_file.createVariable( 'building_type', 'i1', ('y','x'), fill_value=fill_val_int8) nc_building_type[:,:] = fill_val_int8
nc_zt = self.nc_file.createVariable( \ 'zt', 'f4', ('y','x'), fill_value=fill_val_real) nc_zt.long_name = 'orography' nc_zt.units = 'm'
nc_zt[:,:] = 0.0
nc_buildings_2d[1:,1:] = self.nc_target_file[:,:] nc_building_type[:,:] = 2
def finalize(self): """ Close file """ print("Closing file...")
self.nc_file.close()
def read_acs(input_file="new_raster.asc"): data_array = np.loadtxt(input_file, skiprows=5) data_array[data_array < 0] = 0 data_array = np.flipud(data_array)
print(data_array.shape) max_value = max(max(row) for row in data_array) min_value = min(min(row) for row in data_array) print(f"Maximum value: {max_value}") print(f"Minimum value: {min_value}") print(data_array) return data_array
if __name__ == '__main__': driver = StaticDriver() driver.write_global_attributes() driver.define_dimensions() driver.add_variables() driver.finalize()
|