Пример #1
0
function snowpilot_node_write_caaml($node)
{
    $unit_prefs = snowpilot_unit_prefs_get($node, 'node');
    $account = user_load($node->uid);
    $snowpilot_caaml = new DOMDocument('1.0', 'UTF-8');
    $snowpilot_SnowProfile = $snowpilot_caaml->createElement('SnowProfile');
    $snowpilot_caaml->appendChild($snowpilot_SnowProfile);
    $snowpilot_SnowProfile->setAttributeNS('http://www.snowpilot.org', 'app:NodeID', $node->nid);
    $snowpilot_SnowProfile->setAttributeNS('http://www.opengis.net/gml', 'gml:id', 'test');
    $snowpilot_metaDataProperty = $snowpilot_caaml->createElement('metaDataProperty');
    $snowpilot_SnowProfile->appendChild($snowpilot_metaDataProperty);
    $snowpilot_validTime = $snowpilot_caaml->createElement('validTime');
    $snowpilot_SnowProfile->appendChild($snowpilot_validTime);
    $snowpilot_snowProfileResultsOf = $snowpilot_caaml->createElement('snowProfileResultsOf');
    $snowpilot_SnowProfile->appendChild($snowpilot_snowProfileResultsOf);
    $snowpilot_locRef = $snowpilot_caaml->createElement('locRef');
    $snowpilot_SnowProfile->appendChild($snowpilot_locRef);
    //
    //  MetaDataProperty Object
    //
    $MetaData = $snowpilot_caaml->createElement('MetaData');
    $MetaData->appendChild($snowpilot_caaml->createElement('dateTimeReport', date('c', $node->created)));
    $srcRef = $snowpilot_caaml->createElement('srcRef');
    $Operation = $snowpilot_caaml->createElement('Operation');
    $Operation->appendChild($snowpilot_caaml->createElement('name', taxonomy_term_load($account->field_professional_affiliation['und'][0]['tid'])->name));
    $contactPerson = $snowpilot_caaml->createElement('contactPerson');
    $Person = $snowpilot_caaml->createElement('Person');
    $Person->appendChild($snowpilot_caaml->createElement('name', $account->name));
    $contactPerson->appendChild($Person);
    $Operation->appendChild($contactPerson);
    $srcRef->appendChild($Operation);
    $MetaData->appendChild($srcRef);
    $customData = $snowpilot_caaml->createElement('customData');
    $customData->appendChild($snowpilot_caaml->createElement('app:projectID', 'Snowpilot'));
    $MetaData->appendChild($customData);
    $snowpilot_metaDataProperty->appendChild($MetaData);
    // End of $snowpilot_metaDataProperty
    // Observation Time
    //
    $TimeInstant = $snowpilot_caaml->createElement('TimeInstant');
    $TimeInstant->appendChild($snowpilot_caaml->createElement('timePosition', date('c', strtotime($node->field_date_time['und'][0]['value']))));
    $snowpilot_validTime->appendChild($TimeInstant);
    //
    //
    // locRef - location reference
    $ObsPoint = $snowpilot_caaml->createElement('ObsPoint');
    $ObsPoint->appendChild($snowpilot_caaml->createElement('name', $node->title));
    $ObsPoint->appendChild($snowpilot_caaml->createElement('obsPointSubType', 'Snowprofile Site'));
    $validElevation = $snowpilot_caaml->createElement('validElevation');
    $ElevationPosition = $snowpilot_caaml->createElement('ElevationPosition');
    $ElevationPosition->setAttribute('uom', $unit_prefs['field_elevation_units']);
    $ElevationPosition->appendChild($snowpilot_caaml->createElement('position', $node->field_elevation['und'][0]['value']));
    $validElevation->appendChild($ElevationPosition);
    $ObsPoint->appendChild($validElevation);
    $validAspect = $snowpilot_caaml->createElement('validAspect');
    $AspectPosition = $snowpilot_caaml->createElement('AspectPosition');
    $AspectPosition->appendChild($snowpilot_caaml->createElement('position', snowpilot_cardinal_wind_dir($node->field_aspect['und'][0]['value'])));
    $validAspect->appendChild($AspectPosition);
    $ObsPoint->appendChild($validAspect);
    $validSlopeAngle = $snowpilot_caaml->createElement('validSlopeAngle');
    $slopeAnglePosition = $snowpilot_caaml->createElement('slopeAnglePosition');
    $slopeAnglePosition->setAttribute('uom', 'deg');
    $slopeAnglePosition->appendChild($snowpilot_caaml->createElement('position', $node->field_slope_angle['und'][0]['value']));
    $validSlopeAngle->appendChild($slopeAnglePosition);
    $ObsPoint->appendChild($validSlopeAngle);
    $pointLocation = $snowpilot_caaml->createElement('pointLocation');
    $gmlPoint = $snowpilot_caaml->createElement('gml:Point');
    $gmlPoint->setAttribute('srsDimension', '2');
    $gmlPoint->setAttribute('srsname', 'urn:ogc:def:crs:OGC:1.3:CRS84');
    $gmlPoint->appendChild($snowpilot_caaml->createElement('gml:pos', $node->field_latitude['und'][0]['value'] . ' ' . $node->field_longitude['und'][0]['value']));
    $pointLocation->appendChild($gmlPoint);
    $ObsPoint->appendChild($pointLocation);
    $snowpilot_locRef->appendChild($ObsPoint);
    //
    // End Location Reference
    // Beginning snow Profile resultsof
    $SnowProfileMeasurements = $snowpilot_caaml->createElement('SnowProfileMeasurements');
    $SnowProfileMeasurements->setAttribute('dir', $unit_prefs['measureFrom'] == 'top' ? 'top down' : 'bottom up');
    $SnowProfileMeasurements->appendChild($snowpilot_caaml->createElement('comment', $node->body['und'][0]['safe_value']));
    $profileDepth = $snowpilot_caaml->createElement('profileDepth', _snowpilot_find_pit_depth($node));
    $profileDepth->setAttribute('uom', $unit_prefs['field_depth_units']);
    $SnowProfileMeasurements->appendChild($profileDepth);
    $SnowProfileMeasurements->appendChild($snowpilot_caaml->createElement('skyCond', $node->field_sky_cover['und'][0]['value']));
    $SnowProfileMeasurements->appendChild($snowpilot_caaml->createElement('precipTI', isset($node->field_precipitation['und'][0]['value']) ? $node->field_precipitation['und'][0]['value'] : ''));
    // this is not CAAML formatted
    $windSpd = $snowpilot_caaml->createElement('windSpd', $node->field_wind_speed['und'][0]['value']);
    // the values of this too, need to be set in the drupal db
    $windSpd->setAttribute('uom', '');
    $SnowProfileMeasurements->appendChild($windSpd);
    //wind Direction
    $winddir = $snowpilot_caaml->createElement('windDir');
    $windDirAspectPosition = $snowpilot_caaml->createElement('AspectPosition');
    $windDirAspectPosition->appendChild($snowpilot_caaml->createElement('position', snowpilot_cardinal_wind_dir($node->field_wind_direction['und'][0]['value'])));
    $winddir->appendChild($windDirAspectPosition);
    $SnowProfileMeasurements->appendChild($winddir);
    // HoS
    $heightOfSnowpack = $snowpilot_caaml->createElement('hS');
    $hsComponents = $snowpilot_caaml->createElement('Components');
    $snowHeight = $snowpilot_caaml->createElement('snowHeight', isset($node->field_total_height_of_snowpack['und'][0]['value']) ? $node->field_total_height_of_snowpack['und'][0]['value'] : '');
    $snowHeight->setAttribute('uom', $unit_prefs['field_depth_units']);
    $hsComponents->appendChild($snowHeight);
    $heightOfSnowpack->appendChild($hsComponents);
    $SnowProfileMeasurements->appendChild($heightOfSnowpack);
    // Ski Penetration
    if (isset($node->field_surface_penetration['und'][0]['value']) && $node->field_surface_penetration['und'][0]['value'] == 'ski') {
        $penetration = $snowpilot_caaml->createElement('penetrationSki', $node->field_ski_penetration['und'][0]['value']);
    } elseif (isset($node->field_surface_penetration['und'][0]['value']) && $node->field_surface_penetration['und'][0]['value'] == 'boot') {
        $penetration = $snowpilot_caaml->createElement('penetrationFoot', isset($node->field_boot_penetration_depth['und'][0]['value']) ? $node->field_boot_penetration_depth['und'][0]['value'] : '');
    }
    if (isset($penetration)) {
        $penetration->setAttribute('uom', $unit_prefs['field_depth_units']);
        $SnowProfileMeasurements->appendChild($penetration);
    }
    $ids = array();
    foreach ($node->field_layer['und'] as $lay) {
        $ids[] = $lay['value'];
    }
    $all_layers = field_collection_item_load_multiple($ids);
    $stratProfile = $snowpilot_caaml->createElement('stratProfile');
    foreach ($all_layers as $x => $layer) {
        $Layer = $snowpilot_caaml->createElement('Layer');
        $Layer->appendChild($depthTop = $snowpilot_caaml->createElement('depthTop', $layer->field_height['und'][0]['value']));
        $depthTop->setAttribute('uom', $unit_prefs['field_depth_units']);
        $Layer->appendChild($thickness = $snowpilot_caaml->createElement('thickness', abs($layer->field_height['und'][0]['value'] - $layer->field_bottom_depth['und'][0]['value'])));
        $thickness->setAttribute('uom', $unit_prefs['field_depth_units']);
        if (isset($layer->field_grain_type['und'])) {
            $Layer->appendChild($snowpilot_caaml->createElement('grainFormPrimary', taxonomy_term_load($layer->field_grain_type['und'][0]['tid'])->description));
            // this needs to be changed to description as soon as the particle abbreviations are populated into the grain types taxonomy 'description' field. IT also breaks whenever there is a & in a name
        }
        if (isset($layer->field_grain_type_secondary['und'])) {
            $Layer->appendChild($snowpilot_caaml->createElement('grainFormSecondary', taxonomy_term_load($layer->field_grain_type_secondary['und'][0]['tid'])->description));
            // this needs to be changed to description as soon as the particle abbreviations are populated into the grain types taxonomy 'description' field. IT also breaks whenever there is a & in a name
        }
        //
        // Grain size
        if (isset($layer->field_grain_size['und'])) {
            $grainSize = $snowpilot_caaml->createElement('grainSize');
            $grainSize->setAttribute('uom', 'mm');
            $GSComponents = $snowpilot_caaml->createElement('Components');
            $GSComponents->appendChild($snowpilot_caaml->createElement('avg', $layer->field_grain_size['und'][0]['value']));
            if (isset($layer->field_grain_size_max['und'])) {
                $GSComponents->appendChild($snowpilot_caaml->createElement('avgMax', $layer->field_grain_size_max['und'][0]['value']));
            }
            $grainSize->appendChild($GSComponents);
            $Layer->appendChild($grainSize);
        }
        //
        // Hardness
        if (isset($layer->field_hardness['und'])) {
            $Layer->appendChild($hardness = $snowpilot_caaml->createElement('hardness', $layer->field_hardness['und'][0]['value']));
            $hardness->setAttribute('uom', '');
        }
        if (isset($layer->field_hardness2['und'])) {
            $Layer->appendChild($hardness2 = $snowpilot_caaml->createElement('hardness', $layer->field_hardness2['und'][0]['value']));
            $hardness2->setAttribute('uom', '');
        }
        //
        // Water content
        if (isset($layer->field_water_content['und'])) {
            $Layer->appendChild($lwc = $snowpilot_caaml->createElement('lwc', $layer->field_water_content['und'][0]['value']));
            $lwc->setAttribute('uom', '');
        }
        $stratProfile->appendChild($Layer);
    }
    $SnowProfileMeasurements->appendChild($stratProfile);
    //
    // Termperature profile
    $ids = array();
    foreach ($node->field_temp_collection['und'] as $temp) {
        $ids[] = $temp['value'];
    }
    $all_temps = field_collection_item_load_multiple($ids);
    $tempProfile = $snowpilot_caaml->createElement('tempProfile');
    $tempProfile->setAttribute('uomDepth', $unit_prefs['field_depth_units']);
    $tempProfile->setAttribute('uomTemp', 'deg' . $unit_prefs['field_temp_units']);
    foreach ($all_temps as $temp) {
        if (isset($temp->field_depth['und'])) {
            $Obs = $snowpilot_caaml->createElement('Obs');
            $Obs->appendChild($snowpilot_caaml->createElement('depth', $temp->field_depth['und'][0]['value']));
            $Obs->appendChild($snowpilot_caaml->createElement('snowTemp', $temp->field_temp_temp['und'][0]['value']));
        }
        $tempProfile->appendChild($Obs);
    }
    $SnowProfileMeasurements->appendChild($tempProfile);
    //
    //  Denstiy Profile
    //
    $ids = array();
    foreach ($node->field_density_profile['und'] as $dens) {
        $ids[] = $dens['value'];
    }
    $all_densities = field_collection_item_load_multiple($ids);
    $densityProfile = $snowpilot_caaml->createElement('densityProfile');
    $densityProfile->setAttribute('uomDepthTop', $unit_prefs['field_depth_units']);
    $densityProfile->setAttribute('uomThickness', $unit_prefs['field_depth_units']);
    $densityProfile->setAttribute('uomDensity', $unit_prefs['field_density_units']);
    foreach ($all_densities as $density) {
        $Layer = $snowpilot_caaml->createElement('Layer');
        $Layer->appendChild($snowpilot_caaml->createElement('depthTop', $density->field_depth['und'][0]['value']));
        $Layer->appendChild($snowpilot_caaml->createElement('thickness', ''));
        $Layer->appendChild($snowpilot_caaml->createElement('density', $density->field_density_top['und'][0]['value']));
        $densityProfile->appendChild($Layer);
    }
    $SnowProfileMeasurements->appendChild($densityProfile);
    //
    //  stability Tests
    //
    $ids = array();
    foreach ($node->field_test['und'] as $test) {
        $ids[] = $test['value'];
    }
    $shear_tests = field_collection_item_load_multiple($ids);
    $stbTests = $snowpilot_caaml->createElement('stbTests');
    foreach ($shear_tests as $shear_test) {
        switch ($shear_test->field_stability_test_type['und'][0]['value']) {
            case 'CT':
                $ComprTest = $snowpilot_caaml->createElement('ComprTest');
                if ($shear_test->field_stability_test_score['und'][0]['value'] != 'CTN') {
                    $ComprTest->appendChild($failedOn = $snowpilot_caaml->createElement('failedOn'));
                    $Layer = $snowpilot_caaml->createElement('Layer');
                    $Layer->appendChild($depthTop = $snowpilot_caaml->createElement('depthTop', $shear_test->field_depth['und'][0]['value']));
                    $depthTop->setAttribute('uom', $unit_prefs['field_depth_units']);
                    $failedOn->appendChild($Layer);
                    $Results = $snowpilot_caaml->createElement('Results');
                    if (isset($shear_test->field_fracture_character['und'])) {
                        $Results->appendChild($snowpilot_caaml->createElement('fractureCharacter', $shear_test->field_fracture_character['und'][0]['value']));
                    }
                    if (isset($shear_test->field_shear_quality['und'])) {
                        $Results->appendChild($snowpilot_caaml->createElement('fractureQuality', $shear_test->field_shear_quality['und'][0]['value']));
                    }
                    if (isset($shear_test->field_stability_test_score_ct['und'])) {
                        $Results->appendChild($snowpilot_caaml->createElement('testScore', $shear_test->field_ct_score['und'][0]['value']));
                    } else {
                        $Results->appendChild($snowpilot_caaml->createElement('testScore', $shear_test->field_stability_test_score_ct['und'][0]['value']));
                    }
                    $failedOn->appendChild($Results);
                } else {
                    $ComprTest->appendChild($snowpilot_caaml->createElement('noFailure'));
                }
                $stbTests->appendChild($ComprTest);
                break;
            case 'ECT':
                $ExtColumnTest = $snowpilot_caaml->createElement('ExtColumnTest');
                if ($shear_test->field_stability_test_score['und'][0]['value'] != 'ECTX') {
                    $failedOn = $snowpilot_caaml->createElement('failedOn');
                    $Layer = $snowpilot_caaml->createElement('Layer');
                    $Layer->appendChild($depthTop = $snowpilot_caaml->createElement('depthTop', $shear_test->field_depth['und'][0]['value']));
                    $depthTop->setAttribute('uom', $unit_prefs['field_depth_units']);
                    $failedOn->appendChild($Layer);
                    $Results = $snowpilot_caaml->createElement('Results');
                    if (isset($shear_test->field_fracture_character['und'])) {
                        $Results->appendChild($snowpilot_caaml->createElement('fractureCharacter', $shear_test->field_fracture_character['und'][0]['value']));
                    }
                    if (isset($shear_test->field_shear_quality['und'])) {
                        $Results->appendChild($snowpilot_caaml->createElement('fractureQuality', $shear_test->field_shear_quality['und'][0]['value']));
                    }
                    if (isset($shear_test->field_ec_score['und'])) {
                        $Results->appendChild($snowpilot_caaml->createElement('testScore', $shear_test->field_ec_score['und'][0]['value']));
                    } else {
                        $Results->appendChild($snowpilot_caaml->createElement('testScore', $shear_test->field_stability_test_score_ect['und'][0]['value']));
                    }
                    $failedOn->appendChild($Results);
                    $ExtColumnTest->appendChild($failedOn);
                    //
                    //
                } else {
                    $ExtColumnTest->appendChild($snowpilot_caaml->createElement('noFailure'));
                }
                $stbTests->appendChild($ExtColumnTest);
                break;
        }
    }
    $SnowProfileMeasurements->appendChild($stbTests);
    $snowpilot_snowProfileResultsOf->appendChild($SnowProfileMeasurements);
    $outXML = $snowpilot_caaml->saveXML();
    $formatted_xml = new DOMDocument('1.0', 'UTF-8');
    $formatted_xml->preserveWhiteSpace = false;
    $formatted_xml->formatOutput = true;
    $formatted_xml->loadXML($outXML);
    $final_xml = $formatted_xml->saveXML();
    //dsm($final_xml);
    return $final_xml;
}
Пример #2
0
function snowpilot_snowpit_graph_header_write($node, $format = 'jpg')
{
    // also add user account info to this:
    $user_account = user_load($node->uid);
    $snowpit_unit_prefs = snowpilot_unit_prefs_get($node, 'node');
    $pit_depth = _snowpilot_find_pit_depth($node);
    // Image Variables
    $width = 994;
    $height = 840;
    //imageloadfont()
    // Create GD Image
    $img = imagecreatetruecolor($width, $height);
    // Assign some colors
    $black = imagecolorallocate($img, 0, 0, 0);
    $white = imagecolorallocate($img, 255, 255, 255);
    $purple_layer = imagecolorallocate($img, 154, 153, 213);
    $red_layer = imagecolorallocate($img, 178, 36, 35);
    $blue_outline = imagecolorallocate($img, 15, 8, 166);
    $pink_problem = imagecolorallocate($img, 254, 240, 240);
    // Set background color to white
    imagefill($img, 0, 0, $white);
    $label_font = '/sites/all/libraries/fonts/Arial.ttf';
    $value_font = '/sites/all/libraries/fonts/Arial Bold.ttf';
    $snowsymbols_font = '/sites/all/libraries/fonts/ArialMT28.ttf';
    // Label Y axis and draw horizontal lines
    imagettftext($img, 11, 0, 14, 17, $black, $value_font, $node->title);
    // Location information
    if (isset($node->field_loaction['und'][0]['tid'])) {
        $term_obj_region = taxonomy_term_load($node->field_loaction['und'][0]['tid']);
        imagettftext($img, 11, 0, 14, 53, $black, $value_font, $term_obj_region->name);
        if (isset($node->field_loaction['und'][1]['tid'])) {
            $term_obj_region = taxonomy_term_load($node->field_loaction['und'][1]['tid']);
            imagettftext($img, 11, 0, 14, 35, $black, $value_font, $term_obj_region->name);
        }
    }
    $text_pos = imagettftext($img, 11, 0, 14, 71, $black, $label_font, 'Elevation: ');
    if (isset($node->field_elevation['und'])) {
        imagettftext($img, 11, 0, $text_pos[2], 71, $black, $value_font, $node->field_elevation['und'][0]['value'] . ' ' . $node->field_elevation_units['und'][0]['value']);
    }
    $text_pos = imagettftext($img, 11, 0, 14, 89, $black, $label_font, 'Aspect: ');
    if (isset($node->field_aspect['und'])) {
        $aspect = field_view_field('node', $node, 'field_aspect');
        imagettftext($img, 11, 0, $text_pos[2], 89, $black, $value_font, $aspect[0]['#markup']);
    }
    $text_pos = imagettftext($img, 11, 0, 14, 107, $black, $label_font, 'Specifics: ');
    $specifics = _generate_specifics_string($node);
    imagettftext($img, 9, 0, $text_pos[2], 107, $black, $value_font, $specifics);
    // Observer
    imagettftext($img, 11, 0, 183, 17, $black, $value_font, $user_account->field_first_name['und'][0]['value'] . " " . $user_account->field_last_name['und'][0]['value']);
    imagettftext($img, 11, 0, 183, 35, $black, $value_font, date('D M j H:i Y (T) ', strtotime($node->field_date_time['und'][0]['value'] . " " . $node->field_date_time['und'][0]['timezone_db'])));
    //Date / Time of observation
    $text_pos = imagettftext($img, 11, 0, 183, 53, $black, $label_font, "Co-ord: ");
    if ($snowpit_unit_prefs['field_coordinate_type'] != 'UTM') {
        if (isset($node->field_latitude['und']) && isset($node->field_longitude['und'])) {
            imagettftext($img, 11, 0, $text_pos[2], 53, $black, $value_font, number_format($node->field_latitude['und'][0]['value'], 5) . $node->field_latitude_type['und'][0]['value'] . ", " . number_format($node->field_longitude['und'][0]['value'], 5) . $node->field_longitude_type['und'][0]['value']);
        }
    } else {
        // Not Lat long, their preference is UTM
        if (isset($node->field_east['und']) && isset($node->field_north['und'])) {
            imagettftext($img, 11, 0, $text_pos[2], 53, $black, $value_font, $node->field_utm_zone['und'][0]['value'] . ' ' . $node->field_east['und'][0]['value'] . $node->field_longitude_type['und'][0]['value'] . ' ' . $node->field_north['und'][0]['value'] . $node->field_latitude_type['und'][0]['value']);
        }
    }
    $text_pos = imagettftext($img, 11, 0, 183, 71, $black, $label_font, "Slope Angle: ");
    if (isset($node->field_slope_angle['und'])) {
        $slope_angle = field_view_field('node', $node, 'field_slope_angle');
        imagettftext($img, 11, 0, $text_pos[2], 71, $black, $value_font, $slope_angle[0]['#markup']);
    }
    $text_pos = imagettftext($img, 11, 0, 183, 89, $black, $label_font, "Wind Loading: ");
    if (isset($node->field_wind_loading['und'])) {
        imagettftext($img, 11, 0, $text_pos[2], 89, $black, $value_font, $node->field_wind_loading['und'][0]['value']);
    }
    $text_pos = imagettftext($img, 11, 0, 429, 17, $black, $label_font, "Stability: ");
    if (isset($node->field_stability_on_similar_slope['und'])) {
        $similar_stability = field_view_field('node', $node, 'field_stability_on_similar_slope');
        imagettftext($img, 11, 0, $text_pos[2], 17, $black, $value_font, $similar_stability[0]['#markup']);
    }
    $text_pos = imagettftext($img, 11, 0, 429, 35, $black, $label_font, "Air Temperature: ");
    if (isset($node->field_air_temp['und'])) {
        $air_temp = field_view_field('node', $node, 'field_air_temp');
        imagettftext($img, 11, 0, $text_pos[2], 35, $black, $value_font, $air_temp[0]['#markup'] . "°" . $snowpit_unit_prefs['field_temp_units']);
    }
    $text_pos = imagettftext($img, 11, 0, 429, 53, $black, $label_font, "Sky Cover: ");
    if (isset($node->field_sky_cover['und'])) {
        $sky_cover = field_view_field('node', $node, 'field_sky_cover');
        imagettftext($img, 11, 0, $text_pos[2], 53, $black, $value_font, html_entity_decode($sky_cover[0]['#markup']));
    }
    $text_pos = imagettftext($img, 11, 0, 429, 71, $black, $label_font, "Precipitation: ");
    if (isset($node->field_precipitation['und'])) {
        $precipitation = field_view_field('node', $node, 'field_precipitation');
        imagettftext($img, 11, 0, $text_pos[2], 71, $black, $value_font, $precipitation[0]['#markup']);
    }
    $text_pos = imagettftext($img, 11, 0, 429, 89, $black, $label_font, "Wind: ");
    if (isset($node->field_wind_direction['und'][0]['value'])) {
        $text_pos = imagettftext($img, 11, 0, $text_pos[2] + 4, 89, $black, $value_font, snowpilot_cardinal_wind_dir($node->field_wind_direction['und'][0]['value']));
    }
    if (isset($node->field_wind_speed['und'][0]['value'])) {
        $wind_speed = field_view_field('node', $node, 'field_wind_speed');
        imagettftext($img, 11, 0, $text_pos[2], 89, $black, $value_font, " " . $wind_speed[0]['#markup']);
    }
    imagettftext($img, 11, 0, 805, 17, $black, $label_font, 'Layer Notes');
    $textpos = imagettftext($img, 11, 0, 14, 779, $black, $label_font, 'Notes: ');
    if (isset($node->body['und'][0]) && $node->body['und'][0]['safe_value'] != '') {
        $notes_lines = _output_formatted_notes($node->body['und'][0]['safe_value'], $value_font);
        foreach ($notes_lines as $x => $line) {
            if ($x <= 3) {
                imagettftext($img, 9, 0, $textpos[2], 779 + $x * 19, $black, $value_font, $line);
            } else {
                imagettftext($img, 9, 0, 870, 779 + 3 * 19, $red_layer, $value_font, "[ ... more notes ]");
                break;
            }
        }
    }
    //  write stability tests column and comments
    //  TODO : expand into its own function
    $comment_count = 0;
    $textpos = array();
    if (isset($node->field_total_height_of_snowpack['und'][0]['value'])) {
        $textpos = imagettftext($img, 9, 0, 645, 17, $black, $value_font, 'HS' . $node->field_total_height_of_snowpack['und'][0]['value']);
        $comment_count = 1;
    }
    if (isset($node->field_surface_penetration['und']) && $node->field_surface_penetration['und'][0]['value'] == 'boot' && isset($node->field_boot_penetration_depth['und']) && $node->field_boot_penetration_depth['und'][0]['value'] != '') {
        $xpos = count($textpos) ? $textpos[2] + 5 : 645;
        imagettftext($img, 9, 0, $xpos, 17, $black, $value_font, 'PF' . $node->field_boot_penetration_depth['und'][0]['value']);
        $comment_count = 1;
    } elseif (isset($node->field_surface_penetration['und']) && isset($node->field_ski_penetration['und'][0]['value']) && $node->field_surface_penetration['und'][0]['value'] == 'ski' && $node->field_ski_penetration['und'][0]['value'] != '') {
        $xpos = count($textpos) ? $textpos[2] + 5 : 645;
        imagettftext($img, 9, 0, $xpos, 17, $black, $value_font, 'SP' . $node->field_ski_penetration['und'][0]['value']);
        $comment_count = 1;
    }
    if (isset($node->field_test['und'])) {
        $ids = array();
        foreach ($node->field_test['und'] as $test) {
            $ids[] = $test['value'];
        }
        $test_results = field_collection_item_load_multiple($ids);
        // Here we go ahead and set a value for no-release type test results: ECTX, CTN, etc
        foreach ($test_results as $test) {
            if (!isset($test->field_depth['und'][0]['value'])) {
                $test->field_depth['und'][0]['value'] = $snowpit_unit_prefs['field_depth_0_from'] == 'top' ? $pit_depth : 0;
            }
        }
        uasort($test_results, 'depth_val');
        //
        // reversing the order of the test results makes it work when outputting the Stability test results on the graph when measuring from top
        if ($snowpit_unit_prefs['field_depth_0_from'] == 'top') {
            $test_results = array_reverse($test_results);
        }
        $bak = _set_stability_test_pixel_depths($test_results, $pit_depth, $snowpit_unit_prefs['field_depth_0_from']);
        // this sets a $test->y_position = integer which is where the line and text should go in the column on the right
        imagettftext($img, 11, 0, 645, $comment_count * 18 + 17, $black, $label_font, 'Stability Test Notes');
        foreach ($test_results as $x => $test) {
            if (isset($test->field_stability_test_type['und'][0]['value']) && isset($test->y_position)) {
                if (isset($test->y_position)) {
                    // if this has been 'multipled' with another stb test, the y_position won't be set
                    imageline($img, 707, $test->y_position, 941, $test->y_position, $black);
                    imagettftext($img, 9, 0, 712, $test->y_position - 5, $black, $label_font, stability_test_score_shorthand($test, $snowpit_unit_prefs));
                }
                if (count($test->field_stability_comments)) {
                    if ($comment_count < 5) {
                        $test_depth = isset($test->field_depth['und'][0]['value']) ? $test->field_depth['und'][0]['value'] + 0 : 0;
                        imagettftext($img, 9, 0, 645, $comment_count * 13 + 35, $black, $value_font, $test_depth . ': ' . $test->field_stability_comments['und'][0]['safe_value']);
                        $comment_count++;
                    } else {
                        imagettftext($img, 7, 0, 645, $comment_count * 13 + 31, $red_layer, $label_font, '[ More Stability Test Notes ... ]');
                    }
                }
            }
        }
    }
    // end stability test column
    // write rho column info
    //
    if (isset($node->field_density_profile['und'])) {
        foreach ($node->field_density_profile['und'] as $x => $density_item) {
            $density = field_collection_item_load($density_item['value']);
            // this use of imageline will need to be updated to include some kind of cluster management
            imageline($img, 667, snowpit_graph_pixel_depth($density->field_depth['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']), 707, snowpit_graph_pixel_depth($density->field_depth['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']), $black);
            imagettftext($img, 8, 0, 671, snowpit_graph_pixel_depth($density->field_depth['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']) + 12, $black, $label_font, $density->field_density_top['und'][0]['value']);
        }
    }
    //
    //  Prep for the 2 Cycles through layers
    //
    if (isset($node->field_layer['und'])) {
        $ids = array();
        foreach ($node->field_layer['und'] as $lay) {
            $ids[] = $lay['value'];
        }
        $all_layers = field_collection_item_load_multiple($ids);
        foreach ($all_layers as $x => $layer) {
            if ($snowpit_unit_prefs['field_depth_0_from'] == 'top') {
                $layer->y_val_top = $y_val_top = round(snowpit_graph_pixel_depth($layer->field_bottom_depth['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']));
                $layer->y_val = $y_val = round(snowpit_graph_pixel_depth($layer->field_height['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']));
            } else {
                $layer->y_val = $y_val = round(snowpit_graph_pixel_depth($layer->field_bottom_depth['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']));
                $layer->y_val_top = $y_val_top = round(snowpit_graph_pixel_depth($layer->field_height['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']));
            }
        }
        $keyed_all_layers = $all_layers;
        snowpilot_layers_density_xlate($keyed_all_layers, $snowpit_unit_prefs);
        // this solo line goes across the top of the top layer. Could be programmed later if we decide to include the 'headspace' above the top of the pit
        imageline($img, 483, $keyed_all_layers[0]->y_val_top, 667, $keyed_all_layers[0]->y_val_top, $black);
        ///
        // IN this loop, we set the items in the 'density managed' column - grain types, sizes, moisture, etc.
        //
        $comment_counter = 0;
        foreach ($keyed_all_layers as $x => $layer) {
            imageline($img, 511, $layer->y_val_xlate, 667, $layer->y_val_xlate, $black);
            // 'density managed' column - grain types, sizes, moisture, etc.
            imageline($img, 483, $layer->y_val, 491, $layer->y_val, $black);
            // a little tick to start outthe angle transferred stuff
            imageline($img, 491, $layer->y_val, 511, $layer->y_val_xlate, $black);
            // the diagonal line connect
            // Calculate grain type image(s) for this layer
            $grain_type_image = '';
            if (isset($layer->field_grain_type['und'])) {
                $grain_type_image = isset($layer->field_grain_type['und'][1]['tid']) ? _tid2snowsymbols($layer->field_grain_type['und'][1]['tid']) : _tid2snowsymbols($layer->field_grain_type['und'][0]['tid']);
            }
            $secondary_grain_type = '';
            if (isset($layer->field_grain_type_secondary['und'])) {
                $secondary_grain_type_image = isset($layer->field_grain_type_secondary['und'][1]['tid']) ? _tid2snowsymbols($layer->field_grain_type_secondary['und'][1]['tid']) : _tid2snowsymbols($layer->field_grain_type_secondary['und'][0]['tid']);
                $secondary_grain_type = ' (' . $secondary_grain_type_image . ')';
            }
            //output grain symbols
            imagettftext($img, 10, 0, 525, ($layer->y_val_xlate - $layer->y_val_top_xlate) / 2 + $layer->y_val_top_xlate + 5, $black, $snowsymbols_font, $grain_type_image . $secondary_grain_type);
            // calculate grain size string
            $grain_size_string = isset($layer->field_grain_size['und']) ? $layer->field_grain_size['und'][0]['value'] : '';
            if ($layer->field_use_multiple_grain_size['und'][0]['value'] == '1' && isset($layer->field_grain_size_max['und'][0]['value'])) {
                $grain_size_string .= ' - ' . $layer->field_grain_size_max['und'][0]['value'];
            }
            // Ouptut grain sizes
            $textpos = imagettftext($img, 10, 0, 584, ($layer->y_val_xlate - $layer->y_val_top_xlate) / 2 + $layer->y_val_top_xlate + 5, $black, $label_font, $grain_size_string);
            // calculate & output layer moisture
            if (isset($layer->field_water_content['und'])) {
                $moisture = $layer->field_water_content['und'][0]['value'];
                imagettftext($img, 10, 0, $textpos[2] + 5, ($layer->y_val_xlate - $layer->y_val_top_xlate) / 2 + $layer->y_val_top_xlate + 5, $black, $label_font, $moisture);
            }
            // Output Layer comments
            $layer_bottom = $layer->field_bottom_depth['und'][0]['value'] + 0;
            $layer_top = $layer->field_height['und'][0]['value'] + 0;
            if (isset($layer->field_comments['und'])) {
                if ($comment_counter < 5) {
                    imagettftext($img, 9, 0, 805, $comment_counter * 13 + 35, $black, $value_font, $layer_bottom . '-' . $layer_top . ': ' . $layer->field_comments['und'][0]['safe_value']);
                    $comment_counter++;
                } else {
                    imagettftext($img, 7, 0, 805, $comment_counter * 13 + 31, $red_layer, $label_font, '[ More Layer Comments ... ]');
                }
            }
            // write density measurements that are from the 'Layers' tab into the rho column ( in addition to Densities )
            if (isset($layer->field_density_top['und'][0]['value'])) {
                imageline($img, 667, snowpit_graph_pixel_depth($layer->field_height['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']), 707, snowpit_graph_pixel_depth($layer->field_height['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']), $black);
                imagettftext($img, 8, 0, 669, snowpit_graph_pixel_depth($layer->field_height['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']) - 5, $black, $label_font, $layer->field_density_top['und'][0]['value']);
            }
            snowpilot_draw_layer_polygon($img, $layer, $purple_layer, TRUE, $snowpit_unit_prefs['hardnessScaling']);
            // the fill
            snowpilot_draw_layer_polygon($img, $layer, $blue_outline, FALSE, $snowpit_unit_prefs['hardnessScaling']);
            // the outline
            // this mark the layer if its a critical layer, and save some
            if ($layer->field_this_is_my_layer_of_greate['und'][0]['value'] == '1') {
                $x_redline = _h2pix($layer->field_hardness['und'][0]['value'], FALSE, $snowpit_unit_prefs['hardnessScaling']);
                $x_redline_bottom = _h2pix($layer->field_hardness['und'][0]['value'], FALSE, $snowpit_unit_prefs['hardnessScaling']);
                if (isset($layer->field_hardness2['und'][0]['value'])) {
                    $x_redline_bottom = _h2pix($layer->field_hardness2['und'][0]['value'], FALSE, $snowpit_unit_prefs['hardnessScaling']);
                }
                $y_redline_top = $layer->y_val_top;
                $y_redline_bottom = $layer->y_val;
                ///
                $concern_delta = $layer->item_id;
                if ($comment_counter < 5) {
                    imagettftext($img, 9, 0, 805, $comment_counter * 13 + 35, $black, $value_font, $layer_bottom . '-' . $layer_top . ": Problematic layer");
                    $comment_counter++;
                } elseif ($comment_counter == 5) {
                    imagettftext($img, 7, 0, 805, $comment_counter * 13 + 31, $red_layer, $label_font, '[ More Layer Comments ... ]');
                }
                $comment_counter++;
            }
        }
    }
    // now that we are done drawing all the layers, we can overprint the red layer of concern
    if (isset($concern_delta)) {
        $layer_part = isset($all_layers[$concern_delta]->field_concern['und'][0]['value']) ? $all_layers[$concern_delta]->field_concern['und'][0]['value'] : 'entire layer';
        switch ($layer_part) {
            case 'entire layer':
                //full-on red layer was too much; withdrawn for now; in favor of perhaps diagonal lines of red indicating layer of concern ( all layer ) in future
                //
                break;
            case 'top':
                imageline($img, $x_redline, $y_redline_top + 2, 446, $y_redline_top + 2, $red_layer);
                imageline($img, $x_redline, $y_redline_top + 1, 446, $y_redline_top + 1, $red_layer);
                break;
            case 'bottom':
                imageline($img, $x_redline_bottom, $y_redline_bottom - 2, 446, $y_redline_bottom - 2, $red_layer);
                imageline($img, $x_redline_bottom, $y_redline_bottom - 1, 446, $y_redline_bottom - 1, $red_layer);
                break;
        }
    }
    // Temperature Profile:
    // If we have temp profile readings,then we'll make the tick marks
    if (isset($node->field_temp_collection['und'])) {
        $ids = array();
        foreach ($node->field_temp_collection['und'] as $temp) {
            $ids[] = $temp['value'];
        }
        $all_temps = field_collection_item_load_multiple($ids);
        uasort($all_temps, 'depth_val');
        $min_temp = $snowpit_unit_prefs['field_temp_units'] == 'F' ? 12 : -8;
        $min_temp = _temp_profile_find_min_temp($all_temps, $min_temp) - 2;
        if ($snowpit_unit_prefs['field_temp_units'] == 'C') {
            $pixels_per_degree = 433 / $min_temp;
            $increment = $min_temp < -14 ? 2 : 1;
            $x = 0;
            while ($x >= $min_temp) {
                //  tickmarks
                imageline($img, 447 - $pixels_per_degree * $x, 132, 447 - $pixels_per_degree * $x, 140, $black);
                imagettftext($img, 9, 0, 441 - $pixels_per_degree * $x, 130, $black, $label_font, $x);
                $x = $x - $increment;
            }
        } else {
            /// Temperature units = 'F'
            $pixels_per_degree = 433 / $min_temp;
            $increment = $min_temp < 5 ? 2 : 1;
            $x = 32;
            while ($x >= $min_temp) {
                // tickmarks
                imageline($img, 447 - $pixels_per_degree * ($x - 32), 132, 447 - $pixels_per_degree * ($x - 32), 140, $black);
                imagettftext($img, 9, 0, 441 - $pixels_per_degree * ($x - 32), 130, $black, $label_font, $x);
                $x = $x - $increment;
            }
        }
        // end temp units toggle
        // draw points, and line, different $cx calculations for F or C
        $prev_x = 0;
        $prev_y = 0;
        foreach ($all_temps as $x => $temp) {
            $cx = $snowpit_unit_prefs['field_temp_units'] == 'C' ? 447 - $pixels_per_degree * $temp->field_temp_temp['und'][0]['value'] : 447 - $pixels_per_degree * ($temp->field_temp_temp['und'][0]['value'] - 32);
            //dsm($cx);
            if ($cx >= 14 && $cx <= 447) {
                // draw point
                imagefilledellipse($img, $cx, snowpit_graph_pixel_depth($temp->field_depth['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']), 6, 6, $red_layer);
                // draw line
                if ($prev_x <= 447 && $prev_x >= 14 && $prev_y) {
                    imageline($img, $cx, snowpit_graph_pixel_depth($temp->field_depth['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']), $prev_x, $prev_y, $red_layer);
                }
            }
            // save this point location to use to draw the next line
            $prev_x = $cx;
            $prev_y = snowpit_graph_pixel_depth($temp->field_depth['und'][0]['value'], $pit_depth, $snowpit_unit_prefs['field_depth_0_from']);
        }
    }
    // end of drawing the temperature profile
    // cycle through and make depth tick marks
    $x = 0;
    while ($x <= $pit_depth) {
        $y_val = round(snowpit_graph_pixel_depth($x, $pit_depth, $snowpit_unit_prefs['field_depth_0_from']));
        imageline($img, 660, $y_val, 667, $y_val, $black);
        imageline($img, 511, $y_val, 518, $y_val, $black);
        imageline($img, 14, $y_val, 22, $y_val, $black);
        imageline($img, 440, $y_val, 447, $y_val, $black);
        imagettftext($img, 10, 0, 456, $y_val + 5, $black, $label_font, $x);
        $x += 10;
    }
    // Now we make the 5cm tick marks
    $x = 5;
    while ($x <= $pit_depth) {
        $y_val = round(snowpit_graph_pixel_depth($x, $pit_depth, $snowpit_unit_prefs['field_depth_0_from']));
        imageline($img, 664, $y_val, 667, $y_val, $black);
        imageline($img, 511, $y_val, 515, $y_val, $black);
        imageline($img, 14, $y_val, 18, $y_val, $black);
        imageline($img, 443, $y_val, 447, $y_val, $black);
        //imagettftext($img, 10, 0, 638, round(snowpit_graph_pixel_depth($x, $node, 'bottom'))+5, $black, $label_font, $x );
        $x += 10;
    }
    //
    imagettftext($img, 10, 0, 742, 122, $black, $label_font, "Stability tests");
    imagettftext($img, 10, 0, 681, 118, $black, $label_font, "&#x3c1;");
    // Rho symbol for density
    imagettftext($img, 10, 0, 675, 135, $black, $label_font, _density_unit_fix($snowpit_unit_prefs['field_density_units']));
    // the rectabngle around stability and density columns
    imagerectangle($img, 667, 140, 941, 751, $black);
    // the rectangle around the layers hardness profile
    imagerectangle($img, 14, 140, 447, 751, $black);
    //the tickmarks for hardness across the bottom and top, and labels
    foreach (_h2pix(NULL, TRUE, $snowpit_unit_prefs['hardnessScaling']) as $hardness => $pixels) {
        if (substr($hardness, -1) != '+' && substr($hardness, -1) != '-') {
            imageline($img, $pixels, 140, $pixels, 156, $black);
            imageline($img, $pixels, 734, $pixels, 751, $black);
            imagettftext($img, 10, 0, $pixels - 5, 765, $black, $label_font, $hardness);
            //imagettftext($img, 10, 0, $pixels- 5, 172, $black, $label_font, $hardness);
        } else {
            // it is a + or - declaration, shorter ticks and no label
            imageline($img, $pixels, 140, $pixels, 145, $black);
            imageline($img, $pixels, 746, $pixels, 751, $black);
        }
    }
    imageline($img, 707, 140, 707, 751, $black);
    imageline($img, 483, 140, 667, 140, $black);
    // finish line across top
    imageline($img, 483, 751, 667, 751, $black);
    // finish line across bottom
    imageline($img, 483, 140, 483, 751, $black);
    // left edge, first vert line
    imageline($img, 511, 140, 511, 751, $black);
    // beginning of crystal form column
    imageline($img, 575, 135, 575, 751, $black);
    //beginning of crystal size column
    imagettftext($img, 10, 0, 554, 122, $black, $label_font, "Crystal");
    imagettftext($img, 10, 0, 516, 137, $black, $label_font, "Form");
    imagettftext($img, 10, 0, 580, 137, $black, $label_font, "Size (mm)");
    // Snowpilot water mark logo _100
    $sp_watermark = imagecreatefrompng(DRUPAL_ROOT . '/sites/all/themes/sp_theme/images/SnowPilot_Watermark_BlueOrange_100.png');
    imagecopy($img, $sp_watermark, 35, 170, 0, 0, 160, 99);
    imagedestroy($sp_watermark);
    // Output the png image
    $filename = 'graph-' . $node->nid;
    imagejpeg($img, DRUPAL_ROOT . '/sites/default/files/snowpit-profiles/' . $filename . '.jpg', 100);
    imagepng($img, DRUPAL_ROOT . '/sites/default/files/snowpit-profiles/' . $filename . '.png');
    snowpilot_snowpit_crop_layers_write($img, $node->nid);
    // Destroy GD image
    //imagedestroy($img);
    if ($format == 'jpg') {
        return $img;
    } else {
        return $img;
    }
}