Example #1
0
    def write_unstruc_grid(self, ofn):
        """
        
        Write GNOME compatible netCDF file (netCDF3) from unstructured (triangular) grid data
        
        """
        nc = Dataset(ofn, 'w', format='NETCDF3_CLASSIC')

        # Global Attributes
        setattr(nc, 'grid_type', 'Triangular')

        # test u/v dimensions
        if self.data['u'].shape != self.data['v'].shape:
            print('u/v dimensions differ')
            raise

        # determine if its a subset in time
        t_key = 'time'
        try:
            if self.data['u'].shape[0] == len(self.data['time_ss']):
                t_key = 'time_ss'
        except KeyError:
            if self.data['u'].shape[0] != len(self.data['time']):
                print('Dimensions of u/v do not match time variable')
                raise

        lon_key = 'lon'
        lat_key = 'lat'
        nv_key = 'nv'
        nbe_key = 'nbe'
        lonc_key = 'lonc'
        latc_key = 'latc'
        a1u_key = 'a1u'
        a2u_key = 'a2u'
        # determine if its a subset of the grid
        try:
            if self.data['u'].shape[-1] == len(self.data['lon_ss']) or \
                self.data['u'].shape[-1] == self.data['nbe_ss'].shape[-1]:
                lon_key = 'lon_ss'
                lat_key = 'lat_ss'
                nv_key = 'nv_ss'
                nbe_key = 'nbe_ss'
                lonc_key = 'lonc_ss'
                latc_key = 'latc_ss'
                a1u_key = 'a1u_ss'
                a2u_key = 'a2u_ss'
        except KeyError:
            if self.data['u'].shape[-1] != len(self.data['lon']) and \
                self.data['u'].shape[-1] != self.data['nbe'].shape[-1]:
                print('Dimensions of u/v do not match grid variables')
                raise

        # add Dimensions
        nc.createDimension('time', None)
        nc.createDimension('node', len(self.data[lon_key]))
        nc.createDimension('nele', np.shape(self.data[nbe_key])[1])
        nc.createDimension('nbnd', len(self.data['bnd']))
        nc.createDimension('nbi', 4)
        nc.createDimension('three', 3)
        if 'a1u' in self.data:
            nc.createDimension('four', 4)
        if 'sigma' in self.data:
            nc.createDimension('sigma', len(self.data['sigma']))

        try:
            ufill = self.atts['u']['_FillValue']
            vfill = self.atts['v']['_FillValue']
        except KeyError:
            try:
                ufill = self.atts['u']['missing_value']
                vfill = self.atts['v']['missing_value']
            except KeyError:
                ufill = 999.
                vfill = 999.

        # create variables
        nc_time = nc.createVariable('time', 'f4', ('time', ))
        nc_lon = nc.createVariable('lon', 'f4', ('node'))
        nc_lat = nc.createVariable('lat', 'f4', ('node'))
        nc_nbe = nc.createVariable('nbe', 'int32', ('three', 'nele'))
        nc_nv = nc.createVariable('nv', 'int32', ('three', 'nele'))
        nc_bnd = nc.createVariable('bnd', 'int32', ('nbnd', 'nbi'))
        if 'sigma' in self.data:
            nc_sigma = nc.createVariable('sigma', 'f4', ('sigma', 'node'))
            #nc_sigma = nc.createVariable('sigma','f4',('sigma'))
            nc_depth = nc.createVariable('depth', 'f4', ('node'))
        if 'lonc' in self.data:
            nc_lonc = nc.createVariable('lonc', 'f4', ('nele'))
            nc_latc = nc.createVariable('latc', 'f4', ('nele'))
        if 'a1u' in self.data:
            nc_a1u = nc.createVariable('a1u', 'f4', ('four', 'nele'))
            nc_a2u = nc.createVariable('a2u', 'f4', ('four', 'nele'))

        if self.data['u'].shape[-1] == len(
                self.data[lon_key]):  #velocities on nodes
            if len(self.data['u'].shape) == 3:
                nc_u = nc.createVariable('u',
                                         'f4', ('time', 'sigma', 'node'),
                                         fill_value=ufill)
                nc_v = nc.createVariable('v',
                                         'f4', ('time', 'sigma', 'node'),
                                         fill_value=vfill)
            else:
                nc_u = nc.createVariable('u',
                                         'f4', ('time', 'node'),
                                         fill_value=ufill)
                nc_v = nc.createVariable('v',
                                         'f4', ('time', 'node'),
                                         fill_value=vfill)
        else:  #velocities on elements
            if len(self.data['u'].shape) == 3:
                nc_u = nc.createVariable('u',
                                         'f4', ('time', 'sigma', 'nele'),
                                         fill_value=ufill)
                nc_v = nc.createVariable('v',
                                         'f4', ('time', 'sigma', 'nele'),
                                         fill_value=vfill)
            else:
                nc_u = nc.createVariable('u',
                                         'f4', ('time', 'nele'),
                                         fill_value=ufill)
                nc_v = nc.createVariable('v',
                                         'f4', ('time', 'nele'),
                                         fill_value=vfill)

        #adjust time if necessary
        ref_time = self.atts['time']['units'].split(' since ')[1]
        ref_year = int(ref_time[0:4])
        if ref_year < 1970:
            print('Adjusting reference time')
            self.data[t_key],self.atts['time']['units'] = \
                nctools.adjust_time(self.data[t_key],self.atts['time']['units'])

        #add data to netcdf file
        nc_time[:] = self.data[t_key]
        nc_lon[:] = self.data[lon_key]
        nc_lat[:] = self.data[lat_key]
        nc_u[:] = self.data['u']
        nc_v[:] = self.data['v']
        nc_bnd[:] = self.data['bnd']
        nc_nbe[:] = self.data[nbe_key]
        nc_nv[:] = self.data[nv_key]
        if 'sigma' in self.data:
            nc_sigma[:] = self.data['sigma'][:]
            nc_depth[:] = self.data['depth']
        if 'lonc' in self.data:
            lonc = self.data[lonc_key]
            nc_lonc[:] = (lonc > 180).choose(lonc, lonc - 360)
            nc_latc[:] = self.data[latc_key]
        if 'a1u' in self.data:
            print(nc_a1u.shape)
            print(self.data[a1u_key].shape)
            nc_a1u[:] = self.data[a1u_key]
            nc_a2u[:] = self.data[a2u_key]

        #add variable attributes to netcdf file
        for an_att in self.atts['time'].items():
            setattr(nc_time, an_att[0], an_att[1])

        for an_att in self.atts['lon'].items():
            setattr(nc_lon, an_att[0], an_att[1])

        for an_att in self.atts['lat'].items():
            setattr(nc_lat, an_att[0], an_att[1])

        for an_att in self.atts['bnd'].items():
            setattr(nc_bnd, an_att[0], an_att[1])

        for an_att in self.atts['nbe'].items():
            setattr(nc_nbe, an_att[0], an_att[1])

        for an_att in self.atts['nv'].items():
            setattr(nc_nv, an_att[0], an_att[1])

        for an_att in self.atts['u'].items():
            if an_att[0] != '_FillValue':
                setattr(nc_u, an_att[0], an_att[1])

        for an_att in self.atts['v'].items():
            if an_att[0] != '_FillValue':
                setattr(nc_v, an_att[0], an_att[1])

        nc.close()
Example #2
0
    def write_unstruc_grid(self,ofn):
        
        """
        
        Write GNOME compatible netCDF file (netCDF3) from unstructured (triangular) grid data
        
        """  
        nc = Dataset(ofn,'w',format='NETCDF3_CLASSIC')
        
        # Global Attributes
        setattr(nc,'grid_type','Triangular')
        
        # test u/v dimensions
        if self.data['u'].shape != self.data['v'].shape:
            print 'u/v dimensions differ'
            raise
        
        # determine if its a subset in time
        t_key = 'time'
        try:
            if self.data['u'].shape[0] == len(self.data['time_ss']):
                t_key = 'time_ss'
        except KeyError:
            if self.data['u'].shape[0] != len(self.data['time']):
                print 'Dimensions of u/v do not match time variable'
                raise
                
        lon_key = 'lon'; lat_key = 'lat'
        nv_key = 'nv'; nbe_key = 'nbe'
        lonc_key = 'lonc'; latc_key = 'latc'
        a1u_key = 'a1u'; a2u_key = 'a2u'
        # determine if its a subset of the grid
        try:
            if self.data['u'].shape[-1] == len(self.data['lon_ss']) or \
                self.data['u'].shape[-1] == self.data['nbe_ss'].shape[-1]:
                lon_key = 'lon_ss'; lat_key = 'lat_ss'
                nv_key = 'nv_ss'; nbe_key = 'nbe_ss'
                lonc_key = 'lonc_ss'; latc_key = 'latc_ss'
                a1u_key = 'a1u_ss'; a2u_key = 'a2u_ss'
        except KeyError:
            if self.data['u'].shape[-1] != len(self.data['lon']) and \
                self.data['u'].shape[-1] != self.data['nbe'].shape[-1]:
                print 'Dimensions of u/v do not match grid variables'
                raise 
                
        # add Dimensions
        nc.createDimension('time',None)
        nc.createDimension('node',len(self.data[lon_key]))
        nc.createDimension('nele',np.shape(self.data[nbe_key])[1])
        nc.createDimension('nbnd',len(self.data['bnd']))
        nc.createDimension('nbi',4)
        nc.createDimension('three',3)
        nc.createDimension('four',4)
        if self.data.has_key('sigma'):
            nc.createDimension('sigma',len(self.data['sigma'])) 
        
        try:
            ufill = self.atts['u']['_FillValue']
            vfill = self.atts['v']['_FillValue']
        except KeyError:
            try:
                ufill = self.atts['u']['missing_value']
                vfill = self.atts['v']['missing_value']
            except KeyError:
                ufill = 999.
                vfill = 999.
        
        # create variables
        nc_time = nc.createVariable('time','f4',('time',))
        nc_lon = nc.createVariable('lon','f4',('node'))
        nc_lat = nc.createVariable('lat','f4',('node'))
        nc_nbe = nc.createVariable('nbe','int32',('three','nele'))
        nc_nv = nc.createVariable('nv','int32',('three','nele'))
        nc_bnd = nc.createVariable('bnd','int32',('nbnd','nbi'))
        if self.data.has_key('sigma'):
            nc_sigma = nc.createVariable('sigma','f4',('sigma','node'))
            #nc_sigma = nc.createVariable('sigma','f4',('sigma'))
            nc_depth = nc.createVariable('depth','f4',('node'))
        if self.data.has_key('lonc'):
            nc_lonc = nc.createVariable('lonc','f4',('nele'))
            nc_latc = nc.createVariable('latc','f4',('nele'))
        if self.data.has_key('a1u'):
            nc_a1u = nc.createVariable('a1u','f4',('four','nele'))
            nc_a2u = nc.createVariable('a2u','f4',('four','nele'))
            
        if self.data['u'].shape[-1] == len(self.data[lon_key]): #velocities on nodes
            if len(self.data['u'].shape) == 3:
                nc_u = nc.createVariable('u','f4',('time','sigma','node'),fill_value=ufill)
                nc_v = nc.createVariable('v','f4',('time','sigma','node'),fill_value=vfill)  
            else:
                nc_u = nc.createVariable('u','f4',('time','node'),fill_value=ufill)
                nc_v = nc.createVariable('v','f4',('time','node'),fill_value=vfill)
        else: #velocities on elements
            if len(self.data['u'].shape) == 3:
                nc_u = nc.createVariable('u','f4',('time','sigma','nele'),fill_value=ufill)
                nc_v = nc.createVariable('v','f4',('time','sigma','nele'),fill_value=vfill)
            else:   
                nc_u = nc.createVariable('u','f4',('time','nele'),fill_value=ufill)
                nc_v = nc.createVariable('v','f4',('time','nele'),fill_value=vfill)
                
        #adjust time if necessary
        ref_time = self.atts['time']['units'].split(' since ')[1]
        ref_year = int(ref_time[0:4])
        if ref_year < 1970:
            print 'Adjusting reference time'
            self.data[t_key],self.atts['time']['units'] = \
                nctools.adjust_time(self.data[t_key],self.atts['time']['units'])
        
        #add data to netcdf file
        nc_time[:] = self.data[t_key]
        nc_lon[:] = self.data[lon_key]
        nc_lat[:] = self.data[lat_key]
        nc_u[:] = self.data['u']
        nc_v[:] = self.data['v']
        nc_bnd[:] = self.data['bnd']
        nc_nbe[:] = self.data[nbe_key]
        nc_nv[:] = self.data[nv_key]
        if self.data.has_key('sigma'):
            nc_sigma[:] = self.data['sigma'][:]
            nc_depth[:] = self.data['depth']
        if self.data.has_key('lonc'):
            lonc = self.data[lonc_key]
            nc_lonc[:] = (lonc > 180).choose(lonc,lonc-360)
            nc_latc[:] = self.data[latc_key]
        if self.data.has_key('a1u'):
            print nc_a1u.shape
            print self.data[a1u_key].shape
            nc_a1u[:] = self.data[a1u_key]
            nc_a2u[:] = self.data[a2u_key]
            
        #add variable attributes to netcdf file
        for an_att in self.atts['time'].iteritems():
           setattr(nc_time,an_att[0],an_att[1])
        
        for an_att in self.atts['lon'].iteritems():
            setattr(nc_lon,an_att[0],an_att[1])
        
        for an_att in self.atts['lat'].iteritems():
            setattr(nc_lat,an_att[0],an_att[1])
        
        for an_att in self.atts['bnd'].iteritems():
            setattr(nc_bnd,an_att[0],an_att[1])

        for an_att in self.atts['nbe'].iteritems():
            setattr(nc_nbe,an_att[0],an_att[1])

        for an_att in self.atts['nv'].iteritems():
            setattr(nc_nv,an_att[0],an_att[1])
        
        for an_att in self.atts['u'].iteritems():
            if an_att[0] != '_FillValue':
                setattr(nc_u,an_att[0],an_att[1])
        
        for an_att in self.atts['v'].iteritems():
            if an_att[0] != '_FillValue':
                setattr(nc_v,an_att[0],an_att[1])
        
        nc.close()
Example #3
0
            } 
            
hycom = curv_grid.cgrid(url)
hycom.get_dimensions(var_map,get_z=True)
ts = num2date(hycom.data['time'][:],hycom.atts['time']['units'])
tid = np.where(np.logical_and(ts>=sdate,ts<=edate))[0]
print 'Number of time steps:', len(tid)
#adjust longitude: HYCOM lon goes from 74 --> 1019 ??
lon = np.mod(hycom.data['lon'],360)
hycom.data['lon'] = (lon > 180).choose(lon,lon-360)

#HYCOM uses time based on 1900 -- GNOME can't handle pre 1970, adjust before writing to file
native_tunits = hycom.atts['time']['units']

#Determine geographic subset indices
hycom.subset(bbox) #south lat, west lon, north lat, east lon
print hycom.x, hycom.y




for num,ti in enumerate(tid):
    print 'getting data time:', ti
    hycom.get_data(var_map,tindex=[ti,ti+1,1],yindex=hycom.y,xindex=hycom.x,is3d=True)     
    hycom.grid['depth'] = np.ones_like(hycom.data['lon_ss']) * -5000
    hycom.make_vel_mask()
    hycom.data['time_ss'],hycom.atts['time']['units'] = nctools.adjust_time(hycom.data['time_ss'],native_tunits)    
    print num2date(hycom.data['time_ss'],hycom.atts['time']['units'])
    hycom.write_nc(os.path.join(out_dir,'HYCOM_example_ ' + str(num).zfill(3) + '.nc'),is3d=True)
 
nctools.make_filelist_for_GNOME(out_dir,'HYCOM_example_*.nc',outfilename='HYCOM_filelist.txt')   
var_map = { 'time':'MT',
            'lon': 'Longitude',
            'lat': 'Latitude',
            'u': 'u',
            'v': 'v',
            } 
            
hycom = curv_grid.cgrid(url)
hycom.get_dimensions(var_map)
ts = num2date(hycom.data['time'][:],hycom.atts['time']['units'])
tid = np.where(np.logical_and(ts>=sdate,ts<=edate))[0]

#adjust longitude: HYCOM lon goes from 74 --> 1019 ??
lon = np.mod(hycom.data['lon'],360)
hycom.data['lon'] = (lon > 180).choose(lon,lon-360)

#HYCOM uses time based on 1900 -- GNOME can't handle pre 1970, adjust before writing to file
native_tunits = hycom.atts['time']['units']

#Determine geographic subset indices
hycom.subset(bbox) #south lat, west lon, north lat, east lon

for num,ti in enumerate(tid):
    hycom.get_data(var_map,tindex=[ti,ti+1,1],yindex=hycom.y,xindex=hycom.x,zindex=0,is3d=False)     
    hycom.make_vel_mask()
    hycom.data['time_ss'],hycom.atts['time']['units'] = nctools.adjust_time(hycom.data['time_ss'],native_tunits)    
    print num2date(hycom.data['time_ss'],hycom.atts['time']['units'])
    hycom.write_nc(os.path.join(out_dir,'HYCOM_example_ ' + str(num).zfill(3) + '.nc'),is3d=False)
 
nctools.make_filelist_for_GNOME(out_dir,'HYCOM_example_*.nc',outfilename='HYCOM_filelist.txt')   
else:
    arcroms.get_data(var_map)

ofn = os.path.join(data_files_dir, data_file[:-3] + '_gnome.nc')
'''
Right now we need to specify which are center lon/lat and which are stencil
This is because I am using the generic curv_grid cgrid.write_nc method
Eventually we will want a special format for ROMS preserving all the grids so this
is a bit of a kludge at this point...
Also, because we want to load into GUI gnome we have to reduce 
the size of lon/lat to match u/v
'''
if subset:
    arcroms.data['lon_ss'] = arcroms.data['lon_ss'][:-1, :-1]
    arcroms.data['lat_ss'] = arcroms.data['lat_ss'][:-1, :-1]
else:
    arcroms.data['lon'] = arcroms.data['lon_psi'][:-1, :-1]
    arcroms.data['lat'] = arcroms.data['lat_psi'][:-1, :-1]

arcroms.data['lonc'] = arcroms.data['lon_rho']
arcroms.data['latc'] = arcroms.data['lat_rho']
arcroms.grid['mask'] = arcroms.grid['mask_rho']

t, t_units = nctools.adjust_time(arcroms.data['time_ss'],
                                 arcroms.atts['time']['units'])
arcroms.data['time_ss'] = t
arcroms.atts['time']['units'] = t_units
arcroms.write_nc(ofn, gui_gnome=True, is3d=False)

#
is a bit of a kludge at this point...
Also, because we want to load into GUI gnome we have to reduce 
the size of lon/lat to match u/v
'''
if subset:
    arcroms.data['lon_ss'] = arcroms.data['lon_ss'][:-1,:-1]
    arcroms.data['lat_ss'] = arcroms.data['lat_ss'][:-1,:-1]
else:
    arcroms.data['lon'] = arcroms.data['lon_psi'][:-1,:-1]
    arcroms.data['lat'] = arcroms.data['lat_psi'][:-1,:-1]

    
arcroms.data['lonc'] = arcroms.data['lon_rho']
arcroms.data['latc'] = arcroms.data['lat_rho']
arcroms.grid['mask'] = arcroms.grid['mask_rho']

t,t_units = nctools.adjust_time(arcroms.data['time_ss'],arcroms.atts['time']['units'])
arcroms.data['time_ss'] = t
arcroms.atts['time']['units'] = t_units
arcroms.write_nc(ofn,gui_gnome=True,is3d=False)



#