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

Diff of /branches/doubleplusgood/doc/examples/inversion/create_netcdf.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4344 by jfenwick, Wed Feb 27 03:42:40 2013 UTC revision 4345 by jfenwick, Fri Mar 29 07:09:41 2013 UTC
# Line 17  This example shows how to create a netCD Line 17  This example shows how to create a netCD
17  inversions in esys.downunder.  inversions in esys.downunder.
18  """  """
19    
20    import sys
21  from datetime import datetime  from datetime import datetime
22  import numpy as np  import numpy as np
23  from scipy.io import netcdf_file  try:
24        from scipy.io import netcdf_file
25    
26  # output filename      # output filename
27  FILENAME='output.nc'      FILENAME='output.nc'
28    
29  # a concise title and summary of the dataset      # a concise title and summary of the dataset
30  TITLE="custom_data"      TITLE="custom_data"
31  SUMMARY="Bouguer gravity anomaly data"      SUMMARY="Bouguer gravity anomaly data"
32    
33  # Origin longitude (degrees east) and latitude (degrees north)      # Origin longitude (degrees east) and latitude (degrees north)
34  ORIGIN_X=130.2      ORIGIN_X=130.2
35  ORIGIN_Y=-29.1      ORIGIN_Y=-29.1
36    
37  # spacing in longitude,latitude direction (degrees)      # spacing in longitude,latitude direction (degrees)
38  DELTA_X=0.05      DELTA_X=0.05
39  DELTA_Y=0.05      DELTA_Y=0.05
40    
41  # Number of data points in longitude,latitude direction      # Number of data points in longitude,latitude direction
42  NX=20      NX=20
43  NY=10      NY=10
44    
45  # Dummy value (for unset areas)      # Dummy value (for unset areas)
46  MISSING=-9999      MISSING=-9999
47    
48  # Data error (can be constant or variable over the data points)      # Data error (can be constant or variable over the data points)
49  SIGMA = 3.      SIGMA = 3.
50    
51  # The actual data array, must have shape (NY, NX).      # The actual data array, must have shape (NY, NX).
52  # These are just some random numbers.      # These are just some random numbers.
53  DATA = 10*np.random.normal(size=(NY, NX), scale=SIGMA)      DATA = 10*np.random.normal(size=(NY, NX), scale=SIGMA)
54  ERROR = np.ones((NY, NX)) * SIGMA      ERROR = np.ones((NY, NX)) * SIGMA
55    
56  ##############################################################################      # Use MISSING where data is invalid/not available, example:
57  ###################### Keep everything below this line #######################      DATA[NY/2,NX/2]=MISSING
58  ##############################################################################      ERROR[NY/2,NX/2]=MISSING
59  # conventions used in the file  
60  conventions="CF-1.0, COARDS, Unidata Dataset Discovery v1.0"      ##############################################################################
61  # file log      ###################### Keep everything below this line #######################
62  history=datetime.now().strftime("%d-%m-%Y")+" created using python script"      ##############################################################################
63  # license      # conventions used in the file
64  license="Free to use"      conventions="CF-1.0, COARDS, Unidata Dataset Discovery v1.0"
65        # file log
66  # Create the output file and write a few metadata entries      history=datetime.now().strftime("%d-%m-%Y")+" created using python script"
67  o=netcdf_file(FILENAME,'w')      # license
68  o.Conventions=conventions      license="Free to use"
69  o.Metadata_Conventions=conventions  
70  o.history=history      # Create the output file and write a few metadata entries
71  o.license=license      o=netcdf_file(FILENAME,'w')
72  o.summary=SUMMARY      o.Conventions=conventions
73  o.title=TITLE      o.Metadata_Conventions=conventions
74        o.history=history
75  # Create longitude dimension and variable      o.license=license
76  longitude=np.linspace(ORIGIN_X, ORIGIN_X+NX*DELTA_X, NX, endpoint=False)      o.summary=SUMMARY
77  o.createDimension("longitude", NX)      o.title=TITLE
78  v=o.createVariable("longitude", longitude.dtype, ["longitude"])  
79  v.data[:]=longitude      # Create longitude dimension and variable
80  v.units="degrees_east"      longitude=np.linspace(ORIGIN_X, ORIGIN_X+NX*DELTA_X, NX, endpoint=False)
81  v.long_name="Longitude"      o.createDimension("longitude", NX)
82        v=o.createVariable("longitude", longitude.dtype, ["longitude"])
83  # Create latitude dimension and variable      v.data[:]=longitude
84  latitude=np.linspace(ORIGIN_Y, ORIGIN_Y-NY*DELTA_Y, NY, endpoint=False)      v.units="degrees_east"
85  o.createDimension("latitude", NY)      v.long_name="Longitude"
86  v=o.createVariable("latitude", latitude.dtype, ["latitude"])  
87  v.data[:]=latitude      # Create latitude dimension and variable
88  v.units="degrees_north"      latitude=np.linspace(ORIGIN_Y-NY*DELTA_Y, ORIGIN_Y, NY, endpoint=False)
89  v.long_name="Latitude"      o.createDimension("latitude", NY)
90        v=o.createVariable("latitude", latitude.dtype, ["latitude"])
91  # Create the main data variable      v.data[:]=latitude
92  v=o.createVariable("data", DATA.dtype, ["latitude","longitude"])      v.units="degrees_north"
93  v.missing_value=MISSING      v.long_name="Latitude"
94  v.data[:]=DATA  
95  v.long_name="Bouguer_anomaly"      # Create the main data variable
96        v=o.createVariable("data", DATA.dtype, ["latitude","longitude"])
97  # Create the error variable (can be omitted)      v.missing_value=MISSING
98  v=o.createVariable("error", ERROR.dtype, ["latitude","longitude"])      v.data[:]=DATA
99  v.missing_value=MISSING      v.long_name="Bouguer_anomaly"
100  v.data[:]=ERROR  
101        # Create the error variable (can be omitted)
102        v=o.createVariable("error", ERROR.dtype, ["latitude","longitude"])
103        v.missing_value=MISSING
104        v.data[:]=ERROR
105    
106  # Close the file      # Close the file
107  o.close()      o.close()
108    
109    except ImportError:
110        print("The scipy module was not found but is required to write netCDF files. Exiting...")
111    

Legend:
Removed from v.4344  
changed lines
  Added in v.4345

  ViewVC Help
Powered by ViewVC 1.1.26