Project

General

Profile

RE: Need help converting ascii to netcdf files » txt2nc.py

python script to create the nc file - Ralf Mueller, 2015-09-11 12:19

 
import numpy as np
from netCDF4 import Dataset
root = Dataset('fromTxt.nc','w',format='NETCDF4')
root.description = 'Example text input via numpy and netCDF4'
root.createDimension('lon',866)
root.createDimension('lat',654)
latitudes = root.createVariable('lat', 'f4', ('lat',))
langitudes = root.createVariable('lon', 'f4', ('lon',))
temp = root.createVariable('temp', 'f4', ('lat','lon',),fill_value=-9999)
lats = np.arange(-90,90,0.2)
len(lats)
lons = np.arange(-180,180,0.4)
len(lons)
len(lats[0:700])
latitudes[:] = lats[0:654]
langitudes[:] = lons[0:866]
f = open("in.txt")
a = np.loadtxt(f)
temp[:,:] = a[:,:]
root.close()
latitudes

(3-3/3)