Skip to content

Commit

Permalink
Re-introduction of LMR_convertNPZtoNETCDF_gmt.py, improved error hand…
Browse files Browse the repository at this point in the history
…ling in other convertNPZtoNETCDF modules and py3 tested.
  • Loading branch information
rtardifUW committed Feb 21, 2018
1 parent e15f593 commit 70bcf8d
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 11 deletions.
20 changes: 15 additions & 5 deletions LMR_convertNPZtoNETCDF.py
Expand Up @@ -15,6 +15,9 @@
import numpy as np
from netCDF4 import Dataset, num2date

# LMR-specific import
from LMR_utils import natural_sort

# --- Begin section of user-defined parameters ---

# name of directory where the output of LMR experiments are located
Expand All @@ -24,7 +27,6 @@
# name of the experiment
nexp = 'test'


# Dictionary containing definitions of variables that can be handled by this code
var_desc = \
{
Expand All @@ -33,6 +35,7 @@
'psl_sfc_Amon' : ('MSLP', 'Mean sea level pressure anomaly', 'Pa'), \
'pr_sfc_Amon' : ('PRCP', 'Precipitation rate anomaly', 'kg/m2/s1'), \
'scpdsi_sfc_Amon' : ('scpdsi','self-calibrated Palmer Drought Severity Index', ''), \
'scpdsipm_sfc_Amon' : ('scpdsi','self-calibrated Palmer Drought Severity Index (Penman-Monteith)', ''), \
'uas_sfc_Amon' : ('Usfc', 'Near surface zonal wind anomaly', 'm/s'), \
'vas_sfc_Amon' : ('Vsfc', 'Near surface meridional wind anomaly', 'm/s'), \
'zg_500hPa_Amon' : ('H500', '500hPa geopotential height anomaly', 'm'), \
Expand Down Expand Up @@ -64,10 +67,10 @@
print('\n Getting information on Monte-Carlo realizations...\n')

dirs = glob.glob(expdir+"/r*")
# sorted
dirs.sort()
# keep names of MC directories (i.r. "r...") only
mcdirs = [item.split('/')[-1] for item in dirs]
# Make sure list is properly sorted
mcdirs = natural_sort(mcdirs)
# number of MC realizations found
niters = len(mcdirs)

Expand All @@ -92,6 +95,13 @@
# Loop over variables
for var in listvars:
print('\n Variable:', var)

if var not in list(var_desc.keys()):
print(' ***WARNING: Variable %s does not have a corresponding entry in variable definitions'
' (var_desc) at the top of this program. Please make necessary edits to have this variable'
' included in the format conversion output' %var)
continue

# Loop over realizations
r = 0
for dir in mcdirs:
Expand Down Expand Up @@ -140,7 +150,7 @@
print(' field type:', field_type)
else:
print('Cannot handle this variable yet! Variable of unrecognized dimensions... Exiting!')
exit(1)
raise SystemExit(1)


# declare master array that will contain data from all the M-C realizations
Expand Down Expand Up @@ -264,7 +274,7 @@

else:
print('Variable of unrecognized dimensions... Exiting!')
exit(1)
raise SystemExit(1)


# Closing the file
Expand Down
22 changes: 16 additions & 6 deletions LMR_convertNPZtoNETCDF_fullfield.py
Expand Up @@ -17,6 +17,9 @@
from netCDF4 import Dataset
import os

# LMR-specific import
from LMR_utils import natural_sort

# --- Begin section of user-defined parameters ---

# name of directory where the output of LMR experiments are located
Expand All @@ -25,8 +28,7 @@
datadir ='/Users/hakim/data/LMR/archive'

# name of the experiment
#nexp = 'test_pages2kv2_fullfield'
nexp = 'dadt_test_fullensemble'
nexp = 'test'

# Dictionary containing definitions of variables that can be handled by this code
var_desc = \
Expand All @@ -36,6 +38,7 @@
'psl_sfc_Amon' : ('MSLP', 'Mean sea level pressure anomaly', 'Pa'),
'pr_sfc_Amon' : ('PRCP', 'Precipitation rate anomaly', 'kg/m2/s1'),
'scpdsi_sfc_Amon' : ('scpdsi','self-calibrated Palmer Drought Severity Index', ''),
'scpdsipm_sfc_Amon' : ('scpdsi','self-calibrated Palmer Drought Severity Index (Penman-Monteith)', ''),
'uas_sfc_Amon' : ('Usfc', 'Near surface zonal wind anomaly', 'm/s'),
'vas_sfc_Amon' : ('Vsfc', 'Near surface meridional wind anomaly', 'm/s'),
'zg_500hPa_Amon' : ('H500', '500hPa geopotential height anomaly', 'm'),
Expand Down Expand Up @@ -68,10 +71,10 @@
print(('\n Getting information on Monte-Carlo realizations for ' + expdir + '...\n'))

dirs = glob.glob(expdir+"/r*")
# sorted
#dirs.sort()
# keep names of MC directories (i.r. "r...") only
mcdirs = [item.split('/')[-1] for item in dirs]
# Make sure list is properly sorted
mcdirs = natural_sort(mcdirs)
# number of MC realizations found
niters = len(mcdirs)

Expand All @@ -96,6 +99,13 @@
# Loop over variables
for var in listvars:
print(('\n Variable:', var))

if var not in list(var_desc.keys()):
print(' ***WARNING: Variable %s does not have a corresponding entry in variable definitions'
' (var_desc) at the top of this program. Please make necessary edits to have this variable'
' included in the format conversion output' %var)
continue

# Loop over realizations
for dir in mcdirs:
fname = expdir+'/'+dir+'/ensemble_full_'+var+'.npz'
Expand Down Expand Up @@ -141,7 +151,7 @@
print(' field type:', field_type)
else:
print('Cannot handle this variable yet! Variable of unrecognized dimensions... Exiting!')
exit(1)
raise SystemExit(1)


# declare master array that will contain data from all the M-C realizations
Expand Down Expand Up @@ -258,7 +268,7 @@

else:
print('Variable of unrecognized dimensions... Exiting!')
exit(1)
raise SystemExit(1)


# Closing the file
Expand Down
152 changes: 152 additions & 0 deletions LMR_convertNPZtoNETCDF_gmt.py
@@ -0,0 +1,152 @@
"""
Module: LMR_convertNPZtoNETCDF_gmt.py
Purpose: Converts LMR output from .npz files to netcdf files.
Now restricted to gmt reconstruction variables, for every
Monte-Carlo realization included in the experiment.
Originator: Robert Tardif | Univ. of Washington, Dept. of Atmospheric Sciences
Michael Erb | University of Southern California
| June 2017
Revisions: None
"""
import glob
import numpy as np
from netCDF4 import Dataset

# LMR-specific import
from LMR_utils import natural_sort

# --- Begin section of user-defined parameters ---

# name of directory where the output of LMR experiments are located
#datadir = '/home/disk/ekman4/rtardif/LMR/output'
datadir = '/home/disk/kalman3/rtardif/LMR/output'
#datadir = '/home/scec-00/lmr/erbm/LMR/archive_output/older_experiments'

# name of the experiment
nexp = 'test'

# --- End section of user-defined parameters ---

expdir = datadir + '/'+nexp

# where the netcdf files are created
outdir = expdir

print('\n Getting information on Monte-Carlo realizations...\n')

dirs = glob.glob(expdir+"/r*")
# keep names of MC directories (i.r. "r...") only
mcdirs = [item.split('/')[-1] for item in dirs]
# Make sure list is properly sorted
mcdirs = natural_sort(mcdirs)
# number of MC realizations found
niters = len(mcdirs)

print('mcdirs:' + str(mcdirs))
print('niters = ' + str(niters))

print('\n Getting information on reconstructed variables...\n')

# look in first "mcdirs" only. It should be the same for all.
workdir = expdir+'/'+mcdirs[0]

print('\n Global-mean temperature file.')
# Loop over realizations
r = 0
for dir in mcdirs:
fname = expdir+'/'+dir+'/gmt_ensemble.npz'
npzfile = np.load(fname)

# Get the reconstructed field
gmt_values = npzfile['gmt_ensemble']
nhmt_values = npzfile['nhmt_ensemble']
shmt_values = npzfile['shmt_ensemble']

if r == 0: # first realization

npzcontent = npzfile.files
print(' file contents:', npzcontent)

# get the years in the reconstruction
years = npzfile['recon_times']

# no spatial coordinate, must be a scalar (time series)
field_type='1D:time_series'
print(' field type:', field_type)

# declare master array that will contain data from all the M-C realizations
# (i.e. the "Monte-Carlo ensemble")
dims = gmt_values.shape
print(' gmt field dimensions', dims)
tmp_gmt = np.expand_dims(gmt_values, axis=0)
tmp_nhmt = np.expand_dims(nhmt_values, axis=0)
tmp_shmt = np.expand_dims(shmt_values, axis=0)
# Form the array with the right total dimensions
mc_ens_gmt = np.repeat(tmp_gmt,niters,axis=0)
mc_ens_nhmt = np.repeat(tmp_nhmt,niters,axis=0)
mc_ens_shmt = np.repeat(tmp_shmt,niters,axis=0)

else:
mc_ens_gmt[r,:,:] = gmt_values
mc_ens_nhmt[r,:,:] = nhmt_values
mc_ens_shmt[r,:,:] = shmt_values

r = r + 1


# Roll array to get dims as [time, niters, nens]
mc_ens_outarr_gmt = np.swapaxes(mc_ens_gmt,0,1)
mc_ens_outarr_nhmt = np.swapaxes(mc_ens_nhmt,0,1)
mc_ens_outarr_shmt = np.swapaxes(mc_ens_shmt,0,1)

# Create the netcdf file for the current variable
outfile_nc = outdir+'/gmt_ensemble_MCiters.nc'
outfile = Dataset(outfile_nc, 'w', format='NETCDF4')
outfile.description = 'LMR climate reconstruction for global-mean, NH-mean, and SH-mean surface air temperature (K).'
outfile.experiment = nexp
outfile.comment = 'File contains all ensemble values (ensemble_member) for each Monte-Carlo realization (iteration_member)'

# define dimensions
ntime = years.shape[0]
nensemble = mc_ens_outarr_gmt.shape[2]

outfile.createDimension('time', ntime)
outfile.createDimension('iteration_member', niters)
outfile.createDimension('ensemble_member', nensemble)


# define variables & upload the data to file

# time
time = outfile.createVariable('time', 'i', ('time',))
time.description = 'time'
time.long_name = 'year CE'

# reconstructed fields
varout_gmt = outfile.createVariable('gmt', 'f', ('time','iteration_member','ensemble_member'))
varout_gmt.description = 'gmt'
varout_gmt.long_name = 'Global-mean surface air temperature anomaly'
varout_gmt.units = 'K'

varout_nhmt = outfile.createVariable('nhmt', 'f', ('time','iteration_member','ensemble_member'))
varout_nhmt.description = 'nhmt'
varout_nhmt.long_name = 'Northern Hemisphere-mean surface air temperature anomaly'
varout_nhmt.units = 'K'

varout_shmt = outfile.createVariable('shmt', 'f', ('time','iteration_member','ensemble_member'))
varout_shmt.description = 'shmt'
varout_shmt.long_name = 'Southern Hemisphere-mean surface air temperature anomaly'
varout_shmt.units = 'K'

# upload the data to file
time[:] = years
varout_gmt[:] = mc_ens_outarr_gmt
varout_nhmt[:] = mc_ens_outarr_nhmt
varout_shmt[:] = mc_ens_outarr_shmt

# Closing the file
outfile.close()

0 comments on commit 70bcf8d

Please sign in to comment.