All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ucar.nc2.ft.point.writer.WriterCFProfileCollection Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
 * See LICENSE for license information.
 */

package ucar.nc2.ft.point.writer;

import java.io.IOException;
import java.util.*;

import ucar.ma2.*;
import ucar.nc2.*;
import ucar.nc2.constants.CDM;
import ucar.nc2.constants.CF;
import ucar.nc2.dataset.conv.CF1Convention;
import ucar.nc2.ft.PointFeature;
import ucar.nc2.ft.ProfileFeature;
import ucar.nc2.time.CalendarDateUnit;

/**
 * Write a CF "Discrete Sample" profile collection file.
 * Example H.3.5. Contiguous ragged array representation of profiles, H.3.4
 *
 * 

*

 *   writeHeader()
 *   iterate { writeRecord() }
 *   finish()
 * 
* * @see "http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.6/cf-conventions.html#idp8372832" * @author caron * @since April, 2012 */ public class WriterCFProfileCollection extends CFPointWriter { /////////////////////////////////////////////////// // private Formatter coordNames = new Formatter(); protected Structure profileStruct; // used for netcdf4 extended private Map featureVarMap = new HashMap<>(); private boolean headerDone = false; public WriterCFProfileCollection(String fileOut, List globalAtts, List dataVars, CalendarDateUnit timeUnit, String altUnits, CFPointWriterConfig config) throws IOException { super(fileOut, globalAtts, dataVars, timeUnit, altUnits, config); writer.addGroupAttribute(null, new Attribute(CF.DSG_REPRESENTATION, "Contiguous ragged array representation of profiles, H.3.4")); writer.addGroupAttribute(null, new Attribute(CF.FEATURE_TYPE, CF.FeatureType.profile.name())); } public int writeProfile (ProfileFeature profile) throws IOException { int count = 0; for (PointFeature pf : profile) { if (!headerDone) { if (id_strlen == 0) id_strlen = profile.getName().length() * 2; writeHeader(profile, pf); headerDone = true; } writeObsData(pf); count++; } writeProfileData(profile, count); return count; } private void writeHeader(ProfileFeature profile, PointFeature obs) throws IOException { Formatter coordNames = new Formatter().format("%s %s %s", profileTimeName, latName, lonName); List coords = new ArrayList<>(); if (useAlt) { coords.add( VariableSimpleImpl.makeScalar(altitudeCoordinateName, "obs altitude", altUnits, DataType.DOUBLE) .add(new Attribute(CF.STANDARD_NAME, "altitude")) .add(new Attribute(CF.POSITIVE, CF1Convention.getZisPositive(altitudeCoordinateName, altUnits)))); coordNames.format(" %s", altitudeCoordinateName); } super.writeHeader(coords, profile.getFeatureData(), obs.getFeatureData(), coordNames.toString()); } protected void makeFeatureVariables(StructureData featureData, boolean isExtended) throws IOException { // LOOK why not unlimited here ? Dimension profileDim = writer.addDimension(null, profileDimName, nfeatures); // Dimension profileDim = isExtendedModel ? writer.addUnlimitedDimension(profileDimName) : writer.addDimension(null, profileDimName, nprofiles); // add the profile Variables using the profile dimension List profileVars = new ArrayList<>(); profileVars.add(VariableSimpleImpl.makeScalar(latName, "profile latitude", CDM.LAT_UNITS, DataType.DOUBLE)); profileVars.add(VariableSimpleImpl.makeScalar(lonName, "profile longitude", CDM.LON_UNITS, DataType.DOUBLE)); profileVars.add(VariableSimpleImpl.makeString(profileIdName, "profile identifier", null, id_strlen) .add(new Attribute(CF.CF_ROLE, CF.PROFILE_ID))); // profileId:cf_role = "profile_id"; profileVars.add(VariableSimpleImpl.makeScalar(numberOfObsName, "number of obs for this profile", null, DataType.INT) .add(new Attribute(CF.SAMPLE_DIMENSION, recordDimName))); // rowSize:sample_dimension = "obs" profileVars.add(VariableSimpleImpl.makeScalar(profileTimeName, "nominal time of profile", timeUnit.getUdUnit(), DataType.DOUBLE) .add(new Attribute(CF.CALENDAR, timeUnit.getCalendar().toString()))); for (StructureMembers.Member m : featureData.getMembers()) { VariableSimpleIF dv = getDataVar(m.getName()); if (dv != null) profileVars.add(dv); } if (isExtended) { profileStruct = (Structure) writer.addVariable(null, profileStructName, DataType.STRUCTURE, profileDimName); addCoordinatesExtended(profileStruct, profileVars); } else { addCoordinatesClassic(profileDim, profileVars, featureVarMap); } } private int profileRecno = 0; public void writeProfileData(ProfileFeature profile, int nobs) throws IOException { trackBB(profile.getLatLon(), profile.getTime()); StructureDataScalar profileCoords = new StructureDataScalar("Coords"); profileCoords.addMember(latName, null, null, DataType.DOUBLE, profile.getLatLon().getLatitude()); profileCoords.addMember(lonName, null, null, DataType.DOUBLE, profile.getLatLon().getLongitude()); if (profile.getTime() != null) // LOOK time not always part of profile profileCoords.addMember(profileTimeName, null, null, DataType.DOUBLE, timeUnit.makeOffsetFromRefDate(profile.getTime())); profileCoords.addMemberString(profileIdName, null, null, profile.getName().trim(), id_strlen); profileCoords.addMember(numberOfObsName, null, null, DataType.INT, nobs); StructureData profileData = profile.getFeatureData(); StructureDataComposite sdall = new StructureDataComposite(); sdall.add(profileCoords); // coords first so it takes precedence sdall.add(profileData); profileRecno = super.writeStructureData(profileRecno, profileStruct, sdall, featureVarMap); } private int obsRecno = 0; public void writeObsData(PointFeature pf) throws IOException { StructureDataScalar coords = new StructureDataScalar("Coords"); if (useAlt) coords.addMember(altitudeCoordinateName, null, null, DataType.DOUBLE, pf.getLocation().getAltitude()); StructureDataComposite sdall = new StructureDataComposite(); sdall.add(coords); // coords first so it takes precedence sdall.add(pf.getFeatureData()); obsRecno = super.writeStructureData(obsRecno, record, sdall, dataMap); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy