Skip to content

Instantly share code, notes, and snippets.

@andrewxhill
Created January 24, 2015 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewxhill/1df8fd0f770b319d9395 to your computer and use it in GitHub Desktop.
Save andrewxhill/1df8fd0f770b319d9395 to your computer and use it in GitHub Desktop.
# very handy/brute forcey ugly way to rip a flat file from a netcdf
from netCDF4 import Dataset
import numpy as np
from datetime import datetime, timedelta
import csv
b = open('test.csv', 'w')
a = csv.writer(b, delimiter=',')
# pressure levels in air data
# 17 Pressure levels (mb): 1000,925,850,700,600,500,400,300,250,200,150,100,70,50,30,20,10
# float32 air(time, level, lat, lon)
st = datetime(1800, 01, 01, 00, 00, 0)
fh = Dataset('uwnd.mon.mean.nc', mode='r')
# fh = Dataset('air.mon.mean.nc', mode='r')
print fh.variables
lons = fh.variables['lon'][:]
lats = fh.variables['lat'][:]
time = fh.variables['time'][:]
# air = fh.variables['air']
air = fh.variables['uwnd']
tmax_units = fh.variables['time'].units
a.writerow(['lat','lon','time','month','value'])
minc = 731;
c = 0
for t in time:
lt_n = 0
if c > minc:
for lt in lats:
ln_n = 0
for ln in lons:
realtime = st + timedelta(hours=t)
a.writerow([ lats[lt_n], lons[ln_n]-180, realtime, c - minc, air[c][1][lt_n][ln_n]])
# print air[c][1][lt_n][ln_n]
ln_n+=1
lt_n+=1
c+=1
print air[100][10][20]
print len(air)
print fh.variables
print len(time)
print len(lats)
print air
fh.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment