0

I would like to add all the data from an ascii file to a netcdf file. The ascii file has data for every 0.25 degree cell on earth.

I am able to create all the lat/lon dimensions but not able to add the data. The ascii file is here: https://www.dropbox.com/s/lybu6yvm4ph7pcr/tmp.txt?dl=0

Can someone diagnose the code and see what is going wrong?

from netCDF4 import Dataset
import numpy, os, pdb, errno, sys

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)

# Initialize nc file
out_nc   = out_dir+os.path.basename(inp_dir+'tmp.txt')[:-4]+'.nc'
nc_data  = Dataset(out_nc, 'w', format='NETCDF4')
nc_data.description = 'Test'

# dimensions
nc_data.createDimension('lat', ascii_fl.shape[0])
nc_data.createDimension('lon', ascii_fl.shape[1])
nc_data.createDimension('data', 1)

# Populate and output nc file
# variables
latitudes  = nc_data.createVariable('latitude', 'f4', ('lat',))
longitudes = nc_data.createVariable('longitude', 'f4', ('lon',))
glm_data   = nc_data.createVariable('glm_data', 'f4', ('lat', 'lon', 'data'), fill_value=-9999.0)

glm_data.units = ''

# set the variables we know first
latitudes  = numpy.arange(-90.5, 89.5, fl_res)
longitudes = numpy.arange(0.5, 360.5, fl_res)
glm_data   = ascii_fl  ### THIS LINE IS NOT WORKING!!!!!!!


nc_data.close()

1 Answer 1

2

You just need to explicitly write out the 2-dimensions for the variable glm_data:

glm_data[:,:] = ascii_fl[:,:]
Sign up to request clarification or add additional context in comments.

3 Comments

Strange, it's OK on my end. What is not working for you?
hmm, can you try opening it up in Panoply?
Yes, I'm able to view the data. If you use something like ncdump, can you see the data being written out OK to the file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.