[1465] | 1 | # Python script to create a file with mountain peaks and their heights |
---|
| 2 | # L. Fita, LMD March 2017 |
---|
| 3 | # |
---|
| 4 | # To be used as example for PyNCplot `draw_ptZvals' |
---|
| 5 | # |
---|
| 6 | import numpy as np |
---|
| 7 | from netCDF4 import Dataset as NetCDFFile |
---|
| 8 | import generic_tools as gen |
---|
| 9 | import nc_var_tools as ncvar |
---|
| 10 | |
---|
| 11 | # A series of mountain peaks, their heights and their coordinates |
---|
| 12 | # [DEGlon, MINlon, SEClon, DEGlat, MINlat, SEClat] (from Wikipedia) |
---|
| 13 | peaks = {'Everest': [8848., 86., 55., 31., 27., 59., 17.], \ |
---|
| 14 | 'Mont Blanc': [4808.73, 6., 51., 53., 45., 49., 57.], \ |
---|
| 15 | 'Aneto': [3404., -0., -39., -28., 42., 37., 56.], \ |
---|
| 16 | 'Aconcagua': [6962., -70., -0, -40., -32., -39., -12.], \ |
---|
| 17 | 'Denali': [6190., -151., -0., -27., 63., 04., 10.], \ |
---|
| 18 | 'Kilimanjaro': [5895., 37., 21, 35., -3.,-4.,-0.], \ |
---|
| 19 | 'Ulrulu': [863., 131., 2., 11., -25., -20, -42.], \ |
---|
| 20 | 'Elbrus': [5642., 42., 26., 21., 43., 21., 18.], \ |
---|
| 21 | 'Vinson': [4892., -85., -32., -2., -78., -31., -31.], \ |
---|
| 22 | 'Enduwa Kombuglu': [4509., 145., 2., 0., -5., -48., -0.], \ |
---|
| 23 | 'Orizaba': [5636., -97., -16., -12., 19., 01., 48.] } |
---|
| 24 | |
---|
| 25 | # Length of the peaks name |
---|
| 26 | Lstring = 250 |
---|
| 27 | |
---|
| 28 | ofile = 'MountainPeaks.nc' |
---|
| 29 | |
---|
| 30 | ####### ######## |
---|
| 31 | ## MAIN |
---|
| 32 | ####### |
---|
| 33 | |
---|
| 34 | # Sorting peaks' names (just to be fancy...) |
---|
| 35 | peaksnames = list(peaks.keys()) |
---|
| 36 | peaksnames.sort() |
---|
| 37 | |
---|
| 38 | onc = NetCDFFile(ofile, 'w') |
---|
| 39 | |
---|
| 40 | Nmountains = len(peaks.keys()) |
---|
| 41 | |
---|
| 42 | # Dimensions |
---|
| 43 | newdim = onc.createDimension('mountain', Nmountains) |
---|
| 44 | newdim = onc.createDimension('Lstring', Lstring) |
---|
| 45 | |
---|
| 46 | # Variables |
---|
| 47 | newvarname = onc.createVariable('name', 'c', ('mountain', 'Lstring')) |
---|
| 48 | ncvar.basicvardef(newvarname,'name','Name of the peak','-') |
---|
| 49 | newattr = ncvar.set_attribute(newvarname,'coordinates','lon lat') |
---|
| 50 | newvals = ncvar.writing_str_nc(newvarname, peaksnames, Lstring) |
---|
| 51 | |
---|
| 52 | newvarlon = onc.createVariable('lon', 'f8', ('mountain')) |
---|
| 53 | ncvar.basicvardef(newvarlon,'longitude','Longitude','degrees_East') |
---|
| 54 | newattr = ncvar.set_attribute(newvarlon,'axis','X') |
---|
| 55 | newattr = ncvar.set_attribute(newvarlon,'_CoordinateAxisType','Lon') |
---|
| 56 | |
---|
| 57 | newvarlat = onc.createVariable('lat', 'f8', ('mountain')) |
---|
| 58 | ncvar.basicvardef(newvarlat,'latitude','Latitude','degrees_Noth') |
---|
| 59 | newattr = ncvar.set_attribute(newvarlat,'axis','Y') |
---|
| 60 | newattr = ncvar.set_attribute(newvarlat,'_CoordinateAxisType','Lat') |
---|
| 61 | |
---|
| 62 | newvarhgt = onc.createVariable('height', 'f4', ('mountain')) |
---|
| 63 | ncvar.basicvardef(newvarhgt,'height','Height above sea level','m') |
---|
| 64 | |
---|
| 65 | for keyv in peaks.keys(): |
---|
| 66 | peakv = peaks[keyv] |
---|
| 67 | ipeak = peaksnames.index(keyv) |
---|
| 68 | |
---|
| 69 | newvarhgt[ipeak] = peakv[0] |
---|
| 70 | newvarlon[ipeak] = peakv[1] + peakv[2]/60. + peakv[3]/3600. |
---|
| 71 | newvarlat[ipeak] = peakv[4] + peakv[5]/60. + peakv[6]/3600. |
---|
| 72 | |
---|
| 73 | onc.sync() |
---|
| 74 | |
---|
| 75 | # Global values |
---|
| 76 | addglob = ncvar.add_global_PyNCplot(onc, 'create_TopoValues.py', 'main', '0.1') |
---|
| 77 | |
---|
| 78 | onc.sync() |
---|
| 79 | onc.close() |
---|
| 80 | |
---|
| 81 | print "Successful written of '" + ofile + "' !!" |
---|
| 82 | |
---|