Added the ability to create netcdf files and write data in Meteoinfolab, which takes the example of merging multiple NETCDF files into a new netcdf file.
1, create a writable netCDF file object (denoted by Ncfile), with the AddFile function, the first parameter is the file name, the second parameter ' C ' indicates the creation of a new netCDF file.
Ncfile = AddFile (OUTFN, ' C ')
2, add dimension (Dimensions), with Ncfile Adddim function, two parameters are dimension name and dimension length respectively.
STN = 26564
Stdim = Ncfile.adddim (' station ', STN)
3, add global properties, with Ncfile addgroupattr function, two parameters are attribute name and attribute value respectively.
Ncfile.addgroupattr (' conventions ', ' Unidata observation Dataset v1.0 ')
4, add a variable, with ncfile Addvar function, three parameters are variable name, variable data type and variable dimension list. and add some attributes to the variable with the ADDATTR function of the variable object.
var = ncfile.addvar (' streamflow ', ' float ', [Tdim, Stdim])
Var.addattr (' long_name ', ' River Flow ')
Var.addattr (' units ', ' meter^3/sec ')
5, create the netCDF file, the above steps are the settings of the netCDF file, after completion of the need to create a function to indicate that the setup is complete, you can begin to write data.
Ncfile.create ()
6, write the data, with the Ncfile write function, the parameters are variables and the array to write, Origin is a list of the beginning of each dimension, you can control the part of the data written to a variable. If the dimensions of the array to be written are inconsistent with the dimensions of the variables, you need to use the reshape function to ensure that they have the same dimensionality.
Ncfile.write (variables[3], flow, origin=origin)
7. Close the netCDF file and close the netCDF file after the data has been written.
Ncfile.flush ()
Ncfile.close ()
Sample script (merging 4 different times of the netCDF file into a new netcdf file):
DataDir ='D:/TEMP/NC'OUTFN= Os.path.join (DataDir,'Join_file.nc') #New NetCDF FileNcfile = AddFile (OUTFN,'C')#Add DimensionsSTN = 26564Recdim= Ncfile.adddim ('Recnum', STN) Stdim= Ncfile.adddim (' Station', STN) Iddim= Ncfile.adddim ('Id_len', 11) Tdim= Ncfile.adddim (' Time', 4)#ADD Global AttributesNcfile.addgroupattr ('Conventions','unidata Observation Dataset v1.0') ncfile.addgroupattr ('Cdm_datatype',' Station') ncfile.addgroupattr ('Geospatial_lat_max','90.0') ncfile.addgroupattr ('Geospatial_lat_min','-90.0') ncfile.addgroupattr ('Geospatial_lon_max','180.0') ncfile.addgroupattr ('Geospatial_lon_min','-180.0') ncfile.addgroupattr ('stationdimension',' Station') ncfile.addgroupattr ('Missing_value', -8.9999998E15) ncfile.addgroupattr ('Stream_order_output', 1)#ADD Variablesvariables =[]var= Ncfile.addvar ('Latitude','float', [Stdim])#LatitudeVar.addattr ('Long_name','Station Latitude') var.addattr ('units','Degrees_north') Variables.append (Var) var= Ncfile.addvar ('Longitude','float', [Stdim])#LongitudeVar.addattr ('Long_name','Station Longitude') var.addattr ('units','Degrees_east') Variables.append (Var) var= Ncfile.addvar ('Altitude','float', [Stdim])#AltitudeVar.addattr ('Long_name','Station Altitude') var.addattr ('units','meters') Variables.append (Var) var= Ncfile.addvar ('Streamflow','float', [Tdim, Stdim])#Stream flow-add Time DimensionVar.addattr ('Long_name','River Flow') var.addattr ('units','meter^3/sec') Variables.append (Var) tvar= Ncfile.addvar (' Time','int', [Tdim]) tvar.addattr ('Long_name',' Time') tvar.addattr ('units','hours since 1900-01-01 00:00:0.0')#creat netCDF Filencfile.create ()#Write DataStime = Datetime.datetime (2015,8,2, 0) etime= Datetime.datetime (2015,8,2,3) St= Datetime.datetime (1900,1,1) fi=0 whileStime <=etime:Printstime fn= Os.path.join (DataDir, Stime.strftime ('%y%m%d%h') +'00.chrtout_domain2') ifos.path.exists (FN):Print '\ t'+fn F=AddFile (FN) hours= (stime-st). Total_seconds ()//3600Origin=[fi] Ncfile.write (Tvar, Array ([hours]), Origin=origin)iffi = =0:lat= f['Latitude'] [:] Ncfile.write (variables[0], lat) Lon= f['Longitude'] [:] Ncfile.write (variables[1], lon) ALT= f['Altitude'] [:] Ncfile.write (variables[2], alt) Flow= f['Streamflow'[:] Origin=[fi, 0] shape= [1, STN] Flow=Flow.array.reshape (Shape) ncfile.write (variables[3], Flow, origin=origin) Fi+ = 1stime= Stime + Datetime.timedelta (hours=1) #Close NetCDF fileNcfile.flush () ncfile.close ( )Print 'finished!'
To draw a merged file:
f = AddFile ('D:/temp/nc/join_file.nc') Lon= f['Longitude'][:]lat= f['Latitude'][:]var= f['Streamflow']flow= Var[1,:]axesm () Mlayer= Shaperead ('d:/temp/map/bou2_4p.shp') geoshow (mlayer) Levs= Arange (0, 0.1, 0.005) Layer= Scatterm (lon, LAT, flow, Levs, edge=False) Colorbar (layer) T= F.gettime (1) title ('River Flow ('+ T.strftime ('%y-%m-%d%hh)'))
Meteoinfolab Script Example: Creating a netCDF file (merging files)