/[escript]/trunk/doc/examples/inversion/create_netcdf.py
ViewVC logotype

Contents of /trunk/doc/examples/inversion/create_netcdf.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5707 - (show annotations)
Mon Jun 29 03:59:06 2015 UTC (3 years, 8 months ago) by sshaw
File MIME type: text/x-python
File size: 3615 byte(s)
adding copyright headers to files without copyright info, moved header to top of file in some cases where it wasn't
1 ##############################################################################
2 #
3 # Copyright (c) 2003-2015 by The University of Queensland
4 # http://www.uq.edu.au
5 #
6 # Primary Business: Queensland, Australia
7 # Licensed under the Open Software License version 3.0
8 # http://www.opensource.org/licenses/osl-3.0.php
9 #
10 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 # Development 2012-2013 by School of Earth Sciences
12 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
13 #
14 ##############################################################################
15
16 """
17 This example shows how to create a netCDF input file that is suitable for
18 inversions in esys.downunder.
19 """
20 from __future__ import division, print_function
21
22 import sys
23 from datetime import datetime
24 import numpy as np
25 try:
26 from scipy.io import netcdf_file
27
28 # output filename
29 FILENAME='output.nc'
30
31 # a concise title and summary of the dataset
32 TITLE="custom_data"
33 SUMMARY="Bouguer gravity anomaly data"
34
35 # Origin longitude (degrees east) and latitude (degrees north)
36 ORIGIN_X=130.2
37 ORIGIN_Y=-29.1
38
39 # spacing in longitude,latitude direction (degrees)
40 DELTA_X=0.05
41 DELTA_Y=0.05
42
43 # Number of data points in longitude,latitude direction
44 NX=20
45 NY=10
46
47 # Dummy value (for unset areas)
48 MISSING=np.nan
49
50 # Data error (can be constant or variable over the data points)
51 SIGMA = 3.
52
53 # The actual data array, must have shape (NY, NX).
54 # These are just some random numbers.
55 DATA = 10*np.random.normal(size=(NY, NX), scale=SIGMA)
56 ERROR = np.ones((NY, NX)) * SIGMA
57
58 # Use MISSING where data is invalid/not available, example:
59 DATA[NY//2,NX//2]=MISSING
60 ERROR[NY//2,NX//2]=MISSING
61
62 ##############################################################################
63 ###################### Keep everything below this line #######################
64 ##############################################################################
65 # conventions used in the file
66 conventions="CF-1.0, COARDS, Unidata Dataset Discovery v1.0"
67 # file log
68 history=datetime.now().strftime("%d-%m-%Y")+" created using python script"
69 # license
70 license="Free to use"
71
72 # Create the output file and write a few metadata entries
73 o=netcdf_file(FILENAME,'w')
74 o.Conventions=conventions
75 o.Metadata_Conventions=conventions
76 o.history=history
77 o.license=license
78 o.summary=SUMMARY
79 o.title=TITLE
80
81 # Create longitude dimension and variable
82 longitude=np.linspace(ORIGIN_X, ORIGIN_X+NX*DELTA_X, NX, endpoint=False)
83 o.createDimension("longitude", NX)
84 v=o.createVariable("longitude", longitude.dtype, ["longitude"])
85 v.data[:]=longitude
86 v.units="degrees_east"
87 v.long_name="Longitude"
88
89 # Create latitude dimension and variable
90 latitude=np.linspace(ORIGIN_Y-NY*DELTA_Y, ORIGIN_Y, NY, endpoint=False)
91 o.createDimension("latitude", NY)
92 v=o.createVariable("latitude", latitude.dtype, ["latitude"])
93 v.data[:]=latitude
94 v.units="degrees_north"
95 v.long_name="Latitude"
96
97 # Create the main data variable
98 v=o.createVariable("data", DATA.dtype, ["latitude","longitude"])
99 v.missing_value=MISSING
100 v.data[:]=DATA
101 v.long_name="Bouguer_anomaly"
102
103 # Create the error variable (can be omitted)
104 v=o.createVariable("error", ERROR.dtype, ["latitude","longitude"])
105 v.missing_value=MISSING
106 v.data[:]=ERROR
107
108 # Close the file
109 o.close()
110
111 except ImportError:
112 print("The scipy module was not found but is required to write netCDF files. Exiting...")
113

  ViewVC Help
Powered by ViewVC 1.1.26