Я использую библиотеку xray python для добавления всех данных из файла ascii в файл netcdf. Файл ascii содержит данные для каждой ячейки с шагом 0,25 градуса на Земле.
Я могу создать все размеры широты и долготы, но не могу добавить данные. Файл ascii находится здесь: https://www.dropbox.com/s/lybu6yvm4ph7pcr/tmp.txt?dl=0
Может ли кто-нибудь диагностировать код и посмотреть, что происходит не так?
import numpy, os, pdb, errno, sys, xray
NUM_LATS = 180.0
NUM_LONS = 360.0
inp_dir = 'C:\\Input\\'
out_dir = 'C:\\Output\\nc\\'
def make_dir_if_missing(d):
try:
os.makedirs(d)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
make_dir_if_missing(out_dir)
# Read ASCII File
fl_name = inp_dir+'tmp.txt'
ascii_fl = numpy.loadtxt(fl_name, delimiter=' ')
# Compute dimensions of nc file based on # rows/cols in ascii file
fl_res = NUM_LATS/ascii_fl.shape[0]
if fl_res != NUM_LONS/ascii_fl.shape[1]:
print 'Incorrect dimensions in ascii file'
sys.exit(0)
lon = numpy.arange(0.5, 360.5, fl_res)
lat = numpy.arange(-90.5, 89.5, fl_res)
lons, lats = numpy.meshgrid(lon,lat)
d = {}
d['latitudes'] = ('latitudes',lat)
d['longitudes'] = ('longitudes', lon)
d['data'] = (['latitudes','longitudes'], ascii_fl)
dset = xray.Dataset(d)
dset
out_nc = out_dir+os.path.basename(inp_dir+'tmp.txt')[:-4]+'.nc'
dset.to_netcdf(out_nc)