From 97988bbd61942388ce31c497bd2bd6a6c075dccd Mon Sep 17 00:00:00 2001 From: Kaitlin Alexander Date: Wed, 18 Nov 2015 14:41:05 +1100 Subject: [PATCH] Made initialisation files more modular --- calc_z.pyc | Bin 0 -> 984 bytes file_guide.txt | 27 ++- romscice_ini.py | 368 ++++++++++++++++++++------------------- romscice_ini_iceshelf.py | 56 +++--- romscice_ini_woa.py | 303 ++++++++++++++++---------------- woa_netcdf.py | 182 +++++++++---------- 6 files changed, 484 insertions(+), 452 deletions(-) create mode 100644 calc_z.pyc diff --git a/calc_z.pyc b/calc_z.pyc new file mode 100644 index 0000000000000000000000000000000000000000..bf018bf428b12cff8784f747e1b65a4c0b672e03 GIT binary patch literal 984 zcmb7B&2G~`5T13?Bu(?z#ID1ILy?fxLqiT!94ZP5=Nu?M5)z6W$HY;cIAk}4M!ToN zbMR_B0um1Z->egIX0-*3M06n@vMKl(47?NloGv}IP{I^gP=)) zu7e_Cvp+b2fTmcSMHR>syfWSZPu{|ha#OMLkfkn@XR4$SaM*zK?;Qs1nj_MCo4R zREhmCUwwQ_#${9uY?mIm8lhUQ`s^-|?-|c#c;xPT>kcRJLWsVD!A?P&%g-(Gp5wr2 zao_9U0RMkDg~0iBHRA{*~;bIzNE}|%tiB;rmk)Sn#W4U!~Vhl&L ziC3Alnk@@>;dcLd;bs=ailNhNFAu7~QMu98@VBWeA)KE}?7lE|VAEp(VJlhF1Z zBiqI&((*~;Tbq}&8p!p^rVYlKvcr6dXVXO4l!5KBSkFh`ve&~ry0JMo7JKs~00Xi@ zK8DtlNw!kG`Ngx|O&C7C==WCHT#i(hrLq@=X*AlLFYj!_-0Gl6o-#?o ziBoYVPMnt06iwlY4(z@&40v2SCb%u{UO$An@BiN 0.9 # better for floating-point than ==1 -grid_id.close() - -ini_id = Dataset(ini_file, 'a') -is_id = Dataset(is_ini_file, 'r') -num_depth = ini_id.variables['salt'].shape[1] - -for k in range(num_depth): - # For each depth level, replace the salinity values in ice shelf cavities - print 'Processing depth level ' + str(k+1) + ' of ' + str(num_depth) - ini_salt = ini_id.variables['salt'][0,k,:,:] - is_salt = is_id.variables['salt'][0,k,:,:] - ini_salt[zice_index] = is_salt[zice_index] - ini_id.variables['salt'][0,k,:,:] = ini_salt -ini_id.close() -is_id.close() +def run(ini_file, is_ini_file, grid_file): + # Read zice and make a boolean mask + grid_id = Dataset(grid_file, 'r') + mask_zice = grid_id.variables['mask_zice'][:,:] + zice_index = mask_zice > 0.9 # better for floating-point than ==1 + grid_id.close() + + ini_id = Dataset(ini_file, 'a') + is_id = Dataset(is_ini_file, 'r') + num_depth = ini_id.variables['salt'].shape[1] + + for k in range(num_depth): + # For each depth level, replace the salinity values in ice shelf cavities + print 'Processing depth level ' + str(k+1) + ' of ' + str(num_depth) + ini_salt = ini_id.variables['salt'][0,k,:,:] + is_salt = is_id.variables['salt'][0,k,:,:] + ini_salt[zice_index] = is_salt[zice_index] + ini_id.variables['salt'][0,k,:,:] = ini_salt + ini_id.close() + is_id.close() + + +# User parameters +if __name__ == "__main__": + + ini_file = '../data/caisom001_ini_1995.nc' # Initial conditions file to edit + is_ini_file = '../data/caisom001_ini_iceshelf.nc' # Ben's file + grid_file = '../apps/common/grid/caisom001_OneQuartergrd.nc' # ROMS grid file + + run(ini_file, is_ini_file, grid_file) + + diff --git a/romscice_ini_woa.py b/romscice_ini_woa.py index ae7171e..7219d0a 100644 --- a/romscice_ini_woa.py +++ b/romscice_ini_woa.py @@ -15,6 +15,139 @@ from scipy.spatial import KDTree from calc_z import * + +# Main routine +def run (grid_file, woa_file, output_file, Tcline, theta_s, theta_b, hc, N, nbdry_woa): + + # Read WOA data and grid + print 'Reading World Ocean Atlas data' + woa_id = Dataset(woa_file, 'r') + lon_woa = woa_id.variables['longitude'][:] + lat_woa = woa_id.variables['latitude'][:nbdry_woa] + depth_woa = woa_id.variables['depth'][:] + temp_woa = transpose(woa_id.variables['temp'][:,:nbdry_woa,:]) + salt_woa = transpose(woa_id.variables['salt'][:,:nbdry_woa,:]) + woa_id.close() + + # Read ROMS grid + print 'Reading ROMS grid' + grid_id = Dataset(grid_file, 'r') + lon_roms = grid_id.variables['lon_rho'][:,:] + lat_roms = grid_id.variables['lat_rho'][:,:] + h = grid_id.variables['h'][:,:] + zice = grid_id.variables['zice'][:,:] + mask_rho = grid_id.variables['mask_rho'][:,:] + mask_zice = grid_id.variables['mask_zice'][:,:] + grid_id.close() + num_lon = size(lon_roms, 1) + num_lat = size(lon_roms, 0) + # Mask h and zice with zeros + h = h*mask_rho + zice = zice*mask_zice + + # Get 3D array of ROMS z-coordinates, as well as 1D arrays of s-coordinates + # and stretching curves + z_roms_3d, sc_r, Cs_r = calc_z(h, zice, lon_roms, lat_roms, theta_s, theta_b, hc, N) + # Copy the latitude and longitude values into 3D arrays of the same shape + lon_roms_3d = tile(lon_roms, (N,1,1)) + lat_roms_3d = tile(lat_roms, (N,1,1)) + + # Regridding happens here + print 'Interpolating temperature' + temp = interp_woa2roms(temp_woa, lon_woa, lat_woa, depth_woa, lon_roms_3d, lat_roms_3d, z_roms_3d, mask_rho, mask_zice, -0.5) + print 'Interpolating salinity' + salt = interp_woa2roms(salt_woa, lon_woa, lat_woa, depth_woa, lon_roms_3d, lat_roms_3d, z_roms_3d, mask_rho, mask_zice, 34.5) + + # Set initial velocities and sea surface height to zero + u = zeros((N, num_lat, num_lon-1)) + v = zeros((N, num_lat-1, num_lon)) + ubar = zeros((num_lat, num_lon-1)) + vbar = zeros((num_lat-1, num_lon)) + zeta = zeros((num_lat, num_lon)) + + print 'Writing to NetCDF file' + out_id = Dataset(output_file, 'w') + # Define dimensions + out_id.createDimension('xi_u', num_lon-1) + out_id.createDimension('xi_v', num_lon) + out_id.createDimension('xi_rho', num_lon) + out_id.createDimension('eta_u', num_lat) + out_id.createDimension('eta_v', num_lat-1) + out_id.createDimension('eta_rho', num_lat) + out_id.createDimension('s_rho', N) + out_id.createDimension('ocean_time', None) + out_id.createDimension('one', 1); + # Define variables and assign values + out_id.createVariable('tstart', 'f8', ('one')) + out_id.variables['tstart'].long_name = 'start processing day' + out_id.variables['tstart'].units = 'day' + out_id.variables['tstart'][:] = 0.0 + out_id.createVariable('tend', 'f8', ('one')) + out_id.variables['tend'].long_name = 'end processing day' + out_id.variables['tend'].units = 'day' + out_id.variables['tend'][:] = 0.0 + out_id.createVariable('theta_s', 'f8', ('one')) + out_id.variables['theta_s'].long_name = 'S-coordinate surface control parameter' + out_id.variables['theta_s'][:] = theta_s + out_id.createVariable('theta_b', 'f8', ('one')) + out_id.variables['theta_b'].long_name = 'S-coordinate bottom control parameter' + out_id.variables['theta_b'].units = 'nondimensional' + out_id.variables['theta_b'][:] = theta_b + out_id.createVariable('Tcline', 'f8', ('one')) + out_id.variables['Tcline'].long_name = 'S-coordinate surface/bottom layer width' + out_id.variables['Tcline'].units = 'meter' + out_id.variables['Tcline'][:] = Tcline + out_id.createVariable('hc', 'f8', ('one')) + out_id.variables['hc'].long_name = 'S-coordinate parameter, critical depth' + out_id.variables['hc'].units = 'meter' + out_id.variables['hc'][:] = hc + out_id.createVariable('Cs_r', 'f8', ('s_rho')) + out_id.variables['Cs_r'].long_name = 'S-coordinate stretching curves at RHO-points' + out_id.variables['Cs_r'].units = 'nondimensional' + out_id.variables['Cs_r'].valid_min = -1.0 + out_id.variables['Cs_r'].valid_max = 0.0 + out_id.variables['Cs_r'][:] = Cs_r + out_id.createVariable('ocean_time', 'f8', ('ocean_time')) + out_id.variables['ocean_time'].long_name = 'time since initialization' + out_id.variables['ocean_time'].units = 'seconds' + out_id.variables['ocean_time'][0] = 0.0 + out_id.createVariable('u', 'f8', ('ocean_time', 's_rho', 'eta_u', 'xi_u')) + out_id.variables['u'].long_name = 'u-momentum component' + out_id.variables['u'].units = 'meter second-1' + out_id.variables['u'][0,:,:,:] = u + out_id.createVariable('v', 'f8', ('ocean_time', 's_rho', 'eta_v', 'xi_v')) + out_id.variables['v'].long_name = 'v-momentum component' + out_id.variables['v'].units = 'meter second-1' + out_id.variables['v'][0,:,:,:] = v + out_id.createVariable('ubar', 'f8', ('ocean_time', 'eta_u', 'xi_u')) + out_id.variables['ubar'].long_name = 'vertically integrated u-momentum component' + out_id.variables['ubar'].units = 'meter second-1' + out_id.variables['ubar'][0,:,:] = ubar + out_id.createVariable('vbar', 'f8', ('ocean_time', 'eta_v', 'xi_v')) + out_id.variables['vbar'].long_name = 'vertically integrated v-momentum component' + out_id.variables['vbar'].units = 'meter second-1' + out_id.variables['vbar'][0,:,:] = vbar + out_id.createVariable('zeta', 'f8', ('ocean_time', 'eta_rho', 'xi_rho')) + out_id.variables['zeta'].long_name = 'free-surface' + out_id.variables['zeta'].units = 'meter' + out_id.variables['zeta'][0,:,:] = zeta + out_id.createVariable('temp', 'f8', ('ocean_time', 's_rho', 'eta_rho', 'xi_rho')) + out_id.variables['temp'].long_name = 'potential temperature' + out_id.variables['temp'].units = 'Celsius' + out_id.variables['temp'][0,:,:,:] = temp + out_id.createVariable('salt', 'f8', ('ocean_time', 's_rho', 'eta_rho', 'xi_rho')) + out_id.variables['salt'].long_name = 'salinity' + out_id.variables['salt'].units = 'PSU' + out_id.variables['salt'][0,:,:,:] = salt + out_id.createVariable('sc_r', 'f8', ('s_rho')) + out_id.variables['sc_r'].long_name = 'S-coordinate at rho-points' + out_id.variables['sc_r'].units = 'nondimensional' + out_id.variables['sc_r'].valid_min = -1.0 + out_id.variables['sc_r'].valid_max = 0.0 + out_id.variables['sc_r'][:] = sc_r + out_id.close() + + # Given an array on the World Ocean Atlas grid, interpolate onto the ROMS grid, # fill the ice shelf cavities with nearest-neighbour values from the same depth # level, and fill the land mask with constant values. @@ -78,150 +211,28 @@ def interp_woa2roms (A, lon_woa, lat_woa, depth_woa, lon_roms_3d, lat_roms_3d, z return B -# File paths to edit here: -# Path to ROMS grid file -grid_file = '../apps/common/grid/caisom001_OneQuartergrd.nc' -# Path to World Ocean Atlas NetCDF file (converted from FESOM input using -# woa_netcdf.py) -woa_file = '/short/y99/kaa561/FESOM/woa01_ts.nc' -# Path to desired output file -output_file = '../data/caisom001_ini.nc' - -# Grid parameters: check grid_file and *.in file to make sure these are correct -Tcline = 40 -theta_s = 0.9 -theta_b = 4 -hc = 40 -N = 31 - -# Northernmost index of WOA grid to read (1-based) -nbdry_woa = 32 - -# Read WOA data and grid -print 'Reading World Ocean Atlas data' -woa_id = Dataset(woa_file, 'r') -lon_woa = woa_id.variables['longitude'][:] -lat_woa = woa_id.variables['latitude'][:nbdry_woa] -depth_woa = woa_id.variables['depth'][:] -temp_woa = transpose(woa_id.variables['temp'][:,:nbdry_woa,:]) -salt_woa = transpose(woa_id.variables['salt'][:,:nbdry_woa,:]) -woa_id.close() - -# Read ROMS grid -print 'Reading ROMS grid' -grid_id = Dataset(grid_file, 'r') -lon_roms = grid_id.variables['lon_rho'][:,:] -lat_roms = grid_id.variables['lat_rho'][:,:] -h = grid_id.variables['h'][:,:] -zice = grid_id.variables['zice'][:,:] -mask_rho = grid_id.variables['mask_rho'][:,:] -mask_zice = grid_id.variables['mask_zice'][:,:] -grid_id.close() -num_lon = size(lon_roms, 1) -num_lat = size(lon_roms, 0) -# Mask h and zice with zeros -h = h*mask_rho -zice = zice*mask_zice - -# Get 3D array of ROMS z-coordinates, as well as 1D arrays of s-coordinates -# and stretching curves -z_roms_3d, sc_r, Cs_r = calc_z(h, zice, lon_roms, lat_roms, theta_s, theta_b, hc, N) -# Copy the latitude and longitude values into 3D arrays of the same shape -lon_roms_3d = tile(lon_roms, (N,1,1)) -lat_roms_3d = tile(lat_roms, (N,1,1)) - -# Regridding happens here -print 'Interpolating temperature' -temp = interp_woa2roms(temp_woa, lon_woa, lat_woa, depth_woa, lon_roms_3d, lat_roms_3d, z_roms_3d, mask_rho, mask_zice, -0.5) -print 'Interpolating salinity' -salt = interp_woa2roms(salt_woa, lon_woa, lat_woa, depth_woa, lon_roms_3d, lat_roms_3d, z_roms_3d, mask_rho, mask_zice, 34.5) - -# Set initial velocities and sea surface height to zero -u = zeros((N, num_lat, num_lon-1)) -v = zeros((N, num_lat-1, num_lon)) -ubar = zeros((num_lat, num_lon-1)) -vbar = zeros((num_lat-1, num_lon)) -zeta = zeros((num_lat, num_lon)) - -print 'Writing to NetCDF file' -out_id = Dataset(output_file, 'w') -# Define dimensions -out_id.createDimension('xi_u', num_lon-1) -out_id.createDimension('xi_v', num_lon) -out_id.createDimension('xi_rho', num_lon) -out_id.createDimension('eta_u', num_lat) -out_id.createDimension('eta_v', num_lat-1) -out_id.createDimension('eta_rho', num_lat) -out_id.createDimension('s_rho', N) -out_id.createDimension('ocean_time', None) -out_id.createDimension('one', 1); -# Define variables and assign values -out_id.createVariable('tstart', 'f8', ('one')) -out_id.variables['tstart'].long_name = 'start processing day' -out_id.variables['tstart'].units = 'day' -out_id.variables['tstart'][:] = 0.0 -out_id.createVariable('tend', 'f8', ('one')) -out_id.variables['tend'].long_name = 'end processing day' -out_id.variables['tend'].units = 'day' -out_id.variables['tend'][:] = 0.0 -out_id.createVariable('theta_s', 'f8', ('one')) -out_id.variables['theta_s'].long_name = 'S-coordinate surface control parameter' -out_id.variables['theta_s'][:] = theta_s -out_id.createVariable('theta_b', 'f8', ('one')) -out_id.variables['theta_b'].long_name = 'S-coordinate bottom control parameter' -out_id.variables['theta_b'].units = 'nondimensional' -out_id.variables['theta_b'][:] = theta_b -out_id.createVariable('Tcline', 'f8', ('one')) -out_id.variables['Tcline'].long_name = 'S-coordinate surface/bottom layer width' -out_id.variables['Tcline'].units = 'meter' -out_id.variables['Tcline'][:] = Tcline -out_id.createVariable('hc', 'f8', ('one')) -out_id.variables['hc'].long_name = 'S-coordinate parameter, critical depth' -out_id.variables['hc'].units = 'meter' -out_id.variables['hc'][:] = hc -out_id.createVariable('Cs_r', 'f8', ('s_rho')) -out_id.variables['Cs_r'].long_name = 'S-coordinate stretching curves at RHO-points' -out_id.variables['Cs_r'].units = 'nondimensional' -out_id.variables['Cs_r'].valid_min = -1.0 -out_id.variables['Cs_r'].valid_max = 0.0 -out_id.variables['Cs_r'][:] = Cs_r -out_id.createVariable('ocean_time', 'f8', ('ocean_time')) -out_id.variables['ocean_time'].long_name = 'time since initialization' -out_id.variables['ocean_time'].units = 'seconds' -out_id.variables['ocean_time'][0] = 0.0 -out_id.createVariable('u', 'f8', ('ocean_time', 's_rho', 'eta_u', 'xi_u')) -out_id.variables['u'].long_name = 'u-momentum component' -out_id.variables['u'].units = 'meter second-1' -out_id.variables['u'][0,:,:,:] = u -out_id.createVariable('v', 'f8', ('ocean_time', 's_rho', 'eta_v', 'xi_v')) -out_id.variables['v'].long_name = 'v-momentum component' -out_id.variables['v'].units = 'meter second-1' -out_id.variables['v'][0,:,:,:] = v -out_id.createVariable('ubar', 'f8', ('ocean_time', 'eta_u', 'xi_u')) -out_id.variables['ubar'].long_name = 'vertically integrated u-momentum component' -out_id.variables['ubar'].units = 'meter second-1' -out_id.variables['ubar'][0,:,:] = ubar -out_id.createVariable('vbar', 'f8', ('ocean_time', 'eta_v', 'xi_v')) -out_id.variables['vbar'].long_name = 'vertically integrated v-momentum component' -out_id.variables['vbar'].units = 'meter second-1' -out_id.variables['vbar'][0,:,:] = vbar -out_id.createVariable('zeta', 'f8', ('ocean_time', 'eta_rho', 'xi_rho')) -out_id.variables['zeta'].long_name = 'free-surface' -out_id.variables['zeta'].units = 'meter' -out_id.variables['zeta'][0,:,:] = zeta -out_id.createVariable('temp', 'f8', ('ocean_time', 's_rho', 'eta_rho', 'xi_rho')) -out_id.variables['temp'].long_name = 'potential temperature' -out_id.variables['temp'].units = 'Celsius' -out_id.variables['temp'][0,:,:,:] = temp -out_id.createVariable('salt', 'f8', ('ocean_time', 's_rho', 'eta_rho', 'xi_rho')) -out_id.variables['salt'].long_name = 'salinity' -out_id.variables['salt'].units = 'PSU' -out_id.variables['salt'][0,:,:,:] = salt -out_id.createVariable('sc_r', 'f8', ('s_rho')) -out_id.variables['sc_r'].long_name = 'S-coordinate at rho-points' -out_id.variables['sc_r'].units = 'nondimensional' -out_id.variables['sc_r'].valid_min = -1.0 -out_id.variables['sc_r'].valid_max = 0.0 -out_id.variables['sc_r'][:] = sc_r -out_id.close() +# User parameters +if __name__ == "__main__": + + # Path to ROMS grid file + grid_file = '../apps/common/grid/caisom001_OneQuartergrd.nc' + # Path to World Ocean Atlas NetCDF file (converted from FESOM input using + # woa_netcdf.py) + woa_file = '/short/y99/kaa561/FESOM/woa01_ts.nc' + # Path to desired output file + output_file = '../data/caisom001_ini.nc' + + # Grid parameters: check grid_file and *.in file to make sure these are correct + Tcline = 40 + theta_s = 0.9 + theta_b = 4 + hc = 40 + N = 31 + + # Northernmost index of WOA grid to read (1-based) + nbdry_woa = 32 + + run(grid_file, woa_file, output_file, Tcline, theta_s, theta_b, hc, N, nbdry_woa) + + diff --git a/woa_netcdf.py b/woa_netcdf.py index 0be6046..8599d8f 100644 --- a/woa_netcdf.py +++ b/woa_netcdf.py @@ -1,100 +1,102 @@ from netCDF4 import Dataset from numpy import * -# Read the World Ocean Atlas temperature and salinity climatology from a text -# file (FESOM input format), and save in a NetCDF file for easier use. +if __name__ == "__main__": -# File paths -in_file = '/short/y99/kaa561/FESOM/annual_woa01_ts.out' -out_file = '/short/y99/kaa561/FEsOM/woa01_ts.nc' + # Read the World Ocean Atlas temperature and salinity climatology from a text + # file (FESOM input format), and save in a NetCDF file for easier use. -print 'Reading text file' -file = open(in_file, 'r') + # File paths + in_file = '/short/y99/kaa561/FESOM/annual_woa01_ts.out' + out_file = '/short/y99/kaa561/FEsOM/woa01_ts.nc' -# First line contains the number of longitude, latitude, and depth values -sizes = (file.readline()).split() -num_lon = int(sizes[0]) -num_lat = int(sizes[1]) -num_depth = int(sizes[2]) + print 'Reading text file' + file = open(in_file, 'r') -# Read longitude values -lon = [] -while True: - # Each row has multiple values, so split based on white space - lon_vals = (file.readline()).split() - for elm in lon_vals: - lon.append(float(elm)) - if len(lon) == num_lon: - # Finished with longitude - break -# Repeat for latitude -lat = [] -while True: - lat_vals = (file.readline()).split() - for elm in lat_vals: - lat.append(float(elm)) - if len(lat) == num_lat: - break -# Repeat for depth -depth = [] -while True: - depth_vals = (file.readline()).split() - for elm in depth_vals: - # Convert from negative to positive depth - depth.append(-1*float(elm)) - if len(depth) == num_depth: - break -# Read temperature values (save in one long 1D array for now) -temp = [] -while True: - temp_vals = (file.readline()).split() - for elm in temp_vals: - temp.append(float(elm)) - if len(temp) == num_lon*num_lat*num_depth: - break -# Repeat for salinity -salt = [] -while True: - salt_vals = (file.readline()).split() - for elm in salt_vals: - salt.append(float(elm)) - if len(salt) == num_lon*num_lat*num_depth: - break -file.close() + # First line contains the number of longitude, latitude, and depth values + sizes = (file.readline()).split() + num_lon = int(sizes[0]) + num_lat = int(sizes[1]) + num_depth = int(sizes[2]) -# Copy contents of the long 1D temp and salt arrays into 3D arrays -# (depth x latitude x longitude) -print 'Reshaping temperature and salinity arrays' -temp_3d = zeros((num_depth, num_lat, num_lon)) -salt_3d = zeros((num_depth, num_lat, num_lon)) -posn = 0 -for i in range(num_lon): - for j in range(num_lat): - for k in range(num_depth): - temp_3d[k,j,i] = temp[posn] - salt_3d[k,j,i] = salt[posn] - posn = posn+1 + # Read longitude values + lon = [] + while True: + # Each row has multiple values, so split based on white space + lon_vals = (file.readline()).split() + for elm in lon_vals: + lon.append(float(elm)) + if len(lon) == num_lon: + # Finished with longitude + break + # Repeat for latitude + lat = [] + while True: + lat_vals = (file.readline()).split() + for elm in lat_vals: + lat.append(float(elm)) + if len(lat) == num_lat: + break + # Repeat for depth + depth = [] + while True: + depth_vals = (file.readline()).split() + for elm in depth_vals: + # Convert from negative to positive depth + depth.append(-1*float(elm)) + if len(depth) == num_depth: + break + # Read temperature values (save in one long 1D array for now) + temp = [] + while True: + temp_vals = (file.readline()).split() + for elm in temp_vals: + temp.append(float(elm)) + if len(temp) == num_lon*num_lat*num_depth: + break + # Repeat for salinity + salt = [] + while True: + salt_vals = (file.readline()).split() + for elm in salt_vals: + salt.append(float(elm)) + if len(salt) == num_lon*num_lat*num_depth: + break + file.close() -# Output to NetCDF file -print 'Writing NetCDF file' -id = Dataset(out_file, 'w') -id.createDimension('longitude', num_lon) -id.createDimension('latitude', num_lat) -id.createDimension('depth', num_depth) -id.createVariable('longitude', 'f8', ('longitude')) -id.variables['longitude'].units = 'degrees' -id.variables['longitude'][:] = lon -id.createVariable('latitude', 'f8', ('latitude')) -id.variables['latitude'].units = 'degrees' -id.variables['latitude'][:] = lat -id.createVariable('depth', 'f8', ('depth')) -id.variables['depth'].units = 'metres' -id.variables['depth'][:] = depth -id.createVariable('temp', 'f8', ('depth', 'latitude', 'longitude')) -id.variables['temp'].units = 'C' -id.variables['temp'][:,:,:] = temp_3d -id.createVariable('salt', 'f8', ('depth', 'latitude', 'longitude')) -id.variables['salt'].units = 'psu' -id.variables['salt'][:,:,:] = salt_3d -id.close() + # Copy contents of the long 1D temp and salt arrays into 3D arrays + # (depth x latitude x longitude) + print 'Reshaping temperature and salinity arrays' + temp_3d = zeros((num_depth, num_lat, num_lon)) + salt_3d = zeros((num_depth, num_lat, num_lon)) + posn = 0 + for i in range(num_lon): + for j in range(num_lat): + for k in range(num_depth): + temp_3d[k,j,i] = temp[posn] + salt_3d[k,j,i] = salt[posn] + posn = posn+1 + + # Output to NetCDF file + print 'Writing NetCDF file' + id = Dataset(out_file, 'w') + id.createDimension('longitude', num_lon) + id.createDimension('latitude', num_lat) + id.createDimension('depth', num_depth) + id.createVariable('longitude', 'f8', ('longitude')) + id.variables['longitude'].units = 'degrees' + id.variables['longitude'][:] = lon + id.createVariable('latitude', 'f8', ('latitude')) + id.variables['latitude'].units = 'degrees' + id.variables['latitude'][:] = lat + id.createVariable('depth', 'f8', ('depth')) + id.variables['depth'].units = 'metres' + id.variables['depth'][:] = depth + id.createVariable('temp', 'f8', ('depth', 'latitude', 'longitude')) + id.variables['temp'].units = 'C' + id.variables['temp'][:,:,:] = temp_3d + id.createVariable('salt', 'f8', ('depth', 'latitude', 'longitude')) + id.variables['salt'].units = 'psu' + id.variables['salt'][:,:,:] = salt_3d + id.close()