Convert existing time series NetCDF files in 4D (T,Z,Y,X) "grid" format to CF-1.6 format


In [70]:
import glob
import os
import netCDF4
import StringIO
import pandas as pd
import re

In [77]:
projs = '''
ARGO_MERCHANT,B. Butman,Argo Merchant Experiment,A moored array deployed after the ARGO MERCHANT ran aground onNantucket Shoals designed to help understand the fate of the spilled oil.
BUZZ_BAY,B. Butman,Currents and Sediment Transport in Buzzards Bay,Investigation of the near-bottom circulation in Buzzards Bay and consequent transport of fine-grained sediments that may be contaminated with PCBs from inner New Bedford Harbor.
CAMP,B. Butman,California Area Monitoring Program (CAMP),A four-year multi-disciplinary field and laboratory study to investigate the sediment transport regime in the vicinity of production drilling rigs in the Santa Barbara Basin
CAPE_COD_BAY,B. Butman,Currents and Sediment Transport in Cape Cod Bay,A pilot study to determine the effect of winter storms on sediment movement at two potential dredge spoil disposal areas.
CC_MISC,B. Butman,Transport studies - Nauset Inlet,Part of a collaborative study of sediment movement in Nauset Inlet.
DEEP_REEF,J. Lacey,Gulf of Mexico - Pinnacles,Pressure data from the Gulf of Mexico
DWDS_106,B. Butman,Sediment Transport at Deep Water Dump Site 106,Near-bottom current measurements to understand the fate and transport of sludge from the New York Metropolitan region discharged at the sea surface.
ECOHAB_II,R. Signell,Ecology of Harmful Algal Blooms (ECOHAB-II),A field program to continue investigating the transport and fate of toxic dinoflagellate blooms in the western Gulf of Maine.
ECOHAB_I,R. Signell,Ecology of Harmful Algal Blooms (ECOHAB-I),A field program to study the transport and fate of toxic dinoflagellate blooms in the western Gulf of Maine.
EUROSTRATAFORM,C. Sherwood,EuroSTRATAFORM,The EuroSTRATAFORM Po and Apennine Sediment Transport and Accumulation (PASTA) experiment was an international study of sediment-transport processes and formation of geological strata in the Adriatic Sea.
FARALLONES,M. Noble,Farallons,Program to measure the currents and circulation on the continental slope off San Francisco CA and thus infer the transport of dredged materialat the newly-established deep-water disposal site.
GB_SED,B. Butman,Georges Bank Current and Sediment Transport Studies,A series of studies to assess environmental hazards to petroleum development in the Georges Bank and New England Shelf region
GLOBEC_GB,R. Schlitz,GLOBEC Georges Bank Program,A moored array program to investigate the circulation and mixing of plankton on Georges Bank.
GLOBEC_GSC,R. Schlitz,GLOBEC Great South Channel Circulation Experiment,A moored array program to investigate the recirculation of water and plankton around Georges Bank
GULF_MAINE,B. Butman,Deep Circulation in the Gulf of Maine,A two-year field study to investigate the deep flow between the major basins in the Gulf of Maine and the effects on the distribution of suspended sediments.
HUDSON_SVALLEY,B. Butman,Circulation and Sediment Transport in the Hudson Shelf Valley,Field experiments have been carried out to understand the transport of sediments and associated contaminants in the Hudson Shelf Valley offshore of New York.
KARIN_RIDGE,M. Noble,Karin Ridge Experiment,Current measurements collected at 2 sites in Karin Ridge Seamount.
LYDONIA_C,B. Butman,Lydonia Canyon Dynamics Experiment,A major field experiment to determine the importance of submarine canyons in sediment transport along and across the continental margin.
MAB_SED,B. Butman,Sediment Transport Observations in the Middle Atlantic Bight,A series of studies to assess environmental hazards to petroleum development in the Middle Atlantic Bight. 
MAMALA_BAY,D. Cacchione,Mamala bay Experiment,Current measurements collected at 350-450 meters in Mamala Bay near Waikiki Beach. 
MBAY_CIRC,R. Signell, Massachusetts Bay Circulation Experiment,Current measurements collected at 6 sites in Massachusetts Bay throughout the year to map the tidal wind and density driven currents. 
MBAY_IWAVE,B. Butman,Massachusetts Bay Internal Wave Experiment,A 1-month 4-element moored array experiment to measure the currents associated with large-amplitude internal waves generated by tidal flow across Stellwagen Bank.
MBAY_LTB,B. Butman,Long-term observations in Massachusetts Bay; Site B-Scituate,Measurements of currents and other oceanographic properties were made to assess the impact of sewage discharge from the proposed outfall site.
MBAY_LT,B. Butman,Long-term observations in Massachusetts Bay; Site A-Boston Harbor,Measurements of currents and other oceanographic properties were made to assess the impact of sewage discharge from the proposed outfall site.
MBAY_STELL,R. Signell,Monitoring on Stellwagen Bank,A year-long series of current measurements on the eastern flank of Stellwagen Bank to document the currents at the mouth of Massachusetts Bay driven by the Maine Coastal current.
MBAY_WEST,B. Butman,Currents and Sediment Transport in Western Massachusetts Bay,A pilot winter-time experiment to investigate circulation and sediment transport. Designed to provide information to aid in citing the new ocean outfall for the Boston sewer system.
MOBILE_BAY,B. Butman,Mobile Bay Study,Measure currents and transport out of Mobile Bay.
MONTEREY_BAY,M. Noble,Monterey Bay National Marine Sanctuary Program,Part of a large multi-disciplinary experiment to characterize the geologic environment and to generate a sediment budget.
MONTEREY_CAN,M. Noble,Monterey Canyon Experiment, A program to determine the mechanisms that govern the circulation within and the transport of sediment and water through Monterey Submarine Canyon.
MYRTLEBEACH,J. Warner,Myrtle Beach Experiment SC,Measurements collected as part of a larger study to understand the physical processes that control the transport of sediments in Long Bay South Carolina. 
NE_SLOPE,B. Butman,Currents on the New England Continental Slope,A study designed to describe the currents and to investigate the transport of sediment from the shelf to the slope. 
OCEANOG_C,B. Butman,Oceanographer Canyon Dynamics Experiment,A field experiment to determine the importance of submarine canyons in sediment transport along and across the continental margin.
ORANGE_COUNTY,M. Noble,Orange County Sanitation District Studies,Observations to monitor coastal ocean process that transport suspended material and associated comtaminants across the shelf
PONCHARTRAIN,R. Signell,Lake Ponchartrain Project,A series of moored array studies to investigate the circulation and particle transport in Lake Pontchartrain.
PV_SHELF04,M. Noble,Palos Verdes Shelf 2004,Additional observations to estimate the quantity and direction of sediment erosion and transport on the shelf near the White Point ocean outfalls.
PV_SHELF07,M. Noble,Palos Verdes Shelf 2007,Follow-up observations to evaluate how often coastal ocean processes move the DDT contaminated sediments near the White Point ocean outfalls.
PV_SHELF,M. Noble,Palos Verdes Shelf Study,Initial observations of currents and circulation near the White Point ocean outfalls determine how often coastal ocean processes move the DDT contaminated sediments in this region.
SAB_SED,B. Butman,Sediment Transport Observations in the Southern Atlantic Bight,A series of studies to assess environmental hazards to petroleum development in the South Atlantic Bight.
SOUTHERN_CAL,M. Noble,Southern California Project,A series of moorings were deployed to understand how coastal ocean processes that move sediments change with location on the shelf.
STRESS,B. Butman,Sediment Transport on Shelves and Slopes (STRESS),Experiment on the California continental margin to investigate storm-driven sediment transport.
WRIGHTSVILLE,R. Thieler,Wrightsville Beach Study, Measurements of bottom currents and waves to investigate the flow field and sediment transport in a rippled scour depression offshore of Wrightsville Beach NC.
DIAMONDSHOALS,J. Warner,Cape Hatteras- Diamond Shoals,This experiment was designed to investigate the ocean circulation and sediment transport dynamics at Diamond Shoals NC.
CHANDELEUR,C. Sherwood,Chandeleur Islands Oceanographic Measurements,A program to measure waves water levels and currents near the Chandeleur Islands Louisiana and adjacent berm construction site.
WFAL,N. Ganju,West Falmouth Harbor Fluxes,Oceanographic and water-quality observations made at six locations in West Falmouth Harbor and Buzzards Bay.
BW2011,N. Ganju, Blackwater 2011, Oceanographic and Water-Quality Measurements made at several sites in 2 watersheds in Blackwater National Wildlife Refuge.
MVCO_11,C. Sherwood, OASIS MVCO 2011, Near-seabed Oceanographic Observations made as part of the 2011 OASIS Project at the MVCO.
HURRIRENE_BB,B. Butman, Observations in Buzzards Bay during and after a Hurricane, Oceanographic data collected in Buzzards Bay MA during Hurricane Irene August 2011.'''

In [78]:
project = pd.read_csv(StringIO.StringIO(projs.strip()), 
                      sep=",\s*",index_col='project_id',
                      names=['project_id', 'project_pi', 'project_name','project_summary'])

In [9]:
project.ix['PV_SHELF']


Out[9]:
project_pi                                                  M. Noble
project_name                                Palos Verdes Shelf Study
project_summary    Initial observations of currents and circulati...
Name: PV_SHELF, dtype: object

In [57]:
len(project)


Out[57]:
47

In [61]:
for index,row in project.iterrows():
    print index,row['project_pi']


ARGO_MERCHANT B. Butman
BUZZ_BAY B. Butman
CAMP B. Butman
CAPE_COD_BAY B. Butman
CC_MISC B. Butman
DEEP_REEF J. Lacey
DWDS_106 B. Butman
ECOHAB_II R. Signell
ECOHAB_I R. Signell
EUROSTRATAFORM C. Sherwood
FARALLONES M. Noble
GB_SED B. Butman
GLOBEC_GB R. Schlitz
GLOBEC_GSC R. Schlitz
GULF_MAINE B. Butman
HUDSON_SVALLEY B. Butman
KARIN_RIDGE M. Noble
LYDONIA_C B. Butman
MAB_SED B. Butman
MAMALA_BAY D. Cacchione
MBAY_CIRC R. Signell
MBAY_IWAVE B. Butman
MBAY_LTB B. Butman
MBAY_LT B. Butman
MBAY_STELL R. Signell
MBAY_WEST B. Butman
MOBILE_BAY B. Butman
MONTEREY_BAY M. Noble
MONTEREY_CAN M. Noble
MYRTLEBEACH J. Warner
NE_SLOPE B. Butman
OCEANOG_C B. Butman
ORANGE_COUNTY M. Noble
PONCHARTRAIN R. Signell
PV_SHELF04 M. Noble
PV_SHELF07 M. Noble
PV_SHELF M. Noble
SAB_SED B. Butman
SOUTHERN_CAL M. Noble
STRESS B. Butman
WRIGHTSVILLE R. Thieler
DIAMONDSHOALS J. Warner
CHANDELEUR C. Sherwood
WFAL N. Ganju
BW2011 N. Ganju
MVCO_11 C. Sherwood
hurrIrene_bb B. Butman

In [10]:
"""function nname=lookup_cf(long_name)
% LOOKUP_CF  Get CF equivalent name for EPIC variable long_name
% return the new name string or [] if there's no equivalent
%

if(strfind(lower(long_name),'temp'))
    nname='sea_water_temperature';
elseif (strfind(lower(long_name),'cond'))
    nname='sea_water_electrical_conductivity';
elseif (strfind(lower(long_name),'sal'))
    nname='sea_water_salinity';
elseif (strfind(lower(long_name),'sigma'))
    nname='sea_water_sigma_theta';
% also have to deal with the min, max std of vels for burst stats files
elseif (strfind(lower(long_name),'east'))
    nname='eastward_sea_water_velocity';
elseif (strfind(lower(long_name),'north'))
    nname='northward_sea_water_velocity';
elseif (strfind(lower(long_name),'vertical'))
    nname='upward_sea_water_velocity';
elseif (strfind(lower(long_name),'pitch'))
    nname='platform_pitch_angle';
elseif (strfind(lower(long_name),'roll'))
    nname='platform_roll_angle';
elseif (strfind(lower(long_name),'head'))
    nname='platform_orientation';
elseif (strfind(lower(long_name),'pres'))
    if ~isempty(strfind(lower(long_name),'dev')) || ~isempty(strfind(lower(long_name),'std'))
        nname=[];
    else
       nname='sea_water_pressure';
    end
elseif (strfind(lower(long_name),'cond'))
    nname='sea_water_electrical_conductivity';
elseif (strfind(lower(long_name),'speed'))
    if (strfind(lower(long_name),'rotor'))
        nname=[];
    else
       nname='sea_water_speed';
    end
elseif (strfind(lower(long_name),'direction'))
    nname='direction_of_sea_water_velocity';
else
    nname=[];
end
disp([long_name ' : ' nname])
""";

In [40]:
#os.chdir('/usgs/data2/emontgomery/stellwagen/Data/ARGO_MERCHANT')

root_dir='/usgs/data2/emontgomery/stellwagen/Data/'
#root_dir='/usgs/data2/emontgomery/stellwagen/Data/MVCO_11'
odir='/usgs/data2/emontgomery/stellwagen/CF-1.6/'
os.chdir(root_dir)

In [13]:
for path, subdirs, files in os.walk(root_dir,topdown=True):
    subdirs[:]=[]
    print subdirs
    for name in files:
        file= os.path.join(path, name)
        print file


[]
/usgs/data2/emontgomery/stellwagen/Data/.DS_Store

In [14]:
# now find all the unique names, long_names & units
names = set()
long_names = set()
units = set()
for path, subdirs, files in os.walk(root_dir):
    for name in files:
        file= os.path.join(path, name)
        try:
            nc=netCDF4.Dataset(file)
            for var in nc.variables.keys():
                names.add(var)
                try:
                    long_names.add(nc.variables[var].long_name)
                except:
                    pass
                try:
                    units.add(nc.variables[var].units)
                except:
                    pass
                    
        except:
            pass

names= list(names)
long_names = list(long_names)
units = list(units)

print len(names)
print len(long_names)
print len(units)


509
545
153

In [14]:


In [15]:
# let's use Ellyn's approach of matching substrings in the long_names to deduce standard_names

In [16]:
# standard_name to variable mapping
d={}

In [17]:
# air temp, frtemp, laser temp factor, internal, temp diff
filter(lambda x:re.search(r'temp',x.lower()), long_names)


Out[17]:
[u'AIR TEMPERATURE (C)      ',
 u'instrument Transducer Temp.',
 u'Air Temperature (degrees C)',
 u'TEMPERATURE (C)          ',
 u'FR TEMP     ',
 u'ADP Transducer Temp.',
 u'ADCP Transducer Temp.',
 u'TEMPERATURE (C)',
 u'Transducer Temp.    ',
 u'laser temperature factor',
 u'Transducer Temp.',
 u'TEMP 1      ',
 u'TEMP 2      ',
 u'Temperature',
 u'internal temperature',
 u'FRTEMP      ',
 u'TEMP 2 Q    ',
 u'TEMP        ',
 u'TEMP DIFF   ',
 u'TEMP LP     ',
 u'Sea Surface Temperature (degrees C)',
 u'TEMPERATURE ']

In [18]:
d['sea_water_temperature']=['instrument transducer temp.', 'temperature (c)','fr temp',
                            'adp transducer temp.','adcp transducer temp.','transducer temp.','temp 1','temp 2',
                            'temperature','internal temperature','frtemp','temp 2 q','temp','temp lp','sea surface temperature (degrees C)']

In [19]:
# seconds, second
filter(lambda x:re.search(r'cond',x.lower()), long_names)


Out[19]:
[u'SPECIFIC CONDUCTANCE        ',
 u'CONDUCTIVITY             ',
 u'Specific conductance',
 u'SECONDS SINCE START',
 u'second',
 u'CONDUCTIVITY',
 u'SP COND     ',
 u'COND        ',
 u'CTD Conductivity       ',
 u'COND 2      ',
 u'COND 1      ']

In [20]:
filter(lambda x:re.search(r'sal',x.lower()), long_names)


Out[20]:
[u'SALINITY 2 Q',
 u'SALINITY 1  ',
 u'CTD Salinity, PSS-78',
 u'Salinity',
 u'SALINITY 2  ',
 u'SALINITY (PPT)           ',
 u'SALINITY (PSU)           ',
 u'SALINITY    ']

In [21]:
d['sea_water_salinity'] = ['salinity 2 q','salinity 1','ctd salinity, pss-78','salinity','salinity (ppt)','salinity (psu)','salinity']

In [22]:
filter(lambda x:re.search(r'sigma',x.lower()), long_names)


Out[22]:
[u'SIGMA-THETA (KG/M**3) ',
 u'SIGMA-T (KG/M**3)        ',
 u'SIGMA-THETA (KG/M**3)    ',
 u'SIGMA THETA ']

In [23]:
# deal with burst data, std dev, resolution velocity, variance
filter(lambda x:re.search(r'east',x.lower()), long_names)


Out[23]:
[u'Eastward Velocity       ',
 u'Eastward Velocity',
 u'Ratio of variances for burst after points replaced by delgitch: Eastward Velocity',
 u'EAST        ',
 u'Maximum Eastward Velocity',
 u'Minimum Eastward Velocity',
 u'Eastward Resolution Velocity',
 u'Std. Dev. of east component',
 u'Ratio of means for burst after points replaced by delgitch: Eastward Velocity',
 u'EAST VELOCITY VARIANCE   ',
 u'Mean Eastward Velocity',
 u'Eastward Velocity        ',
 u'Eastward Velocity         ',
 u'EAST LP     ',
 u'EAST VELOCITY VARIANCE ',
 u'Mean Resolution VelocityEastward']

In [24]:
d['eastward_sea_water_velocity']=['eastward velocity','east','mean eastward velocity','eastward velocity','east lp']

In [25]:
filter(lambda x:re.search(r'north',x.lower()), long_names)


Out[25]:
[u'NORTH       ',
 u'Northward Velocity        ',
 u'Std. Dev. of north component',
 u'Peak Wave Direction (degrees North)',
 u'Wind Direction (degrees true North)',
 u'Ratio of means for burst after points replaced by delgitch: Northward Velocity',
 u'Minimum Northward Velocity',
 u'Maximum Northward Velocity',
 u'Peak Wave Direction (degrees true North)',
 u'Northward Velocity',
 u'Mean Wave Direction (degrees true North)',
 u'Mean Resolution VelocityNorthward',
 u'NORTH VELOCITY VARIANCE ',
 u'NORTH LP    ',
 u'Northward Resolution Velocity',
 u'Mean Northward Velocity',
 u'Ratio of variances for burst after points replaced by delgitch: Northward Velocity',
 u'Northward Velocity      ',
 u'Northward Velocity       ',
 u'NORTH VELOCITY VARIANCE  ']

In [26]:
d['northward_sea_water_velocity']=['northward velocity','north','mean northward velocity','northward velocity','north lp']

In [27]:
filter(lambda x:re.search(r'vertical',x.lower()), long_names)


Out[27]:
[u'Vertical Velocity    ',
 u'Vertical Velocity        ',
 u'Ratio of means for burst after points replaced by delgitch: Vertical Velocity',
 u'Vertical Resolution Velocity',
 u'Ratio of variances for burst after points replaced by delgitch: Vertical Velocity',
 u'Vertical Velocity',
 u'Maximum Vertical Velocity',
 u'Mean Resolution VelocityVertical',
 u'Vertical Velocity         ',
 u'Mean Vertical Velocity',
 u'Minimum Vertical Velocity']

In [28]:
# wind, rotor speed
filter(lambda x:re.search(r'speed',x.lower()), long_names)


Out[28]:
[u'WIND SPEED (CM/S)        ',
 u'ROTOR SPEED DIFFERENCE',
 u'ROTOR SPEED DIFFERENCE  ',
 u'Current Speed',
 u'UPPER ROTOR SPEED     ',
 u'Wind Speed (m/s)',
 u'CURRENT SPEED (CM/S)      ',
 u'LOWER ROTOR SPEED  ',
 u'CURRENT SPEED (CM/S)    ',
 u'VECTOR SPEED',
 u'LOWER ROTOR SPEED    ',
 u'WIND SPEED (M/S) ',
 u'WIND SPEED (CM/S) ',
 u'CURRENT SPEED (CM/S)       ',
 u'ROTOR SPEED             ',
 u'ROTOR SPEED ']

In [29]:
# std dev
filter(lambda x:re.search(r'pitch',x.lower()), long_names)


Out[29]:
[u'INST Pitch',
 u'Pitch Std. Dev.          ',
 u'Standard Deviation of INST Pitch',
 u'INST Pitch             ']

In [30]:
filter(lambda x:re.search(r'roll',x.lower()), long_names)


Out[30]:
[u'Roll Std. Dev.             ',
 u'INST Roll             ',
 u'INST Roll',
 u'Standard Deviation of INST Roll']

In [31]:
# std dev
filter(lambda x:re.search(r'heading',x.lower()), long_names)


Out[31]:
[u'Median INST Heading             ',
 u'Heading Std. Dev.        ',
 u'INST Heading',
 u'INST Heading             ']

In [32]:
# std, dev, wave height spectra, barometric, presscheck
pres = filter(lambda x:re.search(r'press',x.lower()), long_names)
pres


Out[32]:
[u'STAND.DEV.(PRESS)',
 u'Pressure at Transducer Head',
 u'Pressure',
 u'PRESSURE (PASCALS)',
 u'PRESSURE (DB)          ',
 u'STAND. DEV. (PRESS)     ',
 u'AVERAGE BURST PRESSURE   ',
 u'Bottom Pressure (psia)           ',
 u'BAROMETRIC PRESSURE (MB) ',
 u'AVERAGE BURST PRESSURE  ',
 u'Pressure-derived Non-directional Wave Height Spectrum (mm/sqrt(Hz))',
 u'STAND. DEV. (PRESS)    ',
 u'INTERVAL PRESSURE       ',
 u'Barometric Pressure (mbar)',
 u'PRESS       ',
 u'Barometric Pressure (mb)',
 u'ADCP Pressure at Transducer Head',
 u'AVERAGE RELATIVE PRESSURE',
 u'PRESSURE            ',
 u'Raw pressure Std. Dev.',
 u'PRESSCHECK  ',
 u'INTERVAL PRESSURE        ',
 u'PRESSURE    ']

In [33]:
filter(lambda x:re.search(r'std|dev',x.lower()), pres)


Out[33]:
[u'STAND.DEV.(PRESS)',
 u'STAND. DEV. (PRESS)     ',
 u'STAND. DEV. (PRESS)    ',
 u'Raw pressure Std. Dev.']

In [34]:
# save only direction and current direction
filter(lambda x:re.search(r'direct',x.lower()), long_names)


Out[34]:
[u'beginning of first direction slice, degrees',
 u'Surface-derived Non-directional Wave Height Spectrum (mm/sqrt(Hz))',
 u'Direction (degrees)',
 u'Peak Wave Direction (degrees North)',
 u'Wind Direction (degrees true North)',
 u'V  DIRECTION',
 u'Peak Wave Direction (degrees true North)',
 u'CURRENT DIRECTION (T)      ',
 u'WIND DIRECTION           ',
 u'CURRENT DIRECTION (T)     ',
 u'Pressure-derived Non-directional Wave Height Spectrum (mm/sqrt(Hz))',
 u'WIND DIRECTION STABILITY',
 u'Mean Wave Direction (degrees true North)',
 u'Directional Wave Energy Spectrum (mm^2/Hz/degree)',
 u'Velocity-derived Non-directional Wave Height Spectrum (mm/sqrt(Hz))',
 u'mean wave direction',
 u'Direction (degrees T)',
 u'WIND DIRECTION        ',
 u'Current Direction',
 u'CURRENT DIRECTION (T)   ']

In [35]:
f = open('/usgs/data2/notebook/names.txt','w')
f.write("\n".join(names))
f.close()
f = open('/usgs/data2/notebook/long_names.txt','w')
f.write("\n".join(long_names))
f.close()
f = open('/usgs/data2/notebook/units.txt','w')
f.write("\n".join(units))
f.close()

In [35]:


In [36]:
pwd


Out[36]:
u'/usgs/data2/emontgomery/stellwagen/Data'

In [66]:
root_dir='/usgs/data2/emontgomery/stellwagen/Data/'
#root_dir='/usgs/data2/emontgomery/stellwagen/Data/MVCO_11'
odir='/usgs/data2/emontgomery/stellwagen/CF-1.6/'
os.chdir(root_dir)

In [79]:
types = ('*.nc', '*.cdf')
for index,row in project.iterrows():
    os.chdir(os.path.join(root_dir,index))
    ncfiles=[]
    for files in types:
        ncfiles.extend(glob.glob(files))
    print index, len(ncfiles)


ARGO_MERCHANT 6
BUZZ_BAY 81
CAMP 42
CAPE_COD_BAY 34
CC_MISC 2
DEEP_REEF 1
DWDS_106 16
ECOHAB_II 111
ECOHAB_I 86
EUROSTRATAFORM 4
FARALLONES 41
GB_SED 242
GLOBEC_GB 37
GLOBEC_GSC 42
GULF_MAINE 35
HUDSON_SVALLEY 167
KARIN_RIDGE 14
LYDONIA_C 230
MAB_SED 130
MAMALA_BAY 33
MBAY_CIRC 50
MBAY_IWAVE 31
MBAY_LTB 85
MBAY_LT 1416
MBAY_STELL 36
MBAY_WEST 29
MOBILE_BAY 98
MONTEREY_BAY 122
MONTEREY_CAN 33
MYRTLEBEACH 79
NE_SLOPE 117
OCEANOG_C 14
ORANGE_COUNTY 55
PONCHARTRAIN 35
PV_SHELF04 58
PV_SHELF07 71
PV_SHELF 87
SAB_SED 32
SOUTHERN_CAL 30
STRESS 108
WRIGHTSVILLE 13
DIAMONDSHOALS 39
CHANDELEUR 16
WFAL 28
BW2011 12
MVCO_11 13
HURRIRENE_BB 4

Read a sample old file


In [42]:
file = files[-1]
print file
nc = netCDF4.Dataset(file)


9111aqd-a.nc

In [43]:
vars=nc.variables.keys()

In [44]:
coord_vars = ['time','time2','depth','lat','lon']

In [45]:
# find data variables by removing coordinate variables from the variable list
data_vars = [var for var in vars if var not in coord_vars]
print data_vars


[u'bindist', u'u_1205', u'v_1206', u'w_1204', u'CS_300', u'CD_310', u'P_1', u'Tx_1211', u'AGC_1202', u'Hdg_1215', u'Ptch_1216', u'Roll_1217']

In [46]:
print nc.variables['u_1205']


<type 'netCDF4.Variable'>
float32 u_1205(time, depth, lat, lon)
    name: u
    long_name: Eastward Velocity
    feneric_name: u
    FORTRAN_format:  
    units: cm/s
    epic_code: 1205
    sensor_type: Nortek Aquadopp Profiler
    sensor_depth: 12.8568007307
    initial_sensor_height: 0.16
    height_depth_units: m
    serial_number: AQD 6409
    minimum: [ -37.79090881  -39.59309769  -39.48016739  -40.98865509  -41.56013489
  -42.0097084   -42.66938019  -42.61388397  -43.71333313  -41.31264114
  -46.47284317  -44.69533157  -47.77171707  -49.01902008  -52.09646988
  -54.16630173  -54.65877533  -58.35764313  -58.2725029   -57.30296707
  -72.47348022 -102.24491882 -108.71712494 -107.08737183  -92.03878784]
    maximum: [ 34.93043518  31.40140343  34.67355347  38.76890182  39.16253662
  42.82086945  45.19152069  42.90264511  47.63571548  47.8465271
  49.28278732  45.30502319  49.03020096  47.69023514  47.4201355
  45.45179367  45.16744232  44.54811096  45.65585709  47.87711334
  44.75069046  48.67462158  71.71102905  78.26560974  69.04908752]
    valid_range: []
    _FillValue: 1e+35
unlimited dimensions: time
current shape = (5214, 25, 1, 1)


In [47]:
nt = len(nc.dimensions['time'])
nz = len(nc.dimensions['depth'])
print nt,nz


5214 25

Write a sample "CF-1.6, timeSeries" File


In [48]:
ofile = '/usgs/data2/notebook/%s' % file
id = file.split('.')[0]
print id


9111aqd-a

In [49]:
# create dimensions
nco = netCDF4.Dataset(ofile,'w',clobber=True)
nco.createDimension('time',nt)
if nz > 1:
    nco.createDimension('depth',nz)

nchar=20
nco.createDimension('nchar',nchar)


Out[49]:
<netCDF4.Dimension at 0x3b1ac80>

In [50]:
# create coordinate variables
time_v = nco.createVariable('time', 'f8', ('time'))
lon_v = nco.createVariable('lon','f4')
lat_v = nco.createVariable('lat','f4')
if nz > 1:
    depth_v = nco.createVariable('depth','f4',dimensions='depth')
else:
    depth_v = nco.createVariable('depth','f4')

station_v = nco.createVariable('site','S1',('nchar'))

In [51]:
# write global attributes
g_attdict = nc.__dict__
g_attdict['Conventions'] = 'CF-1.6'
if nz>1:
    g_attdict['featureType'] = 'timeSeriesProfile'
else:
    g_attdict['featureType'] = 'timeSeries'

g_attdict['naming_authority'] = 'gov.usgs'
g_attdict['id'] = id
g_attdict['source'] = 'USGS'
g_attdict['institution'] = 'Woods Hole Coastal and Marine Science Center'
g_attdict['project'] = 'Coastal and Marine Geology Program'
g_attdict['title'] = g_attdict['source'] + g_attdict['id']
g_attdict['keywords']='Oceans > Ocean Pressure > Water Pressure, Oceans > Ocean Temperature > Water Temperature, Oceans > Salinity/Density > Conductivity, Oceans > Salinity/Density > Salinity'
g_attdict['keywords_vocabulary']='GCMD Science Keywords'
g_attdict['standard_name_vocabulary'] = 'CF-1.6'
g_attdict['creator_email'] = 'emontgomery@usgs.gov'
g_attdict['creator_name'] =  'Ellyn Montgomery'

nco.setncatts(g_attdict)

In [52]:
# write station variable
station_v.cf_role = 'timeseries_id'
station_v.standard_name = 'station_id'
data = numpy.empty((1,),'S'+repr(nchar))
data[0] = id
station_v[:] = netCDF4.stringtochar(data)

# write time variable
time_v.units = 'seconds since 1858-11-17 00:00:00 +0:00'
time_v.standard_name = 'time'
time_v.calendar = 'gregorian'
time_v[:] = (nc.variables['time'][:]-2400001.)*3600.*24. + nc.variables['time2'][:]/1000.

# write lon variable
lon_v.units = 'degree_east'
lon_v.standard_name = 'longitude'
lon_v[:] = nc.variables['lon'][:]

# write lat variable
lat_v.units = 'degree_north'
lat_v.standard_name = 'latitude'
lat_v[:] = nc.variables['lat'][:]

# write depth variable
depth_v.units = 'm'
depth_v.standard_name = 'depth'   
depth_v.positive = 'down'
depth_v.axis = 'Z'
depth_v[:] = nc.variables['depth'][:]

In [53]:
nc.variables['depth'][:]


Out[53]:
array([ 12.35680073,  11.85680073,  11.35680073,  10.85680073,
        10.35680073,   9.85680073,   9.35680073,   8.85680073,
         8.35680073,   7.85680073,   7.35680073,   6.85680073,
         6.35680073,   5.85680073,   5.35680073,   4.85680073,
         4.35680073,   3.85680073,   3.35680073,   2.85680073,
         2.35680073,   1.85680073,   1.35680073,   0.85680073,   0.35680073])

In [54]:
# create the data variables
var_v=[]
for varname in data_vars:
    ncvar = nc.variables[varname]
    # if time series variable
    if size(ncvar) == nt:
        var = nco.createVariable(varname,ncvar.dtype,('time'))
    elif size(ncvar) == nz:
        var = nco.createVariable(varname,ncvar.dtype,('depth'))
    else:
        var = nco.createVariable(varname,ncvar.dtype,('time','depth'))

#   load old variable attributes and modify if necessary 
    attdict = ncvar.__dict__
#   if dounpackshort and 'scale_factor' in attdict: del attdict['scale_factor']
  
    attdict['coordinates'] = 'time lon lat depth'

    # assign standard_name if in dictionary
    a =[k for (k, v) in d.iteritems() if attdict['long_name'].strip().lower() in v]
    if len(a)==1: attdict['standard_name']=a[0]
    
    # write variable attributes
    var.setncatts(attdict) 
    # write the data
#    print ncvar
    var[:] = ncvar[:]

In [55]:
nco.close()

In [27]:
root_dir='/usgs/data2/emontgomery/stellwagen/Data/MVCO_11'

In [30]:
cd /usgs/data2/emontgomery/stellwagen/Data/MVCO_11


/usgs/data2/emontgomery/stellwagen/Data/MVCO_11

In [31]:
ls


9102sc-a.nc         9104pcs-cal.cdf   9106advs-cal.cdf  9111aqd-a.nc
9103HRaqdb-cal.cdf  9105advb-cal.cdf  9107sc-a.nc
9103HRaqds-cal.cdf  9105advs-cal.cdf  9107sc-tst2.nc*
9104pcb-cal.cdf     9106advb-cal.cdf  9107sc-tst.nc*

In [32]:
nc = netCDF4.Dataset('9104pcs-cal.cdf')

In [34]:
for (k,v) in nc.variables.iteritems():
    try: 
        print v.long_name
    except:
        pass


Burst Number
EPIC SYSTEM TIME
msec since 0:00 GMT
DEPTH (m)
LONGITUDE
LATITUDE
Mean Eastward Velocity
Mean Northward Velocity
Mean Vertical Velocity
Std. Dev. of east component
Std. Dev. of north component
Std. Dev. of upward component
Minimum Eastward Velocity
Minimum Northward Velocity
Minimum Vertical Velocity
Maximum Eastward Velocity
Maximum Northward Velocity
Maximum Vertical Velocity
CURRENT SPEED (CM/S)      
CURRENT DIRECTION (T)     
Mean Resolution VelocityEastward
Mean Resolution VelocityNorthward
Mean Resolution VelocityVertical
Echo Intensity (AGC) Beam 1
Echo Intensity (AGC) Beam 2
Echo Intensity (AGC) Beam 3
 Beam 1 Correlation
 Beam 2 Correlation
 Beam 3 Correlation
Sensor Range to Boundary
Median INST Heading             
INST Pitch             
INST Roll             
Heading Std. Dev.        
Pitch Std. Dev.          
Roll Std. Dev.             
Transducer Temp.
Distance from transducer head to bin center
ATTENUATION 1
TRANSMISSION (VOLTS) #1
raw Backscattering 2
BACKSCATTER INTENSITY 2

In [36]:
a='FOOBAR   '

In [37]:
a.strip().lower()


Out[37]:
'foobar'

In [ ]: