forked from mhm-ufz/mHM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_netcdf.py
77 lines (57 loc) · 1.76 KB
/
prepare_netcdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
from __future__ import print_function
"""
History
-------
Written Matthias Cuntz & Juliane Mai Nov 2014 - write a netcdf file in L0
"""
# -------------------------------------------------------------------------
# Command line arguments
#
import argparse
addargs = []
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description=('''!!!.'''))
args = parser.parse_args()
del parser, args
# import packages needed after help so that help with command line -h is fast
import numpy as np
import netCDF4 as nc
# -------------------------------------------------------------------------
# Prepare data
#
# test_basin
ncol = 288
nrow = 432
# # test_basin_2
# ncol = 240
# nrow = 240
ntime = 1826
timeunit = "days since 1989-01-01 00:00:00"
varname = 'lai'
longname= 'LAI from Modis'
dat = np.arange(nrow*ncol*ntime).reshape((ntime, nrow, ncol))
dat = np.sin(dat)*3 + 3.
# -------------------------------------------------------------------------
# Write netcdf file
#
# NETCDF - out
filename = 'lai.nc'
print('Create netcdf file ', filename)
f = nc.Dataset(filename, 'w', format='NETCDF4')
# Structure
x = f.createDimension('x', ncol)
y = f.createDimension('y', nrow)
time = f.createDimension('time', None)
lai = f.createVariable(varname, 'f8', ('time','y','x',), fill_value=-9999., zlib=True, least_significant_digit=3)
lai.long_name = longname
time = f.createVariable('time', 'i4', ('time',))
time.units = timeunit
time.calendar = "standard"
f.Production = '2014/11/12'
# Data
lai[:,:,:] = dat
time[:] = np.arange(ntime)
f.close()
# -------------------------------------------------------------------------
# Finish