Commit e3b30962 authored by abuddenberg's avatar abuddenberg

Added code to dump netcdf files to ascii; trim original netcdf files of unused data

parent 3e3741b6
__author__ = 'abuddenberg'
from config import NA_SCENARIOS_FILES, GLOBAL_PRECIP_FILES
from netCDF4 import Dataset
for scenario in NA_SCENARIOS_FILES:
nc = Dataset(NA_SCENARIOS_FILES[scenario], 'r')
# print nc.variables['lon'][9], nc.variables['lat'][23], nc.variables['MAM_percent_change'][23, 9]
with open('../data/pr_{s}_1970-1999_2071-2099.txt'.format(s=scenario.lower().replace('.', '')), 'w') as out_file:
print nc.variables.keys()
out_file.write('{0:>8} {1:>14} {2:18} {3:11} {4:18} {5:11} {6:18} {7:11} {8:18} {9:11}\n'.format(
'Lon',
'Lat',
'DJF_percent_change',
'DJF_stipple',
'MAM_percent_change',
'MAM_stipple',
'JJA_percent_change',
'JJA_stipple',
'SON_percent_change',
'SON_stipple'
))
for lon_idx, lon in enumerate(nc.variables['lon'][:]):
for lat_idx, lat in enumerate(nc.variables['lat'][:]):
out_file.write(
'{lon:>8} {lat: 14.10f} {djf: 18.3f} {djf_s:11.1f} {mam: 18.3f} {mam_s:11.1f} {jja: 18.3f} {jja_s:11.1f} {son: 18.3f} {son_s:11.1f}\n'.format(
lon=lon,
lat=lat,
djf=nc.variables['DJF_percent_change'][lat_idx, lon_idx],
djf_s=nc.variables['DJF_stipple'][lat_idx, lon_idx],
mam=nc.variables['MAM_percent_change'][lat_idx, lon_idx],
mam_s=nc.variables['MAM_stipple'][lat_idx, lon_idx],
jja=nc.variables['JJA_percent_change'][lat_idx, lon_idx],
jja_s=nc.variables['JJA_stipple'][lat_idx, lon_idx],
son=nc.variables['SON_percent_change'][lat_idx, lon_idx],
son_s=nc.variables['SON_stipple'][lat_idx, lon_idx],
)
)
for file, _unused in GLOBAL_PRECIP_FILES:
nc = Dataset(file, 'r')
print nc.variables['lon'][9], nc.variables['lat'][23], nc.variables['annual_percent_change'][23, 9]
scenario = file.split('_')[3]
with open('../data/pr_{s}_global_1970-1999_2071-2099.txt'.format(s=scenario.lower().replace('.', '')), 'w') as out_file:
print nc.variables.keys()
out_file.write('{0:>9} {1:>14} {2:21} {3:14}\n'.format(
'Lon',
'Lat',
'annual_percent_change',
'annual_stipple',
))
for lon_idx, lon in enumerate(nc.variables['lon'][:]):
for lat_idx, lat in enumerate(nc.variables['lat'][:]):
out_file.write(
'{lon:>9} {lat: 14.10f} {ann: 21.3f} {ann_s:14.1f}\n'.format(
lon=lon,
lat=lat,
ann=nc.variables['annual_percent_change'][lat_idx, lon_idx],
ann_s=nc.variables['annual_stipple'][lat_idx, lon_idx],
)
)
nc_out = Dataset('../data/pr_{s}_1970-1999_2071-2099_global_percent_change.nc'.format(s=scenario.lower().replace('.', '')), 'w')
nc_out.createDimension('lat', size=nc.variables['lat'].size)
nc_out.createDimension('lon', size=nc.variables['lon'].size)
latitude = nc_out.createVariable('lat', 'f8', ('lat',))
latitude.units = "degrees_north"
latitude.long_name = "latitude"
latitude.standard_name = "latitude"
latitude.axis = "Y"
latitude[:] = nc.variables['lat'][:]
longitude = nc_out.createVariable('lon', 'f8', ('lon',))
longitude.modulo = 360.
longitude.long_name = "longitude"
longitude.standard_name = "longitude"
longitude.units = "degrees_east"
longitude.axis = "X"
longitude.topology = "circular"
longitude[:] = nc.variables['lon'][:]
annual_change = nc_out.createVariable('annual_percent_change', 'f8', ('lat', 'lon'))
annual_change.unit = 'Percent'
annual_change.missing_value = 1.
annual_change[:] = nc.variables['annual_percent_change'][:]
nc_out.close()
new_nc = Dataset('../data/pr_{s}_1970-1999_2071-2099_global_percent_change.nc'.format(s=scenario.lower().replace('.', '')), 'r')
print new_nc.variables['lon'][9], new_nc.variables['lat'][23], new_nc.variables['annual_percent_change'][23, 9]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment